2009-06-09 11:58:58 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-06-23 19:49:47 -06:00
|
|
|
// Primitive HTTP client. See RFC 2616.
|
2009-06-09 11:58:58 -06:00
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"bufio"
|
2010-09-01 18:01:34 -06:00
|
|
|
"bytes"
|
2010-07-28 19:13:56 -06:00
|
|
|
"crypto/tls"
|
2009-12-15 17:27:45 -07:00
|
|
|
"encoding/base64"
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
2010-09-01 18:01:34 -06:00
|
|
|
"strconv"
|
2009-12-15 16:35:38 -07:00
|
|
|
"strings"
|
2009-06-09 11:58:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
|
|
|
|
// return true if the string includes a port.
|
2009-12-15 16:35:38 -07:00
|
|
|
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
|
2009-06-09 11:58:58 -06:00
|
|
|
|
|
|
|
// Used in Send to implement io.ReadCloser by bundling together the
|
2010-07-28 19:13:56 -06:00
|
|
|
// bufio.Reader through which we read the response, and the underlying
|
2009-06-09 11:58:58 -06:00
|
|
|
// network connection.
|
|
|
|
type readClose struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
io.Reader
|
|
|
|
io.Closer
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2011-02-16 12:06:50 -07:00
|
|
|
// matchNoProxy returns true if requests to addr should not use a proxy,
|
|
|
|
// according to the NO_PROXY or no_proxy environment variable.
|
|
|
|
func matchNoProxy(addr string) bool {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
no_proxy := os.Getenv("NO_PROXY")
|
|
|
|
if len(no_proxy) == 0 {
|
|
|
|
no_proxy = os.Getenv("no_proxy")
|
|
|
|
}
|
|
|
|
if no_proxy == "*" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = strings.ToLower(strings.TrimSpace(addr))
|
|
|
|
if hasPort(addr) {
|
|
|
|
addr = addr[:strings.LastIndex(addr, ":")]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range strings.Split(no_proxy, ",", -1) {
|
|
|
|
p = strings.ToLower(strings.TrimSpace(p))
|
|
|
|
if len(p) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if hasPort(p) {
|
|
|
|
p = p[:strings.LastIndex(p, ":")]
|
|
|
|
}
|
|
|
|
if addr == p || (p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:])) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2009-06-09 11:58:58 -06:00
|
|
|
// Send issues an HTTP request. Caller should close resp.Body when done reading it.
|
|
|
|
//
|
|
|
|
// TODO: support persistent connections (multiple requests on a single connection).
|
|
|
|
// send() method is nonpublic because, when we refactor the code for persistent
|
|
|
|
// connections, it may no longer make sense to have a method with this signature.
|
|
|
|
func send(req *Request) (resp *Response, err os.Error) {
|
2010-07-28 19:13:56 -06:00
|
|
|
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
addr := req.URL.Host
|
2009-06-09 11:58:58 -06:00
|
|
|
if !hasPort(addr) {
|
2010-07-28 19:13:56 -06:00
|
|
|
addr += ":" + req.URL.Scheme
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2010-09-27 19:54:04 -06:00
|
|
|
info := req.URL.RawUserinfo
|
2009-12-15 17:27:45 -07:00
|
|
|
if len(info) > 0 {
|
|
|
|
enc := base64.URLEncoding
|
|
|
|
encoded := make([]byte, enc.EncodedLen(len(info)))
|
2010-02-25 17:01:29 -07:00
|
|
|
enc.Encode(encoded, []byte(info))
|
2009-12-15 17:27:45 -07:00
|
|
|
if req.Header == nil {
|
|
|
|
req.Header = make(map[string]string)
|
|
|
|
}
|
|
|
|
req.Header["Authorization"] = "Basic " + string(encoded)
|
|
|
|
}
|
2010-07-28 19:13:56 -06:00
|
|
|
|
2011-02-16 12:06:50 -07:00
|
|
|
var proxyURL *URL
|
|
|
|
proxyAuth := ""
|
2011-02-17 15:14:50 -07:00
|
|
|
proxy := ""
|
|
|
|
if !matchNoProxy(addr) {
|
|
|
|
proxy = os.Getenv("HTTP_PROXY")
|
|
|
|
if proxy == "" {
|
|
|
|
proxy = os.Getenv("http_proxy")
|
|
|
|
}
|
2011-02-16 12:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if proxy != "" {
|
2011-02-17 15:14:50 -07:00
|
|
|
proxyURL, err = ParseRequestURL(proxy)
|
2010-09-11 21:41:12 -06:00
|
|
|
if err != nil {
|
2011-02-16 12:06:50 -07:00
|
|
|
return nil, os.ErrorString("invalid proxy address")
|
|
|
|
}
|
2011-02-17 15:14:50 -07:00
|
|
|
if proxyURL.Host == "" {
|
|
|
|
proxyURL, err = ParseRequestURL("http://" + proxy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.ErrorString("invalid proxy address")
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 12:06:50 -07:00
|
|
|
addr = proxyURL.Host
|
|
|
|
proxyInfo := proxyURL.RawUserinfo
|
|
|
|
if proxyInfo != "" {
|
|
|
|
enc := base64.URLEncoding
|
|
|
|
encoded := make([]byte, enc.EncodedLen(len(proxyInfo)))
|
|
|
|
enc.Encode(encoded, []byte(proxyInfo))
|
|
|
|
proxyAuth = "Basic " + string(encoded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to server or proxy.
|
|
|
|
conn, err := net.Dial("tcp", "", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.URL.Scheme == "http" {
|
|
|
|
// Include proxy http header if needed.
|
|
|
|
if proxyAuth != "" {
|
|
|
|
req.Header["Proxy-Authorization"] = proxyAuth
|
2010-09-11 21:41:12 -06:00
|
|
|
}
|
2010-07-28 19:13:56 -06:00
|
|
|
} else { // https
|
2011-02-16 12:06:50 -07:00
|
|
|
if proxyURL != nil {
|
|
|
|
// Ask proxy for direct connection to server.
|
|
|
|
// addr defaults above to ":https" but we need to use numbers
|
|
|
|
addr = req.URL.Host
|
|
|
|
if !hasPort(addr) {
|
|
|
|
addr += ":443"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\n", addr)
|
|
|
|
fmt.Fprintf(conn, "Host: %s\r\n", addr)
|
|
|
|
if proxyAuth != "" {
|
|
|
|
fmt.Fprintf(conn, "Proxy-Authorization: %s\r\n", proxyAuth)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(conn, "\r\n")
|
|
|
|
|
|
|
|
// Read response.
|
|
|
|
// Okay to use and discard buffered reader here, because
|
|
|
|
// TLS server will not speak until spoken to.
|
|
|
|
br := bufio.NewReader(conn)
|
|
|
|
resp, err := ReadResponse(br, "CONNECT")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
f := strings.Split(resp.Status, " ", 2)
|
|
|
|
return nil, os.ErrorString(f[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate TLS and check remote host name against certificate.
|
|
|
|
conn = tls.Client(conn, nil)
|
|
|
|
if err = conn.(*tls.Conn).Handshake(); err != nil {
|
2010-09-11 21:41:12 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h := req.URL.Host
|
|
|
|
if hasPort(h) {
|
2011-02-16 12:06:50 -07:00
|
|
|
h = h[:strings.LastIndex(h, ":")]
|
2010-09-11 21:41:12 -06:00
|
|
|
}
|
2011-02-16 12:06:50 -07:00
|
|
|
if err = conn.(*tls.Conn).VerifyHostname(h); err != nil {
|
2010-09-11 21:41:12 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
err = req.Write(conn)
|
2009-06-09 11:58:58 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
reader := bufio.NewReader(conn)
|
2010-01-18 22:46:59 -07:00
|
|
|
resp, err = ReadResponse(reader, req.Method)
|
2009-06-09 11:58:58 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2010-01-18 22:46:59 -07:00
|
|
|
resp.Body = readClose{resp.Body, conn}
|
2009-06-23 19:49:47 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// True if the specified HTTP status code is one for which the Get utility should
|
|
|
|
// automatically redirect.
|
|
|
|
func shouldRedirect(statusCode int) bool {
|
|
|
|
switch statusCode {
|
|
|
|
case StatusMovedPermanently, StatusFound, StatusSeeOther, StatusTemporaryRedirect:
|
2009-11-09 13:07:39 -07:00
|
|
|
return true
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return false
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a GET to the specified URL. If the response is one of the following
|
|
|
|
// redirect codes, it follows the redirect, up to a maximum of 10 redirects:
|
|
|
|
//
|
|
|
|
// 301 (Moved Permanently)
|
|
|
|
// 302 (Found)
|
|
|
|
// 303 (See Other)
|
|
|
|
// 307 (Temporary Redirect)
|
|
|
|
//
|
2010-01-18 22:46:59 -07:00
|
|
|
// finalURL is the URL from which the response was fetched -- identical to the
|
|
|
|
// input URL unless redirects were followed.
|
2009-06-09 11:58:58 -06:00
|
|
|
//
|
|
|
|
// Caller should close r.Body when done reading it.
|
2009-06-25 21:24:55 -06:00
|
|
|
func Get(url string) (r *Response, finalURL string, err os.Error) {
|
2009-06-09 11:58:58 -06:00
|
|
|
// TODO: if/when we add cookie support, the redirected request shouldn't
|
|
|
|
// necessarily supply the same cookies as the original.
|
2009-06-25 21:24:55 -06:00
|
|
|
// TODO: set referrer header on redirects.
|
2011-01-20 10:50:27 -07:00
|
|
|
var base *URL
|
2009-10-06 20:41:51 -06:00
|
|
|
for redirect := 0; ; redirect++ {
|
2009-06-25 21:24:55 -06:00
|
|
|
if redirect >= 10 {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = os.ErrorString("stopped after 10 redirects")
|
|
|
|
break
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
var req Request
|
2011-01-20 10:50:27 -07:00
|
|
|
if base == nil {
|
|
|
|
req.URL, err = ParseURL(url)
|
|
|
|
} else {
|
|
|
|
req.URL, err = base.ParseURL(url)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2010-08-26 11:32:16 -06:00
|
|
|
url = req.URL.String()
|
2009-06-25 21:24:55 -06:00
|
|
|
if r, err = send(&req); err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if shouldRedirect(r.StatusCode) {
|
2009-12-15 16:35:38 -07:00
|
|
|
r.Body.Close()
|
2009-06-25 21:24:55 -06:00
|
|
|
if url = r.GetHeader("Location"); url == "" {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = os.ErrorString(fmt.Sprintf("%d response missing Location header", r.StatusCode))
|
|
|
|
break
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2011-01-20 10:50:27 -07:00
|
|
|
base = req.URL
|
2009-12-15 16:35:38 -07:00
|
|
|
continue
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
finalURL = url
|
|
|
|
return
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
err = &URLError{"Get", url, err}
|
|
|
|
return
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post issues a POST to the specified URL.
|
|
|
|
//
|
2009-06-23 19:49:47 -06:00
|
|
|
// Caller should close r.Body when done reading it.
|
|
|
|
func Post(url string, bodyType string, body io.Reader) (r *Response, err os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var req Request
|
|
|
|
req.Method = "POST"
|
2010-02-22 15:46:59 -07:00
|
|
|
req.ProtoMajor = 1
|
|
|
|
req.ProtoMinor = 1
|
2010-02-24 16:13:39 -07:00
|
|
|
req.Close = true
|
2010-01-25 19:49:08 -07:00
|
|
|
req.Body = nopCloser{body}
|
2009-10-06 20:41:51 -06:00
|
|
|
req.Header = map[string]string{
|
2009-06-23 19:49:47 -06:00
|
|
|
"Content-Type": bodyType,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2010-02-22 15:46:59 -07:00
|
|
|
req.TransferEncoding = []string{"chunked"}
|
2009-06-23 19:49:47 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
req.URL, err = ParseURL(url)
|
2009-06-09 11:58:58 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return send(&req)
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2010-01-25 19:49:08 -07:00
|
|
|
|
2010-09-01 18:01:34 -06:00
|
|
|
// PostForm issues a POST to the specified URL,
|
|
|
|
// with data's keys and values urlencoded as the request body.
|
|
|
|
//
|
|
|
|
// Caller should close r.Body when done reading it.
|
|
|
|
func PostForm(url string, data map[string]string) (r *Response, err os.Error) {
|
|
|
|
var req Request
|
|
|
|
req.Method = "POST"
|
|
|
|
req.ProtoMajor = 1
|
|
|
|
req.ProtoMinor = 1
|
|
|
|
req.Close = true
|
|
|
|
body := urlencode(data)
|
|
|
|
req.Body = nopCloser{body}
|
|
|
|
req.Header = map[string]string{
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
"Content-Length": strconv.Itoa(body.Len()),
|
|
|
|
}
|
|
|
|
req.ContentLength = int64(body.Len())
|
|
|
|
|
|
|
|
req.URL, err = ParseURL(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return send(&req)
|
|
|
|
}
|
|
|
|
|
2010-12-07 13:54:04 -07:00
|
|
|
// TODO: remove this function when PostForm takes a multimap.
|
2010-09-01 18:01:34 -06:00
|
|
|
func urlencode(data map[string]string) (b *bytes.Buffer) {
|
2010-12-07 13:54:04 -07:00
|
|
|
m := make(map[string][]string, len(data))
|
2010-09-01 18:01:34 -06:00
|
|
|
for k, v := range data {
|
2010-12-07 13:54:04 -07:00
|
|
|
m[k] = []string{v}
|
2010-09-01 18:01:34 -06:00
|
|
|
}
|
2010-12-07 13:54:04 -07:00
|
|
|
return bytes.NewBuffer([]byte(EncodeQuery(m)))
|
2010-09-01 18:01:34 -06:00
|
|
|
}
|
|
|
|
|
2010-06-07 17:28:40 -06:00
|
|
|
// Head issues a HEAD to the specified URL.
|
|
|
|
func Head(url string) (r *Response, err os.Error) {
|
|
|
|
var req Request
|
|
|
|
req.Method = "HEAD"
|
|
|
|
if req.URL, err = ParseURL(url); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-02-21 16:40:37 -07:00
|
|
|
return send(&req)
|
2010-06-07 17:28:40 -06:00
|
|
|
}
|
|
|
|
|
2010-01-25 19:49:08 -07:00
|
|
|
type nopCloser struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() os.Error { return nil }
|