request.go 26.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

// HTTP Request reading and parsing.

package http

import (
10
	"bufio"
11
	"bytes"
12
	"crypto/tls"
13
	"encoding/base64"
14
	"errors"
15 16 17
	"fmt"
	"io"
	"io/ioutil"
18 19
	"mime"
	"mime/multipart"
20
	"net/textproto"
21
	"net/url"
22
	"strconv"
23
	"strings"
Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
24
	"sync"
25 26 27
)

const (
28
	defaultMaxMemory = 32 << 20 // 32 MB
29 30
)

31 32
// ErrMissingFile is returned by FormFile when the provided file field name
// is either not present in the request or not a file field.
33
var ErrMissingFile = errors.New("http: no such file")
34

Russ Cox's avatar
Russ Cox committed
35
// HTTP request parsing errors.
Russ Cox's avatar
Russ Cox committed
36
type ProtocolError struct {
37
	ErrorString string
Russ Cox's avatar
Russ Cox committed
38
}
Russ Cox's avatar
Russ Cox committed
39

40
func (err *ProtocolError) Error() string { return err.ErrorString }
41

Russ Cox's avatar
Russ Cox committed
42
var (
43 44 45 46 47
	ErrHeaderTooLong        = &ProtocolError{"header too long"}
	ErrShortBody            = &ProtocolError{"entity body too short"}
	ErrNotSupported         = &ProtocolError{"feature not supported"}
	ErrUnexpectedTrailer    = &ProtocolError{"trailer header without chunked transfer encoding"}
	ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"}
48
	ErrNotMultipart         = &ProtocolError{"request Content-Type isn't multipart/form-data"}
49
	ErrMissingBoundary      = &ProtocolError{"no multipart boundary param in Content-Type"}
50 51
)

Russ Cox's avatar
Russ Cox committed
52
type badStringError struct {
53 54
	what string
	str  string
Russ Cox's avatar
Russ Cox committed
55 56
}

57
func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
Russ Cox's avatar
Russ Cox committed
58

59 60
// Headers that Request.Write handles itself and should be skipped.
var reqWriteExcludeHeader = map[string]bool{
61
	"Host":              true, // not in Header map anyway
62 63 64 65
	"User-Agent":        true,
	"Content-Length":    true,
	"Transfer-Encoding": true,
	"Trailer":           true,
66
}
67

68 69
// A Request represents an HTTP request received by a server
// or to be sent by a client.
70 71 72 73
//
// The field semantics differ slightly between client and server
// usage. In addition to the notes on the fields below, see the
// documentation for Request.Write and RoundTripper.
Russ Cox's avatar
Russ Cox committed
74
type Request struct {
75 76 77
	// Method specifies the HTTP method (GET, POST, PUT, etc.).
	// For client requests an empty string means GET.
	Method string
Rick Arnold's avatar
Rick Arnold committed
78

79 80
	// URL specifies either the URI being requested (for server
	// requests) or the URL to access (for client requests).
Rick Arnold's avatar
Rick Arnold committed
81
	//
82 83 84 85 86 87 88 89 90
	// For server requests the URL is parsed from the URI
	// supplied on the Request-Line as stored in RequestURI.  For
	// most requests, fields other than Path and RawQuery will be
	// empty. (See RFC 2616, Section 5.1.2)
	//
	// For client requests, the URL's Host specifies the server to
	// connect to, while the Request's Host field optionally
	// specifies the Host header value to send in the HTTP
	// request.
Rick Arnold's avatar
Rick Arnold committed
91
	URL *url.URL
92 93

	// The protocol version for incoming requests.
94
	// Client requests always use HTTP/1.1.
95 96 97
	Proto      string // "HTTP/1.0"
	ProtoMajor int    // 1
	ProtoMinor int    // 0
Russ Cox's avatar
Russ Cox committed
98

Russ Cox's avatar
Russ Cox committed
99
	// A header maps request lines to their values.
Russ Cox's avatar
Russ Cox committed
100 101 102
	// If the header says
	//
	//	accept-encoding: gzip, deflate
Andrew Gerrand's avatar
Andrew Gerrand committed
103
	//	Accept-Language: en-us
Russ Cox's avatar
Russ Cox committed
104 105 106 107
	//	Connection: keep-alive
	//
	// then
	//
108 109 110 111
	//	Header = map[string][]string{
	//		"Accept-Encoding": {"gzip, deflate"},
	//		"Accept-Language": {"en-us"},
	//		"Connection": {"keep-alive"},
Russ Cox's avatar
Russ Cox committed
112 113 114 115 116 117
	//	}
	//
	// HTTP defines that header names are case-insensitive.
	// The request parser implements this by canonicalizing the
	// name, making the first character and any characters
	// following a hyphen uppercase and the rest lowercase.
118 119 120 121 122
	//
	// For client requests certain headers are automatically
	// added and may override values in Header.
	//
	// See the documentation for the Request.Write method.
123
	Header Header
Russ Cox's avatar
Russ Cox committed
124

125 126
	// Body is the request's body.
	//
127
	// For client requests a nil body means the request has no
128 129 130
	// body, such as a GET request. The HTTP Client's Transport
	// is responsible for calling the Close method.
	//
131
	// For server requests the Request Body is always non-nil
132 133 134
	// but will return EOF immediately when no body is present.
	// The Server will close the request body. The ServeHTTP
	// Handler does not need to.
135
	Body io.ReadCloser
David Symonds's avatar
David Symonds committed
136

137 138
	// ContentLength records the length of the associated content.
	// The value -1 indicates that the length is unknown.
139 140
	// Values >= 0 indicate that the given number of bytes may
	// be read from Body.
141
	// For client requests, a value of 0 means unknown if Body is not nil.
142 143
	ContentLength int64

144 145 146 147 148
	// TransferEncoding lists the transfer encodings from outermost to
	// innermost. An empty list denotes the "identity" encoding.
	// TransferEncoding can usually be ignored; chunked encoding is
	// automatically added and removed as necessary when sending and
	// receiving requests.
149 150
	TransferEncoding []string

151
	// Close indicates whether to close the connection after
152 153
	// replying to this request (for servers) or after sending
	// the request (for clients).
154
	Close bool
Russ Cox's avatar
Russ Cox committed
155

156 157 158
	// For server requests Host specifies the host on which the
	// URL is sought. Per RFC 2616, this is either the value of
	// the "Host" header or the host name given in the URL itself.
159
	// It may be of the form "host:port".
160 161 162 163
	//
	// For client requests Host optionally overrides the Host
	// header to send. If empty, the Request.Write method uses
	// the value of URL.Host.
164
	Host string
Russ Cox's avatar
Russ Cox committed
165

166 167 168 169
	// Form contains the parsed form data, including both the URL
	// field's query parameters and the POST or PUT form data.
	// This field is only available after ParseForm is called.
	// The HTTP client ignores Form and uses Body instead.
Rob Pike's avatar
Rob Pike committed
170
	Form url.Values
171

172 173 174 175 176 177
	// PostForm contains the parsed form data from POST or PUT
	// body parameters.
	// This field is only available after ParseForm is called.
	// The HTTP client ignores PostForm and uses Body instead.
	PostForm url.Values

178 179 180
	// MultipartForm is the parsed multipart form, including file uploads.
	// This field is only available after ParseMultipartForm is called.
	// The HTTP client ignores MultipartForm and uses Body instead.
181 182
	MultipartForm *multipart.Form

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	// Trailer specifies additional headers that are sent after the request
	// body.
	//
	// For server requests the Trailer map initially contains only the
	// trailer keys, with nil values. (The client declares which trailers it
	// will later send.)  While the handler is reading from Body, it must
	// not reference Trailer. After reading from Body returns EOF, Trailer
	// can be read again and will contain non-nil values, if they were sent
	// by the client.
	//
	// For client requests Trailer must be initialized to a map containing
	// the trailer keys to later send. The values may be nil or their final
	// values. The ContentLength must be 0 or -1, to send a chunked request.
	// After the HTTP request is sent the map values can be updated while
	// the request body is read. Once the body returns EOF, the caller must
	// not mutate Trailer.
	//
	// Few HTTP clients, servers, or proxies support HTTP trailers.
201
	Trailer Header
202 203 204 205 206 207 208

	// RemoteAddr allows HTTP servers and other software to record
	// the network address that sent the request, usually for
	// logging. This field is not filled in by ReadRequest and
	// has no defined format. The HTTP server in this package
	// sets RemoteAddr to an "IP:port" address before invoking a
	// handler.
209
	// This field is ignored by the HTTP client.
210 211
	RemoteAddr string

212 213 214 215 216 217
	// RequestURI is the unmodified Request-URI of the
	// Request-Line (RFC 2616, Section 5.1) as sent by the client
	// to a server. Usually the URL field should be used instead.
	// It is an error to set this field in an HTTP client request.
	RequestURI string

218 219 220 221 222 223
	// TLS allows HTTP servers and other software to record
	// information about the TLS connection on which the request
	// was received. This field is not filled in by ReadRequest.
	// The HTTP server in this package sets the field for
	// TLS-enabled connections before invoking a handler;
	// otherwise it leaves the field nil.
224
	// This field is ignored by the HTTP client.
225
	TLS *tls.ConnectionState
226 227
}

