1 Star 0 Fork 0

micro-tools/wf

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
ghttp_response_writer.go 1.82 KB
Copy Edit Raw Blame History
545403892 authored 2023-09-27 22:16 +08:00 . 升级go-ole
//
package ghttp
import (
"bufio"
"bytes"
"net"
"net/http"
)
// ResponseWriter is the custom writer for http response.
type ResponseWriter struct {
Status int // HTTP status.
writer http.ResponseWriter // The underlying ResponseWriter.
buffer *bytes.Buffer // The output buffer.
hijacked bool // Mark this request is hijacked or not.
wroteHeader bool // Is header wrote or not, avoiding error: superfluous/multiple response.WriteHeader call.
}
// RawWriter returns the underlying ResponseWriter.
func (w *ResponseWriter) RawWriter() http.ResponseWriter {
return w.writer
}
// Header implements the interface function of http.ResponseWriter.Header.
func (w *ResponseWriter) Header() http.Header {
return w.writer.Header()
}
// Write implements the interface function of http.ResponseWriter.Write.
func (w *ResponseWriter) Write(data []byte) (int, error) {
w.buffer.Write(data)
return len(data), nil
}
// WriteHeader implements the interface of http.ResponseWriter.WriteHeader.
func (w *ResponseWriter) WriteHeader(status int) {
w.Status = status
}
// Hijack implements the interface function of http.Hijacker.Hijack.
func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
w.hijacked = true
return w.writer.(http.Hijacker).Hijack()
}
// OutputBuffer outputs the buffer to client and clears the buffer.
func (w *ResponseWriter) Flush() {
if w.hijacked {
return
}
if w.Status != 0 && !w.wroteHeader {
w.wroteHeader = true
w.writer.WriteHeader(w.Status)
}
// Default status text output.
if w.Status != http.StatusOK && w.buffer.Len() == 0 {
w.buffer.WriteString(http.StatusText(w.Status))
}
if w.buffer.Len() > 0 {
w.writer.Write(w.buffer.Bytes())
w.buffer.Reset()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

Search