Source file src/net/http/request.go

     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: Follow the WHATWG URL Specification.
   793  	//
   794  	// WHATWG accepts all ASCII-only names (although sometimes with advisory
   795  	// validation errors), so skipping the relatively expensive IDNA processing
   796  	// on them is fine.
   797  	if ascii.Is(v) {
   798  		return v, nil
   799  	}
   800  	return idna.Lookup.ToASCII(v)
   801  }
   802  
   803  // removeZone removes IPv6 zone identifier from host.
   804  // E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
   805  func removeZone(host string) string {
   806  	if !strings.HasPrefix(host, "[") {
   807  		return host
   808  	}
   809  	i := strings.LastIndex(host, "]")
   810  	if i < 0 {
   811  		return host
   812  	}
   813  	j := strings.LastIndex(host[:i], "%")
   814  	if j < 0 {
   815  		return host
   816  	}
   817  	return host[:j] + host[i:]
   818  }
   819  
   820  // ParseHTTPVersion parses an HTTP version string according to RFC 7230, section 2.6.
   821  // "HTTP/1.0" returns (1, 0, true). Note that strings without
   822  // a minor version, such as "HTTP/2", are not valid.
   823  func ParseHTTPVersion(vers string) (major, minor int, ok bool) {
   824  	switch vers {
   825  	case "HTTP/1.1":
   826  		return 1, 1, true
   827  	case "HTTP/1.0":
   828  		return 1, 0, true
   829  	}
   830  	if !strings.HasPrefix(vers, "HTTP/") {
   831  		return 0, 0, false
   832  	}
   833  	if len(vers) != len("HTTP/X.Y") {
   834  		return 0, 0, false
   835  	}
   836  	if vers[6] != '.' {
   837  		return 0, 0, false
   838  	}
   839  	maj, err := strconv.ParseUint(vers[5:6], 10, 0)
   840  	if err != nil {
   841  		return 0, 0, false
   842  	}
   843  	min, err := strconv.ParseUint(vers[7:8], 10, 0)
   844  	if err != nil {
   845  		return 0, 0, false
   846  	}
   847  	return int(maj), int(min), true
   848  }
   849  
   850  func validMethod(method string) bool {
   851  	/*
   852  	     Method         = "OPTIONS"                ; Section 9.2
   853  	                    | "GET"                    ; Section 9.3
   854  	                    | "HEAD"                   ; Section 9.4
   855  	                    | "POST"                   ; Section 9.5
   856  	                    | "PUT"                    ; Section 9.6
   857  	                    | "DELETE"                 ; Section 9.7
   858  	                    | "TRACE"                  ; Section 9.8
   859  	                    | "CONNECT"                ; Section 9.9
   860  	                    | extension-method
   861  	   extension-method = token
   862  	     token          = 1*<any CHAR except CTLs or separators>
   863  	*/
   864  	return isToken(method)
   865  }
   866  
   867  // NewRequest wraps [NewRequestWithContext] using [context.Background].
   868  func NewRequest(method, url string, body io.Reader) (*Request, error) {
   869  	return NewRequestWithContext(context.Background(), method, url, body)
   870  }
   871  
   872  // NewRequestWithContext returns a new [Request] given a method, URL, and
   873  // optional body.
   874  //
   875  // If the provided body is also an [io.Closer], the returned
   876  // [Request.Body] is set to body and will be closed (possibly
   877  // asynchronously) by the Client methods Do, Post, and PostForm,
   878  // and [Transport.RoundTrip].
   879  //
   880  // NewRequestWithContext returns a Request suitable for use with
   881  // [Client.Do] or [Transport.RoundTrip]. To create a request for use with
   882  // testing a Server Handler, either use the [net/http/httptest.NewRequest] function,
   883  // use [ReadRequest], or manually update the Request fields.
   884  // For an outgoing client request, the context
   885  // controls the entire lifetime of a request and its response:
   886  // obtaining a connection, sending the request, and reading the
   887  // response headers and body. See the [Request] type's documentation for
   888  // the difference between inbound and outbound request fields.
   889  //
   890  // If body is of type [*bytes.Buffer], [*bytes.Reader], or
   891  // [*strings.Reader], the returned request's ContentLength is set to its
   892  // exact value (instead of -1), GetBody is populated (so 307 and 308
   893  // redirects can replay the body), and Body is set to [NoBody] if the
   894  // ContentLength is 0.
   895  func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error) {
   896  	if method == "" {
   897  		// We document that "" means "GET" for Request.Method, and people have
   898  		// relied on that from NewRequest, so keep that working.
   899  		// We still enforce validMethod for non-empty methods.
   900  		method = "GET"
   901  	}
   902  	if !validMethod(method) {
   903  		return nil, fmt.Errorf("net/http: invalid method %q", method)
   904  	}
   905  	if ctx == nil {
   906  		return nil, errors.New("net/http: nil Context")
   907  	}
   908  	u, err := urlpkg.Parse(url)
   909  	if err != nil {
   910  		return nil, err
   911  	}
   912  	rc, ok := body.(io.ReadCloser)
   913  	if !ok && body != nil {
   914  		rc = io.NopCloser(body)
   915  	}
   916  	// The host's colon:port should be normalized. See Issue 14836.
   917  	u.Host = strings.TrimSuffix(u.Host, ":")
   918  	req := &Request{
   919  		ctx:        ctx,
   920  		Method:     method,
   921  		URL:        u,
   922  		Proto:      "HTTP/1.1",
   923  		ProtoMajor: 1,
   924  		ProtoMinor: 1,
   925  		Header:     make(Header),
   926  		Body:       rc,
   927  		Host:       u.Host,
   928  	}
   929  	if body != nil {
   930  		switch v := body.(type) {
   931  		case *bytes.Buffer:
   932  			req.ContentLength = int64(v.Len())
   933  			buf := v.Bytes()
   934  			req.GetBody = func() (io.ReadCloser, error) {
   935  				r := bytes.NewReader(buf)
   936  				return io.NopCloser(r), nil
   937  			}
   938  		case *bytes.Reader:
   939  			req.ContentLength = int64(v.Len())
   940  			snapshot := *v
   941  			req.GetBody = func() (io.ReadCloser, error) {
   942  				r := snapshot
   943  				return io.NopCloser(&r), nil
   944  			}
   945  		case *strings.Reader:
   946  			req.ContentLength = int64(v.Len())
   947  			snapshot := *v
   948  			req.GetBody = func() (io.ReadCloser, error) {
   949  				r := snapshot
   950  				return io.NopCloser(&r), nil
   951  			}
   952  		default:
   953  			// This is where we'd set it to -1 (at least
   954  			// if body != NoBody) to mean unknown, but
   955  			// that broke people during the Go 1.8 testing
   956  			// period. People depend on it being 0 I
   957  			// guess. Maybe retry later. See Issue 18117.
   958  		}
   959  		// For client requests, Request.ContentLength of 0
   960  		// means either actually 0, or unknown. The only way
   961  		// to explicitly say that the ContentLength is zero is
   962  		// to set the Body to nil. But turns out too much code
   963  		// depends on NewRequest returning a non-nil Body,
   964  		// so we use a well-known ReadCloser variable instead
   965  		// and have the http package also treat that sentinel
   966  		// variable to mean explicitly zero.
   967  		if req.GetBody != nil && req.ContentLength == 0 {
   968  			req.Body = NoBody
   969  			req.GetBody = func() (io.ReadCloser, error) { return NoBody, nil }
   970  		}
   971  	}
   972  
   973  	return req, nil
   974  }
   975  
   976  // BasicAuth returns the username and password provided in the request's
   977  // Authorization header, if the request uses HTTP Basic Authentication.
   978  // See RFC 2617, Section 2.
   979  func (r *Request) BasicAuth() (username, password string, ok bool) {
   980  	auth := r.Header.Get("Authorization")
   981  	if auth == "" {
   982  		return "", "", false
   983  	}
   984  	return parseBasicAuth(auth)
   985  }
   986  
   987  // parseBasicAuth parses an HTTP Basic Authentication string.
   988  // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
   989  //
   990  // parseBasicAuth should be an internal detail,
   991  // but widely used packages access it using linkname.
   992  // Notable members of the hall of shame include:
   993  //   - github.com/sagernet/sing
   994  //
   995  // Do not remove or change the type signature.
   996  // See go.dev/issue/67401.
   997  //
   998  //go:linkname parseBasicAuth
   999  func parseBasicAuth(auth string) (username, password string, ok bool) {
  1000  	const prefix = "Basic "
  1001  	// Case insensitive prefix match. See Issue 22736.
  1002  	if len(auth) < len(prefix) || !ascii.EqualFold(auth[:len(prefix)], prefix) {
  1003  		return "", "", false
  1004  	}
  1005  	c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
  1006  	if err != nil {
  1007  		return "", "", false
  1008  	}
  1009  	cs := string(c)
  1010  	username, password, ok = strings.Cut(cs, ":")
  1011  	if !ok {
  1012  		return "", "", false
  1013  	}
  1014  	return username, password, true
  1015  }
  1016  
  1017  // SetBasicAuth sets the request's Authorization header to use HTTP
  1018  // Basic Authentication with the provided username and password.
  1019  //
  1020  // With HTTP Basic Authentication the provided username and password
  1021  // are not encrypted. It should generally only be used in an HTTPS
  1022  // request.
  1023  //
  1024  // The username may not contain a colon. Some protocols may impose
  1025  // additional requirements on pre-escaping the username and
  1026  // password. For instance, when used with OAuth2, both arguments must
  1027  // be URL encoded first with [url.QueryEscape].
  1028  func (r *Request) SetBasicAuth(username, password string) {
  1029  	r.Header.Set("Authorization", "Basic "+basicAuth(username, password))
  1030  }
  1031  
  1032  // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
  1033  func parseRequestLine(line string) (method, requestURI, proto string, ok bool) {
  1034  	method, rest, ok1 := strings.Cut(line, " ")
  1035  	requestURI, proto, ok2 := strings.Cut(rest, " ")
  1036  	if !ok1 || !ok2 {
  1037  		return "", "", "", false
  1038  	}
  1039  	return method, requestURI, proto, true
  1040  }
  1041  
  1042  var textprotoReaderPool sync.Pool
  1043  
  1044  func newTextprotoReader(br *bufio.Reader) *textproto.Reader {
  1045  	if v := textprotoReaderPool.Get(); v != nil {
  1046  		tr := v.(*textproto.Reader)
  1047  		tr.R = br
  1048  		return tr
  1049  	}
  1050  	return textproto.NewReader(br)
  1051  }
  1052  
  1053  func putTextprotoReader(r *textproto.Reader) {
  1054  	r.R = nil
  1055  	textprotoReaderPool.Put(r)
  1056  }
  1057  
  1058  // ReadRequest reads and parses an incoming request from b.
  1059  //
  1060  // ReadRequest is a low-level function and should only be used for
  1061  // specialized applications; most code should use the [Server] to read
  1062  // requests and handle them via the [Handler] interface. ReadRequest
  1063  // only supports HTTP/1.x requests. For HTTP/2, use golang.org/x/net/http2.
  1064  func ReadRequest(b *bufio.Reader) (*Request, error) {
  1065  	req, err := readRequest(b)
  1066  	if err != nil {
  1067  		return nil, err
  1068  	}
  1069  
  1070  	delete(req.Header, "Host")
  1071  	return req, nil
  1072  }
  1073  
  1074  // readMIMEHeader is defined in package [net/textproto].
  1075  //
  1076  //go:linkname readMIMEHeader net/textproto.readMIMEHeader
  1077  func readMIMEHeader(r *textproto.Reader, maxMemory, maxHeaders int64) (textproto.MIMEHeader, error)
  1078  
  1079  // readRequest should be an internal detail,
  1080  // but widely used packages access it using linkname.
  1081  // Notable members of the hall of shame include:
  1082  //   - github.com/sagernet/sing
  1083  //   - github.com/v2fly/v2ray-core/v4
  1084  //   - github.com/v2fly/v2ray-core/v5
  1085  //
  1086  // Do not remove or change the type signature.
  1087  // See go.dev/issue/67401.
  1088  //
  1089  //go:linkname readRequest
  1090  func readRequest(b *bufio.Reader) (req *Request, err error) {
  1091  	return readRequestLimit(b, math.MaxInt64)
  1092  }
  1093  
  1094  func readRequestLimit(b *bufio.Reader, maxHeaders int64) (req *Request, err error) {
  1095  	tp := newTextprotoReader(b)
  1096  	defer putTextprotoReader(tp)
  1097  
  1098  	req = new(Request)
  1099  
  1100  	// First line: GET /index.html HTTP/1.0
  1101  	var s string
  1102  	if s, err = tp.ReadLine(); err != nil {
  1103  		return nil, err
  1104  	}
  1105  	defer func() {
  1106  		if err == io.EOF {
  1107  			err = io.ErrUnexpectedEOF
  1108  		}
  1109  	}()
  1110  
  1111  	var ok bool
  1112  	req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s)
  1113  	if !ok {
  1114  		return nil, badStringError("malformed HTTP request", s)
  1115  	}
  1116  	if !validMethod(req.Method) {
  1117  		return nil, badStringError("invalid method", req.Method)
  1118  	}
  1119  	rawurl := req.RequestURI
  1120  	if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok {
  1121  		return nil, badStringError("malformed HTTP version", req.Proto)
  1122  	}
  1123  
  1124  	// CONNECT requests are used two different ways, and neither uses a full URL:
  1125  	// The standard use is to tunnel HTTPS through an HTTP proxy.
  1126  	// It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is
  1127  	// just the authority section of a URL. This information should go in req.URL.Host.
  1128  	//
  1129  	// The net/rpc package also uses CONNECT, but there the parameter is a path
  1130  	// that starts with a slash. It can be parsed with the regular URL parser,
  1131  	// and the path will end up in req.URL.Path, where it needs to be in order for
  1132  	// RPC to work.
  1133  	justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/")
  1134  	if justAuthority {
  1135  		rawurl = "http://" + rawurl
  1136  	}
  1137  
  1138  	if req.URL, err = url.ParseRequestURI(rawurl); err != nil {
  1139  		return nil, err
  1140  	}
  1141  
  1142  	if justAuthority {
  1143  		// Strip the bogus "http://" back off.
  1144  		req.URL.Scheme = ""
  1145  	}
  1146  
  1147  	// Subsequent lines: Key: value.
  1148  	mimeHeader, err := readMIMEHeader(tp, math.MaxInt64, maxHeaders)
  1149  	if err != nil {
  1150  		// TODO: Add a distinguishable error to net/textproto.
  1151  		if err.Error() == "message too large" {
  1152  			return nil, errTooLarge
  1153  		}
  1154  		return nil, err
  1155  	}
  1156  	req.Header = Header(mimeHeader)
  1157  	if len(req.Header["Host"]) > 1 {
  1158  		return nil, fmt.Errorf("too many Host headers")
  1159  	}
  1160  
  1161  	// RFC 7230, section 5.3: Must treat
  1162  	//	GET /index.html HTTP/1.1
  1163  	//	Host: www.google.com
  1164  	// and
  1165  	//	GET http://www.google.com/index.html HTTP/1.1
  1166  	//	Host: doesntmatter
  1167  	// the same. In the second case, any Host line is ignored.
  1168  	req.Host = req.URL.Host
  1169  	if req.Host == "" {
  1170  		req.Host = req.Header.get("Host")
  1171  	}
  1172  
  1173  	fixPragmaCacheControl(req.Header)
  1174  
  1175  	req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header, false)
  1176  
  1177  	err = readTransfer(req, b, maxHeaders)
  1178  	if err != nil {
  1179  		return nil, err
  1180  	}
  1181  
  1182  	if req.isH2Upgrade() {
  1183  		// Because it's neither chunked, nor declared:
  1184  		req.ContentLength = -1
  1185  
  1186  		// We want to give handlers a chance to hijack the
  1187  		// connection, but we need to prevent the Server from
  1188  		// dealing with the connection further if it's not
  1189  		// hijacked. Set Close to ensure that:
  1190  		req.Close = true
  1191  	}
  1192  	return req, nil
  1193  }
  1194  
  1195  // MaxBytesReader is similar to [io.LimitReader] but is intended for
  1196  // limiting the size of incoming request bodies. In contrast to
  1197  // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a
  1198  // non-nil error of type [*MaxBytesError] for a Read beyond the limit,
  1199  // and closes the underlying reader when its Close method is called.
  1200  //
  1201  // MaxBytesReader prevents clients from accidentally or maliciously
  1202  // sending a large request and wasting server resources. If possible,
  1203  // it tells the [ResponseWriter] to close the connection after the limit
  1204  // has been reached.
  1205  func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser {
  1206  	if n < 0 { // Treat negative limits as equivalent to 0.
  1207  		n = 0
  1208  	}
  1209  	return &maxBytesReader{w: w, r: r, i: n, n: n}
  1210  }
  1211  
  1212  // MaxBytesError is returned by [MaxBytesReader] when its read limit is exceeded.
  1213  type MaxBytesError struct {
  1214  	Limit int64
  1215  }
  1216  
  1217  func (e *MaxBytesError) Error() string {
  1218  	// Due to Hyrum's law, this text cannot be changed.
  1219  	return "http: request body too large"
  1220  }
  1221  
  1222  type maxBytesReader struct {
  1223  	w   ResponseWriter
  1224  	r   io.ReadCloser // underlying reader
  1225  	i   int64         // max bytes initially, for MaxBytesError
  1226  	n   int64         // max bytes remaining
  1227  	err error         // sticky error
  1228  }
  1229  
  1230  func (l *maxBytesReader) Read(p []byte) (n int, err error) {
  1231  	if l.err != nil {
  1232  		return 0, l.err
  1233  	}
  1234  	if len(p) == 0 {
  1235  		return 0, nil
  1236  	}
  1237  	// If they asked for a 32KB byte read but only 5 bytes are
  1238  	// remaining, no need to read 32KB. 6 bytes will answer the
  1239  	// question of the whether we hit the limit or go past it.
  1240  	// 0 < len(p) < 2^63
  1241  	if int64(len(p))-1 > l.n {
  1242  		p = p[:l.n+1]
  1243  	}
  1244  	n, err = l.r.Read(p)
  1245  
  1246  	if int64(n) <= l.n {
  1247  		l.n -= int64(n)
  1248  		l.err = err
  1249  		return n, err
  1250  	}
  1251  
  1252  	n = int(l.n)
  1253  	l.n = 0
  1254  
  1255  	// The server code and client code both use
  1256  	// maxBytesReader. This "requestTooLarge" check is
  1257  	// only used by the server code. To prevent binaries
  1258  	// which only using the HTTP Client code (such as
  1259  	// cmd/go) from also linking in the HTTP server, don't
  1260  	// use a static type assertion to the server
  1261  	// "*response" type. Check this interface instead:
  1262  	type requestTooLarger interface {
  1263  		requestTooLarge()
  1264  	}
  1265  	if res, ok := l.w.(requestTooLarger); ok {
  1266  		res.requestTooLarge()
  1267  	}
  1268  	l.err = &MaxBytesError{l.i}
  1269  	return n, l.err
  1270  }
  1271  
  1272  func (l *maxBytesReader) Close() error {
  1273  	return l.r.Close()
  1274  }
  1275  
  1276  func copyValues(dst, src url.Values) {
  1277  	for k, vs := range src {
  1278  		dst[k] = append(dst[k], vs...)
  1279  	}
  1280  }
  1281  
  1282  func parsePostForm(r *Request) (vs url.Values, err error) {
  1283  	if r.Body == nil {
  1284  		err = errors.New("missing form body")
  1285  		return
  1286  	}
  1287  	ct := r.Header.Get("Content-Type")
  1288  	// RFC 7231, section 3.1.1.5 - empty type
  1289  	//   MAY be treated as application/octet-stream
  1290  	if ct == "" {
  1291  		ct = "application/octet-stream"
  1292  	}
  1293  	ct, _, err = mime.ParseMediaType(ct)
  1294  	switch {
  1295  	case ct == "application/x-www-form-urlencoded":
  1296  		var reader io.Reader = r.Body
  1297  		maxFormSize := int64(1<<63 - 1)
  1298  		if _, ok := r.Body.(*maxBytesReader); !ok {
  1299  			maxFormSize = int64(10 << 20) // 10 MB is a lot of text.
  1300  			reader = io.LimitReader(r.Body, maxFormSize+1)
  1301  		}
  1302  		b, e := io.ReadAll(reader)
  1303  		if e != nil {
  1304  			if err == nil {
  1305  				err = e
  1306  			}
  1307  			break
  1308  		}
  1309  		if int64(len(b)) > maxFormSize {
  1310  			err = errors.New("http: POST too large")
  1311  			return
  1312  		}
  1313  		vs, e = url.ParseQuery(string(b))
  1314  		if err == nil {
  1315  			err = e
  1316  		}
  1317  	case ct == "multipart/form-data":
  1318  		// handled by ParseMultipartForm (which is calling us, or should be)
  1319  		// TODO(bradfitz): there are too many possible
  1320  		// orders to call too many functions here.
  1321  		// Clean this up and write more tests.
  1322  		// request_test.go contains the start of this,
  1323  		// in TestParseMultipartFormOrder and others.
  1324  	}
  1325  	return
  1326  }
  1327  
  1328  // ParseForm populates r.Form and r.PostForm.
  1329  //
  1330  // For all requests, ParseForm parses the raw query from the URL and updates
  1331  // r.Form.
  1332  //
  1333  // For POST, PUT, and PATCH requests, it also reads the request body, parses it
  1334  // as a form and puts the results into both r.PostForm and r.Form. Request body
  1335  // parameters take precedence over URL query string values in r.Form.
  1336  //
  1337  // If the request Body's size has not already been limited by [MaxBytesReader],
  1338  // the size is capped at 10MB.
  1339  //
  1340  // For other HTTP methods, or when the Content-Type is not
  1341  // application/x-www-form-urlencoded, the request Body is not read, and
  1342  // r.PostForm is initialized to a non-nil, empty value.
  1343  //
  1344  // [Request.ParseMultipartForm] calls ParseForm automatically.
  1345  // ParseForm is idempotent.
  1346  func (r *Request) ParseForm() error {
  1347  	var err error
  1348  	if r.PostForm == nil {
  1349  		if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
  1350  			r.PostForm, err = parsePostForm(r)
  1351  		}
  1352  		if r.PostForm == nil {
  1353  			r.PostForm = make(url.Values)
  1354  		}
  1355  	}
  1356  	if r.Form == nil {
  1357  		if len(r.PostForm) > 0 {
  1358  			r.Form = make(url.Values)
  1359  			copyValues(r.Form, r.PostForm)
  1360  		}
  1361  		var newValues url.Values
  1362  		if r.URL != nil {
  1363  			var e error
  1364  			newValues, e = url.ParseQuery(r.URL.RawQuery)
  1365  			if err == nil {
  1366  				err = e
  1367  			}
  1368  		}
  1369  		if newValues == nil {
  1370  			newValues = make(url.Values)
  1371  		}
  1372  		if r.Form == nil {
  1373  			r.Form = newValues
  1374  		} else {
  1375  			copyValues(r.Form, newValues)
  1376  		}
  1377  	}
  1378  	return err
  1379  }
  1380  
  1381  // ParseMultipartForm parses a request body as multipart/form-data.
  1382  // The whole request body is parsed and up to a total of maxMemory bytes of
  1383  // its file parts are stored in memory, with the remainder stored on
  1384  // disk in temporary files.
  1385  // ParseMultipartForm calls [Request.ParseForm] if necessary.
  1386  // If ParseForm returns an error, ParseMultipartForm returns it but also
  1387  // continues parsing the request body.
  1388  // After one call to ParseMultipartForm, subsequent calls have no effect.
  1389  func (r *Request) ParseMultipartForm(maxMemory int64) error {
  1390  	if r.MultipartForm == multipartByReader {
  1391  		return errors.New("http: multipart handled by MultipartReader")
  1392  	}
  1393  	var parseFormErr error
  1394  	if r.Form == nil {
  1395  		// Let errors in ParseForm fall through, and just
  1396  		// return it at the end.
  1397  		parseFormErr = r.ParseForm()
  1398  	}
  1399  	if r.MultipartForm != nil {
  1400  		return nil
  1401  	}
  1402  
  1403  	mr, err := r.multipartReader(false)
  1404  	if err != nil {
  1405  		return err
  1406  	}
  1407  
  1408  	f, err := mr.ReadForm(maxMemory)
  1409  	if err != nil {
  1410  		return err
  1411  	}
  1412  
  1413  	if r.PostForm == nil {
  1414  		r.PostForm = make(url.Values)
  1415  	}
  1416  	for k, v := range f.Value {
  1417  		r.Form[k] = append(r.Form[k], v...)
  1418  		// r.PostForm should also be populated. See Issue 9305.
  1419  		r.PostForm[k] = append(r.PostForm[k], v...)
  1420  	}
  1421  
  1422  	r.MultipartForm = f
  1423  
  1424  	return parseFormErr
  1425  }
  1426  
  1427  // FormValue returns the first value for the named component of the query.
  1428  // The precedence order:
  1429  //  1. application/x-www-form-urlencoded form body (POST, PUT, PATCH only)
  1430  //  2. query parameters (always)
  1431  //  3. multipart/form-data form body (always)
  1432  //
  1433  // FormValue calls [Request.ParseMultipartForm] and [Request.ParseForm]
  1434  // if necessary and ignores any errors returned by these functions.
  1435  // If key is not present, FormValue returns the empty string.
  1436  // To access multiple values of the same key, call ParseForm and
  1437  // then inspect [Request.Form] directly.
  1438  func (r *Request) FormValue(key string) string {
  1439  	if r.Form == nil {
  1440  		r.ParseMultipartForm(defaultMaxMemory)
  1441  	}
  1442  	if vs := r.Form[key]; len(vs) > 0 {
  1443  		return vs[0]
  1444  	}
  1445  	return ""
  1446  }
  1447  
  1448  // PostFormValue returns the first value for the named component of the POST,
  1449  // PUT, or PATCH request body. URL query parameters are ignored.
  1450  // PostFormValue calls [Request.ParseMultipartForm] and [Request.ParseForm] if necessary and ignores
  1451  // any errors returned by these functions.
  1452  // If key is not present, PostFormValue returns the empty string.
  1453  func (r *Request) PostFormValue(key string) string {
  1454  	if r.PostForm == nil {
  1455  		r.ParseMultipartForm(defaultMaxMemory)
  1456  	}
  1457  	if vs := r.PostForm[key]; len(vs) > 0 {
  1458  		return vs[0]
  1459  	}
  1460  	return ""
  1461  }
  1462  
  1463  // FormFile returns the first file for the provided form key.
  1464  // FormFile calls [Request.ParseMultipartForm] and [Request.ParseForm] if necessary.
  1465  func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
  1466  	if r.MultipartForm == multipartByReader {
  1467  		return nil, nil, errors.New("http: multipart handled by MultipartReader")
  1468  	}
  1469  	if r.MultipartForm == nil {
  1470  		err := r.ParseMultipartForm(defaultMaxMemory)
  1471  		if err != nil {
  1472  			return nil, nil, err
  1473  		}
  1474  	}
  1475  	if r.MultipartForm != nil && r.MultipartForm.File != nil {
  1476  		if fhs := r.MultipartForm.File[key]; len(fhs) > 0 {
  1477  			f, err := fhs[0].Open()
  1478  			return f, fhs[0], err
  1479  		}
  1480  	}
  1481  	return nil, nil, ErrMissingFile
  1482  }
  1483  
  1484  // PathValue returns the value for the named path wildcard in the [ServeMux] pattern
  1485  // that matched the request.
  1486  // It returns the empty string if the request was not matched against a pattern
  1487  // or there is no such wildcard in the pattern.
  1488  //
  1489  // The value is unescaped. For example, if the pattern "/b/{bucket}" matches
  1490  // the path "/b/a%2fb", PathValue("bucket") returns "a/b".
  1491  func (r *Request) PathValue(name string) string {
  1492  	if i := r.patIndex(name); i >= 0 {
  1493  		return r.matches[i]
  1494  	}
  1495  	return r.otherValues[name]
  1496  }
  1497  
  1498  // SetPathValue sets name to value, so that subsequent calls to r.PathValue(name)
  1499  // return value.
  1500  // It does not unescape value.
  1501  func (r *Request) SetPathValue(name, value string) {
  1502  	if i := r.patIndex(name); i >= 0 {
  1503  		r.matches[i] = value
  1504  	} else {
  1505  		if r.otherValues == nil {
  1506  			r.otherValues = map[string]string{}
  1507  		}
  1508  		r.otherValues[name] = value
  1509  	}
  1510  }
  1511  
  1512  // patIndex returns the index of name in the list of named wildcards of the
  1513  // request's pattern, or -1 if there is no such name.
  1514  func (r *Request) patIndex(name string) int {
  1515  	// The linear search seems expensive compared to a map, but just creating the map
  1516  	// takes a lot of time, and most patterns will just have a couple of wildcards.
  1517  	if r.pat == nil {
  1518  		return -1
  1519  	}
  1520  	i := 0
  1521  	for _, seg := range r.pat.segments {
  1522  		if seg.wild && seg.s != "" {
  1523  			if name == seg.s {
  1524  				return i
  1525  			}
  1526  			i++
  1527  		}
  1528  	}
  1529  	return -1
  1530  }
  1531  
  1532  func (r *Request) expectsContinue() bool {
  1533  	return hasToken(r.Header.get("Expect"), "100-continue")
  1534  }
  1535  
  1536  func (r *Request) wantsHttp10KeepAlive() bool {
  1537  	if r.ProtoMajor != 1 || r.ProtoMinor != 0 {
  1538  		return false
  1539  	}
  1540  	return hasToken(r.Header.get("Connection"), "keep-alive")
  1541  }
  1542  
  1543  func (r *Request) wantsClose() bool {
  1544  	if r.Close {
  1545  		return true
  1546  	}
  1547  	return hasToken(r.Header.get("Connection"), "close")
  1548  }
  1549  
  1550  func (r *Request) closeBody() error {
  1551  	if r.Body == nil {
  1552  		return nil
  1553  	}
  1554  	return r.Body.Close()
  1555  }
  1556  
  1557  func (r *Request) isReplayable() bool {
  1558  	if r.Body == nil || r.Body == NoBody || r.GetBody != nil {
  1559  		switch valueOrDefault(r.Method, "GET") {
  1560  		case "GET", "HEAD", "OPTIONS", "TRACE":
  1561  			return true
  1562  		}
  1563  		// The Idempotency-Key, while non-standard, is widely used to
  1564  		// mean a POST or other request is idempotent. See
  1565  		// https://golang.org/issue/19943#issuecomment-421092421
  1566  		if r.Header.has("Idempotency-Key") || r.Header.has("X-Idempotency-Key") {
  1567  			return true
  1568  		}
  1569  	}
  1570  	return false
  1571  }
  1572  
  1573  // outgoingLength reports the Content-Length of this outgoing (Client) request.
  1574  // It maps 0 into -1 (unknown) when the Body is non-nil.
  1575  func (r *Request) outgoingLength() int64 {
  1576  	if r.Body == nil || r.Body == NoBody {
  1577  		return 0
  1578  	}
  1579  	if r.ContentLength != 0 {
  1580  		return r.ContentLength
  1581  	}
  1582  	return -1
  1583  }
  1584  
  1585  // requestMethodUsuallyLacksBody reports whether the given request
  1586  // method is one that typically does not involve a request body.
  1587  // This is used by the Transport (via
  1588  // transferWriter.shouldSendChunkedRequestBody) to determine whether
  1589  // we try to test-read a byte from a non-nil Request.Body when
  1590  // Request.outgoingLength() returns -1. See the comments in
  1591  // shouldSendChunkedRequestBody.
  1592  func requestMethodUsuallyLacksBody(method string) bool {
  1593  	switch method {
  1594  	case "GET", "HEAD", "DELETE", "OPTIONS", "PROPFIND", "SEARCH":
  1595  		return true
  1596  	}
  1597  	return false
  1598  }
  1599  
  1600  // requiresHTTP1 reports whether this request requires being sent on
  1601  // an HTTP/1 connection.
  1602  func (r *Request) requiresHTTP1() bool {
  1603  	return hasToken(r.Header.Get("Connection"), "upgrade") &&
  1604  		ascii.EqualFold(r.Header.Get("Upgrade"), "websocket")
  1605  }
  1606  

View as plain text