2020-09-01 16:16:04 +00:00
|
|
|
package headers
|
|
|
|
|
|
|
|
import (
|
2020-09-07 07:26:03 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-09-01 16:16:04 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-09-01 16:16:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type responseModifier struct {
|
2021-07-13 10:28:07 +00:00
|
|
|
req *http.Request
|
|
|
|
rw http.ResponseWriter
|
2020-09-01 16:16:04 +00:00
|
|
|
|
|
|
|
headersSent bool // whether headers have already been sent
|
|
|
|
code int // status code, must default to 200
|
|
|
|
|
|
|
|
modifier func(*http.Response) error // can be nil
|
|
|
|
modified bool // whether modifier has already been called for the current request
|
|
|
|
modifierErr error // returned by modifier call
|
|
|
|
}
|
|
|
|
|
|
|
|
// modifier can be nil.
|
2021-08-19 16:02:07 +00:00
|
|
|
func newResponseModifier(w http.ResponseWriter, r *http.Request, modifier func(*http.Response) error) http.ResponseWriter {
|
2022-11-16 10:38:07 +00:00
|
|
|
return &responseModifier{
|
2021-07-13 10:28:07 +00:00
|
|
|
req: r,
|
|
|
|
rw: w,
|
2020-09-01 16:16:04 +00:00
|
|
|
modifier: modifier,
|
|
|
|
code: http.StatusOK,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// WriteHeader is, in the specific case of 1xx status codes, a direct call to the wrapped ResponseWriter, without marking headers as sent,
|
|
|
|
// allowing so further calls.
|
2021-07-13 10:28:07 +00:00
|
|
|
func (r *responseModifier) WriteHeader(code int) {
|
|
|
|
if r.headersSent {
|
2020-09-01 16:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-14 15:42:44 +00:00
|
|
|
|
|
|
|
// Handling informational headers.
|
|
|
|
if code >= 100 && code <= 199 {
|
|
|
|
r.rw.WriteHeader(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:16:04 +00:00
|
|
|
defer func() {
|
2021-07-13 10:28:07 +00:00
|
|
|
r.code = code
|
|
|
|
r.headersSent = true
|
2020-09-01 16:16:04 +00:00
|
|
|
}()
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
if r.modifier == nil || r.modified {
|
|
|
|
r.rw.WriteHeader(code)
|
2020-09-01 16:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := http.Response{
|
2021-07-13 10:28:07 +00:00
|
|
|
Header: r.rw.Header(),
|
|
|
|
Request: r.req,
|
2020-09-01 16:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
if err := r.modifier(&resp); err != nil {
|
|
|
|
r.modifierErr = err
|
2020-09-01 16:16:04 +00:00
|
|
|
// we are propagating when we are called in Write, but we're logging anyway,
|
|
|
|
// because we could be called from another place which does not take care of
|
|
|
|
// checking w.modifierErr.
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msg("Error when applying response modifier")
|
2021-07-13 10:28:07 +00:00
|
|
|
r.rw.WriteHeader(http.StatusInternalServerError)
|
2020-09-01 16:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
r.modified = true
|
|
|
|
r.rw.WriteHeader(code)
|
2020-09-01 16:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
func (r *responseModifier) Header() http.Header {
|
|
|
|
return r.rw.Header()
|
2020-09-01 16:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
func (r *responseModifier) Write(b []byte) (int, error) {
|
|
|
|
r.WriteHeader(r.code)
|
|
|
|
if r.modifierErr != nil {
|
|
|
|
return 0, r.modifierErr
|
2020-09-01 16:16:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
return r.rw.Write(b)
|
2020-09-01 16:16:04 +00:00
|
|
|
}
|
2020-09-07 07:26:03 +00:00
|
|
|
|
|
|
|
// Hijack hijacks the connection.
|
2021-07-13 10:28:07 +00:00
|
|
|
func (r *responseModifier) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if h, ok := r.rw.(http.Hijacker); ok {
|
2020-09-07 07:26:03 +00:00
|
|
|
return h.Hijack()
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:28:07 +00:00
|
|
|
return nil, nil, fmt.Errorf("not a hijacker: %T", r.rw)
|
2020-09-07 07:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
2021-07-13 10:28:07 +00:00
|
|
|
func (r *responseModifier) Flush() {
|
|
|
|
if flusher, ok := r.rw.(http.Flusher); ok {
|
2020-09-07 07:26:03 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|