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