summaryrefslogtreecommitdiff
path: root/src/pkg/http/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/http/client.go')
-rw-r--r--src/pkg/http/client.go120
1 files changed, 60 insertions, 60 deletions
diff --git a/src/pkg/http/client.go b/src/pkg/http/client.go
index 70fa49fcc..aa3d3be6d 100644
--- a/src/pkg/http/client.go
+++ b/src/pkg/http/client.go
@@ -7,19 +7,19 @@
package http
import (
- "bufio";
- "fmt";
- "io";
- "net";
- "os";
- "strconv";
- "strings";
+ "bufio"
+ "fmt"
+ "io"
+ "net"
+ "os"
+ "strconv"
+ "strings"
)
// Response represents the response from an HTTP request.
type Response struct {
- Status string; // e.g. "200 OK"
- StatusCode int; // e.g. 200
+ Status string // e.g. "200 OK"
+ StatusCode int // e.g. 200
// Header maps header keys to values. If the response had multiple
// headers with the same key, they will be concatenated, with comma
@@ -27,10 +27,10 @@ type Response struct {
// be semantically equivalent to a comma-delimited sequence.)
//
// Keys in the map are canonicalized (see CanonicalHeaderKey).
- Header map[string]string;
+ Header map[string]string
// Stream from which the response body can be read.
- Body io.ReadCloser;
+ Body io.ReadCloser
}
// GetHeader returns the value of the response header with the given
@@ -39,15 +39,15 @@ type Response struct {
// 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) {
- value, _ = r.Header[CanonicalHeaderKey(key)];
- return;
+ value, _ = r.Header[CanonicalHeaderKey(key)]
+ return
}
// AddHeader adds a value under the given key. Keys are not case sensitive.
func (r *Response) AddHeader(key, value string) {
- key = CanonicalHeaderKey(key);
+ key = CanonicalHeaderKey(key)
- oldValues, oldValuesPresent := r.Header[key];
+ oldValues, oldValuesPresent := r.Header[key]
if oldValuesPresent {
r.Header[key] = oldValues + "," + value
} else {
@@ -57,50 +57,50 @@ func (r *Response) AddHeader(key, value string) {
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
-func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
+func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
// 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 {
- io.Reader;
- io.Closer;
+ io.Reader
+ io.Closer
}
// ReadResponse reads and returns an HTTP response from r.
func ReadResponse(r *bufio.Reader) (*Response, os.Error) {
- resp := new(Response);
+ resp := new(Response)
// Parse the first line of the response.
- resp.Header = make(map[string]string);
+ resp.Header = make(map[string]string)
- line, err := readLine(r);
+ line, err := readLine(r)
if err != nil {
return nil, err
}
- f := strings.Split(line, " ", 3);
+ f := strings.Split(line, " ", 3)
if len(f) < 3 {
return nil, &badStringError{"malformed HTTP response", line}
}
- resp.Status = f[1] + " " + f[2];
- resp.StatusCode, err = strconv.Atoi(f[1]);
+ resp.Status = f[1] + " " + f[2]
+ resp.StatusCode, err = strconv.Atoi(f[1])
if err != nil {
return nil, &badStringError{"malformed HTTP status code", f[1]}
}
// Parse the response headers.
for {
- key, value, err := readKeyValue(r);
+ key, value, err := readKeyValue(r)
if err != nil {
return nil, err
}
if key == "" {
- break // end of response header
+ break // end of response header
}
- resp.AddHeader(key, value);
+ resp.AddHeader(key, value)
}
- return resp, nil;
+ return resp, nil
}
@@ -114,41 +114,41 @@ func send(req *Request) (resp *Response, err os.Error) {
return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
}
- addr := req.URL.Host;
+ addr := req.URL.Host
if !hasPort(addr) {
addr += ":http"
}
- conn, err := net.Dial("tcp", "", addr);
+ conn, err := net.Dial("tcp", "", addr)
if err != nil {
return nil, err
}
- err = req.Write(conn);
+ err = req.Write(conn)
if err != nil {
- conn.Close();
- return nil, err;
+ conn.Close()
+ return nil, err
}
- reader := bufio.NewReader(conn);
- resp, err = ReadResponse(reader);
+ reader := bufio.NewReader(conn)
+ resp, err = ReadResponse(reader)
if err != nil {
- conn.Close();
- return nil, err;
+ conn.Close()
+ return nil, err
}
- r := io.Reader(reader);
+ r := io.Reader(reader)
if v := resp.GetHeader("Transfer-Encoding"); v == "chunked" {
r = newChunkedReader(reader)
} else if v := resp.GetHeader("Content-Length"); v != "" {
- n, err := strconv.Atoi64(v);
+ n, err := strconv.Atoi64(v)
if err != nil {
return nil, &badStringError{"invalid Content-Length", v}
}
- r = io.LimitReader(r, n);
+ r = io.LimitReader(r, n)
}
- resp.Body = readClose{r, conn};
+ resp.Body = readClose{r, conn}
- return;
+ return
}
// True if the specified HTTP status code is one for which the Get utility should
@@ -158,7 +158,7 @@ func shouldRedirect(statusCode int) bool {
case StatusMovedPermanently, StatusFound, StatusSeeOther, StatusTemporaryRedirect:
return true
}
- return false;
+ return false
}
// Get issues a GET to the specified URL. If the response is one of the following
@@ -179,11 +179,11 @@ func Get(url string) (r *Response, finalURL string, err os.Error) {
// TODO: set referrer header on redirects.
for redirect := 0; ; redirect++ {
if redirect >= 10 {
- err = os.ErrorString("stopped after 10 redirects");
- break;
+ err = os.ErrorString("stopped after 10 redirects")
+ break
}
- var req Request;
+ var req Request
if req.URL, err = ParseURL(url); err != nil {
break
}
@@ -191,19 +191,19 @@ func Get(url string) (r *Response, finalURL string, err os.Error) {
break
}
if shouldRedirect(r.StatusCode) {
- r.Body.Close();
+ r.Body.Close()
if url = r.GetHeader("Location"); url == "" {
- err = os.ErrorString(fmt.Sprintf("%d response missing Location header", r.StatusCode));
- break;
+ err = os.ErrorString(fmt.Sprintf("%d response missing Location header", r.StatusCode))
+ break
}
- continue;
+ continue
}
- finalURL = url;
- return;
+ finalURL = url
+ return
}
- err = &URLError{"Get", url, err};
- return;
+ err = &URLError{"Get", url, err}
+ return
}
@@ -211,18 +211,18 @@ func Get(url string) (r *Response, finalURL string, err os.Error) {
//
// Caller should close r.Body when done reading it.
func Post(url string, bodyType string, body io.Reader) (r *Response, err os.Error) {
- var req Request;
- req.Method = "POST";
- req.Body = body;
+ var req Request
+ req.Method = "POST"
+ req.Body = body
req.Header = map[string]string{
"Content-Type": bodyType,
"Transfer-Encoding": "chunked",
- };
+ }
- req.URL, err = ParseURL(url);
+ req.URL, err = ParseURL(url)
if err != nil {
return nil, err
}
- return send(&req);
+ return send(&req)
}