buf := new(bytes.Buffer)
shouldBuffer := func(status int, header http.Header) bool {
// Returning false means headers/body will be streamed out directly to the underlying writer.
return false
}
ww := caddyhttp.NewResponseRecorder(w, buf, shouldBuffer)
// Do stuff with ww
// ...
// Write response and return error if needed
return ww.WriteResponse()
This worked, but when reading through the sources of caddy and the documentation of NewResponseRecorder we realized that we could also initialize it without the buffer and function:
ww := caddyhttp.NewResponseRecorder(w, nil, nil)
This matches how caddy initializes it, and is explicitly allowed in the docs.
However, with that change we noticed panics happening:
Checking the code shows that this seems to a confusion inside the response recorder: rr.stream is set by WriteHeader, so checking that and then realizing that the status code is unset (i.e. WriteHeader hadn't been called!) is the wrong way around.
In one of our modules we had this code:
This worked, but when reading through the sources of caddy and the documentation of NewResponseRecorder we realized that we could also initialize it without the buffer and function:
This matches how caddy initializes it, and is explicitly allowed in the docs.
However, with that change we noticed panics happening:
Checking the code shows that this seems to a confusion inside the response recorder:
rr.stream
is set byWriteHeader
, so checking that and then realizing that the status code is unset (i.e.WriteHeader
hadn't been called!) is the wrong way around.