Commit 063125df authored by Russ Cox's avatar Russ Cox

http: send full URL in proxy requests

Fixes #53.  (again)

R=agl1
CC=golang-dev
https://golang.org/cl/4167054
parent f80d0024
...@@ -92,19 +92,25 @@ func send(req *Request) (resp *Response, err os.Error) { ...@@ -92,19 +92,25 @@ func send(req *Request) (resp *Response, err os.Error) {
var proxyURL *URL var proxyURL *URL
proxyAuth := "" proxyAuth := ""
proxy := os.Getenv("HTTP_PROXY") proxy := ""
if proxy == "" { if !matchNoProxy(addr) {
proxy = os.Getenv("http_proxy") proxy = os.Getenv("HTTP_PROXY")
} if proxy == "" {
if matchNoProxy(addr) { proxy = os.Getenv("http_proxy")
proxy = "" }
} }
if proxy != "" { if proxy != "" {
proxyURL, err = ParseURL(proxy) proxyURL, err = ParseRequestURL(proxy)
if err != nil { if err != nil {
return nil, os.ErrorString("invalid proxy address") return nil, os.ErrorString("invalid proxy address")
} }
if proxyURL.Host == "" {
proxyURL, err = ParseRequestURL("http://" + proxy)
if err != nil {
return nil, os.ErrorString("invalid proxy address")
}
}
addr = proxyURL.Host addr = proxyURL.Host
proxyInfo := proxyURL.RawUserinfo proxyInfo := proxyURL.RawUserinfo
if proxyInfo != "" { if proxyInfo != "" {
......
...@@ -184,6 +184,17 @@ const defaultUserAgent = "Go http package" ...@@ -184,6 +184,17 @@ const defaultUserAgent = "Go http package"
// If Body is present, Write forces "Transfer-Encoding: chunked" as a header // If Body is present, Write forces "Transfer-Encoding: chunked" as a header
// and then closes Body when finished sending it. // and then closes Body when finished sending it.
func (req *Request) Write(w io.Writer) os.Error { func (req *Request) Write(w io.Writer) os.Error {
return req.write(w, false)
}
// WriteProxy is like Write but writes the request in the form
// expected by an HTTP proxy. It includes the scheme and host
// name in the URI instead of using a separate Host: header line.
func (req *Request) WriteProxy(w io.Writer) os.Error {
return req.write(w, true)
}
func (req *Request) write(w io.Writer, usingProxy bool) os.Error {
host := req.Host host := req.Host
if host == "" { if host == "" {
host = req.URL.Host host = req.URL.Host
...@@ -197,10 +208,19 @@ func (req *Request) Write(w io.Writer) os.Error { ...@@ -197,10 +208,19 @@ func (req *Request) Write(w io.Writer) os.Error {
} }
} }
if usingProxy {
if uri == "" || uri[0] != '/' {
uri = "/" + uri
}
uri = req.URL.Scheme + "://" + host + uri
}
fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), uri) fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), uri)
// Header lines // Header lines
fmt.Fprintf(w, "Host: %s\r\n", host) if !usingProxy {
fmt.Fprintf(w, "Host: %s\r\n", host)
}
fmt.Fprintf(w, "User-Agent: %s\r\n", valueOrDefault(req.UserAgent, defaultUserAgent)) fmt.Fprintf(w, "User-Agent: %s\r\n", valueOrDefault(req.UserAgent, defaultUserAgent))
if req.Referer != "" { if req.Referer != "" {
fmt.Fprintf(w, "Referer: %s\r\n", req.Referer) fmt.Fprintf(w, "Referer: %s\r\n", req.Referer)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment