github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/net/http/request.go (about) 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. 4 5 // HTTP Request reading and parsing. 6 7 package http 8 9 import ( 10 "bufio" 11 "bytes" 12 "crypto/tls" 13 "encoding/base64" 14 "errors" 15 "fmt" 16 "io" 17 "io/ioutil" 18 "mime" 19 "mime/multipart" 20 "net/textproto" 21 "net/url" 22 "strconv" 23 "strings" 24 "sync" 25 ) 26 27 const ( 28 defaultMaxMemory = 32 << 20 // 32 MB 29 ) 30 31 // ErrMissingFile is returned by FormFile when the provided file field name 32 // is either not present in the request or not a file field. 33 var ErrMissingFile = errors.New("http: no such file") 34 35 // HTTP request parsing errors. 36 type ProtocolError struct { 37 ErrorString string 38 } 39 40 func (err *ProtocolError) Error() string { return err.ErrorString } 41 42 var ( 43 ErrHeaderTooLong = &ProtocolError{"header too long"} 44 ErrShortBody = &ProtocolError{"entity body too short"} 45 ErrNotSupported = &ProtocolError{"feature not supported"} 46 ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"} 47 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 52 type badStringError struct { 53 what string 54 str string 55 } 56 57 func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 58 59 // Headers that Request.Write handles itself and should be skipped. 60 var reqWriteExcludeHeader = map[string]bool{ 61 "Host": true, // not in Header map anyway 62 "User-Agent": true, 63 "Content-Length": true, 64 "Transfer-Encoding": true, 65 "Trailer": true, 66 } 67 68 // A Request represents an HTTP request received by a server 69 // or to be sent by a client. 70 // 71 // The field semantics differ slightly between client and server 72 // usage. In addition to the notes on the fields below, see the 73 // documentation for Request.Write and RoundTripper. 74 type Request struct { 75 // Method specifies the HTTP method (GET, POST, PUT, etc.). 76 // For client requests an empty string means GET. 77 Method string 78 79 // URL specifies either the URI being requested (for server 80 // requests) or the URL to access (for client requests). 81 // 82 // For server requests the URL is parsed from the URI 83 // supplied on the Request-Line as stored in RequestURI. For 84 // most requests, fields other than Path and RawQuery will be 85 // empty. (See RFC 2616, Section 5.1.2) 86 // 87 // For client requests, the URL's Host specifies the server to 88 // connect to, while the Request's Host field optionally 89 // specifies the Host header value to send in the HTTP 90 // request. 91 URL *url.URL 92 93 // The protocol version for incoming requests. 94 // Client requests always use HTTP/1.1. 95 Proto string // "HTTP/1.0" 96 ProtoMajor int // 1 97 ProtoMinor int // 0 98 99 // A header maps request lines to their values. 100 // If the header says 101 // 102 // accept-encoding: gzip, deflate 103 // Accept-Language: en-us 104 // Connection: keep-alive 105 // 106 // then 107 // 108 // Header = map[string][]string{ 109 // "Accept-Encoding": {"gzip, deflate"}, 110 // "Accept-Language": {"en-us"}, 111 // "Connection": {"keep-alive"}, 112 // } 113 // 114 // HTTP defines that header names are case-insensitive. 115 // The request parser implements this by canonicalizing the 116 // name, making the first character and any characters 117 // following a hyphen uppercase and the rest lowercase. 118 // 119 // For client requests certain headers are automatically 120 // added and may override values in Header. 121 // 122 // See the documentation for the Request.Write method. 123 Header Header 124 125 // Body is the request's body. 126 // 127 // For client requests a nil body means the request has no 128 // body, such as a GET request. The HTTP Client's Transport 129 // is responsible for calling the Close method. 130 // 131 // For server requests the Request Body is always non-nil 132 // but will return EOF immediately when no body is present. 133 // The Server will close the request body. The ServeHTTP 134 // Handler does not need to. 135 Body io.ReadCloser 136 137 // ContentLength records the length of the associated content. 138 // The value -1 indicates that the length is unknown. 139 // Values >= 0 indicate that the given number of bytes may 140 // be read from Body. 141 // For client requests, a value of 0 means unknown if Body is not nil. 142 ContentLength int64 143 144 // TransferEncoding lists the transfer encodings from outermost to 145 // innermost. An empty list denotes the "identity" encoding. 146 // TransferEncoding can usually be ignored; chunked encoding is 147 // automatically added and removed as necessary when sending and 148 // receiving requests. 149 TransferEncoding []string 150 151 // Close indicates whether to close the connection after 152 // replying to this request (for servers) or after sending 153 // the request (for clients). 154 Close bool 155 156 // For server requests Host specifies the host on which the 157 // URL is sought. Per RFC 2616, this is either the value of 158 // the "Host" header or the host name given in the URL itself. 159 // It may be of the form "host:port". 160 // 161 // For client requests Host optionally overrides the Host 162 // header to send. If empty, the Request.Write method uses 163 // the value of URL.Host. 164 Host string 165 166 // Form contains the parsed form data, including both the URL 167 // field's query parameters and the POST or PUT form data. 168 // This field is only available after ParseForm is called. 169 // The HTTP client ignores Form and uses Body instead. 170 Form url.Values 171 172 // PostForm contains the parsed form data from POST, PATCH, 173 // or PUT body parameters. 174 // 175 // This field is only available after ParseForm is called. 176 // The HTTP client ignores PostForm and uses Body instead. 177 PostForm url.Values 178 179 // MultipartForm is the parsed multipart form, including file uploads. 180 // This field is only available after ParseMultipartForm is called. 181 // The HTTP client ignores MultipartForm and uses Body instead. 182 MultipartForm *multipart.Form 183 184 // Trailer specifies additional headers that are sent after the request 185 // body. 186 // 187 // For server requests the Trailer map initially contains only the 188 // trailer keys, with nil values. (The client declares which trailers it 189 // will later send.) While the handler is reading from Body, it must 190 // not reference Trailer. After reading from Body returns EOF, Trailer 191 // can be read again and will contain non-nil values, if they were sent 192 // by the client. 193 // 194 // For client requests Trailer must be initialized to a map containing 195 // the trailer keys to later send. The values may be nil or their final 196 // values. The ContentLength must be 0 or -1, to send a chunked request. 197 // After the HTTP request is sent the map values can be updated while 198 // the request body is read. Once the body returns EOF, the caller must 199 // not mutate Trailer. 200 // 201 // Few HTTP clients, servers, or proxies support HTTP trailers. 202 Trailer Header 203 204 // RemoteAddr allows HTTP servers and other software to record 205 // the network address that sent the request, usually for 206 // logging. This field is not filled in by ReadRequest and 207 // has no defined format. The HTTP server in this package 208 // sets RemoteAddr to an "IP:port" address before invoking a 209 // handler. 210 // This field is ignored by the HTTP client. 211 RemoteAddr string 212 213 // RequestURI is the unmodified Request-URI of the 214 // Request-Line (RFC 2616, Section 5.1) as sent by the client 215 // to a server. Usually the URL field should be used instead. 216 // It is an error to set this field in an HTTP client request. 217 RequestURI string 218 219 // TLS allows HTTP servers and other software to record 220 // information about the TLS connection on which the request 221 // was received. This field is not filled in by ReadRequest. 222 // The HTTP server in this package sets the field for 223 // TLS-enabled connections before invoking a handler; 224 // otherwise it leaves the field nil. 225 // This field is ignored by the HTTP client. 226 TLS *tls.ConnectionState 227 228 // Cancel is an optional channel whose closure indicates that the client 229 // request should be regarded as canceled. Not all implementations of 230 // RoundTripper may support Cancel. 231 // 232 // For server requests, this field is not applicable. 233 Cancel <-chan struct{} 234 } 235 236 // ProtoAtLeast reports whether the HTTP protocol used 237 // in the request is at least major.minor. 238 func (r *Request) ProtoAtLeast(major, minor int) bool { 239 return r.ProtoMajor > major || 240 r.ProtoMajor == major && r.ProtoMinor >= minor 241 } 242 243 // UserAgent returns the client's User-Agent, if sent in the request. 244 func (r *Request) UserAgent() string { 245 return r.Header.Get("User-Agent") 246 } 247 248 // Cookies parses and returns the HTTP cookies sent with the request. 249 func (r *Request) Cookies() []*Cookie { 250 return readCookies(r.Header, "") 251 } 252 253 // ErrNoCookie is returned by Request's Cookie method when a cookie is not found. 254 var ErrNoCookie = errors.New("http: named cookie not present") 255 256 // Cookie returns the named cookie provided in the request or 257 // ErrNoCookie if not found. 258 func (r *Request) Cookie(name string) (*Cookie, error) { 259 for _, c := range readCookies(r.Header, name) { 260 return c, nil 261 } 262 return nil, ErrNoCookie 263 } 264 265 // AddCookie adds a cookie to the request. Per RFC 6265 section 5.4, 266 // AddCookie does not attach more than one Cookie header field. That 267 // means all cookies, if any, are written into the same line, 268 // separated by semicolon. 269 func (r *Request) AddCookie(c *Cookie) { 270 s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value)) 271 if c := r.Header.Get("Cookie"); c != "" { 272 r.Header.Set("Cookie", c+"; "+s) 273 } else { 274 r.Header.Set("Cookie", s) 275 } 276 } 277 278 // Referer returns the referring URL, if sent in the request. 279 // 280 // Referer is misspelled as in the request itself, a mistake from the 281 // earliest days of HTTP. This value can also be fetched from the 282 // Header map as Header["Referer"]; the benefit of making it available 283 // as a method is that the compiler can diagnose programs that use the 284 // alternate (correct English) spelling req.Referrer() but cannot 285 // diagnose programs that use Header["Referrer"]. 286 func (r *Request) Referer() string { 287 return r.Header.Get("Referer") 288 } 289 290 // multipartByReader is a sentinel value. 291 // Its presence in Request.MultipartForm indicates that parsing of the request 292 // body has been handed off to a MultipartReader instead of ParseMultipartFrom. 293 var multipartByReader = &multipart.Form{ 294 Value: make(map[string][]string), 295 File: make(map[string][]*multipart.FileHeader), 296 } 297 298 // MultipartReader returns a MIME multipart reader if this is a 299 // multipart/form-data POST request, else returns nil and an error. 300 // Use this function instead of ParseMultipartForm to 301 // process the request body as a stream. 302 func (r *Request) MultipartReader() (*multipart.Reader, error) { 303 if r.MultipartForm == multipartByReader { 304 return nil, errors.New("http: MultipartReader called twice") 305 } 306 if r.MultipartForm != nil { 307 return nil, errors.New("http: multipart handled by ParseMultipartForm") 308 } 309 r.MultipartForm = multipartByReader 310 return r.multipartReader() 311 } 312 313 func (r *Request) multipartReader() (*multipart.Reader, error) { 314 v := r.Header.Get("Content-Type") 315 if v == "" { 316 return nil, ErrNotMultipart 317 } 318 d, params, err := mime.ParseMediaType(v) 319 if err != nil || d != "multipart/form-data" { 320 return nil, ErrNotMultipart 321 } 322 boundary, ok := params["boundary"] 323 if !ok { 324 return nil, ErrMissingBoundary 325 } 326 return multipart.NewReader(r.Body, boundary), nil 327 } 328 329 // Return value if nonempty, def otherwise. 330 func valueOrDefault(value, def string) string { 331 if value != "" { 332 return value 333 } 334 return def 335 } 336 337 // NOTE: This is not intended to reflect the actual Go version being used. 338 // It was changed at the time of Go 1.1 release because the former User-Agent 339 // had ended up on a blacklist for some intrusion detection systems. 340 // See https://codereview.appspot.com/7532043. 341 const defaultUserAgent = "Go-http-client/1.1" 342 343 // Write writes an HTTP/1.1 request, which is the header and body, in wire format. 344 // This method consults the following fields of the request: 345 // Host 346 // URL 347 // Method (defaults to "GET") 348 // Header 349 // ContentLength 350 // TransferEncoding 351 // Body 352 // 353 // If Body is present, Content-Length is <= 0 and TransferEncoding 354 // hasn't been set to "identity", Write adds "Transfer-Encoding: 355 // chunked" to the header. Body is closed after it is sent. 356 func (r *Request) Write(w io.Writer) error { 357 return r.write(w, false, nil, nil) 358 } 359 360 // WriteProxy is like Write but writes the request in the form 361 // expected by an HTTP proxy. In particular, WriteProxy writes the 362 // initial Request-URI line of the request with an absolute URI, per 363 // section 5.1.2 of RFC 2616, including the scheme and host. 364 // In either case, WriteProxy also writes a Host header, using 365 // either r.Host or r.URL.Host. 366 func (r *Request) WriteProxy(w io.Writer) error { 367 return r.write(w, true, nil, nil) 368 } 369 370 // errMissingHost is returned by Write when there is no Host or URL present in 371 // the Request. 372 var errMissingHost = errors.New("http: Request.Write on Request with no Host or URL set") 373 374 // extraHeaders may be nil 375 // waitForContinue may be nil 376 func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitForContinue func() bool) error { 377 // Find the target host. Prefer the Host: header, but if that 378 // is not given, use the host from the request URL. 379 // 380 // Clean the host, in case it arrives with unexpected stuff in it. 381 host := cleanHost(req.Host) 382 if host == "" { 383 if req.URL == nil { 384 return errMissingHost 385 } 386 host = cleanHost(req.URL.Host) 387 } 388 389 // According to RFC 6874, an HTTP client, proxy, or other 390 // intermediary must remove any IPv6 zone identifier attached 391 // to an outgoing URI. 392 host = removeZone(host) 393 394 ruri := req.URL.RequestURI() 395 if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" { 396 ruri = req.URL.Scheme + "://" + host + ruri 397 } else if req.Method == "CONNECT" && req.URL.Path == "" { 398 // CONNECT requests normally give just the host and port, not a full URL. 399 ruri = host 400 } 401 // TODO(bradfitz): escape at least newlines in ruri? 402 403 // Wrap the writer in a bufio Writer if it's not already buffered. 404 // Don't always call NewWriter, as that forces a bytes.Buffer 405 // and other small bufio Writers to have a minimum 4k buffer 406 // size. 407 var bw *bufio.Writer 408 if _, ok := w.(io.ByteWriter); !ok { 409 bw = bufio.NewWriter(w) 410 w = bw 411 } 412 413 _, err := fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri) 414 if err != nil { 415 return err 416 } 417 418 // Header lines 419 _, err = fmt.Fprintf(w, "Host: %s\r\n", host) 420 if err != nil { 421 return err 422 } 423 424 // Use the defaultUserAgent unless the Header contains one, which 425 // may be blank to not send the header. 426 userAgent := defaultUserAgent 427 if req.Header != nil { 428 if ua := req.Header["User-Agent"]; len(ua) > 0 { 429 userAgent = ua[0] 430 } 431 } 432 if userAgent != "" { 433 _, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent) 434 if err != nil { 435 return err 436 } 437 } 438 439 // Process Body,ContentLength,Close,Trailer 440 tw, err := newTransferWriter(req) 441 if err != nil { 442 return err 443 } 444 err = tw.WriteHeader(w) 445 if err != nil { 446 return err 447 } 448 449 err = req.Header.WriteSubset(w, reqWriteExcludeHeader) 450 if err != nil { 451 return err 452 } 453 454 if extraHeaders != nil { 455 err = extraHeaders.Write(w) 456 if err != nil { 457 return err 458 } 459 } 460 461 _, err = io.WriteString(w, "\r\n") 462 if err != nil { 463 return err 464 } 465 466 // Flush and wait for 100-continue if expected. 467 if waitForContinue != nil { 468 if bw, ok := w.(*bufio.Writer); ok { 469 err = bw.Flush() 470 if err != nil { 471 return err 472 } 473 } 474 475 if !waitForContinue() { 476 req.closeBody() 477 return nil 478 } 479 } 480 481 // Write body and trailer 482 err = tw.WriteBody(w) 483 if err != nil { 484 return err 485 } 486 487 if bw != nil { 488 return bw.Flush() 489 } 490 return nil 491 } 492 493 // cleanHost strips anything after '/' or ' '. 494 // Ideally we'd clean the Host header according to the spec: 495 // https://tools.ietf.org/html/rfc7230#section-5.4 (Host = uri-host [ ":" port ]") 496 // https://tools.ietf.org/html/rfc7230#section-2.7 (uri-host -> rfc3986's host) 497 // https://tools.ietf.org/html/rfc3986#section-3.2.2 (definition of host) 498 // But practically, what we are trying to avoid is the situation in 499 // issue 11206, where a malformed Host header used in the proxy context 500 // would create a bad request. So it is enough to just truncate at the 501 // first offending character. 502 func cleanHost(in string) string { 503 if i := strings.IndexAny(in, " /"); i != -1 { 504 return in[:i] 505 } 506 return in 507 } 508 509 // removeZone removes IPv6 zone identifer from host. 510 // E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080" 511 func removeZone(host string) string { 512 if !strings.HasPrefix(host, "[") { 513 return host 514 } 515 i := strings.LastIndex(host, "]") 516 if i < 0 { 517 return host 518 } 519 j := strings.LastIndex(host[:i], "%") 520 if j < 0 { 521 return host 522 } 523 return host[:j] + host[i:] 524 } 525 526 // ParseHTTPVersion parses a HTTP version string. 527 // "HTTP/1.0" returns (1, 0, true). 528 func ParseHTTPVersion(vers string) (major, minor int, ok bool) { 529 const Big = 1000000 // arbitrary upper bound 530 switch vers { 531 case "HTTP/1.1": 532 return 1, 1, true 533 case "HTTP/1.0": 534 return 1, 0, true 535 } 536 if !strings.HasPrefix(vers, "HTTP/") { 537 return 0, 0, false 538 } 539 dot := strings.Index(vers, ".") 540 if dot < 0 { 541 return 0, 0, false 542 } 543 major, err := strconv.Atoi(vers[5:dot]) 544 if err != nil || major < 0 || major > Big { 545 return 0, 0, false 546 } 547 minor, err = strconv.Atoi(vers[dot+1:]) 548 if err != nil || minor < 0 || minor > Big { 549 return 0, 0, false 550 } 551 return major, minor, true 552 } 553 554 func validMethod(method string) bool { 555 /* 556 Method = "OPTIONS" ; Section 9.2 557 | "GET" ; Section 9.3 558 | "HEAD" ; Section 9.4 559 | "POST" ; Section 9.5 560 | "PUT" ; Section 9.6 561 | "DELETE" ; Section 9.7 562 | "TRACE" ; Section 9.8 563 | "CONNECT" ; Section 9.9 564 | extension-method 565 extension-method = token 566 token = 1*<any CHAR except CTLs or separators> 567 */ 568 return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1 569 } 570 571 // NewRequest returns a new Request given a method, URL, and optional body. 572 // 573 // If the provided body is also an io.Closer, the returned 574 // Request.Body is set to body and will be closed by the Client 575 // methods Do, Post, and PostForm, and Transport.RoundTrip. 576 // 577 // NewRequest returns a Request suitable for use with Client.Do or 578 // Transport.RoundTrip. 579 // To create a request for use with testing a Server Handler use either 580 // ReadRequest or manually update the Request fields. See the Request 581 // type's documentation for the difference between inbound and outbound 582 // request fields. 583 func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { 584 if !validMethod(method) { 585 return nil, fmt.Errorf("net/http: invalid method %q", method) 586 } 587 u, err := url.Parse(urlStr) 588 if err != nil { 589 return nil, err 590 } 591 rc, ok := body.(io.ReadCloser) 592 if !ok && body != nil { 593 rc = ioutil.NopCloser(body) 594 } 595 req := &Request{ 596 Method: method, 597 URL: u, 598 Proto: "HTTP/1.1", 599 ProtoMajor: 1, 600 ProtoMinor: 1, 601 Header: make(Header), 602 Body: rc, 603 Host: u.Host, 604 } 605 if body != nil { 606 switch v := body.(type) { 607 case *bytes.Buffer: 608 req.ContentLength = int64(v.Len()) 609 case *bytes.Reader: 610 req.ContentLength = int64(v.Len()) 611 case *strings.Reader: 612 req.ContentLength = int64(v.Len()) 613 } 614 } 615 616 return req, nil 617 } 618 619 // BasicAuth returns the username and password provided in the request's 620 // Authorization header, if the request uses HTTP Basic Authentication. 621 // See RFC 2617, Section 2. 622 func (r *Request) BasicAuth() (username, password string, ok bool) { 623 auth := r.Header.Get("Authorization") 624 if auth == "" { 625 return 626 } 627 return parseBasicAuth(auth) 628 } 629 630 // parseBasicAuth parses an HTTP Basic Authentication string. 631 // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true). 632 func parseBasicAuth(auth string) (username, password string, ok bool) { 633 const prefix = "Basic " 634 if !strings.HasPrefix(auth, prefix) { 635 return 636 } 637 c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) 638 if err != nil { 639 return 640 } 641 cs := string(c) 642 s := strings.IndexByte(cs, ':') 643 if s < 0 { 644 return 645 } 646 return cs[:s], cs[s+1:], true 647 } 648 649 // SetBasicAuth sets the request's Authorization header to use HTTP 650 // Basic Authentication with the provided username and password. 651 // 652 // With HTTP Basic Authentication the provided username and password 653 // are not encrypted. 654 func (r *Request) SetBasicAuth(username, password string) { 655 r.Header.Set("Authorization", "Basic "+basicAuth(username, password)) 656 } 657 658 // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. 659 func parseRequestLine(line string) (method, requestURI, proto string, ok bool) { 660 s1 := strings.Index(line, " ") 661 s2 := strings.Index(line[s1+1:], " ") 662 if s1 < 0 || s2 < 0 { 663 return 664 } 665 s2 += s1 + 1 666 return line[:s1], line[s1+1 : s2], line[s2+1:], true 667 } 668 669 var textprotoReaderPool sync.Pool 670 671 func newTextprotoReader(br *bufio.Reader) *textproto.Reader { 672 if v := textprotoReaderPool.Get(); v != nil { 673 tr := v.(*textproto.Reader) 674 tr.R = br 675 return tr 676 } 677 return textproto.NewReader(br) 678 } 679 680 func putTextprotoReader(r *textproto.Reader) { 681 r.R = nil 682 textprotoReaderPool.Put(r) 683 } 684 685 // ReadRequest reads and parses an incoming request from b. 686 func ReadRequest(b *bufio.Reader) (req *Request, err error) { 687 688 tp := newTextprotoReader(b) 689 req = new(Request) 690 691 // First line: GET /index.html HTTP/1.0 692 var s string 693 if s, err = tp.ReadLine(); err != nil { 694 return nil, err 695 } 696 defer func() { 697 putTextprotoReader(tp) 698 if err == io.EOF { 699 err = io.ErrUnexpectedEOF 700 } 701 }() 702 703 var ok bool 704 req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s) 705 if !ok { 706 return nil, &badStringError{"malformed HTTP request", s} 707 } 708 rawurl := req.RequestURI 709 if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok { 710 return nil, &badStringError{"malformed HTTP version", req.Proto} 711 } 712 713 // CONNECT requests are used two different ways, and neither uses a full URL: 714 // The standard use is to tunnel HTTPS through an HTTP proxy. 715 // It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is 716 // just the authority section of a URL. This information should go in req.URL.Host. 717 // 718 // The net/rpc package also uses CONNECT, but there the parameter is a path 719 // that starts with a slash. It can be parsed with the regular URL parser, 720 // and the path will end up in req.URL.Path, where it needs to be in order for 721 // RPC to work. 722 justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/") 723 if justAuthority { 724 rawurl = "http://" + rawurl 725 } 726 727 if req.URL, err = url.ParseRequestURI(rawurl); err != nil { 728 return nil, err 729 } 730 731 if justAuthority { 732 // Strip the bogus "http://" back off. 733 req.URL.Scheme = "" 734 } 735 736 // Subsequent lines: Key: value. 737 mimeHeader, err := tp.ReadMIMEHeader() 738 if err != nil { 739 return nil, err 740 } 741 req.Header = Header(mimeHeader) 742 743 // RFC2616: Must treat 744 // GET /index.html HTTP/1.1 745 // Host: www.google.com 746 // and 747 // GET http://www.google.com/index.html HTTP/1.1 748 // Host: doesntmatter 749 // the same. In the second case, any Host line is ignored. 750 req.Host = req.URL.Host 751 if req.Host == "" { 752 req.Host = req.Header.get("Host") 753 } 754 delete(req.Header, "Host") 755 756 fixPragmaCacheControl(req.Header) 757 758 req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header, false) 759 760 err = readTransfer(req, b) 761 if err != nil { 762 return nil, err 763 } 764 765 return req, nil 766 } 767 768 // MaxBytesReader is similar to io.LimitReader but is intended for 769 // limiting the size of incoming request bodies. In contrast to 770 // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a 771 // non-EOF error for a Read beyond the limit, and closes the 772 // underlying reader when its Close method is called. 773 // 774 // MaxBytesReader prevents clients from accidentally or maliciously 775 // sending a large request and wasting server resources. 776 func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser { 777 return &maxBytesReader{w: w, r: r, n: n} 778 } 779 780 type maxBytesReader struct { 781 w ResponseWriter 782 r io.ReadCloser // underlying reader 783 n int64 // max bytes remaining 784 stopped bool 785 sawEOF bool 786 } 787 788 func (l *maxBytesReader) tooLarge() (n int, err error) { 789 if !l.stopped { 790 l.stopped = true 791 if res, ok := l.w.(*response); ok { 792 res.requestTooLarge() 793 } 794 } 795 return 0, errors.New("http: request body too large") 796 } 797 798 func (l *maxBytesReader) Read(p []byte) (n int, err error) { 799 toRead := l.n 800 if l.n == 0 { 801 if l.sawEOF { 802 return l.tooLarge() 803 } 804 // The underlying io.Reader may not return (0, io.EOF) 805 // at EOF if the requested size is 0, so read 1 byte 806 // instead. The io.Reader docs are a bit ambiguous 807 // about the return value of Read when 0 bytes are 808 // requested, and {bytes,strings}.Reader gets it wrong 809 // too (it returns (0, nil) even at EOF). 810 toRead = 1 811 } 812 if int64(len(p)) > toRead { 813 p = p[:toRead] 814 } 815 n, err = l.r.Read(p) 816 if err == io.EOF { 817 l.sawEOF = true 818 } 819 if l.n == 0 { 820 // If we had zero bytes to read remaining (but hadn't seen EOF) 821 // and we get a byte here, that means we went over our limit. 822 if n > 0 { 823 return l.tooLarge() 824 } 825 return 0, err 826 } 827 l.n -= int64(n) 828 if l.n < 0 { 829 l.n = 0 830 } 831 return 832 } 833 834 func (l *maxBytesReader) Close() error { 835 return l.r.Close() 836 } 837 838 func copyValues(dst, src url.Values) { 839 for k, vs := range src { 840 for _, value := range vs { 841 dst.Add(k, value) 842 } 843 } 844 } 845 846 func parsePostForm(r *Request) (vs url.Values, err error) { 847 if r.Body == nil { 848 err = errors.New("missing form body") 849 return 850 } 851 ct := r.Header.Get("Content-Type") 852 // RFC 2616, section 7.2.1 - empty type 853 // SHOULD be treated as application/octet-stream 854 if ct == "" { 855 ct = "application/octet-stream" 856 } 857 ct, _, err = mime.ParseMediaType(ct) 858 switch { 859 case ct == "application/x-www-form-urlencoded": 860 var reader io.Reader = r.Body 861 maxFormSize := int64(1<<63 - 1) 862 if _, ok := r.Body.(*maxBytesReader); !ok { 863 maxFormSize = int64(10 << 20) // 10 MB is a lot of text. 864 reader = io.LimitReader(r.Body, maxFormSize+1) 865 } 866 b, e := ioutil.ReadAll(reader) 867 if e != nil { 868 if err == nil { 869 err = e 870 } 871 break 872 } 873 if int64(len(b)) > maxFormSize { 874 err = errors.New("http: POST too large") 875 return 876 } 877 vs, e = url.ParseQuery(string(b)) 878 if err == nil { 879 err = e 880 } 881 case ct == "multipart/form-data": 882 // handled by ParseMultipartForm (which is calling us, or should be) 883 // TODO(bradfitz): there are too many possible 884 // orders to call too many functions here. 885 // Clean this up and write more tests. 886 // request_test.go contains the start of this, 887 // in TestParseMultipartFormOrder and others. 888 } 889 return 890 } 891 892 // ParseForm parses the raw query from the URL and updates r.Form. 893 // 894 // For POST or PUT requests, it also parses the request body as a form and 895 // put the results into both r.PostForm and r.Form. 896 // POST and PUT body parameters take precedence over URL query string values 897 // in r.Form. 898 // 899 // If the request Body's size has not already been limited by MaxBytesReader, 900 // the size is capped at 10MB. 901 // 902 // ParseMultipartForm calls ParseForm automatically. 903 // It is idempotent. 904 func (r *Request) ParseForm() error { 905 var err error 906 if r.PostForm == nil { 907 if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" { 908 r.PostForm, err = parsePostForm(r) 909 } 910 if r.PostForm == nil { 911 r.PostForm = make(url.Values) 912 } 913 } 914 if r.Form == nil { 915 if len(r.PostForm) > 0 { 916 r.Form = make(url.Values) 917 copyValues(r.Form, r.PostForm) 918 } 919 var newValues url.Values 920 if r.URL != nil { 921 var e error 922 newValues, e = url.ParseQuery(r.URL.RawQuery) 923 if err == nil { 924 err = e 925 } 926 } 927 if newValues == nil { 928 newValues = make(url.Values) 929 } 930 if r.Form == nil { 931 r.Form = newValues 932 } else { 933 copyValues(r.Form, newValues) 934 } 935 } 936 return err 937 } 938 939 // ParseMultipartForm parses a request body as multipart/form-data. 940 // The whole request body is parsed and up to a total of maxMemory bytes of 941 // its file parts are stored in memory, with the remainder stored on 942 // disk in temporary files. 943 // ParseMultipartForm calls ParseForm if necessary. 944 // After one call to ParseMultipartForm, subsequent calls have no effect. 945 func (r *Request) ParseMultipartForm(maxMemory int64) error { 946 if r.MultipartForm == multipartByReader { 947 return errors.New("http: multipart handled by MultipartReader") 948 } 949 if r.Form == nil { 950 err := r.ParseForm() 951 if err != nil { 952 return err 953 } 954 } 955 if r.MultipartForm != nil { 956 return nil 957 } 958 959 mr, err := r.multipartReader() 960 if err != nil { 961 return err 962 } 963 964 f, err := mr.ReadForm(maxMemory) 965 if err != nil { 966 return err 967 } 968 for k, v := range f.Value { 969 r.Form[k] = append(r.Form[k], v...) 970 } 971 r.MultipartForm = f 972 973 return nil 974 } 975 976 // FormValue returns the first value for the named component of the query. 977 // POST and PUT body parameters take precedence over URL query string values. 978 // FormValue calls ParseMultipartForm and ParseForm if necessary and ignores 979 // any errors returned by these functions. 980 // If key is not present, FormValue returns the empty string. 981 // To access multiple values of the same key, call ParseForm and 982 // then inspect Request.Form directly. 983 func (r *Request) FormValue(key string) string { 984 if r.Form == nil { 985 r.ParseMultipartForm(defaultMaxMemory) 986 } 987 if vs := r.Form[key]; len(vs) > 0 { 988 return vs[0] 989 } 990 return "" 991 } 992 993 // PostFormValue returns the first value for the named component of the POST 994 // or PUT request body. URL query parameters are ignored. 995 // PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores 996 // any errors returned by these functions. 997 // If key is not present, PostFormValue returns the empty string. 998 func (r *Request) PostFormValue(key string) string { 999 if r.PostForm == nil { 1000 r.ParseMultipartForm(defaultMaxMemory) 1001 } 1002 if vs := r.PostForm[key]; len(vs) > 0 { 1003 return vs[0] 1004 } 1005 return "" 1006 } 1007 1008 // FormFile returns the first file for the provided form key. 1009 // FormFile calls ParseMultipartForm and ParseForm if necessary. 1010 func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) { 1011 if r.MultipartForm == multipartByReader { 1012 return nil, nil, errors.New("http: multipart handled by MultipartReader") 1013 } 1014 if r.MultipartForm == nil { 1015 err := r.ParseMultipartForm(defaultMaxMemory) 1016 if err != nil { 1017 return nil, nil, err 1018 } 1019 } 1020 if r.MultipartForm != nil && r.MultipartForm.File != nil { 1021 if fhs := r.MultipartForm.File[key]; len(fhs) > 0 { 1022 f, err := fhs[0].Open() 1023 return f, fhs[0], err 1024 } 1025 } 1026 return nil, nil, ErrMissingFile 1027 } 1028 1029 func (r *Request) expectsContinue() bool { 1030 return hasToken(r.Header.get("Expect"), "100-continue") 1031 } 1032 1033 func (r *Request) wantsHttp10KeepAlive() bool { 1034 if r.ProtoMajor != 1 || r.ProtoMinor != 0 { 1035 return false 1036 } 1037 return hasToken(r.Header.get("Connection"), "keep-alive") 1038 } 1039 1040 func (r *Request) wantsClose() bool { 1041 return hasToken(r.Header.get("Connection"), "close") 1042 } 1043 1044 func (r *Request) closeBody() { 1045 if r.Body != nil { 1046 r.Body.Close() 1047 } 1048 } 1049 1050 func (r *Request) isReplayable() bool { 1051 return r.Body == nil && 1052 (r.Method == "GET" || 1053 r.Method == "HEAD" || 1054 r.Method == "OPTIONS" || 1055 r.Method == "TRACE") 1056 }