]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: don't set Content-Type with empty body automatically
authorTw <tw19881113@gmail.com>
Sat, 24 Jun 2017 06:23:17 +0000 (14:23 +0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 15 Nov 2017 02:22:21 +0000 (02:22 +0000)
We set Content-Type to "text/plain; charset=utf-8" even with blank body
before. Let's strip this unnecessary header though it's harmless in most
cases.

Fixes #20784

Signed-off-by: Tw <tw19881113@gmail.com>
Change-Id: Ic58a410dcbc89f457c6ddd92961d9cbf545b2f4f
Reviewed-on: https://go-review.googlesource.com/46631
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/fs_test.go
src/net/http/h2_bundle.go
src/net/http/serve_test.go
src/net/http/server.go
src/net/http/sniff_test.go

index 798cb30b2935fa8ede03f26388fd55e025f0b76a..e766dc69f470f06086daa3095351c748139e7acd 100644 (file)
@@ -948,8 +948,7 @@ func TestServeContent(t *testing.T) {
                        reqHeader: map[string]string{
                                "If-Match": `"B"`,
                        },
-                       wantStatus:      412,
-                       wantContentType: "text/plain; charset=utf-8",
+                       wantStatus: 412,
                },
                "ifmatch_fails_on_weak_etag": {
                        file:      "testdata/style.css",
@@ -957,8 +956,7 @@ func TestServeContent(t *testing.T) {
                        reqHeader: map[string]string{
                                "If-Match": `W/"A"`,
                        },
-                       wantStatus:      412,
-                       wantContentType: "text/plain; charset=utf-8",
+                       wantStatus: 412,
                },
                "if_unmodified_since_true": {
                        file:    "testdata/style.css",
@@ -976,9 +974,8 @@ func TestServeContent(t *testing.T) {
                        reqHeader: map[string]string{
                                "If-Unmodified-Since": htmlModTime.Add(-2 * time.Second).UTC().Format(TimeFormat),
                        },
-                       wantStatus:      412,
-                       wantContentType: "text/plain; charset=utf-8",
-                       wantLastMod:     htmlModTime.UTC().Format(TimeFormat),
+                       wantStatus:  412,
+                       wantLastMod: htmlModTime.UTC().Format(TimeFormat),
                },
        }
        for testName, tt := range tests {
index 1faddbff4841e604bb3bae3c2898a4e80435a681..95b3305061d4ae14b0dff9934b78654fd5379ce8 100644 (file)
@@ -6011,7 +6011,7 @@ func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) {
                        clen = strconv.Itoa(len(p))
                }
                _, hasContentType := rws.snapHeader["Content-Type"]
-               if !hasContentType && http2bodyAllowedForStatus(rws.status) {
+               if !hasContentType && http2bodyAllowedForStatus(rws.status) && len(p) > 0 {
                        ctype = DetectContentType(p)
                }
                var date string
index b000bf0e616ada25c7f11159524d1133158f8e6f..174f6845aa2099ed1a4b619de16152f2ad4326fb 100644 (file)
@@ -3439,9 +3439,6 @@ func TestHeaderToWire(t *testing.T) {
                        handler: func(rw ResponseWriter, r *Request) {
                        },
                        check: func(got string) error {
-                               if !strings.Contains(got, "Content-Type: text/plain") {
-                                       return errors.New("wrong content-type; want text/plain")
-                               }
                                if !strings.Contains(got, "Content-Length: 0") {
                                        return errors.New("want 0 content-length")
                                }
index 453024f4db46a9a5f11eb93d4bdfdf48e749ea5d..7a4ff88baf8da58e4c86476c47bee6d9fc703854 100644 (file)
@@ -1311,7 +1311,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
        if bodyAllowedForStatus(code) {
                // If no content type, apply sniffing algorithm to body.
                _, haveType := header["Content-Type"]
-               if !haveType && !hasTE {
+               if !haveType && !hasTE && len(p) > 0 {
                        setHeader.contentType = DetectContentType(p)
                }
        } else {
index 24f1298e5d9a1cf52d32c281698ee182e5eb8ad0..c7622531dfb85c17de0c8c681fa71715de37f0fc 100644 (file)
@@ -23,7 +23,6 @@ var sniffTests = []struct {
        contentType string
 }{
        // Some nonsense.
-       {"Empty", []byte{}, "text/plain; charset=utf-8"},
        {"Binary", []byte{1, 2, 3}, "application/octet-stream"},
 
        {"HTML document #1", []byte(`<HtMl><bOdY>blah blah blah</body></html>`), "text/html; charset=utf-8"},