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"
|
2009-12-15 17:27:45 -07:00
|
|
|
"encoding/base64"
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2009-06-09 11:58:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Response represents the response from an HTTP request.
|
|
|
|
type Response struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
Status string // e.g. "200 OK"
|
|
|
|
StatusCode int // e.g. 200
|
2009-06-09 11:58:58 -06:00
|
|
|
|
|
|
|
// Header maps header keys to values. If the response had multiple
|
|
|
|
// headers with the same key, they will be concatenated, with comma
|
|
|
|
// delimiters. (Section 4.2 of RFC 2616 requires that multiple headers
|
|
|
|
// be semantically equivalent to a comma-delimited sequence.)
|
|
|
|
//
|
|
|
|
// Keys in the map are canonicalized (see CanonicalHeaderKey).
|
2009-12-15 16:35:38 -07:00
|
|
|
Header map[string]string
|
2009-06-09 11:58:58 -06:00
|
|
|
|
|
|
|
// Stream from which the response body can be read.
|
2009-12-15 16:35:38 -07:00
|
|
|
Body io.ReadCloser
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader returns the value of the response header with the given
|
|
|
|
// key, and true. If there were multiple headers with this key, their
|
|
|
|
// values are concatenated, with a comma delimiter. If there were no
|
|
|
|
// response headers with the given key, it returns the empty string and
|
|
|
|
// false. Keys are not case sensitive.
|
|
|
|
func (r *Response) GetHeader(key string) (value string) {
|
2009-12-15 16:35:38 -07:00
|
|
|
value, _ = r.Header[CanonicalHeaderKey(key)]
|
|
|
|
return
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddHeader adds a value under the given key. Keys are not case sensitive.
|
|
|
|
func (r *Response) AddHeader(key, value string) {
|
2009-12-15 16:35:38 -07:00
|
|
|
key = CanonicalHeaderKey(key)
|
2009-06-09 11:58:58 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
oldValues, oldValuesPresent := r.Header[key]
|
2009-06-09 11:58:58 -06:00
|
|
|
if oldValuesPresent {
|
2009-11-09 13:07:39 -07:00
|
|
|
r.Header[key] = oldValues + "," + value
|
2009-06-09 11:58:58 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
r.Header[key] = value
|
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
|
|
|
|
// io.BufReader through which we read the response, and the underlying
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2009-07-15 11:49:47 -06:00
|
|
|
// ReadResponse reads and returns an HTTP response from r.
|
|
|
|
func ReadResponse(r *bufio.Reader) (*Response, os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
resp := new(Response)
|
2009-07-15 11:49:47 -06:00
|
|
|
|
|
|
|
// Parse the first line of the response.
|
2009-12-15 16:35:38 -07:00
|
|
|
resp.Header = make(map[string]string)
|
2009-07-15 11:49:47 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
line, err := readLine(r)
|
2009-07-15 11:49:47 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
f := strings.Split(line, " ", 3)
|
2009-07-15 11:49:47 -06:00
|
|
|
if len(f) < 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &badStringError{"malformed HTTP response", line}
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
resp.Status = f[1] + " " + f[2]
|
|
|
|
resp.StatusCode, err = strconv.Atoi(f[1])
|
2009-07-15 11:49:47 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &badStringError{"malformed HTTP status code", f[1]}
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response headers.
|
|
|
|
for {
|
2009-12-15 16:35:38 -07:00
|
|
|
key, value, err := readKeyValue(r)
|
2009-07-15 11:49:47 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
|
|
|
if key == "" {
|
2009-12-15 16:35:38 -07:00
|
|
|
break // end of response header
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
resp.AddHeader(key, value)
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return resp, nil
|
2009-07-15 11:49:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2009-11-08 22:46:20 -07:00
|
|
|
if req.URL.Scheme != "http" {
|
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) {
|
2009-11-09 13:07:39 -07:00
|
|
|
addr += ":http"
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2009-12-15 17:27:45 -07:00
|
|
|
info := req.URL.Userinfo
|
|
|
|
if len(info) > 0 {
|
|
|
|
enc := base64.URLEncoding
|
|
|
|
encoded := make([]byte, enc.EncodedLen(len(info)))
|
|
|
|
enc.Encode(encoded, strings.Bytes(info))
|
|
|
|
if req.Header == nil {
|
|
|
|
req.Header = make(map[string]string)
|
|
|
|
}
|
|
|
|
req.Header["Authorization"] = "Basic " + string(encoded)
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
conn, err := net.Dial("tcp", "", addr)
|
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
|
|
|
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)
|
|
|
|
resp, err = ReadResponse(reader)
|
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
|
|
|
r := io.Reader(reader)
|
2009-06-23 19:49:47 -06:00
|
|
|
if v := resp.GetHeader("Transfer-Encoding"); v == "chunked" {
|
2009-11-09 13:07:39 -07:00
|
|
|
r = newChunkedReader(reader)
|
2009-08-26 17:23:54 -06:00
|
|
|
} else if v := resp.GetHeader("Content-Length"); v != "" {
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err := strconv.Atoi64(v)
|
2009-08-26 17:23:54 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &badStringError{"invalid Content-Length", v}
|
2009-08-26 17:23:54 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
r = io.LimitReader(r, n)
|
2009-06-23 19:49:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
resp.Body = readClose{r, 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)
|
|
|
|
//
|
2009-11-08 22:46:20 -07:00
|
|
|
// finalURL is the URL from which the response was fetched -- identical to the input
|
2009-06-09 11:58:58 -06:00
|
|
|
// URL unless redirects were followed.
|
|
|
|
//
|
|
|
|
// 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.
|
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
|
2009-11-08 22:46:20 -07:00
|
|
|
if req.URL, err = ParseURL(url); 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 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
|
|
|
}
|
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"
|
|
|
|
req.Body = 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,
|
|
|
|
"Transfer-Encoding": "chunked",
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
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
|
|
|
}
|