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 "context" 13 "crypto/tls" 14 "encoding/base64" 15 "errors" 16 "fmt" 17 "io" 18 "maps" 19 "math" 20 "mime" 21 "mime/multipart" 22 "net/http/httptrace" 23 "net/http/internal/ascii" 24 "net/textproto" 25 "net/url" 26 urlpkg "net/url" 27 "strconv" 28 "strings" 29 "sync" 30 _ "unsafe" // for linkname 31 32 "golang.org/x/net/http/httpguts" 33 "golang.org/x/net/idna" 34 ) 35 36 const ( 37 defaultMaxMemory = 32 << 20 // 32 MB 38 ) 39 40 // ErrMissingFile is returned by FormFile when the provided file field name 41 // is either not present in the request or not a file field. 42 var ErrMissingFile = errors.New("http: no such file") 43 44 // ProtocolError represents an HTTP protocol error. 45 // 46 // Deprecated: Not all errors in the http package related to protocol errors 47 // are of type ProtocolError. 48 type ProtocolError struct { 49 ErrorString string 50 } 51 52 func (pe *ProtocolError) Error() string { return pe.ErrorString } 53 54 // Is lets http.ErrNotSupported match errors.ErrUnsupported. 55 func (pe *ProtocolError) Is(err error) bool { 56 return pe == ErrNotSupported && err == errors.ErrUnsupported 57 } 58 59 var ( 60 // ErrNotSupported indicates that a feature is not supported. 61 // 62 // It is returned by ResponseController methods to indicate that 63 // the handler does not support the method, and by the Push method 64 // of Pusher implementations to indicate that HTTP/2 Push support 65 // is not available. 66 ErrNotSupported = &ProtocolError{"feature not supported"} 67 68 // Deprecated: ErrUnexpectedTrailer is no longer returned by 69 // anything in the net/http package. Callers should not 70 // compare errors against this variable. 71 ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"} 72 73 // ErrMissingBoundary is returned by Request.MultipartReader when the 74 // request's Content-Type does not include a "boundary" parameter. 75 ErrMissingBoundary = &ProtocolError{"no multipart boundary param in Content-Type"} 76 77 // ErrNotMultipart is returned by Request.MultipartReader when the 78 // request's Content-Type is not multipart/form-data. 79 ErrNotMultipart = &ProtocolError{"request Content-Type isn't multipart/form-data"} 80 81 // Deprecated: ErrHeaderTooLong is no longer returned by 82 // anything in the net/http package. Callers should not 83 // compare errors against this variable. 84 ErrHeaderTooLong = &ProtocolError{"header too long"} 85 86 // Deprecated: ErrShortBody is no longer returned by 87 // anything in the net/http package. Callers should not 88 // compare errors against this variable. 89 ErrShortBody = &ProtocolError{"entity body too short"} 90 91 // Deprecated: ErrMissingContentLength is no longer returned by 92 // anything in the net/http package. Callers should not 93 // compare errors against this variable. 94 ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"} 95 ) 96 97 func badStringError(what, val string) error { return fmt.Errorf("%s %q", what, val) } 98 99 // Headers that Request.Write handles itself and should be skipped. 100 var reqWriteExcludeHeader = map[string]bool{ 101 "Host": true, // not in Header map anyway 102 "User-Agent": true, 103 "Content-Length": true, 104 "Transfer-Encoding": true, 105 "Trailer": true, 106 } 107 108 // A Request represents an HTTP request received by a server 109 // or to be sent by a client. 110 // 111 // The field semantics differ slightly between client and server 112 // usage. In addition to the notes on the fields below, see the 113 // documentation for [Request.Write] and [RoundTripper]. 114 type Request struct { 115 // Method specifies the HTTP method (GET, POST, PUT, etc.). 116 // For client requests, an empty string means GET. 117 Method string 118 119 // URL specifies either the URI being requested (for server 120 // requests) or the URL to access (for client requests). 121 // 122 // For server requests, the URL is parsed from the URI 123 // supplied on the Request-Line as stored in RequestURI. For 124 // most requests, fields other than Path and RawQuery will be 125 // empty. (See RFC 7230, Section 5.3) 126 // 127 // For client requests, the URL's Host specifies the server to 128 // connect to, while the Request's Host field optionally 129 // specifies the Host header value to send in the HTTP 130 // request. 131 URL *url.URL 132 133 // The protocol version for incoming server requests. 134 // 135 // For client requests, these fields are ignored. The HTTP 136 // client code always uses either HTTP/1.1 or HTTP/2. 137 // See the docs on Transport for details. 138 Proto string // "HTTP/1.0" 139 ProtoMajor int // 1 140 ProtoMinor int // 0 141 142 // Header contains the request header fields either received 143 // by the server or to be sent by the client. 144 // 145 // If a server received a request with header lines, 146 // 147 // Host: example.com 148 // accept-encoding: gzip, deflate 149 // Accept-Language: en-us 150 // fOO: Bar 151 // foo: two 152 // 153 // then 154 // 155 // Header = map[string][]string{ 156 // "Accept-Encoding": {"gzip, deflate"}, 157 // "Accept-Language": {"en-us"}, 158 // "Foo": {"Bar", "two"}, 159 // } 160 // 161 // For incoming requests, the Host header is promoted to the 162 // Request.Host field and removed from the Header map. 163 // 164 // HTTP defines that header names are case-insensitive. The 165 // request parser implements this by using CanonicalHeaderKey, 166 // making the first character and any characters following a 167 // hyphen uppercase and the rest lowercase. 168 // 169 // For client requests, certain headers such as Content-Length 170 // and Connection are automatically written when needed and 171 // values in Header may be ignored. See the documentation 172 // for the Request.Write method. 173 Header Header 174 175 // Body is the request's body. 176 // 177 // For client requests, a nil body means the request has no 178 // body, such as a GET request. The HTTP Client's Transport 179 // is responsible for calling the Close method. 180 // 181 // For server requests, the Request Body is always non-nil 182 // but will return EOF immediately when no body is present. 183 // The Server will close the request body. The ServeHTTP 184 // Handler does not need to. 185 // 186 // Body must allow Read to be called concurrently with Close. 187 // In particular, calling Close should unblock a Read waiting 188 // for input. 189 Body io.ReadCloser 190 191 // GetBody defines an optional func to return a new copy of 192 // Body. It is used for client requests when a redirect requires 193 // reading the body more than once. Use of GetBody still 194 // requires setting Body. 195 // 196 // For server requests, it is unused. 197 GetBody func() (io.ReadCloser, error) 198 199 // ContentLength records the length of the associated content. 200 // The value -1 indicates that the length is unknown. 201 // Values >= 0 indicate that the given number of bytes may 202 // be read from Body. 203 // 204 // For client requests, a value of 0 with a non-nil Body is 205 // also treated as unknown. 206 ContentLength int64 207 208 // TransferEncoding lists the transfer encodings from outermost to 209 // innermost. An empty list denotes the "identity" encoding. 210 // TransferEncoding can usually be ignored; chunked encoding is 211 // automatically added and removed as necessary when sending and 212 // receiving requests. 213 TransferEncoding []string 214 215 // Close indicates whether to close the connection after 216 // replying to this request (for servers) or after sending this 217 // request and reading its response (for clients). 218 // 219 // For server requests, the HTTP server handles this automatically 220 // and this field is not needed by Handlers. 221 // 222 // For client requests, setting this field prevents re-use of 223 // TCP connections between requests to the same hosts, as if 224 // Transport.DisableKeepAlives were set. 225 Close bool 226 227 // For server requests, Host specifies the host on which the 228 // URL is sought. For HTTP/1 (per RFC 7230, section 5.4), this 229 // is either the value of the "Host" header or the host name 230 // given in the URL itself. For HTTP/2, it is the value of the 231 // ":authority" pseudo-header field. 232 // It may be of the form "host:port". For international domain 233 // names, Host may be in Punycode or Unicode form. Use 234 // golang.org/x/net/idna to convert it to either format if 235 // needed. 236 // To prevent DNS rebinding attacks, server Handlers should 237 // validate that the Host header has a value for which the 238 // Handler considers itself authoritative. The included 239 // ServeMux supports patterns registered to particular host 240 // names and thus protects its registered Handlers. 241 // 242 // For client requests, Host optionally overrides the Host 243 // header to send. If empty, the Request.Write method uses 244 // the value of URL.Host. Host may contain an international 245 // domain name. 246 Host string 247 248 // Form contains the parsed form data, including both the URL 249 // field's query parameters and the PATCH, POST, or PUT form data. 250 // This field is only available after ParseForm is called. 251 // The HTTP client ignores Form and uses Body instead. 252 Form url.Values 253 254 // PostForm contains the parsed form data from PATCH, POST 255 // or PUT body parameters. 256 // 257 // This field is only available after ParseForm is called. 258 // The HTTP client ignores PostForm and uses Body instead. 259 PostForm url.Values 260 261 // MultipartForm is the parsed multipart form, including file uploads. 262 // This field is only available after ParseMultipartForm is called. 263 // The HTTP client ignores MultipartForm and uses Body instead. 264 MultipartForm *multipart.Form 265 266 // Trailer specifies additional headers that are sent after the request 267 // body. 268 // 269 // For server requests, the Trailer map initially contains only the 270 // trailer keys, with nil values. (The client declares which trailers it 271 // will later send.) While the handler is reading from Body, it must 272 // not reference Trailer. After reading from Body returns EOF, Trailer 273 // can be read again and will contain non-nil values, if they were sent 274 // by the client. 275 // 276 // For client requests, Trailer must be initialized to a map containing 277 // the trailer keys to later send. The values may be nil or their final 278 // values. The ContentLength must be 0 or -1, to send a chunked request. 279 // After the HTTP request is sent the map values can be updated while 280 // the request body is read. Once the body returns EOF, the caller must 281 // not mutate Trailer. 282 // 283 // Writing a request whose Trailer contains a key with invalid bytes 284 // (such as CR or LF), or such a value present when Write begins, 285 // returns an error. 286 // 287 // Few HTTP clients, servers, or proxies support HTTP trailers. 288 Trailer Header 289 290 // RemoteAddr allows HTTP servers and other software to record 291 // the network address that sent the request, usually for 292 // logging. This field is not filled in by ReadRequest and 293 // has no defined format. The HTTP server in this package 294 // sets RemoteAddr to an "IP:port" address before invoking a 295 // handler. 296 // This field is ignored by the HTTP client. 297 RemoteAddr string 298 299 // RequestURI is the unmodified request-target of the 300 // Request-Line (RFC 7230, Section 3.1.1) as sent by the client 301 // to a server. Usually the URL field should be used instead. 302 // It is an error to set this field in an HTTP client request. 303 RequestURI string 304 305 // TLS allows HTTP servers and other software to record 306 // information about the TLS connection on which the request 307 // was received. This field is not filled in by ReadRequest. 308 // The HTTP server in this package sets the field for 309 // TLS-enabled connections before invoking a handler; 310 // otherwise it leaves the field nil. 311 // This field is ignored by the HTTP client. 312 TLS *tls.ConnectionState 313 314 // Cancel is an optional channel whose closure indicates that the client 315 // request should be regarded as canceled. Not all implementations of 316 // RoundTripper may support Cancel. 317 // 318 // For server requests, this field is not applicable. 319 // 320 // Deprecated: Set the Request's context with NewRequestWithContext 321 // instead. If a Request's Cancel field and context are both 322 // set, it is undefined whether Cancel is respected. 323 Cancel <-chan struct{} 324 325 // Response is the redirect response which caused this request 326 // to be created. This field is only populated during client 327 // redirects. 328 Response *Response 329 330 // Pattern is the [ServeMux] pattern that matched the request. 331 // It is empty if the request was not matched against a pattern. 332 Pattern string 333 334 // ctx is either the client or server context. It should only 335 // be modified via copying the whole Request using Clone or WithContext. 336 // It is unexported to prevent people from using Context wrong 337 // and mutating the contexts held by callers of the same request. 338 ctx context.Context 339 340 // The following fields are for requests matched by ServeMux. 341 pat *pattern // the pattern that matched 342 matches []string // values for the matching wildcards in pat 343 otherValues map[string]string // for calls to SetPathValue that don't match a wildcard 344 } 345 346 // Context returns the request's context. To change the context, use 347 // [Request.Clone] or [Request.WithContext]. 348 // 349 // The returned context is always non-nil; it defaults to the 350 // background context. 351 // 352 // For outgoing client requests, the context controls cancellation. 353 // 354 // For incoming server requests, the context is canceled when the 355 // client's connection closes, the request is canceled (with HTTP/2), 356 // or when the ServeHTTP method returns. 357 func (r *Request) Context() context.Context { 358 if r.ctx != nil { 359 return r.ctx 360 } 361 return context.Background() 362 } 363 364 // WithContext returns a shallow copy of r with its context changed 365 // to ctx. The provided ctx must be non-nil. 366 // 367 // For outgoing client request, the context controls the entire 368 // lifetime of a request and its response: obtaining a connection, 369 // sending the request, and reading the response headers and body. 370 // 371 // To create a new request with a context, use [NewRequestWithContext]. 372 // To make a deep copy of a request with a new context, use [Request.Clone]. 373 func (r *Request) WithContext(ctx context.Context) *Request { 374 if ctx == nil { 375 panic("nil context") 376 } 377 r2 := new(Request) 378 *r2 = *r 379 r2.ctx = ctx 380 return r2 381 } 382 383 // Clone returns a deep copy of r with its context changed to ctx. 384 // The provided ctx must be non-nil. 385 // 386 // Clone only makes a shallow copy of the Body field. 387 // 388 // For an outgoing client request, the context controls the entire 389 // lifetime of a request and its response: obtaining a connection, 390 // sending the request, and reading the response headers and body. 391 func (r *Request) Clone(ctx context.Context) *Request { 392 if ctx == nil { 393 panic("nil context") 394 } 395 r2 := new(Request) 396 *r2 = *r 397 r2.ctx = ctx 398 r2.URL = cloneURL(r.URL) 399 r2.Header = r.Header.Clone() 400 r2.Trailer = r.Trailer.Clone() 401 if s := r.TransferEncoding; s != nil { 402 s2 := make([]string, len(s)) 403 copy(s2, s) 404 r2.TransferEncoding = s2 405 } 406 r2.Form = cloneURLValues(r.Form) 407 r2.PostForm = cloneURLValues(r.PostForm) 408 r2.MultipartForm = cloneMultipartForm(r.MultipartForm) 409 410 // Copy matches and otherValues. See issue 61410. 411 if s := r.matches; s != nil { 412 s2 := make([]string, len(s)) 413 copy(s2, s) 414 r2.matches = s2 415 } 416 r2.otherValues = maps.Clone(r.otherValues) 417 return r2 418 } 419 420 // ProtoAtLeast reports whether the HTTP protocol used 421 // in the request is at least major.minor. 422 func (r *Request) ProtoAtLeast(major, minor int) bool { 423 return r.ProtoMajor > major || 424 r.ProtoMajor == major && r.ProtoMinor >= minor 425 } 426 427 // UserAgent returns the client's User-Agent, if sent in the request. 428 func (r *Request) UserAgent() string { 429 return r.Header.Get("User-Agent") 430 } 431 432 // Cookies parses and returns the HTTP cookies sent with the request. 433 func (r *Request) Cookies() []*Cookie { 434 return readCookies(r.Header, "") 435 } 436 437 // CookiesNamed parses and returns the named HTTP cookies sent with the request 438 // or an empty slice if none matched. 439 func (r *Request) CookiesNamed(name string) []*Cookie { 440 if name == "" { 441 return []*Cookie{} 442 } 443 return readCookies(r.Header, name) 444 } 445 446 // ErrNoCookie is returned by Request's Cookie method when a cookie is not found. 447 var ErrNoCookie = errors.New("http: named cookie not present") 448 449 // Cookie returns the named cookie provided in the request or 450 // [ErrNoCookie] if not found. 451 // If multiple cookies match the given name, only one cookie will 452 // be returned. 453 func (r *Request) Cookie(name string) (*Cookie, error) { 454 if name == "" { 455 return nil, ErrNoCookie 456 } 457 for _, c := range readCookies(r.Header, name) { 458 return c, nil 459 } 460 return nil, ErrNoCookie 461 } 462 463 // AddCookie adds a cookie to the request. Per RFC 6265 section 5.4, 464 // AddCookie does not attach more than one [Cookie] header field. That 465 // means all cookies, if any, are written into the same line, 466 // separated by semicolon. 467 // AddCookie only sanitizes c's name and value, and does not sanitize 468 // a Cookie header already present in the request. 469 func (r *Request) AddCookie(c *Cookie) { 470 s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value, c.Quoted)) 471 if c := r.Header.Get("Cookie"); c != "" { 472 r.Header.Set("Cookie", c+"; "+s) 473 } else { 474 r.Header.Set("Cookie", s) 475 } 476 } 477 478 // Referer returns the referring URL, if sent in the request. 479 // 480 // Referer is misspelled as in the request itself, a mistake from the 481 // earliest days of HTTP. This value can also be fetched from the 482 // [Header] map as Header["Referer"]; the benefit of making it available 483 // as a method is that the compiler can diagnose programs that use the 484 // alternate (correct English) spelling req.Referrer() but cannot 485 // diagnose programs that use Header["Referrer"]. 486 func (r *Request) Referer() string { 487 return r.Header.Get("Referer") 488 } 489 490 // multipartByReader is a sentinel value. 491 // Its presence in Request.MultipartForm indicates that parsing of the request 492 // body has been handed off to a MultipartReader instead of ParseMultipartForm. 493 var multipartByReader = &multipart.Form{ 494 Value: make(map[string][]string), 495 File: make(map[string][]*multipart.FileHeader), 496 } 497 498 // MultipartReader returns a MIME multipart reader if this is a 499 // multipart/form-data or a multipart/mixed POST request, else returns nil and an error. 500 // Use this function instead of [Request.ParseMultipartForm] to 501 // process the request body as a stream. 502 func (r *Request) MultipartReader() (*multipart.Reader, error) { 503 if r.MultipartForm == multipartByReader { 504 return nil, errors.New("http: MultipartReader called twice") 505 } 506 if r.MultipartForm != nil { 507 return nil, errors.New("http: multipart handled by ParseMultipartForm") 508 } 509 r.MultipartForm = multipartByReader 510 return r.multipartReader(true) 511 } 512 513 func (r *Request) multipartReader(allowMixed bool) (*multipart.Reader, error) { 514 v := r.Header.Get("Content-Type") 515 if v == "" { 516 return nil, ErrNotMultipart 517 } 518 if r.Body == nil { 519 return nil, errors.New("missing form body") 520 } 521 d, params, err := mime.ParseMediaType(v) 522 if err != nil || !(d == "multipart/form-data" || allowMixed && d == "multipart/mixed") { 523 return nil, ErrNotMultipart 524 } 525 boundary, ok := params["boundary"] 526 if !ok { 527 return nil, ErrMissingBoundary 528 } 529 return multipart.NewReader(r.Body, boundary), nil 530 } 531 532 // isH2Upgrade reports whether r represents the http2 "client preface" 533 // magic string. 534 func (r *Request) isH2Upgrade() bool { 535 return r.Method == "PRI" && len(r.Header) == 0 && r.URL.Path == "*" && r.Proto == "HTTP/2.0" 536 } 537 538 // Return value if nonempty, def otherwise. 539 func valueOrDefault(value, def string) string { 540 if value != "" { 541 return value 542 } 543 return def 544 } 545 546 // NOTE: This is not intended to reflect the actual Go version being used. 547 // It was changed at the time of Go 1.1 release because the former User-Agent 548 // had ended up blocked by some intrusion detection systems. 549 // See https://codereview.appspot.com/7532043. 550 const defaultUserAgent = "Go-http-client/1.1" 551 552 // Write writes an HTTP/1.1 request, which is the header and body, in wire format. 553 // This method consults the following fields of the request: 554 // 555 // Host 556 // URL 557 // Method (defaults to "GET") 558 // Header 559 // ContentLength 560 // TransferEncoding 561 // Body 562 // 563 // If Body is present, Content-Length is <= 0 and [Request.TransferEncoding] 564 // hasn't been set to "identity", Write adds "Transfer-Encoding: 565 // chunked" to the header. Body is closed after it is sent. 566 // 567 // Header values for Host, Content-Length, Transfer-Encoding, 568 // and Trailer are not used; these are derived from other Request fields. 569 // If the Header does not contain a User-Agent value, Write uses 570 // "Go-http-client/1.1". 571 func (r *Request) Write(w io.Writer) error { 572 return r.write(w, false, nil, nil) 573 } 574 575 // WriteProxy is like [Request.Write] but writes the request in the form 576 // expected by an HTTP proxy. In particular, [Request.WriteProxy] writes the 577 // initial Request-URI line of the request with an absolute URI, per 578 // section 5.3 of RFC 7230, including the scheme and host. 579 // In either case, WriteProxy also writes a Host header, using 580 // either r.Host or r.URL.Host. 581 func (r *Request) WriteProxy(w io.Writer) error { 582 return r.write(w, true, nil, nil) 583 } 584 585 // errMissingHost is returned by Write when there is no Host or URL present in 586 // the Request. 587 var errMissingHost = errors.New("http: Request.Write on Request with no Host or URL set") 588 589 // extraHeaders may be nil 590 // waitForContinue may be nil 591 // always closes body 592 func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitForContinue func() bool) (err error) { 593 trace := httptrace.ContextClientTrace(r.Context()) 594 if trace != nil && trace.WroteRequest != nil { 595 defer func() { 596 trace.WroteRequest(httptrace.WroteRequestInfo{ 597 Err: err, 598 }) 599 }() 600 } 601 closed := false 602 defer func() { 603 if closed { 604 return 605 } 606 if closeErr := r.closeBody(); closeErr != nil && err == nil { 607 err = closeErr 608 } 609 }() 610 611 // Find the target host. Prefer the Host: header, but if that 612 // is not given, use the host from the request URL. 613 // 614 // Clean the host, in case it arrives with unexpected stuff in it. 615 host := r.Host 616 if host == "" { 617 if r.URL == nil { 618 return errMissingHost 619 } 620 host = r.URL.Host 621 } 622 host, err = httpguts.PunycodeHostPort(host) 623 if err != nil { 624 return err 625 } 626 // Validate that the Host header is a valid header in general, 627 // but don't validate the host itself. This is sufficient to avoid 628 // header or request smuggling via the Host field. 629 // The server can (and will, if it's a net/http server) reject 630 // the request if it doesn't consider the host valid. 631 if !httpguts.ValidHostHeader(host) { 632 // Historically, we would truncate the Host header after '/' or ' '. 633 // Some users have relied on this truncation to convert a network 634 // address such as Unix domain socket path into a valid, ignored 635 // Host header (see https://go.dev/issue/61431). 636 // 637 // We don't preserve the truncation, because sending an altered 638 // header field opens a smuggling vector. Instead, zero out the 639 // Host header entirely if it isn't valid. (An empty Host is valid; 640 // see RFC 9112 Section 3.2.) 641 // 642 // Return an error if we're sending to a proxy, since the proxy 643 // probably can't do anything useful with an empty Host header. 644 if !usingProxy { 645 host = "" 646 } else { 647 return errors.New("http: invalid Host header") 648 } 649 } 650 651 // According to RFC 6874, an HTTP client, proxy, or other 652 // intermediary must remove any IPv6 zone identifier attached 653 // to an outgoing URI. 654 host = removeZone(host) 655 656 ruri := r.URL.RequestURI() 657 if usingProxy && r.URL.Scheme != "" && r.URL.Opaque == "" { 658 ruri = r.URL.Scheme + "://" + host + ruri 659 } else if r.Method == "CONNECT" && r.URL.Path == "" { 660 // CONNECT requests normally give just the host and port, not a full URL. 661 ruri = host 662 if r.URL.Opaque != "" { 663 ruri = r.URL.Opaque 664 } 665 } 666 if stringContainsCTLByte(ruri) { 667 return errors.New("net/http: can't write control character in Request.URL") 668 } 669 // TODO: validate r.Method too? At least it's less likely to 670 // come from an attacker (more likely to be a constant in 671 // code). 672 673 // Wrap the writer in a bufio Writer if it's not already buffered. 674 // Don't always call NewWriter, as that forces a bytes.Buffer 675 // and other small bufio Writers to have a minimum 4k buffer 676 // size. 677 var bw *bufio.Writer 678 if _, ok := w.(io.ByteWriter); !ok { 679 bw = bufio.NewWriter(w) 680 w = bw 681 } 682 683 _, err = fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(r.Method, "GET"), ruri) 684 if err != nil { 685 return err 686 } 687 688 // Header lines 689 _, err = fmt.Fprintf(w, "Host: %s\r\n", host) 690 if err != nil { 691 return err 692 } 693 if trace != nil && trace.WroteHeaderField != nil { 694 trace.WroteHeaderField("Host", []string{host}) 695 } 696 697 // Use the defaultUserAgent unless the Header contains one, which 698 // may be blank to not send the header. 699 userAgent := defaultUserAgent 700 if r.Header.has("User-Agent") { 701 userAgent = r.Header.Get("User-Agent") 702 } 703 if userAgent != "" { 704 userAgent = headerNewlineToSpace.Replace(userAgent) 705 userAgent = textproto.TrimString(userAgent) 706 _, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent) 707 if err != nil { 708 return err 709 } 710 if trace != nil && trace.WroteHeaderField != nil { 711 trace.WroteHeaderField("User-Agent", []string{userAgent}) 712 } 713 } 714 715 // Process Body,ContentLength,Close,Trailer 716 tw, err := newTransferWriter(r) 717 if err != nil { 718 return err 719 } 720 err = tw.writeHeader(w, trace) 721 if err != nil { 722 return err 723 } 724 725 err = r.Header.writeSubset(w, reqWriteExcludeHeader, trace) 726 if err != nil { 727 return err 728 } 729 730 if extraHeaders != nil { 731 err = extraHeaders.write(w, trace) 732 if err != nil { 733 return err 734 } 735 } 736 737 _, err = io.WriteString(w, "\r\n") 738 if err != nil { 739 return err 740 } 741 742 if trace != nil && trace.WroteHeaders != nil { 743 trace.WroteHeaders() 744 } 745 746 // Flush and wait for 100-continue if expected. 747 if waitForContinue != nil { 748 if bw, ok := w.(*bufio.Writer); ok { 749 err = bw.Flush() 750 if err != nil { 751 return err 752 } 753 } 754 if trace != nil && trace.Wait100Continue != nil { 755 trace.Wait100Continue() 756 } 757 if !waitForContinue() { 758 closed = true 759 r.closeBody() 760 return nil 761 } 762 } 763 764 if bw, ok := w.(*bufio.Writer); ok && tw.FlushHeaders { 765 if err := bw.Flush(); err != nil { 766 return err 767 } 768 } 769 770 // Write body and trailer 771 closed = true 772 err = tw.writeBody(w) 773 if err != nil { 774 if tw.bodyReadError == err { 775 err = requestBodyReadError{err} 776 } 777 return err 778 } 779 780 if bw != nil { 781 return bw.Flush() 782 } 783 return nil 784 } 785 786 // requestBodyReadError wraps an error from (*Request).write to indicate 787 // that the error came from a Read call on the Request.Body. 788 // This error type should not escape the net/http package to users. 789 type requestBodyReadError struct{ error } 790 791 func idnaASCII(v string) (string, error) { 792 // TODO: Consider removing this check after verifying performance is okay. 793 // Right now punycode verification, length checks, context checks, and the 794 // permissible character tests are all omitted. It also prevents the ToASCII 795 // call from salvaging an invalid IDN, when possible. As a result it may be 796 // possible to have two IDNs that appear identical to the user where the 797 // ASCII-only version causes an error downstream whereas the non-ASCII 798 // version does not. 799 // Note that for correct ASCII IDNs ToASCII will only do considerably more 800 // work, but it will not cause an allocation. 801 if ascii.Is(v) { 802 return v, nil 803 } 804 return idna.Lookup.ToASCII(v) 805 } 806 807 // removeZone removes IPv6 zone identifier from host. 808 // E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080" 809 func removeZone(host string) string { 810 if !strings.HasPrefix(host, "[") { 811 return host 812 } 813 i := strings.LastIndex(host, "]") 814 if i < 0 { 815 return host 816 } 817 j := strings.LastIndex(host[:i], "%") 818 if j < 0 { 819 return host 820 } 821 return host[:j] + host[i:] 822 } 823 824 // ParseHTTPVersion parses an HTTP version string according to RFC 7230, section 2.6. 825 // "HTTP/1.0" returns (1, 0, true). Note that strings without 826 // a minor version, such as "HTTP/2", are not valid. 827 func ParseHTTPVersion(vers string) (major, minor int, ok bool) { 828 switch vers { 829 case "HTTP/1.1": 830 return 1, 1, true 831 case "HTTP/1.0": 832 return 1, 0, true 833 } 834 if !strings.HasPrefix(vers, "HTTP/") { 835 return 0, 0, false 836 } 837 if len(vers) != len("HTTP/X.Y") { 838 return 0, 0, false 839 } 840 if vers[6] != '.' { 841 return 0, 0, false 842 } 843 maj, err := strconv.ParseUint(vers[5:6], 10, 0) 844 if err != nil { 845 return 0, 0, false 846 } 847 min, err := strconv.ParseUint(vers[7:8], 10, 0) 848 if err != nil { 849 return 0, 0, false 850 } 851 return int(maj), int(min), true 852 } 853 854 func validMethod(method string) bool { 855 /* 856 Method = "OPTIONS" ; Section 9.2 857 | "GET" ; Section 9.3 858 | "HEAD" ; Section 9.4 859 | "POST" ; Section 9.5 860 | "PUT" ; Section 9.6 861 | "DELETE" ; Section 9.7 862 | "TRACE" ; Section 9.8 863 | "CONNECT" ; Section 9.9 864 | extension-method 865 extension-method = token 866 token = 1*<any CHAR except CTLs or separators> 867 */ 868 return isToken(method) 869 } 870 871 // NewRequest wraps [NewRequestWithContext] using [context.Background]. 872 func NewRequest(method, url string, body io.Reader) (*Request, error) { 873 return NewRequestWithContext(context.Background(), method, url, body) 874 } 875 876 // NewRequestWithContext returns a new [Request] given a method, URL, and 877 // optional body. 878 // 879 // If the provided body is also an [io.Closer], the returned 880 // [Request.Body] is set to body and will be closed (possibly 881 // asynchronously) by the Client methods Do, Post, and PostForm, 882 // and [Transport.RoundTrip]. 883 // 884 // NewRequestWithContext returns a Request suitable for use with 885 // [Client.Do] or [Transport.RoundTrip]. To create a request for use with 886 // testing a Server Handler, either use the [net/http/httptest.NewRequest] function, 887 // use [ReadRequest], or manually update the Request fields. 888 // For an outgoing client request, the context 889 // controls the entire lifetime of a request and its response: 890 // obtaining a connection, sending the request, and reading the 891 // response headers and body. See the [Request] type's documentation for 892 // the difference between inbound and outbound request fields. 893 // 894 // If body is of type [*bytes.Buffer], [*bytes.Reader], or 895 // [*strings.Reader], the returned request's ContentLength is set to its 896 // exact value (instead of -1), GetBody is populated (so 307 and 308 897 // redirects can replay the body), and Body is set to [NoBody] if the 898 // ContentLength is 0. 899 func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error) { 900 if method == "" { 901 // We document that "" means "GET" for Request.Method, and people have 902 // relied on that from NewRequest, so keep that working. 903 // We still enforce validMethod for non-empty methods. 904 method = "GET" 905 } 906 if !validMethod(method) { 907 return nil, fmt.Errorf("net/http: invalid method %q", method) 908 } 909 if ctx == nil { 910 return nil, errors.New("net/http: nil Context") 911 } 912 u, err := urlpkg.Parse(url) 913 if err != nil { 914 return nil, err 915 } 916 rc, ok := body.(io.ReadCloser) 917 if !ok && body != nil { 918 rc = io.NopCloser(body) 919 } 920 // The host's colon:port should be normalized. See Issue 14836. 921 u.Host = strings.TrimSuffix(u.Host, ":") 922 req := &Request{ 923 ctx: ctx, 924 Method: method, 925 URL: u, 926 Proto: "HTTP/1.1", 927 ProtoMajor: 1, 928 ProtoMinor: 1, 929 Header: make(Header), 930 Body: rc, 931 Host: u.Host, 932 } 933 if body != nil { 934 switch v := body.(type) { 935 case *bytes.Buffer: 936 req.ContentLength = int64(v.Len()) 937 buf := v.Bytes() 938 req.GetBody = func() (io.ReadCloser, error) { 939 r := bytes.NewReader(buf) 940 return io.NopCloser(r), nil 941 } 942 case *bytes.Reader: 943 req.ContentLength = int64(v.Len()) 944 snapshot := *v 945 req.GetBody = func() (io.ReadCloser, error) { 946 r := snapshot 947 return io.NopCloser(&r), nil 948 } 949 case *strings.Reader: 950 req.ContentLength = int64(v.Len()) 951 snapshot := *v 952 req.GetBody = func() (io.ReadCloser, error) { 953 r := snapshot 954 return io.NopCloser(&r), nil 955 } 956 default: 957 // This is where we'd set it to -1 (at least 958 // if body != NoBody) to mean unknown, but 959 // that broke people during the Go 1.8 testing 960 // period. People depend on it being 0 I 961 // guess. Maybe retry later. See Issue 18117. 962 } 963 // For client requests, Request.ContentLength of 0 964 // means either actually 0, or unknown. The only way 965 // to explicitly say that the ContentLength is zero is 966 // to set the Body to nil. But turns out too much code 967 // depends on NewRequest returning a non-nil Body, 968 // so we use a well-known ReadCloser variable instead 969 // and have the http package also treat that sentinel 970 // variable to mean explicitly zero. 971 if req.GetBody != nil && req.ContentLength == 0 { 972 req.Body = NoBody 973 req.GetBody = func() (io.ReadCloser, error) { return NoBody, nil } 974 } 975 } 976 977 return req, nil 978 } 979 980 // BasicAuth returns the username and password provided in the request's 981 // Authorization header, if the request uses HTTP Basic Authentication. 982 // See RFC 2617, Section 2. 983 func (r *Request) BasicAuth() (username, password string, ok bool) { 984 auth := r.Header.Get("Authorization") 985 if auth == "" { 986 return "", "", false 987 } 988 return parseBasicAuth(auth) 989 } 990 991 // parseBasicAuth parses an HTTP Basic Authentication string. 992 // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true). 993 // 994 // parseBasicAuth should be an internal detail, 995 // but widely used packages access it using linkname. 996 // Notable members of the hall of shame include: 997 // - github.com/sagernet/sing 998 // 999 // Do not remove or change the type signature. 1000 // See go.dev/issue/67401. 1001 // 1002 //go:linkname parseBasicAuth 1003 func parseBasicAuth(auth string) (username, password string, ok bool) { 1004 const prefix = "Basic " 1005 // Case insensitive prefix match. See Issue 22736. 1006 if len(auth) < len(prefix) || !ascii.EqualFold(auth[:len(prefix)], prefix) { 1007 return "", "", false 1008 } 1009 c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) 1010 if err != nil { 1011 return "", "", false 1012 } 1013 cs := string(c) 1014 username, password, ok = strings.Cut(cs, ":") 1015 if !ok { 1016 return "", "", false 1017 } 1018 return username, password, true 1019 } 1020 1021 // SetBasicAuth sets the request's Authorization header to use HTTP 1022 // Basic Authentication with the provided username and password. 1023 // 1024 // With HTTP Basic Authentication the provided username and password 1025 // are not encrypted. It should generally only be used in an HTTPS 1026 // request. 1027 // 1028 // The username may not contain a colon. Some protocols may impose 1029 // additional requirements on pre-escaping the username and 1030 // password. For instance, when used with OAuth2, both arguments must 1031 // be URL encoded first with [url.QueryEscape]. 1032 func (r *Request) SetBasicAuth(username, password string) { 1033 r.Header.Set("Authorization", "Basic "+basicAuth(username, password)) 1034 } 1035 1036 // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. 1037 func parseRequestLine(line string) (method, requestURI, proto string, ok bool) { 1038 method, rest, ok1 := strings.Cut(line, " ") 1039 requestURI, proto, ok2 := strings.Cut(rest, " ") 1040 if !ok1 || !ok2 { 1041 return "", "", "", false 1042 } 1043 return method, requestURI, proto, true 1044 } 1045 1046 var textprotoReaderPool sync.Pool 1047 1048 func newTextprotoReader(br *bufio.Reader) *textproto.Reader { 1049 if v := textprotoReaderPool.Get(); v != nil { 1050 tr := v.(*textproto.Reader) 1051 tr.R = br 1052 return tr 1053 } 1054 return textproto.NewReader(br) 1055 } 1056 1057 func putTextprotoReader(r *textproto.Reader) { 1058 r.R = nil 1059 textprotoReaderPool.Put(r) 1060 } 1061 1062 // ReadRequest reads and parses an incoming request from b. 1063 // 1064 // ReadRequest is a low-level function and should only be used for 1065 // specialized applications; most code should use the [Server] to read 1066 // requests and handle them via the [Handler] interface. ReadRequest 1067 // only supports HTTP/1.x requests. For HTTP/2, use golang.org/x/net/http2. 1068 func ReadRequest(b *bufio.Reader) (*Request, error) { 1069 req, err := readRequest(b) 1070 if err != nil { 1071 return nil, err 1072 } 1073 1074 delete(req.Header, "Host") 1075 return req, nil 1076 } 1077 1078 // readMIMEHeader is defined in package [net/textproto]. 1079 // 1080 //go:linkname readMIMEHeader net/textproto.readMIMEHeader 1081 func readMIMEHeader(r *textproto.Reader, maxMemory, maxHeaders int64) (textproto.MIMEHeader, error) 1082 1083 // readRequest should be an internal detail, 1084 // but widely used packages access it using linkname. 1085 // Notable members of the hall of shame include: 1086 // - github.com/sagernet/sing 1087 // - github.com/v2fly/v2ray-core/v4 1088 // - github.com/v2fly/v2ray-core/v5 1089 // 1090 // Do not remove or change the type signature. 1091 // See go.dev/issue/67401. 1092 // 1093 //go:linkname readRequest 1094 func readRequest(b *bufio.Reader) (req *Request, err error) { 1095 return readRequestLimit(b, math.MaxInt64) 1096 } 1097 1098 func readRequestLimit(b *bufio.Reader, maxHeaders int64) (req *Request, err error) { 1099 tp := newTextprotoReader(b) 1100 defer putTextprotoReader(tp) 1101 1102 req = new(Request) 1103 1104 // First line: GET /index.html HTTP/1.0 1105 var s string 1106 if s, err = tp.ReadLine(); err != nil { 1107 return nil, err 1108 } 1109 defer func() { 1110 if err == io.EOF { 1111 err = io.ErrUnexpectedEOF 1112 } 1113 }() 1114 1115 var ok bool 1116 req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s) 1117 if !ok { 1118 return nil, badStringError("malformed HTTP request", s) 1119 } 1120 if !validMethod(req.Method) { 1121 return nil, badStringError("invalid method", req.Method) 1122 } 1123 rawurl := req.RequestURI 1124 if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok { 1125 return nil, badStringError("malformed HTTP version", req.Proto) 1126 } 1127 1128 // CONNECT requests are used two different ways, and neither uses a full URL: 1129 // The standard use is to tunnel HTTPS through an HTTP proxy. 1130 // It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is 1131 // just the authority section of a URL. This information should go in req.URL.Host. 1132 // 1133 // The net/rpc package also uses CONNECT, but there the parameter is a path 1134 // that starts with a slash. It can be parsed with the regular URL parser, 1135 // and the path will end up in req.URL.Path, where it needs to be in order for 1136 // RPC to work. 1137 justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/") 1138 if justAuthority { 1139 rawurl = "http://" + rawurl 1140 } 1141 1142 if req.URL, err = url.ParseRequestURI(rawurl); err != nil { 1143 return nil, err 1144 } 1145 1146 if justAuthority { 1147 // Strip the bogus "http://" back off. 1148 req.URL.Scheme = "" 1149 } 1150 1151 // Subsequent lines: Key: value. 1152 mimeHeader, err := readMIMEHeader(tp, math.MaxInt64, maxHeaders) 1153 if err != nil { 1154 // TODO: Add a distinguishable error to net/textproto. 1155 if err.Error() == "message too large" { 1156 return nil, errTooLarge 1157 } 1158 return nil, err 1159 } 1160 req.Header = Header(mimeHeader) 1161 if len(req.Header["Host"]) > 1 { 1162 return nil, fmt.Errorf("too many Host headers") 1163 } 1164 1165 // RFC 7230, section 5.3: Must treat 1166 // GET /index.html HTTP/1.1 1167 // Host: www.google.com 1168 // and 1169 // GET http://www.google.com/index.html HTTP/1.1 1170 // Host: doesntmatter 1171 // the same. In the second case, any Host line is ignored. 1172 req.Host = req.URL.Host 1173 if req.Host == "" { 1174 req.Host = req.Header.get("Host") 1175 } 1176 1177 fixPragmaCacheControl(req.Header) 1178 1179 req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header, false) 1180 1181 err = readTransfer(req, b, maxHeaders) 1182 if err != nil { 1183 return nil, err 1184 } 1185 1186 if req.isH2Upgrade() { 1187 // Because it's neither chunked, nor declared: 1188 req.ContentLength = -1 1189 1190 // We want to give handlers a chance to hijack the 1191 // connection, but we need to prevent the Server from 1192 // dealing with the connection further if it's not 1193 // hijacked. Set Close to ensure that: 1194 req.Close = true 1195 } 1196 return req, nil 1197 } 1198 1199 // MaxBytesReader is similar to [io.LimitReader] but is intended for 1200 // limiting the size of incoming request bodies. In contrast to 1201 // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a 1202 // non-nil error of type [*MaxBytesError] for a Read beyond the limit, 1203 // and closes the underlying reader when its Close method is called. 1204 // 1205 // MaxBytesReader prevents clients from accidentally or maliciously 1206 // sending a large request and wasting server resources. If possible, 1207 // it tells the [ResponseWriter] to close the connection after the limit 1208 // has been reached. 1209 func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser { 1210 if n < 0 { // Treat negative limits as equivalent to 0. 1211 n = 0 1212 } 1213 return &maxBytesReader{w: w, r: r, i: n, n: n} 1214 } 1215 1216 // MaxBytesError is returned by [MaxBytesReader] when its read limit is exceeded. 1217 type MaxBytesError struct { 1218 Limit int64 1219 } 1220 1221 func (e *MaxBytesError) Error() string { 1222 // Due to Hyrum's law, this text cannot be changed. 1223 return "http: request body too large" 1224 } 1225 1226 type maxBytesReader struct { 1227 w ResponseWriter 1228 r io.ReadCloser // underlying reader 1229 i int64 // max bytes initially, for MaxBytesError 1230 n int64 // max bytes remaining 1231 err error // sticky error 1232 } 1233 1234 func (l *maxBytesReader) Read(p []byte) (n int, err error) { 1235 if l.err != nil { 1236 return 0, l.err 1237 } 1238 if len(p) == 0 { 1239 return 0, nil 1240 } 1241 // If they asked for a 32KB byte read but only 5 bytes are 1242 // remaining, no need to read 32KB. 6 bytes will answer the 1243 // question of the whether we hit the limit or go past it. 1244 // 0 < len(p) < 2^63 1245 if int64(len(p))-1 > l.n { 1246 p = p[:l.n+1] 1247 } 1248 n, err = l.r.Read(p) 1249 1250 if int64(n) <= l.n { 1251 l.n -= int64(n) 1252 l.err = err 1253 return n, err 1254 } 1255 1256 n = int(l.n) 1257 l.n = 0 1258 1259 // The server code and client code both use 1260 // maxBytesReader. This "requestTooLarge" check is 1261 // only used by the server code. To prevent binaries 1262 // which only using the HTTP Client code (such as 1263 // cmd/go) from also linking in the HTTP server, don't 1264 // use a static type assertion to the server 1265 // "*response" type. Check this interface instead: 1266 type requestTooLarger interface { 1267 requestTooLarge() 1268 } 1269 if res, ok := l.w.(requestTooLarger); ok { 1270 res.requestTooLarge() 1271 } 1272 l.err = &MaxBytesError{l.i} 1273 return n, l.err 1274 } 1275 1276 func (l *maxBytesReader) Close() error { 1277 return l.r.Close() 1278 } 1279 1280 func copyValues(dst, src url.Values) { 1281 for k, vs := range src { 1282 dst[k] = append(dst[k], vs...) 1283 } 1284 } 1285 1286 func parsePostForm(r *Request) (vs url.Values, err error) { 1287 if r.Body == nil { 1288 err = errors.New("missing form body") 1289 return 1290 } 1291 ct := r.Header.Get("Content-Type") 1292 // RFC 7231, section 3.1.1.5 - empty type 1293 // MAY be treated as application/octet-stream 1294 if ct == "" { 1295 ct = "application/octet-stream" 1296 } 1297 ct, _, err = mime.ParseMediaType(ct) 1298 switch { 1299 case ct == "application/x-www-form-urlencoded": 1300 var reader io.Reader = r.Body 1301 maxFormSize := int64(1<<63 - 1) 1302 if _, ok := r.Body.(*maxBytesReader); !ok { 1303 maxFormSize = int64(10 << 20) // 10 MB is a lot of text. 1304 reader = io.LimitReader(r.Body, maxFormSize+1) 1305 } 1306 b, e := io.ReadAll(reader) 1307 if e != nil { 1308 if err == nil { 1309 err = e 1310 } 1311 break 1312 } 1313 if int64(len(b)) > maxFormSize { 1314 err = errors.New("http: POST too large") 1315 return 1316 } 1317 vs, e = url.ParseQuery(string(b)) 1318 if err == nil { 1319 err = e 1320 } 1321 case ct == "multipart/form-data": 1322 // handled by ParseMultipartForm (which is calling us, or should be) 1323 // TODO(bradfitz): there are too many possible 1324 // orders to call too many functions here. 1325 // Clean this up and write more tests. 1326 // request_test.go contains the start of this, 1327 // in TestParseMultipartFormOrder and others. 1328 } 1329 return 1330 } 1331 1332 // ParseForm populates r.Form and r.PostForm. 1333 // 1334 // For all requests, ParseForm parses the raw query from the URL and updates 1335 // r.Form. 1336 // 1337 // For POST, PUT, and PATCH requests, it also reads the request body, parses it 1338 // as a form and puts the results into both r.PostForm and r.Form. Request body 1339 // parameters take precedence over URL query string values in r.Form. 1340 // 1341 // If the request Body's size has not already been limited by [MaxBytesReader], 1342 // the size is capped at 10MB. 1343 // 1344 // For other HTTP methods, or when the Content-Type is not 1345 // application/x-www-form-urlencoded, the request Body is not read, and 1346 // r.PostForm is initialized to a non-nil, empty value. 1347 // 1348 // [Request.ParseMultipartForm] calls ParseForm automatically. 1349 // ParseForm is idempotent. 1350 func (r *Request) ParseForm() error { 1351 var err error 1352 if r.PostForm == nil { 1353 if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" { 1354 r.PostForm, err = parsePostForm(r) 1355 } 1356 if r.PostForm == nil { 1357 r.PostForm = make(url.Values) 1358 } 1359 } 1360 if r.Form == nil { 1361 if len(r.PostForm) > 0 { 1362 r.Form = make(url.Values) 1363 copyValues(r.Form, r.PostForm) 1364 } 1365 var newValues url.Values 1366 if r.URL != nil { 1367 var e error 1368 newValues, e = url.ParseQuery(r.URL.RawQuery) 1369 if err == nil { 1370 err = e 1371 } 1372 } 1373 if newValues == nil { 1374 newValues = make(url.Values) 1375 } 1376 if r.Form == nil { 1377 r.Form = newValues 1378 } else { 1379 copyValues(r.Form, newValues) 1380 } 1381 } 1382 return err 1383 } 1384 1385 // ParseMultipartForm parses a request body as multipart/form-data. 1386 // The whole request body is parsed and up to a total of maxMemory bytes of 1387 // its file parts are stored in memory, with the remainder stored on 1388 // disk in temporary files. 1389 // ParseMultipartForm calls [Request.ParseForm] if necessary. 1390 // If ParseForm returns an error, ParseMultipartForm returns it but also 1391 // continues parsing the request body. 1392 // After one call to ParseMultipartForm, subsequent calls have no effect. 1393 func (r *Request) ParseMultipartForm(maxMemory int64) error { 1394 if r.MultipartForm == multipartByReader { 1395 return errors.New("http: multipart handled by MultipartReader") 1396 } 1397 var parseFormErr error 1398 if r.Form == nil { 1399 // Let errors in ParseForm fall through, and just 1400 // return it at the end. 1401 parseFormErr = r.ParseForm() 1402 } 1403 if r.MultipartForm != nil { 1404 return nil 1405 } 1406 1407 mr, err := r.multipartReader(false) 1408 if err != nil { 1409 return err 1410 } 1411 1412 f, err := mr.ReadForm(maxMemory) 1413 if err != nil { 1414 return err 1415 } 1416 1417 if r.PostForm == nil { 1418 r.PostForm = make(url.Values) 1419 } 1420 for k, v := range f.Value { 1421 r.Form[k] = append(r.Form[k], v...) 1422 // r.PostForm should also be populated. See Issue 9305. 1423 r.PostForm[k] = append(r.PostForm[k], v...) 1424 } 1425 1426 r.MultipartForm = f 1427 1428 return parseFormErr 1429 } 1430 1431 // FormValue returns the first value for the named component of the query. 1432 // The precedence order: 1433 // 1. application/x-www-form-urlencoded form body (POST, PUT, PATCH only) 1434 // 2. query parameters (always) 1435 // 3. multipart/form-data form body (always) 1436 // 1437 // FormValue calls [Request.ParseMultipartForm] and [Request.ParseForm] 1438 // if necessary and ignores any errors returned by these functions. 1439 // If key is not present, FormValue returns the empty string. 1440 // To access multiple values of the same key, call ParseForm and 1441 // then inspect [Request.Form] directly. 1442 func (r *Request) FormValue(key string) string { 1443 if r.Form == nil { 1444 r.ParseMultipartForm(defaultMaxMemory) 1445 } 1446 if vs := r.Form[key]; len(vs) > 0 { 1447 return vs[0] 1448 } 1449 return "" 1450 } 1451 1452 // PostFormValue returns the first value for the named component of the POST, 1453 // PUT, or PATCH request body. URL query parameters are ignored. 1454 // PostFormValue calls [Request.ParseMultipartForm] and [Request.ParseForm] if necessary and ignores 1455 // any errors returned by these functions. 1456 // If key is not present, PostFormValue returns the empty string. 1457 func (r *Request) PostFormValue(key string) string { 1458 if r.PostForm == nil { 1459 r.ParseMultipartForm(defaultMaxMemory) 1460 } 1461 if vs := r.PostForm[key]; len(vs) > 0 { 1462 return vs[0] 1463 } 1464 return "" 1465 } 1466 1467 // FormFile returns the first file for the provided form key. 1468 // FormFile calls [Request.ParseMultipartForm] and [Request.ParseForm] if necessary. 1469 func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) { 1470 if r.MultipartForm == multipartByReader { 1471 return nil, nil, errors.New("http: multipart handled by MultipartReader") 1472 } 1473 if r.MultipartForm == nil { 1474 err := r.ParseMultipartForm(defaultMaxMemory) 1475 if err != nil { 1476 return nil, nil, err 1477 } 1478 } 1479 if r.MultipartForm != nil && r.MultipartForm.File != nil { 1480 if fhs := r.MultipartForm.File[key]; len(fhs) > 0 { 1481 f, err := fhs[0].Open() 1482 return f, fhs[0], err 1483 } 1484 } 1485 return nil, nil, ErrMissingFile 1486 } 1487 1488 // PathValue returns the value for the named path wildcard in the [ServeMux] pattern 1489 // that matched the request. 1490 // It returns the empty string if the request was not matched against a pattern 1491 // or there is no such wildcard in the pattern. 1492 // 1493 // The value is unescaped. For example, if the pattern "/b/{bucket}" matches 1494 // the path "/b/a%2fb", PathValue("bucket") returns "a/b". 1495 func (r *Request) PathValue(name string) string { 1496 if i := r.patIndex(name); i >= 0 { 1497 return r.matches[i] 1498 } 1499 return r.otherValues[name] 1500 } 1501 1502 // SetPathValue sets name to value, so that subsequent calls to r.PathValue(name) 1503 // return value. 1504 // It does not unescape value. 1505 func (r *Request) SetPathValue(name, value string) { 1506 if i := r.patIndex(name); i >= 0 { 1507 r.matches[i] = value 1508 } else { 1509 if r.otherValues == nil { 1510 r.otherValues = map[string]string{} 1511 } 1512 r.otherValues[name] = value 1513 } 1514 } 1515 1516 // patIndex returns the index of name in the list of named wildcards of the 1517 // request's pattern, or -1 if there is no such name. 1518 func (r *Request) patIndex(name string) int { 1519 // The linear search seems expensive compared to a map, but just creating the map 1520 // takes a lot of time, and most patterns will just have a couple of wildcards. 1521 if r.pat == nil { 1522 return -1 1523 } 1524 i := 0 1525 for _, seg := range r.pat.segments { 1526 if seg.wild && seg.s != "" { 1527 if name == seg.s { 1528 return i 1529 } 1530 i++ 1531 } 1532 } 1533 return -1 1534 } 1535 1536 func (r *Request) expectsContinue() bool { 1537 return hasToken(r.Header.get("Expect"), "100-continue") 1538 } 1539 1540 func (r *Request) wantsHttp10KeepAlive() bool { 1541 if r.ProtoMajor != 1 || r.ProtoMinor != 0 { 1542 return false 1543 } 1544 return hasToken(r.Header.get("Connection"), "keep-alive") 1545 } 1546 1547 func (r *Request) wantsClose() bool { 1548 if r.Close { 1549 return true 1550 } 1551 return hasToken(r.Header.get("Connection"), "close") 1552 } 1553 1554 func (r *Request) closeBody() error { 1555 if r.Body == nil { 1556 return nil 1557 } 1558 return r.Body.Close() 1559 } 1560 1561 func (r *Request) isReplayable() bool { 1562 if r.Body == nil || r.Body == NoBody || r.GetBody != nil { 1563 switch valueOrDefault(r.Method, "GET") { 1564 case "GET", "HEAD", "OPTIONS", "TRACE": 1565 return true 1566 } 1567 // The Idempotency-Key, while non-standard, is widely used to 1568 // mean a POST or other request is idempotent. See 1569 // https://golang.org/issue/19943#issuecomment-421092421 1570 if r.Header.has("Idempotency-Key") || r.Header.has("X-Idempotency-Key") { 1571 return true 1572 } 1573 } 1574 return false 1575 } 1576 1577 // outgoingLength reports the Content-Length of this outgoing (Client) request. 1578 // It maps 0 into -1 (unknown) when the Body is non-nil. 1579 func (r *Request) outgoingLength() int64 { 1580 if r.Body == nil || r.Body == NoBody { 1581 return 0 1582 } 1583 if r.ContentLength != 0 { 1584 return r.ContentLength 1585 } 1586 return -1 1587 } 1588 1589 // requestMethodUsuallyLacksBody reports whether the given request 1590 // method is one that typically does not involve a request body. 1591 // This is used by the Transport (via 1592 // transferWriter.shouldSendChunkedRequestBody) to determine whether 1593 // we try to test-read a byte from a non-nil Request.Body when 1594 // Request.outgoingLength() returns -1. See the comments in 1595 // shouldSendChunkedRequestBody. 1596 func requestMethodUsuallyLacksBody(method string) bool { 1597 switch method { 1598 case "GET", "HEAD", "DELETE", "OPTIONS", "PROPFIND", "SEARCH": 1599 return true 1600 } 1601 return false 1602 } 1603 1604 // requiresHTTP1 reports whether this request requires being sent on 1605 // an HTTP/1 connection. 1606 func (r *Request) requiresHTTP1() bool { 1607 return hasToken(r.Header.Get("Connection"), "upgrade") && 1608 ascii.EqualFold(r.Header.Get("Upgrade"), "websocket") 1609 } 1610