228
// ProtoAtLeast reports whether the HTTP protocol used
Russ Cox's avatar
Russ Cox committed
229
// in the request is at least major.minor.
Russ Cox's avatar
Russ Cox committed
230 231
func (r *Request) ProtoAtLeast(major, minor int) bool {
	return r.ProtoMajor > major ||
232
		r.ProtoMajor == major && r.ProtoMinor >= minor
Russ Cox's avatar
Russ Cox committed
233 234
}

235 236 237 238 239 240 241 242 243 244
// UserAgent returns the client's User-Agent, if sent in the request.
func (r *Request) UserAgent() string {
	return r.Header.Get("User-Agent")
}

// Cookies parses and returns the HTTP cookies sent with the request.
func (r *Request) Cookies() []*Cookie {
	return readCookies(r.Header, "")
}

Russ Cox's avatar
Russ Cox committed
245
var ErrNoCookie = errors.New("http: named cookie not present")
246 247 248

// Cookie returns the named cookie provided in the request or
// ErrNoCookie if not found.
249
func (r *Request) Cookie(name string) (*Cookie, error) {
250 251 252 253 254 255 256 257 258 259 260
	for _, c := range readCookies(r.Header, name) {
		return c, nil
	}
	return nil, ErrNoCookie
}

// AddCookie adds a cookie to the request.  Per RFC 6265 section 5.4,
// AddCookie does not attach more than one Cookie header field.  That
// means all cookies, if any, are written into the same line,
// separated by semicolon.
func (r *Request) AddCookie(c *Cookie) {
261
	s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value))
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	if c := r.Header.Get("Cookie"); c != "" {
		r.Header.Set("Cookie", c+"; "+s)
	} else {
		r.Header.Set("Cookie", s)
	}
}

// Referer returns the referring URL, if sent in the request.
//
// Referer is misspelled as in the request itself, a mistake from the
// earliest days of HTTP.  This value can also be fetched from the
// Header map as Header["Referer"]; the benefit of making it available
// as a method is that the compiler can diagnose programs that use the
// alternate (correct English) spelling req.Referrer() but cannot
// diagnose programs that use Header["Referrer"].
func (r *Request) Referer() string {
	return r.Header.Get("Referer")
}

281 282 283 284 285 286 287 288
// multipartByReader is a sentinel value.
// Its presence in Request.MultipartForm indicates that parsing of the request
// body has been handed off to a MultipartReader instead of ParseMultipartFrom.
var multipartByReader = &multipart.Form{
	Value: make(map[string][]string),
	File:  make(map[string][]*multipart.FileHeader),
}

289 290
// MultipartReader returns a MIME multipart reader if this is a
// multipart/form-data POST request, else returns nil and an error.
291 292
// Use this function instead of ParseMultipartForm to
// process the request body as a stream.
293
func (r *Request) MultipartReader() (*multipart.Reader, error) {
294
	if r.MultipartForm == multipartByReader {
295
		return nil, errors.New("http: MultipartReader called twice")
296 297
	}
	if r.MultipartForm != nil {
298
		return nil, errors.New("http: multipart handled by ParseMultipartForm")
299 300 301 302 303
	}
	r.MultipartForm = multipartByReader
	return r.multipartReader()
}

