mirror of
https://github.com/golang/go
synced 2024-11-20 00:14:44 -07:00
net/http: don't set Content-Type with empty body automatically
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>
This commit is contained in:
parent
bd926e1c65
commit
8cdd999bf5
@ -949,7 +949,6 @@ func TestServeContent(t *testing.T) {
|
|||||||
"If-Match": `"B"`,
|
"If-Match": `"B"`,
|
||||||
},
|
},
|
||||||
wantStatus: 412,
|
wantStatus: 412,
|
||||||
wantContentType: "text/plain; charset=utf-8",
|
|
||||||
},
|
},
|
||||||
"ifmatch_fails_on_weak_etag": {
|
"ifmatch_fails_on_weak_etag": {
|
||||||
file: "testdata/style.css",
|
file: "testdata/style.css",
|
||||||
@ -958,7 +957,6 @@ func TestServeContent(t *testing.T) {
|
|||||||
"If-Match": `W/"A"`,
|
"If-Match": `W/"A"`,
|
||||||
},
|
},
|
||||||
wantStatus: 412,
|
wantStatus: 412,
|
||||||
wantContentType: "text/plain; charset=utf-8",
|
|
||||||
},
|
},
|
||||||
"if_unmodified_since_true": {
|
"if_unmodified_since_true": {
|
||||||
file: "testdata/style.css",
|
file: "testdata/style.css",
|
||||||
@ -977,7 +975,6 @@ func TestServeContent(t *testing.T) {
|
|||||||
"If-Unmodified-Since": htmlModTime.Add(-2 * time.Second).UTC().Format(TimeFormat),
|
"If-Unmodified-Since": htmlModTime.Add(-2 * time.Second).UTC().Format(TimeFormat),
|
||||||
},
|
},
|
||||||
wantStatus: 412,
|
wantStatus: 412,
|
||||||
wantContentType: "text/plain; charset=utf-8",
|
|
||||||
wantLastMod: htmlModTime.UTC().Format(TimeFormat),
|
wantLastMod: htmlModTime.UTC().Format(TimeFormat),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -6011,7 +6011,7 @@ func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) {
|
|||||||
clen = strconv.Itoa(len(p))
|
clen = strconv.Itoa(len(p))
|
||||||
}
|
}
|
||||||
_, hasContentType := rws.snapHeader["Content-Type"]
|
_, hasContentType := rws.snapHeader["Content-Type"]
|
||||||
if !hasContentType && http2bodyAllowedForStatus(rws.status) {
|
if !hasContentType && http2bodyAllowedForStatus(rws.status) && len(p) > 0 {
|
||||||
ctype = DetectContentType(p)
|
ctype = DetectContentType(p)
|
||||||
}
|
}
|
||||||
var date string
|
var date string
|
||||||
|
@ -3439,9 +3439,6 @@ func TestHeaderToWire(t *testing.T) {
|
|||||||
handler: func(rw ResponseWriter, r *Request) {
|
handler: func(rw ResponseWriter, r *Request) {
|
||||||
},
|
},
|
||||||
check: func(got string) error {
|
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") {
|
if !strings.Contains(got, "Content-Length: 0") {
|
||||||
return errors.New("want 0 content-length")
|
return errors.New("want 0 content-length")
|
||||||
}
|
}
|
||||||
|
@ -1311,7 +1311,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
|
|||||||
if bodyAllowedForStatus(code) {
|
if bodyAllowedForStatus(code) {
|
||||||
// If no content type, apply sniffing algorithm to body.
|
// If no content type, apply sniffing algorithm to body.
|
||||||
_, haveType := header["Content-Type"]
|
_, haveType := header["Content-Type"]
|
||||||
if !haveType && !hasTE {
|
if !haveType && !hasTE && len(p) > 0 {
|
||||||
setHeader.contentType = DetectContentType(p)
|
setHeader.contentType = DetectContentType(p)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -23,7 +23,6 @@ var sniffTests = []struct {
|
|||||||
contentType string
|
contentType string
|
||||||
}{
|
}{
|
||||||
// Some nonsense.
|
// Some nonsense.
|
||||||
{"Empty", []byte{}, "text/plain; charset=utf-8"},
|
|
||||||
{"Binary", []byte{1, 2, 3}, "application/octet-stream"},
|
{"Binary", []byte{1, 2, 3}, "application/octet-stream"},
|
||||||
|
|
||||||
{"HTML document #1", []byte(`<HtMl><bOdY>blah blah blah</body></html>`), "text/html; charset=utf-8"},
|
{"HTML document #1", []byte(`<HtMl><bOdY>blah blah blah</body></html>`), "text/html; charset=utf-8"},
|
||||||
|
Loading…
Reference in New Issue
Block a user