1
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// HTTP Response reading and parsing.
13
"github.com/Azure/azure-sdk-for-go/core/tls"
21
var respExcludeHeader = map[string]bool{
22
"Content-Length": true,
23
"Transfer-Encoding": true,
27
// Response represents the response from an HTTP request.
29
type Response struct {
30
Status string // e.g. "200 OK"
31
StatusCode int // e.g. 200
32
Proto string // e.g. "HTTP/1.0"
33
ProtoMajor int // e.g. 1
34
ProtoMinor int // e.g. 0
36
// Header maps header keys to values. If the response had multiple
37
// headers with the same key, they may be concatenated, with comma
38
// delimiters. (Section 4.2 of RFC 2616 requires that multiple headers
39
// be semantically equivalent to a comma-delimited sequence.) Values
40
// duplicated by other fields in this struct (e.g., ContentLength) are
41
// omitted from Header.
43
// Keys in the map are canonicalized (see CanonicalHeaderKey).
46
// Body represents the response body.
48
// The http Client and Transport guarantee that Body is always
49
// non-nil, even on responses without a body or responses with
50
// a zero-length body. It is the caller's responsibility to
53
// The Body is automatically dechunked if the server replied
54
// with a "chunked" Transfer-Encoding.
57
// ContentLength records the length of the associated content. The
58
// value -1 indicates that the length is unknown. Unless Request.Method
59
// is "HEAD", values >= 0 indicate that the given number of bytes may
63
// Contains transfer encodings from outer-most to inner-most. Value is
64
// nil, means that "identity" encoding is used.
65
TransferEncoding []string
67
// Close records whether the header directed that the connection be
68
// closed after reading Body. The value is advice for clients: neither
69
// ReadResponse nor Response.Write ever closes a connection.
72
// Trailer maps trailer keys to values, in the same
73
// format as the header.
76
// The Request that was sent to obtain this Response.
77
// Request's Body is nil (having already been consumed).
78
// This is only populated for Client requests.
81
// TLS contains information about the TLS connection on which the
82
// response was received. It is nil for unencrypted responses.
83
// The pointer is shared between responses and should not be
85
TLS *tls.ConnectionState
88
// Cookies parses and returns the cookies set in the Set-Cookie headers.
89
func (r *Response) Cookies() []*Cookie {
90
return readSetCookies(r.Header)
93
var ErrNoLocation = errors.New("http: no Location header in response")
95
// Location returns the URL of the response's "Location" header,
96
// if present. Relative redirects are resolved relative to
97
// the Response's Request. ErrNoLocation is returned if no
98
// Location header is present.
99
func (r *Response) Location() (*url.URL, error) {
100
lv := r.Header.Get("Location")
102
return nil, ErrNoLocation
104
if r.Request != nil && r.Request.URL != nil {
105
return r.Request.URL.Parse(lv)
110
// ReadResponse reads and returns an HTTP response from r.
111
// The req parameter optionally specifies the Request that corresponds
112
// to this Response. If nil, a GET request is assumed.
113
// Clients must call resp.Body.Close when finished reading resp.Body.
114
// After that call, clients can inspect resp.Trailer to find key/value
115
// pairs included in the response trailer.
116
func ReadResponse(r *bufio.Reader, req *Request) (*Response, error) {
117
tp := textproto.NewReader(r)
122
// Parse the first line of the response.
123
line, err := tp.ReadLine()
126
err = io.ErrUnexpectedEOF
130
f := strings.SplitN(line, " ", 3)
132
return nil, &badStringError{"malformed HTTP response", line}
138
resp.Status = f[1] + " " + reasonPhrase
139
resp.StatusCode, err = strconv.Atoi(f[1])
141
return nil, &badStringError{"malformed HTTP status code", f[1]}
146
if resp.ProtoMajor, resp.ProtoMinor, ok = ParseHTTPVersion(resp.Proto); !ok {
147
return nil, &badStringError{"malformed HTTP version", resp.Proto}
150
// Parse the response headers.
151
mimeHeader, err := tp.ReadMIMEHeader()
154
err = io.ErrUnexpectedEOF
158
resp.Header = Header(mimeHeader)
160
fixPragmaCacheControl(resp.Header)
162
err = readTransfer(resp, r)
170
// RFC2616: Should treat
173
// Cache-Control: no-cache
174
func fixPragmaCacheControl(header Header) {
175
if hp, ok := header["Pragma"]; ok && len(hp) > 0 && hp[0] == "no-cache" {
176
if _, presentcc := header["Cache-Control"]; !presentcc {
177
header["Cache-Control"] = []string{"no-cache"}
182
// ProtoAtLeast reports whether the HTTP protocol used
183
// in the response is at least major.minor.
184
func (r *Response) ProtoAtLeast(major, minor int) bool {
185
return r.ProtoMajor > major ||
186
r.ProtoMajor == major && r.ProtoMinor >= minor
189
// Writes the response (header, body and trailer) in wire format. This method
190
// consults the following fields of the response:
200
// Header, values for non-canonical keys will have unpredictable behavior
202
// Body is closed after it is sent.
203
func (r *Response) Write(w io.Writer) error {
208
text, ok = statusText[r.StatusCode]
210
text = "status code " + strconv.Itoa(r.StatusCode)
213
protoMajor, protoMinor := strconv.Itoa(r.ProtoMajor), strconv.Itoa(r.ProtoMinor)
214
statusCode := strconv.Itoa(r.StatusCode) + " "
215
text = strings.TrimPrefix(text, statusCode)
216
if _, err := io.WriteString(w, "HTTP/"+protoMajor+"."+protoMinor+" "+statusCode+text+"\r\n"); err != nil {
220
// Clone it, so we can modify r1 as needed.
223
if r1.ContentLength == 0 && r1.Body != nil {
224
// Is it actually 0 length? Or just unknown?
226
n, err := r1.Body.Read(buf[:])
227
if err != nil && err != io.EOF {
231
// Reset it to a known zero reader, in case underlying one
232
// is unhappy being read repeatedly.
235
r1.ContentLength = -1
240
io.MultiReader(bytes.NewReader(buf[:1]), r.Body),
245
// If we're sending a non-chunked HTTP/1.1 response without a
246
// content-length, the only way to do that is the old HTTP/1.0
247
// way, by noting the EOF with a connection close, so we need
249
if r1.ContentLength == -1 && !r1.Close && r1.ProtoAtLeast(1, 1) && !chunked(r1.TransferEncoding) {
253
// Process Body,ContentLength,Close,Trailer
254
tw, err := newTransferWriter(r1)
258
err = tw.WriteHeader(w)
264
err = r.Header.WriteSubset(w, respExcludeHeader)
269
// contentLengthAlreadySent may have been already sent for
270
// POST/PUT requests, even if zero length. See Issue 8180.
271
contentLengthAlreadySent := tw.shouldSendContentLength()
272
if r1.ContentLength == 0 && !chunked(r1.TransferEncoding) && !contentLengthAlreadySent {
273
if _, err := io.WriteString(w, "Content-Length: 0\r\n"); err != nil {
279
if _, err := io.WriteString(w, "\r\n"); err != nil {
283
// Write body and trailer
284
err = tw.WriteBody(w)