304
func (r *Request) multipartReader() (*multipart.Reader, error) {
305 306
	v := r.Header.Get("Content-Type")
	if v == "" {
307 308
		return nil, ErrNotMultipart
	}
309 310
	d, params, err := mime.ParseMediaType(v)
	if err != nil || d != "multipart/form-data" {
311 312 313 314 315 316 317 318 319
		return nil, ErrNotMultipart
	}
	boundary, ok := params["boundary"]
	if !ok {
		return nil, ErrMissingBoundary
	}
	return multipart.NewReader(r.Body, boundary), nil
}

Steve Newman's avatar
Steve Newman committed
320 321 322
// Return value if nonempty, def otherwise.
func valueOrDefault(value, def string) string {
	if value != "" {
323
		return value
Steve Newman's avatar
Steve Newman committed
324
	}
325
	return def
Steve Newman's avatar
Steve Newman committed
326 327
}

328 329 330 331 332
// NOTE: This is not intended to reflect the actual Go version being used.
// It was changed from "Go http package" to "Go 1.1 package http" at the
// time of the Go 1.1 release because the former User-Agent had ended up
// on a blacklist for some intrusion detection systems.
// See https://codereview.appspot.com/7532043.
333
const defaultUserAgent = "Go 1.1 package http"
Steve Newman's avatar
Steve Newman committed
334

335
// Write writes an HTTP/1.1 request -- header and body -- in wire format.
336
// This method consults the following fields of the request:
337
//	Host
338
//	URL
339
//	Method (defaults to "GET")
340
//	Header
341 342
//	ContentLength
//	TransferEncoding
343 344
//	Body
//
345 346 347
// If Body is present, Content-Length is <= 0 and TransferEncoding
// hasn't been set to "identity", Write adds "Transfer-Encoding:
// chunked" to the header. Body is closed after it is sent.
348 349
func (r *Request) Write(w io.Writer) error {
	return r.write(w, false, nil)
350 351 352
}

// WriteProxy is like Write but writes the request in the form
353 354
// expected by an HTTP proxy.  In particular, WriteProxy writes the
// initial Request-URI line of the request with an absolute URI, per
355 356 357 358 359
// section 5.1.2 of RFC 2616, including the scheme and host.
// In either case, WriteProxy also writes a Host header, using
// either r.Host or r.URL.Host.
func (r *Request) WriteProxy(w io.Writer) error {
	return r.write(w, true, nil)
360 361
}

362
// extraHeaders may be nil
363
func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header) error {
364 365
	host := req.Host
	if host == "" {
366
		if req.URL == nil {
367
			return errors.New("http: Request.Write on Request with no Host or URL set")
368
		}
369 370 371
		host = req.URL.Host
	}

372 373 374
	ruri := req.URL.RequestURI()
	if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" {
		ruri = req.URL.Scheme + "://" + host + ruri
375 376 377
	} else if req.Method == "CONNECT" && req.URL.Path == "" {
		// CONNECT requests normally give just the host and port, not a full URL.
		ruri = host
378
	}
379
	// TODO(bradfitz): escape at least newlines in ruri?
380

381 382 383 384 385 386 387 388 389 390
	// Wrap the writer in a bufio Writer if it's not already buffered.
	// Don't always call NewWriter, as that forces a bytes.Buffer
	// and other small bufio Writers to have a minimum 4k buffer
	// size.
	var bw *bufio.Writer
	if _, ok := w.(io.ByteWriter); !ok {
		bw = bufio.NewWriter(w)
		w = bw
	}

391 392 393 394
	_, err := fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri)
	if err != nil {
		return err
	}
395 396

	// Header lines
397 398 399 400
	_, err = fmt.Fprintf(w, "Host: %s\r\n", host)
	if err != nil {
		return err
	}
401 402 403 404 405 406 407 408 409 410

	// Use the defaultUserAgent unless the Header contains one, which
	// may be blank to not send the header.
	userAgent := defaultUserAgent
	if req.Header != nil {
		if ua := req.Header["User-Agent"]; len(ua) > 0 {
			userAgent = ua[0]
		}
	}
	if userAgent != "" {
411 412 413 414
		_, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent)
		if err != nil {
			return err
		}
