Source file src/net/http/request.go

Documentation: net/http

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

View as plain text