1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:34:39 -07:00

net/http: Don't set Content-Type header for HEAD requests by default

since the real type is not inferred.
Fixes #2885.

R=golang-dev, dsymonds, bradfitz
CC=golang-dev
https://golang.org/cl/5633045
This commit is contained in:
Patrick Mylund Nielsen 2012-02-06 17:55:47 +11:00 committed by David Symonds
parent f2d2b38c92
commit fb86bbe239
2 changed files with 8 additions and 3 deletions

View File

@ -504,8 +504,9 @@ func Test304Responses(t *testing.T) {
} }
// TestHeadResponses verifies that responses to HEAD requests don't // TestHeadResponses verifies that responses to HEAD requests don't
// declare that they're chunking in their response headers and aren't // declare that they're chunking in their response headers, aren't
// allowed to produce output. // allowed to produce output, and don't set a Content-Type since
// the real type of the body data cannot be inferred.
func TestHeadResponses(t *testing.T) { func TestHeadResponses(t *testing.T) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
_, err := w.Write([]byte("Ignored body")) _, err := w.Write([]byte("Ignored body"))
@ -527,6 +528,10 @@ func TestHeadResponses(t *testing.T) {
if len(res.TransferEncoding) > 0 { if len(res.TransferEncoding) > 0 {
t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding) t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
} }
ct := res.Header.Get("Content-Type")
if ct != "" {
t.Errorf("expected no Content-Type; got %s", ct)
}
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
t.Error(err) t.Error(err)

View File

@ -341,7 +341,7 @@ func (w *response) WriteHeader(code int) {
} }
} else { } else {
// If no content type, apply sniffing algorithm to body. // If no content type, apply sniffing algorithm to body.
if w.header.Get("Content-Type") == "" { if w.header.Get("Content-Type") == "" && w.req.Method != "HEAD" {
w.needSniff = true w.needSniff = true
} }
} }