Steve Newman's avatar
Steve Newman committed
415 416
	}

417 418 419 420 421
	// Process Body,ContentLength,Close,Trailer
	tw, err := newTransferWriter(req)
	if err != nil {
		return err
	}
422
	err = tw.WriteHeader(w)
423 424
	if err != nil {
		return err
425 426
	}

427
	err = req.Header.WriteSubset(w, reqWriteExcludeHeader)
428 429 430
	if err != nil {
		return err
	}
Steve Newman's avatar
Steve Newman committed
431

432
	if extraHeaders != nil {
433
		err = extraHeaders.Write(w)
434 435 436 437 438
		if err != nil {
			return err
		}
	}

439 440 441 442
	_, err = io.WriteString(w, "\r\n")
	if err != nil {
		return err
	}
Steve Newman's avatar
Steve Newman committed
443

444
	// Write body and trailer
445
	err = tw.WriteBody(w)
446 447
	if err != nil {
		return err
Steve Newman's avatar
Steve Newman committed
448
	}
449

450 451 452 453
	if bw != nil {
		return bw.Flush()
	}
	return nil
Steve Newman's avatar
Steve Newman committed
454 455
}

456
// ParseHTTPVersion parses a HTTP version string.
457
// "HTTP/1.0" returns (1, 0, true).
458
func ParseHTTPVersion(vers string) (major, minor int, ok bool) {
459 460 461 462 463 464 465 466 467 468
	const Big = 1000000 // arbitrary upper bound
	switch vers {
	case "HTTP/1.1":
		return 1, 1, true
	case "HTTP/1.0":
		return 1, 0, true
	}
	if !strings.HasPrefix(vers, "HTTP/") {
		return 0, 0, false
	}
469
	dot := strings.Index(vers, ".")
470
	if dot < 0 {
471
		return 0, 0, false
472
	}
473 474
	major, err := strconv.Atoi(vers[5:dot])
	if err != nil || major < 0 || major > Big {
475
		return 0, 0, false
476
	}
477 478
	minor, err = strconv.Atoi(vers[dot+1:])
	if err != nil || minor < 0 || minor > Big {
479
		return 0, 0, false
480
	}
481
	return major, minor, true
482 483
}

484
// NewRequest returns a new Request given a method, URL, and optional body.
485 486 487 488
//
// If the provided body is also an io.Closer, the returned
// Request.Body is set to body and will be closed by the Client
// methods Do, Post, and PostForm, and Transport.RoundTrip.
489
func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
Rob Pike's avatar
Rob Pike committed
490
	u, err := url.Parse(urlStr)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	if err != nil {
		return nil, err
	}
	rc, ok := body.(io.ReadCloser)
	if !ok && body != nil {
		rc = ioutil.NopCloser(body)
	}
	req := &Request{
		Method:     method,
		URL:        u,
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header:     make(Header),
		Body:       rc,
		Host:       u.Host,
	}
508 509 510 511
	if body != nil {
		switch v := body.(type) {
		case *bytes.Buffer:
			req.ContentLength = int64(v.Len())
512 513 514 515
		case *bytes.Reader:
			req.ContentLength = int64(v.Len())
		case *strings.Reader:
			req.ContentLength = int64(v.Len())
516
		}
517 518
	}

519 520 521
	return req, nil
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
// See RFC 2617, Section 2.
func (r *Request) BasicAuth() (username, password string, ok bool) {
	auth := r.Header.Get("Authorization")
	if auth == "" {
		return
	}
	return parseBasicAuth(auth)
}

// parseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func parseBasicAuth(auth string) (username, password string, ok bool) {
536 537
	const prefix = "Basic "
	if !strings.HasPrefix(auth, prefix) {
538 539
		return
	}
540
	c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
541 542 543 544 545 546 547 548 549 550 551
	if err != nil {
		return
	}
	cs := string(c)
	s := strings.IndexByte(cs, ':')
	if s < 0 {
		return
	}
	return cs[:s], cs[s+1:], true
}

