3 Star 6 Fork 1

Walle / ewa

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
response_writer.go 1.88 KB
Copy Edit Raw Blame History
JackHunx authored 2020-12-05 16:58 . sth wrong test test test....
// this code is copyed from goframe see below url
// https://github.com/gogf/gf/blob/v1.14.5/net/ghttp/ghttp_response_writer.go
package http
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()
}
}
Go
1
https://gitee.com/wallesoft/ewa.git
git@gitee.com:wallesoft/ewa.git
wallesoft
ewa
ewa
15a7b9f14a04

Search

53164aa7 5694891 3bd8fe86 5694891