]> Cypherpunks repositories - gostls13.git/commitdiff
http: include Host header in requests, even with proxies
authorBrad Fitzpatrick <bradfitz@golang.org>
Sat, 21 May 2011 02:40:23 +0000 (19:40 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 21 May 2011 02:40:23 +0000 (19:40 -0700)
A user pointed out that Go didn't work with their
corp proxy, always throwing 400 Bad Request errors.

Looking at the RFC 2616, Host is always required,
even with proxies.

The old code assumed that writing an absolute URL
in the first line of an HTTP request implied
that the Host header was no longer necessary.

Double-checked behavior with curl.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4539075

src/pkg/http/request.go
src/pkg/http/requestwrite_test.go

index 353b1c62c91fe77ace819d4d179fb0a35957672b..05d4892110cf68e2d91a45f4ab7d172737038c00 100644 (file)
@@ -276,9 +276,7 @@ func (req *Request) write(w io.Writer, usingProxy bool) os.Error {
        fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), uri)
 
        // Header lines
-       if !usingProxy {
-               fmt.Fprintf(w, "Host: %s\r\n", host)
-       }
+       fmt.Fprintf(w, "Host: %s\r\n", host)
        fmt.Fprintf(w, "User-Agent: %s\r\n", valueOrDefault(req.UserAgent, defaultUserAgent))
        if req.Referer != "" {
                fmt.Fprintf(w, "Referer: %s\r\n", req.Referer)
index bb000c701ff5b48c65ab6f35e6d4744ece0772bd..beb51fb8d75cb8e7aeb133f566812385cec77152 100644 (file)
@@ -69,6 +69,7 @@ var reqWriteTests = []reqWriteTest{
                        "Proxy-Connection: keep-alive\r\n\r\n",
 
                "GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
+                       "Host: www.techcrunch.com\r\n" +
                        "User-Agent: Fake\r\n" +
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
                        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
@@ -101,6 +102,7 @@ var reqWriteTests = []reqWriteTest{
                        "6\r\nabcdef\r\n0\r\n\r\n",
 
                "GET http://www.google.com/search HTTP/1.1\r\n" +
+                       "Host: www.google.com\r\n" +
                        "User-Agent: Go http package\r\n" +
                        "Transfer-Encoding: chunked\r\n\r\n" +
                        "6\r\nabcdef\r\n0\r\n\r\n",
@@ -131,6 +133,7 @@ var reqWriteTests = []reqWriteTest{
                        "6\r\nabcdef\r\n0\r\n\r\n",
 
                "POST http://www.google.com/search HTTP/1.1\r\n" +
+                       "Host: www.google.com\r\n" +
                        "User-Agent: Go http package\r\n" +
                        "Connection: close\r\n" +
                        "Transfer-Encoding: chunked\r\n\r\n" +
@@ -164,6 +167,7 @@ var reqWriteTests = []reqWriteTest{
                        "abcdef",
 
                "POST http://www.google.com/search HTTP/1.1\r\n" +
+                       "Host: www.google.com\r\n" +
                        "User-Agent: Go http package\r\n" +
                        "Connection: close\r\n" +
                        "Content-Length: 6\r\n" +
@@ -188,6 +192,7 @@ var reqWriteTests = []reqWriteTest{
 
                // Looks weird but RawURL overrides what WriteProxy would choose.
                "GET /search HTTP/1.1\r\n" +
+                       "Host: www.google.com\r\n" +
                        "User-Agent: Go http package\r\n" +
                        "\r\n",
        },