withcoderesponsewriter.go 783 B

1234567891011121314151617181920212223242526272829303132
  1. package security
  2. import "net/http"
  3. // A WithCodeResponseWriter is a helper to delay sealing a http.ResponseWriter on writing code.
  4. type WithCodeResponseWriter struct {
  5. Writer http.ResponseWriter
  6. Code int
  7. }
  8. // Flush flushes the response writer.
  9. func (w *WithCodeResponseWriter) Flush() {
  10. if flusher, ok := w.Writer.(http.Flusher); ok {
  11. flusher.Flush()
  12. }
  13. }
  14. // Header returns the http header.
  15. func (w *WithCodeResponseWriter) Header() http.Header {
  16. return w.Writer.Header()
  17. }
  18. // Write writes bytes into w.
  19. func (w *WithCodeResponseWriter) Write(bytes []byte) (int, error) {
  20. return w.Writer.Write(bytes)
  21. }
  22. // WriteHeader writes code into w, and not sealing the writer.
  23. func (w *WithCodeResponseWriter) WriteHeader(code int) {
  24. w.Writer.WriteHeader(code)
  25. w.Code = code
  26. }