Skip to content

Commit

Permalink
net/http: don't send Accept-Encoding on HEAD requests
Browse files Browse the repository at this point in the history
Works around a bug in nginx: http://trac.nginx.org/nginx/ticket/358

Fixes #5522

R=iant
CC=gobot, golang-dev
https://golang.org/cl/9627043
  • Loading branch information
bradfitz committed May 21, 2013
1 parent 0e10196 commit ddda798
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/pkg/net/http/transport.go
Expand Up @@ -831,10 +831,15 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
// uncompress the gzip stream if we were the layer that
// requested it.
requestedGzip := false
if !pc.t.DisableCompression && req.Header.Get("Accept-Encoding") == "" {
if !pc.t.DisableCompression && req.Header.Get("Accept-Encoding") == "" && req.Method != "HEAD" {
// Request gzip only, not deflate. Deflate is ambiguous and
// not as universally supported anyway.
// See: http://www.gzip.org/zlib/zlib_faq.html#faq38
//
// Note that we don't request this for HEAD requests,
// due to a bug in nginx:
// http://trac.nginx.org/nginx/ticket/358
// http://golang.org/issue/5522
requestedGzip = true
req.extraHeaders().Set("Accept-Encoding", "gzip")
}
Expand Down
9 changes: 6 additions & 3 deletions src/pkg/net/http/transport_test.go
Expand Up @@ -585,13 +585,16 @@ func TestTransportGzip(t *testing.T) {
const testString = "The test string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
const nRandBytes = 1024 * 1024
ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
if req.Method == "HEAD" {
if g := req.Header.Get("Accept-Encoding"); g != "" {
t.Errorf("HEAD request sent with Accept-Encoding of %q; want none", g)
}
return
}
if g, e := req.Header.Get("Accept-Encoding"), "gzip"; g != e {
t.Errorf("Accept-Encoding = %q, want %q", g, e)
}
rw.Header().Set("Content-Encoding", "gzip")
if req.Method == "HEAD" {
return
}

var w io.Writer = rw
var buf bytes.Buffer
Expand Down

0 comments on commit ddda798

Please sign in to comment.