diff options
Diffstat (limited to 'libgo/go/http/response.go')
-rw-r--r-- | libgo/go/http/response.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/libgo/go/http/response.go b/libgo/go/http/response.go index 915327a..56c65b53 100644 --- a/libgo/go/http/response.go +++ b/libgo/go/http/response.go @@ -13,6 +13,7 @@ import ( "os" "strconv" "strings" + "url" ) var respExcludeHeader = map[string]bool{ @@ -41,6 +42,10 @@ type Response struct { Header Header // Body represents the response body. + // + // The http Client and Transport guarantee that Body is always + // non-nil, even on responses without a body or responses with + // a zero-lengthed body. Body io.ReadCloser // ContentLength records the length of the associated content. The @@ -73,6 +78,23 @@ func (r *Response) Cookies() []*Cookie { return readSetCookies(r.Header) } +var ErrNoLocation = os.NewError("http: no Location header in response") + +// Location returns the URL of the response's "Location" header, +// if present. Relative redirects are resolved relative to +// the Response's Request. ErrNoLocation is returned if no +// Location header is present. +func (r *Response) Location() (*url.URL, os.Error) { + lv := r.Header.Get("Location") + if lv == "" { + return nil, ErrNoLocation + } + if r.Request != nil && r.Request.URL != nil { + return r.Request.URL.Parse(lv) + } + return url.Parse(lv) +} + // ReadResponse reads and returns an HTTP response from r. The // req parameter specifies the Request that corresponds to // this Response. Clients must call resp.Body.Close when finished |