Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: setting Content-Type header to []string{""} in http handler causes two Content-Type headers to be emitted #5953

Closed
josharian opened this issue Jul 25, 2013 · 9 comments
Milestone

Comments

@josharian
Copy link
Contributor

What steps will reproduce the problem?

Start up this server locally:

http://play.golang.org/p/GSGc0TFU0A

then:

curl -v localhost:6000


What is the expected output?

Only one Content-Type header in the raw response. (See below.)


What do you see instead?

After trimming a bit of noise:

$ curl -v localhost:6000
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x
zlib/1.2.5
> Host: localhost:6000
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: 
< Content-Type: text/plain; charset=utf-8
< Content-Length: 0
< Date: Thu, 25 Jul 2013 01:08:53 GMT


Which compiler are you using (5g, 6g, 8g, gccgo)?

6g


Which operating system are you using?

OS X


Which version are you using?  (run 'go version')

1.1.1


Please provide any additional information below.

The problematic line is:

  header["Content-Type"] = []string{""}

Any non-empty string here will not trigger the double headers. See
https://golang.org/issue/5950 for context about why I encountered
this.

A similar issue does not occur for the Date header, the Content-Length header, nor any
other header that I tried.
@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 1:

This happens because we're cutting corners testing for the presence of the header.

Labels changed: added priority-later, removed priority-triage.

Owner changed to @bradfitz.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 2:

Labels changed: added go1.2.

@bradfitz
Copy link
Contributor

Comment 3:

https://golang.org/cl/12117043

Status changed to Started.

@bradfitz
Copy link
Contributor

bradfitz commented Aug 1, 2013

Comment 4:

This issue was closed by revision 252c107.

Status changed to Fixed.

@dsymonds
Copy link
Contributor

Comment 5:

This issue was closed by revision 21e6b90.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@odeke-em
Copy link
Member

odeke-em commented Dec 5, 2015

This issue seems to persist for http2 please see CL https://go-review.googlesource.com/17462 that uncovered it
OR

commit eec4d694e3ed2fcc91d72f82c9a2c37c7d17f5c2
Author: Emmanuel Odeke <emm.odeke@gmail.com>
Date:   Fri Dec 4 17:21:08 2015 -0700

    net/http: sniffTest for 5953 set in http2 mode

    Change-Id: I345b6ea7024a1c85c805b2d2e22f9f79aeca5816

diff --git a/src/net/http/sniff_test.go b/src/net/http/sniff_test.go
index 293a7f5..a372210 100644
--- a/src/net/http/sniff_test.go
+++ b/src/net/http/sniff_test.go
@@ -88,15 +88,18 @@ func testServerContentType(t *testing.T, h2 bool) {

 // Issue 5953: shouldn't sniff if the handler set a Content-Type header,
 // even if it's the empty string.
-func TestServerIssue5953(t *testing.T) {
+func TestServerIssue5953_h1(t *testing.T) { testServerIssue5953(t, false) }
+func TestServerIssue5953_h2(t *testing.T) { testServerIssue5953(t, true) }
+
+func testServerIssue5953(t *testing.T, h2 bool) {
    defer afterTest(t)
-   ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+   cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
        w.Header()["Content-Type"] = []string{""}
        fmt.Fprintf(w, "<html><head></head><body>hi</body></html>")
    }))
-   defer ts.Close()
+   defer cst.close()

-   resp, err := Get(ts.URL)
+   resp, err := cst.c.Get(cst.ts.URL)
    if err != nil {
        t.Fatal(err)
    }
$ go test --cover
--- FAIL: TestServerIssue5953_h2 (0.00s)
    sniff_test.go:110: Content-Type = ["" "text/html; charset=utf-8"]; want [""]
FAIL
coverage: 77.4% of statements
exit status 1
FAIL    net/http    22.409s

@odeke-em
Copy link
Member

odeke-em commented Dec 5, 2015

cc @bradfitz

@bradfitz
Copy link
Contributor

bradfitz commented Dec 5, 2015

@odeke-em, please open a new bug and reference this one. Once bugs are closed, we no longer use them.

@odeke-em
Copy link
Member

odeke-em commented Dec 5, 2015

Aye aye, thanks for that. I'll do that right now.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants