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

33 lines
783 B
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package security
2020-07-26 17:09:05 +08:00
import "net/http"
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-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
}