net/http: ResponseWriter implementations don't always follow documentation #17653
Labels
Comments
Nevermind, I was thinking a bit oddly about this, that something else is causing |
If someone else finds this issue because they were trying to do what I'm doing, I believe this is a valid base to start off a type MyResponseWriter struct {
http.ResponseWriter
wroteHeader bool
}
func (w *MyResponseWriter) Write(p []byte) (n int, err error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
return w.ResponseWriter.Write(p)
}
func (w *MyResponseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
// Check after in case there's error handling in the wrapped ResponseWriter.
if !w.wroteHeader {
return
}
w.wroteHeader = true
// Do other stuff here.
} |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
These are excerpts from the documentation for the
ResponseWriter
interface. ForWrite
:For
WriteHeader
:The code below never calls
ResponseWriter.WriteHeader
for simple requests. I had expected that the implementation of theResponseWriter
interface would follow it by calling out to the interfaceWriteHeader
method instead of directly calling its internalwriteHeader
function.I also put this code on the Go Playground.
Go version that I'm using:
The text was updated successfully, but these errors were encountered: