aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Mylund Nielsen <patrick@patrickmn.com>2012-02-06 17:55:47 +1100
committerDavid Symonds <dsymonds@golang.org>2012-02-06 17:55:47 +1100
commitfb86bbe2397453aaf793ec00a7233b858f17bd2c (patch)
tree16bd2cf3def94993596e4720df838fd2534fb1a7
parentf2d2b38c92f65c738c0c6cda4c34c7db34026a3e (diff)
downloadgo-fb86bbe2397453aaf793ec00a7233b858f17bd2c.tar.gz
go-fb86bbe2397453aaf793ec00a7233b858f17bd2c.zip
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
-rw-r--r--src/pkg/net/http/serve_test.go9
-rw-r--r--src/pkg/net/http/server.go2
2 files changed, 8 insertions, 3 deletions
diff --git a/src/pkg/net/http/serve_test.go b/src/pkg/net/http/serve_test.go
index 147c216ec7..e2860c3edc 100644
--- a/src/pkg/net/http/serve_test.go
+++ b/src/pkg/net/http/serve_test.go
@@ -504,8 +504,9 @@ func Test304Responses(t *testing.T) {
}
// TestHeadResponses verifies that responses to HEAD requests don't
-// declare that they're chunking in their response headers and aren't
-// allowed to produce output.
+// declare that they're chunking in their response headers, aren't
+// 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) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
_, err := w.Write([]byte("Ignored body"))
@@ -527,6 +528,10 @@ func TestHeadResponses(t *testing.T) {
if len(res.TransferEncoding) > 0 {
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)
if err != nil {
t.Error(err)
diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go
index dea75b1dfd..288539ba57 100644
--- a/src/pkg/net/http/server.go
+++ b/src/pkg/net/http/server.go
@@ -341,7 +341,7 @@ func (w *response) WriteHeader(code int) {
}
} else {
// 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
}
}