Skip to content

net/http: http.ServeFile and http.ServeContent Explicitly Set http.StatusOK #11269

Closed
@elithrar

Description

@elithrar

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 call w.WriteHeader(code) on an error condition. Set var code int (i.e. zero value) instead of defaulting to code := StatusOK.
  • If code still has the zero value before we write to the ResponseWriter, then don't call WriteHeader.
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions