Description
What version of Go are you using (go version)?
go version go1.4.2 darwin/amd64
What operating system and processor architecture are you using?
OS X 10.10.3 x86-64
What did you do?
Set w.WriteHeader(http.StatusInternalServerError)
prior to calling http.ServeFile
What did you expect to see?
A HTTP 500
status.
What did you see instead?
A HTTP 200
status (and the usual multiple header writes error for messing it up).
Currently http.ServeFile
(which calls http.ServeContent
and then eventually http.serveContent
) implicitly sets the status code to HTTP 200 as per this line in src/http/fs.go
From what I can see, removing this line would result in net/http
implicitly calling w.WriteHeader(StatusOK)
as per usual (http://golang.org/src/net/http/server.go#L990) - which is the behaviour I'd expected until running into this.
Proposed Fix
- Modify
http.serveContent
to only callw.WriteHeader(code)
on an error condition. Setvar code int
(i.e. zero value) instead of defaulting tocode := StatusOK
. - If
code
still has the zero value before we write to the ResponseWriter, then don't callWriteHeader
. - This allows the server to either: a) write the header set by the package user or; b) write the implicit
http.StatusOK
if no header has been set by the time we're ready to write back.
Writing StatusOK
in serveContent appears to be redundant.