]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: set Content-Type header for HEAD as well
authorDmitri Shuralyov <shurcooL@gmail.com>
Thu, 21 Sep 2017 05:53:13 +0000 (01:53 -0400)
committerTom Bergan <tombergan@google.com>
Wed, 1 Nov 2017 18:45:35 +0000 (18:45 +0000)
In CL 50510, the Content-Type header started to be set in Redirect when
request method is GET. (Prior to that, it wasn't set at all, which is
what said CL was fixing.) However, according to HTTP specification,
the expected response for a HEAD request is identical to that of a
GET request, but without the response body.

This CL updates the behavior to set the Content-Type header for HEAD
method in addition to GET.

This actually allows a simpler implementation than before. This change
largely reverts CL 50510, and applies the simpler implementation.

Add a test for Content-Type header and body for GET, HEAD requests.

Updates CL 50510.

Change-Id: If33ea3f4bbc5246bb5dc751458004828cfe681b9
Reviewed-on: https://go-review.googlesource.com/65190
Run-TryBot: Dmitri Shuralyov <shurcool@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Tom Bergan <tombergan@google.com>
src/net/http/serve_test.go
src/net/http/server.go

index bbc6ed5f4478e0637a1a24ca7c0a04ce8a90915d..508d8b53f1c14ebc39fed39ae1ea5cdcdafcfd13 100644 (file)
@@ -2506,6 +2506,37 @@ func TestRedirect(t *testing.T) {
        }
 }
 
+// Test that Content-Type header is set for GET and HEAD requests.
+func TestRedirectContentTypeAndBody(t *testing.T) {
+       var tests = []struct {
+               method   string
+               wantCT   string
+               wantBody string
+       }{
+               {MethodGet, "text/html; charset=utf-8", "<a href=\"/foo\">Found</a>.\n\n"},
+               {MethodHead, "text/html; charset=utf-8", ""},
+               {MethodPost, "", ""},
+               {MethodDelete, "", ""},
+               {"foo", "", ""},
+       }
+       for _, tt := range tests {
+               req := httptest.NewRequest(tt.method, "http://example.com/qux/", nil)
+               rec := httptest.NewRecorder()
+               Redirect(rec, req, "/foo", 302)
+               if got, want := rec.Header().Get("Content-Type"), tt.wantCT; got != want {
+                       t.Errorf("Redirect(%q) generated Content-Type header %q; want %q", tt.method, got, want)
+               }
+               resp := rec.Result()
+               body, err := ioutil.ReadAll(resp.Body)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if got, want := string(body), tt.wantBody; got != want {
+                       t.Errorf("Redirect(%q) generated Body %q; want %q", tt.method, got, want)
+               }
+       }
+}
+
 // TestZeroLengthPostAndResponse exercises an optimization done by the Transport:
 // when there is no body (either because the method doesn't permit a body, or an
 // explicit Content-Length of zero is present), then the transport can re-use the
index cfc6654ff43fc8822aacf8e17393298f25973dc0..91f481ed50ef16a1081b32bf6e508c29ac2529e1 100644 (file)
@@ -2016,17 +2016,16 @@ func Redirect(w ResponseWriter, r *Request, url string, code int) {
                }
        }
 
-       // RFC 2616 recommends that a short note "SHOULD" be included in the
-       // response because older user agents may not understand 301/307.
-       // Shouldn't send the response for POST or HEAD; that leaves GET.
-       writeNote := r.Method == "GET"
-
        w.Header().Set("Location", hexEscapeNonASCII(url))
-       if writeNote {
+       if r.Method == "GET" || r.Method == "HEAD" {
                w.Header().Set("Content-Type", "text/html; charset=utf-8")
        }
        w.WriteHeader(code)
-       if writeNote {
+
+       // RFC 2616 recommends that a short note "SHOULD" be included in the
+       // response because older user agents may not understand 301/307.
+       // Shouldn't send the response for POST or HEAD; that leaves GET.
+       if r.Method == "GET" {
                note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code] + "</a>.\n"
                fmt.Fprintln(w, note)
        }