go-zero/rest/internal/security/withcoderesponsewriter.go

43 lines
1.1 KiB
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package security
2020-07-26 17:09:05 +08:00
2021-03-09 21:30:45 +08:00
import (
"bufio"
"net"
"net/http"
)
2020-07-26 17:09:05 +08:00
2021-03-01 19:15:35 +08:00
// A WithCodeResponseWriter is a helper to delay sealing a http.ResponseWriter on writing code.
2020-07-26 17:09:05 +08:00
type WithCodeResponseWriter struct {
Writer http.ResponseWriter
Code int
}
2021-03-01 19:15:35 +08:00
// Flush flushes the response writer.
func (w *WithCodeResponseWriter) Flush() {
if flusher, ok := w.Writer.(http.Flusher); ok {
flusher.Flush()
}
}
2021-03-01 19:15:35 +08:00
// Header returns the http header.
2020-07-26 17:09:05 +08:00
func (w *WithCodeResponseWriter) Header() http.Header {
return w.Writer.Header()
}
2021-03-09 21:30:45 +08:00
// Hijack implements the http.Hijacker interface.
// This expands the Response to fulfill http.Hijacker if the underlying http.ResponseWriter supports it.
func (w *WithCodeResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.Writer.(http.Hijacker).Hijack()
}
2021-03-01 19:15:35 +08:00
// Write writes bytes into w.
2020-07-26 17:09:05 +08:00
func (w *WithCodeResponseWriter) Write(bytes []byte) (int, error) {
return w.Writer.Write(bytes)
}
2021-03-01 19:15:35 +08:00
// WriteHeader writes code into w, and not sealing the writer.
2020-07-26 17:09:05 +08:00
func (w *WithCodeResponseWriter) WriteHeader(code int) {
w.Writer.WriteHeader(code)
w.Code = code
}