]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: Don't set Content-Type header for HEAD requests by default
authorPatrick Mylund Nielsen <patrick@patrickmn.com>
Mon, 6 Feb 2012 06:55:47 +0000 (17:55 +1100)
committerDavid Symonds <dsymonds@golang.org>
Mon, 6 Feb 2012 06:55:47 +0000 (17:55 +1100)
since the real type is not inferred.
Fixes #2885.

R=golang-dev, dsymonds, bradfitz
CC=golang-dev
https://golang.org/cl/5633045

src/pkg/net/http/serve_test.go
src/pkg/net/http/server.go

index 147c216ec78744a1dd1036a3fed0fb676806a519..e2860c3edcf55fbd9eaa0cf80637716fa8517fd3 100644 (file)
@@ -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)
index dea75b1dfd44764e9f47e8b113137eaffddc21ae..288539ba5760745a5f0b50dc45905a57675acb2b 100644 (file)
@@ -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
                }
        }