...
Run Format

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

View as plain text