You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
3 years ago
|
package response
|
||
4 years ago
|
|
||
4 years ago
|
import (
|
||
|
"bufio"
|
||
3 years ago
|
"errors"
|
||
4 years ago
|
"net"
|
||
|
"net/http"
|
||
|
)
|
||
4 years ago
|
|
||
4 years ago
|
// A WithCodeResponseWriter is a helper to delay sealing a http.ResponseWriter on writing code.
|
||
4 years ago
|
type WithCodeResponseWriter struct {
|
||
|
Writer http.ResponseWriter
|
||
|
Code int
|
||
|
}
|
||
|
|
||
4 years ago
|
// Flush flushes the response writer.
|
||
4 years ago
|
func (w *WithCodeResponseWriter) Flush() {
|
||
|
if flusher, ok := w.Writer.(http.Flusher); ok {
|
||
|
flusher.Flush()
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
// Header returns the http header.
|
||
4 years ago
|
func (w *WithCodeResponseWriter) Header() http.Header {
|
||
|
return w.Writer.Header()
|
||
|
}
|
||
|
|
||
4 years ago
|
// 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) {
|
||
3 years ago
|
if hijacked, ok := w.Writer.(http.Hijacker); ok {
|
||
|
return hijacked.Hijack()
|
||
|
}
|
||
|
|
||
|
return nil, nil, errors.New("server doesn't support hijacking")
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// Write writes bytes into w.
|
||
4 years ago
|
func (w *WithCodeResponseWriter) Write(bytes []byte) (int, error) {
|
||
|
return w.Writer.Write(bytes)
|
||
|
}
|
||
|
|
||
4 years ago
|
// WriteHeader writes code into w, and not sealing the writer.
|
||
4 years ago
|
func (w *WithCodeResponseWriter) WriteHeader(code int) {
|
||
|
w.Writer.WriteHeader(code)
|
||
|
w.Code = code
|
||
|
}
|