552 553 554 555 556 557
// SetBasicAuth sets the request's Authorization header to use HTTP
// Basic Authentication with the provided username and password.
//
// With HTTP Basic Authentication the provided username and password
// are not encrypted.
func (r *Request) SetBasicAuth(username, password string) {
558
	r.Header.Set("Authorization", "Basic "+basicAuth(username, password))
559 560
}

561 562
// parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
func parseRequestLine(line string) (method, requestURI, proto string, ok bool) {
563 564
	s1 := strings.Index(line, " ")
	s2 := strings.Index(line[s1+1:], " ")
565 566 567 568 569 570 571
	if s1 < 0 || s2 < 0 {
		return
	}
	s2 += s1 + 1
	return line[:s1], line[s1+1 : s2], line[s2+1:], true
}

Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
572
var textprotoReaderPool sync.Pool
573 574

func newTextprotoReader(br *bufio.Reader) *textproto.Reader {
Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
575 576 577 578
	if v := textprotoReaderPool.Get(); v != nil {
		tr := v.(*textproto.Reader)
		tr.R = br
		return tr
579
	}
Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
580
	return textproto.NewReader(br)
581 582 583 584
}

func putTextprotoReader(r *textproto.Reader) {
	r.R = nil
Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
585
	textprotoReaderPool.Put(r)
586 587
}

Russ Cox's avatar
Russ Cox committed
588
// ReadRequest reads and parses a request from b.
589
func ReadRequest(b *bufio.Reader) (req *Request, err error) {
590

591
	tp := newTextprotoReader(b)
592
	req = new(Request)
593 594

	// First line: GET /index.html HTTP/1.0
595
	var s string
596
	if s, err = tp.ReadLine(); err != nil {
597 598 599
		return nil, err
	}
	defer func() {
600
		putTextprotoReader(tp)
601
		if err == io.EOF {
602 603
			err = io.ErrUnexpectedEOF
		}
604
	}()
605

606 607 608
	var ok bool
	req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s)
	if !ok {
609
		return nil, &badStringError{"malformed HTTP request", s}
610
	}
611
	rawurl := req.RequestURI
612
	if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok {
613
		return nil, &badStringError{"malformed HTTP version", req.Proto}
614 615
	}

616 617 618 619 620 621 622 623 624 625 626 627 628 629
	// CONNECT requests are used two different ways, and neither uses a full URL:
	// The standard use is to tunnel HTTPS through an HTTP proxy.
	// It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is
	// just the authority section of a URL. This information should go in req.URL.Host.
	//
	// The net/rpc package also uses CONNECT, but there the parameter is a path
	// that starts with a slash. It can be parsed with the regular URL parser,
	// and the path will end up in req.URL.Path, where it needs to be in order for
	// RPC to work.
	justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/")
	if justAuthority {
		rawurl = "http://" + rawurl
	}

Russ Cox's avatar
Russ Cox committed
630
	if req.URL, err = url.ParseRequestURI(rawurl); err != nil {
Russ Cox's avatar
Russ Cox committed
631
		return nil, err
632 633
	}

634 635 636 637 638
	if justAuthority {
		// Strip the bogus "http://" back off.
		req.URL.Scheme = ""
	}

639
	// Subsequent lines: Key: value.
640 641 642
	mimeHeader, err := tp.ReadMIMEHeader()
	if err != nil {
		return nil, err
643
	}
644
	req.Header = Header(mimeHeader)
645 646 647 648 649 650 651 652

	// RFC2616: Must treat
	//	GET /index.html HTTP/1.1
	//	Host: www.google.com
	// and
	//	GET http://www.google.com/index.html HTTP/1.1
	//	Host: doesntmatter
	// the same.  In the second case, any Host line is ignored.
Russ Cox's avatar
Russ Cox committed
653
	req.Host = req.URL.Host
654
	if req.Host == "" {
655
		req.Host = req.Header.get("Host")
656
	}
657
	delete(req.Header, "Host")
658

659
	fixPragmaCacheControl(req.Header)
660

661 662 663
	err = readTransfer(req, b)
	if err != nil {
		return nil, err
David Symonds's avatar
David Symonds committed
664 665
	}

