diff options
Diffstat (limited to 'libgo/go/http/response.go')
-rw-r--r-- | libgo/go/http/response.go | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/libgo/go/http/response.go b/libgo/go/http/response.go index a65c2b14df6..915327a69ec 100644 --- a/libgo/go/http/response.go +++ b/libgo/go/http/response.go @@ -30,10 +30,6 @@ type Response struct { ProtoMajor int // e.g. 1 ProtoMinor int // e.g. 0 - // RequestMethod records the method used in the HTTP request. - // Header fields such as Content-Length have method-specific meaning. - RequestMethod string // e.g. "HEAD", "CONNECT", "GET", etc. - // 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 @@ -44,9 +40,6 @@ type Response struct { // Keys in the map are canonicalized (see CanonicalHeaderKey). Header Header - // SetCookie records the Set-Cookie requests sent with the response. - SetCookie []*Cookie - // Body represents the response body. Body io.ReadCloser @@ -68,19 +61,31 @@ type Response struct { // Trailer maps trailer keys to values, in the same // format as the header. Trailer Header + + // The Request that was sent to obtain this Response. + // Request's Body is nil (having already been consumed). + // This is only populated for Client requests. + Request *Request +} + +// Cookies parses and returns the cookies set in the Set-Cookie headers. +func (r *Response) Cookies() []*Cookie { + return readSetCookies(r.Header) } -// ReadResponse reads and returns an HTTP response from r. The RequestMethod -// parameter specifies the method used in the corresponding request (e.g., -// "GET", "HEAD"). Clients must call resp.Body.Close when finished reading -// resp.Body. After that call, clients can inspect resp.Trailer to find -// key/value pairs included in the response trailer. -func ReadResponse(r *bufio.Reader, requestMethod string) (resp *Response, err os.Error) { +// 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 +// reading resp.Body. After that call, clients can inspect +// resp.Trailer to find key/value pairs included in the response +// trailer. +func ReadResponse(r *bufio.Reader, req *Request) (resp *Response, err os.Error) { tp := textproto.NewReader(r) resp = new(Response) - resp.RequestMethod = strings.ToUpper(requestMethod) + resp.Request = req + resp.Request.Method = strings.ToUpper(resp.Request.Method) // Parse the first line of the response. line, err := tp.ReadLine() @@ -90,7 +95,7 @@ func ReadResponse(r *bufio.Reader, requestMethod string) (resp *Response, err os } return nil, err } - f := strings.Split(line, " ", 3) + f := strings.SplitN(line, " ", 3) if len(f) < 2 { return nil, &badStringError{"malformed HTTP response", line} } @@ -124,8 +129,6 @@ func ReadResponse(r *bufio.Reader, requestMethod string) (resp *Response, err os return nil, err } - resp.SetCookie = readSetCookies(resp.Header) - return resp, nil } @@ -164,7 +167,9 @@ func (r *Response) ProtoAtLeast(major, minor int) bool { func (resp *Response) Write(w io.Writer) os.Error { // RequestMethod should be upper-case - resp.RequestMethod = strings.ToUpper(resp.RequestMethod) + if resp.Request != nil { + resp.Request.Method = strings.ToUpper(resp.Request.Method) + } // Status line text := resp.Status @@ -195,10 +200,6 @@ func (resp *Response) Write(w io.Writer) os.Error { return err } - if err = writeSetCookies(w, resp.SetCookie); err != nil { - return err - } - // End-of-header io.WriteString(w, "\r\n") |