666
	req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header, false)
667
	return req, nil
668
}
669

670
// MaxBytesReader is similar to io.LimitReader but is intended for
671
// limiting the size of incoming request bodies. In contrast to
672
// io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a
673
// non-EOF error for a Read beyond the limit, and closes the
674 675 676 677
// underlying reader when its Close method is called.
//
// MaxBytesReader prevents clients from accidentally or maliciously
// sending a large request and wasting server resources.
678 679 680 681 682 683 684 685 686 687 688
func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser {
	return &maxBytesReader{w: w, r: r, n: n}
}

type maxBytesReader struct {
	w       ResponseWriter
	r       io.ReadCloser // underlying reader
	n       int64         // max bytes remaining
	stopped bool
}

689
func (l *maxBytesReader) Read(p []byte) (n int, err error) {
690 691 692 693 694 695 696
	if l.n <= 0 {
		if !l.stopped {
			l.stopped = true
			if res, ok := l.w.(*response); ok {
				res.requestTooLarge()
			}
		}
697
		return 0, errors.New("http: request body too large")
698 699 700 701 702 703 704 705 706
	}
	if int64(len(p)) > l.n {
		p = p[:l.n]
	}
	n, err = l.r.Read(p)
	l.n -= int64(n)
	return
}

707
func (l *maxBytesReader) Close() error {
708 709 710
	return l.r.Close()
}

711 712 713 714 715 716 717 718 719 720 721 722 723 724
func copyValues(dst, src url.Values) {
	for k, vs := range src {
		for _, value := range vs {
			dst.Add(k, value)
		}
	}
}

func parsePostForm(r *Request) (vs url.Values, err error) {
	if r.Body == nil {
		err = errors.New("missing form body")
		return
	}
	ct := r.Header.Get("Content-Type")
725 726 727 728 729
	// RFC 2616, section 7.2.1 - empty type
	//   SHOULD be treated as application/octet-stream
	if ct == "" {
		ct = "application/octet-stream"
	}
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	ct, _, err = mime.ParseMediaType(ct)
	switch {
	case ct == "application/x-www-form-urlencoded":
		var reader io.Reader = r.Body
		maxFormSize := int64(1<<63 - 1)
		if _, ok := r.Body.(*maxBytesReader); !ok {
			maxFormSize = int64(10 << 20) // 10 MB is a lot of text.
			reader = io.LimitReader(r.Body, maxFormSize+1)
		}
		b, e := ioutil.ReadAll(reader)
		if e != nil {
			if err == nil {
				err = e
			}
			break
		}
		if int64(len(b)) > maxFormSize {
			err = errors.New("http: POST too large")
			return
		}
		vs, e = url.ParseQuery(string(b))
		if err == nil {
			err = e
		}
	case ct == "multipart/form-data":
		// handled by ParseMultipartForm (which is calling us, or should be)
		// TODO(bradfitz): there are too many possible
		// orders to call too many functions here.
		// Clean this up and write more tests.
		// request_test.go contains the start of this,
760
		// in TestParseMultipartFormOrder and others.
761 762 763 764
	}
	return
}

765 766 767 768 769 770
// ParseForm parses the raw query from the URL and updates r.Form.
//
// For POST or PUT requests, it also parses the request body as a form and
// put the results into both r.PostForm and r.Form.
// POST and PUT body parameters take precedence over URL query string values
// in r.Form.
771 772 773 774
//
// If the request Body's size has not already been limited by MaxBytesReader,
// the size is capped at 10MB.
//
775
// ParseMultipartForm calls ParseForm automatically.
776
// It is idempotent.
777 778
func (r *Request) ParseForm() error {
	var err error
779
	if r.PostForm == nil {
780
		if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
781 782 783 784 785
			r.PostForm, err = parsePostForm(r)
		}
		if r.PostForm == nil {
			r.PostForm = make(url.Values)
		}
786
	}
787 788 789 790
	if r.Form == nil {
		if len(r.PostForm) > 0 {
			r.Form = make(url.Values)
			copyValues(r.Form, r.PostForm)
791
		}
792 793 794 795
		var newValues url.Values
		if r.URL != nil {
			var e error
			newValues, e = url.ParseQuery(r.URL.RawQuery)
796 797
			if err == nil {
				err = e
798
			}
799 800 801 802 803 804 805 806
		}
		if newValues == nil {
			newValues = make(url.Values)
		}
		if r.Form == nil {
			r.Form = newValues
		} else {
			copyValues(r.Form, newValues)
807 808
		}
	}
809
	return err
810 811
}

812 813 814 815 816 817
// ParseMultipartForm parses a request body as multipart/form-data.
// The whole request body is parsed and up to a total of maxMemory bytes of
// its file parts are stored in memory, with the remainder stored on
// disk in temporary files.
// ParseMultipartForm calls ParseForm if necessary.
// After one call to ParseMultipartForm, subsequent calls have no effect.
818
func (r *Request) ParseMultipartForm(maxMemory int64) error {
819
	if r.MultipartForm == multipartByReader {
820
		return errors.New("http: multipart handled by MultipartReader")
821
	}
822 823 824 825 826 827 828 829 830 831 832
	if r.Form == nil {
		err := r.ParseForm()
		if err != nil {
			return err
		}
	}
	if r.MultipartForm != nil {
		return nil
	}

	mr, err := r.multipartReader()
833
	if err != nil {
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		return err
	}

	f, err := mr.ReadForm(maxMemory)
	if err != nil {
		return err
	}
	for k, v := range f.Value {
		r.Form[k] = append(r.Form[k], v...)
	}
	r.MultipartForm = f

	return nil
}

849
// FormValue returns the first value for the named component of the query.
850
// POST and PUT body parameters take precedence over URL query string values.
851 852
// FormValue calls ParseMultipartForm and ParseForm if necessary and ignores
// any errors returned by these functions.
853 854
// To access multiple values of the same key, call ParseForm and
// then inspect Request.Form directly.
855 856
func (r *Request) FormValue(key string) string {
	if r.Form == nil {
857
		r.ParseMultipartForm(defaultMaxMemory)
858
	}
859
	if vs := r.Form[key]; len(vs) > 0 {
860
		return vs[0]
861
	}
862
	return ""
863
}
864

865 866
// PostFormValue returns the first value for the named component of the POST
// or PUT request body. URL query parameters are ignored.
867 868
// PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores
// any errors returned by these functions.
869 870 871 872 873 874 875 876 877 878
func (r *Request) PostFormValue(key string) string {
	if r.PostForm == nil {
		r.ParseMultipartForm(defaultMaxMemory)
	}
	if vs := r.PostForm[key]; len(vs) > 0 {
		return vs[0]
	}
	return ""
}

879 880
// FormFile returns the first file for the provided form key.
// FormFile calls ParseMultipartForm and ParseForm if necessary.
881
func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
882
	if r.MultipartForm == multipartByReader {
883
		return nil, nil, errors.New("http: multipart handled by MultipartReader")
884 885 886 887 888 889 890
	}
	if r.MultipartForm == nil {
		err := r.ParseMultipartForm(defaultMaxMemory)
		if err != nil {
			return nil, nil, err
		}
	}
891 892 893 894 895
	if r.MultipartForm != nil && r.MultipartForm.File != nil {
		if fhs := r.MultipartForm.File[key]; len(fhs) > 0 {
			f, err := fhs[0].Open()
			return f, fhs[0], err
		}
896 897 898 899
	}
	return nil, nil, ErrMissingFile
}

900
func (r *Request) expectsContinue() bool {
901
	return hasToken(r.Header.get("Expect"), "100-continue")
902
}
903 904 905 906 907

func (r *Request) wantsHttp10KeepAlive() bool {
	if r.ProtoMajor != 1 || r.ProtoMinor != 0 {
		return false
	}
908
	return hasToken(r.Header.get("Connection"), "keep-alive")
Russ Cox's avatar
Russ Cox committed
909 910 911
}

func (r *Request) wantsClose() bool {
912
	return hasToken(r.Header.get("Connection"), "close")
Russ Cox's avatar
Russ Cox committed
913
}
914 915 916 917 918 919

func (r *Request) closeBody() {
	if r.Body != nil {
		r.Body.Close()
	}
}