github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/net/http/request.go (about)

     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  	"crypto/tls"
    13  	"encoding/base64"
    14  	"errors"
    15  	"fmt"
    16  	"io"
    17  	"io/ioutil"
    18  	"mime"
    19  	"mime/multipart"
    20  	"net/textproto"
    21  	"net/url"
    22  	"strconv"
    23  	"strings"
    24  	"sync"
    25  )
    26  
    27  const (
    28  	defaultMaxMemory = 32 << 20 // 32 MB
    29  )
    30  
    31  // ErrMissingFile is returned by FormFile when the provided file field name
    32  // is either not present in the request or not a file field.
    33  var ErrMissingFile = errors.New("http: no such file")
    34  
    35  // HTTP request parsing errors.
    36  type ProtocolError struct {
    37  	ErrorString string
    38  }
    39  
    40  func (err *ProtocolError) Error() string { return err.ErrorString }
    41  
    42  var (
    43  	ErrHeaderTooLong        = &ProtocolError{"header too long"}
    44  	ErrShortBody            = &ProtocolError{"entity body too short"}
    45  	ErrNotSupported         = &ProtocolError{"feature not supported"}
    46  	ErrUnexpectedTrailer    = &ProtocolError{"trailer header without chunked transfer encoding"}
    47  	ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"}
    48  	ErrNotMultipart         = &ProtocolError{"request Content-Type isn't multipart/form-data"}
    49  	ErrMissingBoundary      = &ProtocolError{"no multipart boundary param in Content-Type"}
    50  )
    51  
    52  type badStringError struct {
    53  	what string
    54  	str  string
    55  }
    56  
    57  func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
    58  
    59  // Headers that Request.Write handles itself and should be skipped.
    60  var reqWriteExcludeHeader = map[string]bool{
    61  	"Host":              true, // not in Header map anyway
    62  	"User-Agent":        true,
    63  	"Content-Length":    true,
    64  	"Transfer-Encoding": true,
    65  	"Trailer":           true,
    66  }
    67  
    68  // A Request represents an HTTP request received by a server
    69  // or to be sent by a client.
    70  //
    71  // The field semantics differ slightly between client and server
    72  // usage. In addition to the notes on the fields below, see the
    73  // documentation for Request.Write and RoundTripper.
    74  type Request struct {
    75  	// Method specifies the HTTP method (GET, POST, PUT, etc.).
    76  	// For client requests an empty string means GET.
    77  	Method string
    78  
    79  	// URL specifies either the URI being requested (for server
    80  	// requests) or the URL to access (for client requests).
    81  	//
    82  	// For server requests the URL is parsed from the URI
    83  	// supplied on the Request-Line as stored in RequestURI.  For
    84  	// most requests, fields other than Path and RawQuery will be
    85  	// empty. (See RFC 2616, Section 5.1.2)
    86  	//
    87  	// For client requests, the URL's Host specifies the server to
    88  	// connect to, while the Request's Host field optionally
    89  	// specifies the Host header value to send in the HTTP
    90  	// request.
    91  	URL *url.URL
    92  
    93  	// The protocol version for incoming requests.
    94  	// Client requests always use HTTP/1.1.
    95  	Proto      string // "HTTP/1.0"
    96  	ProtoMajor int    // 1
    97  	ProtoMinor int    // 0
    98  
    99  	// A header maps request lines to their values.
   100  	// If the header says
   101  	//
   102  	//	accept-encoding: gzip, deflate
   103  	//	Accept-Language: en-us
   104  	//	Connection: keep-alive
   105  	//
   106  	// then
   107  	//
   108  	//	Header = map[string][]string{
   109  	//		"Accept-Encoding": {"gzip, deflate"},
   110  	//		"Accept-Language": {"en-us"},
   111  	//		"Connection": {"keep-alive"},
   112  	//	}
   113  	//
   114  	// HTTP defines that header names are case-insensitive.
   115  	// The request parser implements this by canonicalizing the
   116  	// name, making the first character and any characters
   117  	// following a hyphen uppercase and the rest lowercase.
   118  	//
   119  	// For client requests certain headers are automatically
   120  	// added and may override values in Header.
   121  	//
   122  	// See the documentation for the Request.Write method.
   123  	Header Header
   124  
   125  	// Body is the request's body.
   126  	//
   127  	// For client requests a nil body means the request has no
   128  	// body, such as a GET request. The HTTP Client's Transport
   129  	// is responsible for calling the Close method.
   130  	//
   131  	// For server requests the Request Body is always non-nil
   132  	// but will return EOF immediately when no body is present.
   133  	// The Server will close the request body. The ServeHTTP
   134  	// Handler does not need to.
   135  	Body io.ReadCloser
   136  
   137  	// ContentLength records the length of the associated content.
   138  	// The value -1 indicates that the length is unknown.
   139  	// Values >= 0 indicate that the given number of bytes may
   140  	// be read from Body.
   141  	// For client requests, a value of 0 means unknown if Body is not nil.
   142  	ContentLength int64
   143  
   144  	// TransferEncoding lists the transfer encodings from outermost to
   145  	// innermost. An empty list denotes the "identity" encoding.
   146  	// TransferEncoding can usually be ignored; chunked encoding is
   147  	// automatically added and removed as necessary when sending and
   148  	// receiving requests.
   149  	TransferEncoding []string
   150  
   151  	// Close indicates whether to close the connection after
   152  	// replying to this request (for servers) or after sending
   153  	// the request (for clients).
   154  	Close bool
   155  
   156  	// For server requests Host specifies the host on which the
   157  	// URL is sought. Per RFC 2616, this is either the value of
   158  	// the "Host" header or the host name given in the URL itself.
   159  	// It may be of the form "host:port".
   160  	//
   161  	// For client requests Host optionally overrides the Host
   162  	// header to send. If empty, the Request.Write method uses
   163  	// the value of URL.Host.
   164  	Host string
   165  
   166  	// Form contains the parsed form data, including both the URL
   167  	// field's query parameters and the POST or PUT form data.
   168  	// This field is only available after ParseForm is called.
   169  	// The HTTP client ignores Form and uses Body instead.
   170  	Form url.Values
   171  
   172  	// PostForm contains the parsed form data from POST, PATCH,
   173  	// or PUT body parameters.
   174  	//
   175  	// This field is only available after ParseForm is called.
   176  	// The HTTP client ignores PostForm and uses Body instead.
   177  	PostForm url.Values
   178  
   179  	// MultipartForm is the parsed multipart form, including file uploads.
   180  	// This field is only available after ParseMultipartForm is called.
   181  	// The HTTP client ignores MultipartForm and uses Body instead.
   182  	MultipartForm *multipart.Form
   183  
   184  	// Trailer specifies additional headers that are sent after the request
   185  	// body.
   186  	//
   187  	// For server requests the Trailer map initially contains only the
   188  	// trailer keys, with nil values. (The client declares which trailers it
   189  	// will later send.)  While the handler is reading from Body, it must
   190  	// not reference Trailer. After reading from Body returns EOF, Trailer
   191  	// can be read again and will contain non-nil values, if they were sent
   192  	// by the client.
   193  	//
   194  	// For client requests Trailer must be initialized to a map containing
   195  	// the trailer keys to later send. The values may be nil or their final
   196  	// values. The ContentLength must be 0 or -1, to send a chunked request.
   197  	// After the HTTP request is sent the map values can be updated while
   198  	// the request body is read. Once the body returns EOF, the caller must
   199  	// not mutate Trailer.
   200  	//
   201  	// Few HTTP clients, servers, or proxies support HTTP trailers.
   202  	Trailer Header
   203  
   204  	// RemoteAddr allows HTTP servers and other software to record
   205  	// the network address that sent the request, usually for
   206  	// logging. This field is not filled in by ReadRequest and
   207  	// has no defined format. The HTTP server in this package
   208  	// sets RemoteAddr to an "IP:port" address before invoking a
   209  	// handler.
   210  	// This field is ignored by the HTTP client.
   211  	RemoteAddr string
   212  
   213  	// RequestURI is the unmodified Request-URI of the
   214  	// Request-Line (RFC 2616, Section 5.1) as sent by the client
   215  	// to a server. Usually the URL field should be used instead.
   216  	// It is an error to set this field in an HTTP client request.
   217  	RequestURI string
   218  
   219  	// TLS allows HTTP servers and other software to record
   220  	// information about the TLS connection on which the request
   221  	// was received. This field is not filled in by ReadRequest.
   222  	// The HTTP server in this package sets the field for
   223  	// TLS-enabled connections before invoking a handler;
   224  	// otherwise it leaves the field nil.
   225  	// This field is ignored by the HTTP client.
   226  	TLS *tls.ConnectionState
   227  
   228  	// Cancel is an optional channel whose closure indicates that the client
   229  	// request should be regarded as canceled. Not all implementations of
   230  	// RoundTripper may support Cancel.
   231  	//
   232  	// For server requests, this field is not applicable.
   233  	Cancel <-chan struct{}
   234  }
   235  
   236  // ProtoAtLeast reports whether the HTTP protocol used
   237  // in the request is at least major.minor.
   238  func (r *Request) ProtoAtLeast(major, minor int) bool {
   239  	return r.ProtoMajor > major ||
   240  		r.ProtoMajor == major && r.ProtoMinor >= minor
   241  }
   242  
   243  // UserAgent returns the client's User-Agent, if sent in the request.
   244  func (r *Request) UserAgent() string {
   245  	return r.Header.Get("User-Agent")
   246  }
   247  
   248  // Cookies parses and returns the HTTP cookies sent with the request.
   249  func (r *Request) Cookies() []*Cookie {
   250  	return readCookies(r.Header, "")
   251  }
   252  
   253  // ErrNoCookie is returned by Request's Cookie method when a cookie is not found.
   254  var ErrNoCookie = errors.New("http: named cookie not present")
   255  
   256  // Cookie returns the named cookie provided in the request or
   257  // ErrNoCookie if not found.
   258  func (r *Request) Cookie(name string) (*Cookie, error) {
   259  	for _, c := range readCookies(r.Header, name) {
   260  		return c, nil
   261  	}
   262  	return nil, ErrNoCookie
   263  }
   264  
   265  // AddCookie adds a cookie to the request.  Per RFC 6265 section 5.4,
   266  // AddCookie does not attach more than one Cookie header field.  That
   267  // means all cookies, if any, are written into the same line,
   268  // separated by semicolon.
   269  func (r *Request) AddCookie(c *Cookie) {
   270  	s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value))
   271  	if c := r.Header.Get("Cookie"); c != "" {
   272  		r.Header.Set("Cookie", c+"; "+s)
   273  	} else {
   274  		r.Header.Set("Cookie", s)
   275  	}
   276  }
   277  
   278  // Referer returns the referring URL, if sent in the request.
   279  //
   280  // Referer is misspelled as in the request itself, a mistake from the
   281  // earliest days of HTTP.  This value can also be fetched from the
   282  // Header map as Header["Referer"]; the benefit of making it available
   283  // as a method is that the compiler can diagnose programs that use the
   284  // alternate (correct English) spelling req.Referrer() but cannot
   285  // diagnose programs that use Header["Referrer"].
   286  func (r *Request) Referer() string {
   287  	return r.Header.Get("Referer")
   288  }
   289  
   290  // multipartByReader is a sentinel value.
   291  // Its presence in Request.MultipartForm indicates that parsing of the request
   292  // body has been handed off to a MultipartReader instead of ParseMultipartFrom.
   293  var multipartByReader = &multipart.Form{
   294  	Value: make(map[string][]string),
   295  	File:  make(map[string][]*multipart.FileHeader),
   296  }
   297  
   298  // MultipartReader returns a MIME multipart reader if this is a
   299  // multipart/form-data POST request, else returns nil and an error.
   300  // Use this function instead of ParseMultipartForm to
   301  // process the request body as a stream.
   302  func (r *Request) MultipartReader() (*multipart.Reader, error) {
   303  	if r.MultipartForm == multipartByReader {
   304  		return nil, errors.New("http: MultipartReader called twice")
   305  	}
   306  	if r.MultipartForm != nil {
   307  		return nil, errors.New("http: multipart handled by ParseMultipartForm")
   308  	}
   309  	r.MultipartForm = multipartByReader
   310  	return r.multipartReader()
   311  }
   312  
   313  func (r *Request) multipartReader() (*multipart.Reader, error) {
   314  	v := r.Header.Get("Content-Type")
   315  	if v == "" {
   316  		return nil, ErrNotMultipart
   317  	}
   318  	d, params, err := mime.ParseMediaType(v)
   319  	if err != nil || d != "multipart/form-data" {
   320  		return nil, ErrNotMultipart
   321  	}
   322  	boundary, ok := params["boundary"]
   323  	if !ok {
   324  		return nil, ErrMissingBoundary
   325  	}
   326  	return multipart.NewReader(r.Body, boundary), nil
   327  }
   328  
   329  // Return value if nonempty, def otherwise.
   330  func valueOrDefault(value, def string) string {
   331  	if value != "" {
   332  		return value
   333  	}
   334  	return def
   335  }
   336  
   337  // NOTE: This is not intended to reflect the actual Go version being used.
   338  // It was changed at the time of Go 1.1 release because the former User-Agent
   339  // had ended up on a blacklist for some intrusion detection systems.
   340  // See https://codereview.appspot.com/7532043.
   341  const defaultUserAgent = "Go-http-client/1.1"
   342  
   343  // Write writes an HTTP/1.1 request, which is the header and body, in wire format.
   344  // This method consults the following fields of the request:
   345  //	Host
   346  //	URL
   347  //	Method (defaults to "GET")
   348  //	Header
   349  //	ContentLength
   350  //	TransferEncoding
   351  //	Body
   352  //
   353  // If Body is present, Content-Length is <= 0 and TransferEncoding
   354  // hasn't been set to "identity", Write adds "Transfer-Encoding:
   355  // chunked" to the header. Body is closed after it is sent.
   356  func (r *Request) Write(w io.Writer) error {
   357  	return r.write(w, false, nil, nil)
   358  }
   359  
   360  // WriteProxy is like Write but writes the request in the form
   361  // expected by an HTTP proxy.  In particular, WriteProxy writes the
   362  // initial Request-URI line of the request with an absolute URI, per
   363  // section 5.1.2 of RFC 2616, including the scheme and host.
   364  // In either case, WriteProxy also writes a Host header, using
   365  // either r.Host or r.URL.Host.
   366  func (r *Request) WriteProxy(w io.Writer) error {
   367  	return r.write(w, true, nil, nil)
   368  }
   369  
   370  // errMissingHost is returned by Write when there is no Host or URL present in
   371  // the Request.
   372  var errMissingHost = errors.New("http: Request.Write on Request with no Host or URL set")
   373  
   374  // extraHeaders may be nil
   375  // waitForContinue may be nil
   376  func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitForContinue func() bool) error {
   377  	// Find the target host. Prefer the Host: header, but if that
   378  	// is not given, use the host from the request URL.
   379  	//
   380  	// Clean the host, in case it arrives with unexpected stuff in it.
   381  	host := cleanHost(req.Host)
   382  	if host == "" {
   383  		if req.URL == nil {
   384  			return errMissingHost
   385  		}
   386  		host = cleanHost(req.URL.Host)
   387  	}
   388  
   389  	// According to RFC 6874, an HTTP client, proxy, or other
   390  	// intermediary must remove any IPv6 zone identifier attached
   391  	// to an outgoing URI.
   392  	host = removeZone(host)
   393  
   394  	ruri := req.URL.RequestURI()
   395  	if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" {
   396  		ruri = req.URL.Scheme + "://" + host + ruri
   397  	} else if req.Method == "CONNECT" && req.URL.Path == "" {
   398  		// CONNECT requests normally give just the host and port, not a full URL.
   399  		ruri = host
   400  	}
   401  	// TODO(bradfitz): escape at least newlines in ruri?
   402  
   403  	// Wrap the writer in a bufio Writer if it's not already buffered.
   404  	// Don't always call NewWriter, as that forces a bytes.Buffer
   405  	// and other small bufio Writers to have a minimum 4k buffer
   406  	// size.
   407  	var bw *bufio.Writer
   408  	if _, ok := w.(io.ByteWriter); !ok {
   409  		bw = bufio.NewWriter(w)
   410  		w = bw
   411  	}
   412  
   413  	_, err := fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri)
   414  	if err != nil {
   415  		return err
   416  	}
   417  
   418  	// Header lines
   419  	_, err = fmt.Fprintf(w, "Host: %s\r\n", host)
   420  	if err != nil {
   421  		return err
   422  	}
   423  
   424  	// Use the defaultUserAgent unless the Header contains one, which
   425  	// may be blank to not send the header.
   426  	userAgent := defaultUserAgent
   427  	if req.Header != nil {
   428  		if ua := req.Header["User-Agent"]; len(ua) > 0 {
   429  			userAgent = ua[0]
   430  		}
   431  	}
   432  	if userAgent != "" {
   433  		_, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent)
   434  		if err != nil {
   435  			return err
   436  		}
   437  	}
   438  
   439  	// Process Body,ContentLength,Close,Trailer
   440  	tw, err := newTransferWriter(req)
   441  	if err != nil {
   442  		return err
   443  	}
   444  	err = tw.WriteHeader(w)
   445  	if err != nil {
   446  		return err
   447  	}
   448  
   449  	err = req.Header.WriteSubset(w, reqWriteExcludeHeader)
   450  	if err != nil {
   451  		return err
   452  	}
   453  
   454  	if extraHeaders != nil {
   455  		err = extraHeaders.Write(w)
   456  		if err != nil {
   457  			return err
   458  		}
   459  	}
   460  
   461  	_, err = io.WriteString(w, "\r\n")
   462  	if err != nil {
   463  		return err
   464  	}
   465  
   466  	// Flush and wait for 100-continue if expected.
   467  	if waitForContinue != nil {
   468  		if bw, ok := w.(*bufio.Writer); ok {
   469  			err = bw.Flush()
   470  			if err != nil {
   471  				return err
   472  			}
   473  		}
   474  
   475  		if !waitForContinue() {
   476  			req.closeBody()
   477  			return nil
   478  		}
   479  	}
   480  
   481  	// Write body and trailer
   482  	err = tw.WriteBody(w)
   483  	if err != nil {
   484  		return err
   485  	}
   486  
   487  	if bw != nil {
   488  		return bw.Flush()
   489  	}
   490  	return nil
   491  }
   492  
   493  // cleanHost strips anything after '/' or ' '.
   494  // Ideally we'd clean the Host header according to the spec:
   495  //   https://tools.ietf.org/html/rfc7230#section-5.4 (Host = uri-host [ ":" port ]")
   496  //   https://tools.ietf.org/html/rfc7230#section-2.7 (uri-host -> rfc3986's host)
   497  //   https://tools.ietf.org/html/rfc3986#section-3.2.2 (definition of host)
   498  // But practically, what we are trying to avoid is the situation in
   499  // issue 11206, where a malformed Host header used in the proxy context
   500  // would create a bad request. So it is enough to just truncate at the
   501  // first offending character.
   502  func cleanHost(in string) string {
   503  	if i := strings.IndexAny(in, " /"); i != -1 {
   504  		return in[:i]
   505  	}
   506  	return in
   507  }
   508  
   509  // removeZone removes IPv6 zone identifer from host.
   510  // E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
   511  func removeZone(host string) string {
   512  	if !strings.HasPrefix(host, "[") {
   513  		return host
   514  	}
   515  	i := strings.LastIndex(host, "]")
   516  	if i < 0 {
   517  		return host
   518  	}
   519  	j := strings.LastIndex(host[:i], "%")
   520  	if j < 0 {
   521  		return host
   522  	}
   523  	return host[:j] + host[i:]
   524  }
   525  
   526  // ParseHTTPVersion parses a HTTP version string.
   527  // "HTTP/1.0" returns (1, 0, true).
   528  func ParseHTTPVersion(vers string) (major, minor int, ok bool) {
   529  	const Big = 1000000 // arbitrary upper bound
   530  	switch vers {
   531  	case "HTTP/1.1":
   532  		return 1, 1, true
   533  	case "HTTP/1.0":
   534  		return 1, 0, true
   535  	}
   536  	if !strings.HasPrefix(vers, "HTTP/") {
   537  		return 0, 0, false
   538  	}
   539  	dot := strings.Index(vers, ".")
   540  	if dot < 0 {
   541  		return 0, 0, false
   542  	}
   543  	major, err := strconv.Atoi(vers[5:dot])
   544  	if err != nil || major < 0 || major > Big {
   545  		return 0, 0, false
   546  	}
   547  	minor, err = strconv.Atoi(vers[dot+1:])
   548  	if err != nil || minor < 0 || minor > Big {
   549  		return 0, 0, false
   550  	}
   551  	return major, minor, true
   552  }
   553  
   554  func validMethod(method string) bool {
   555  	/*
   556  	     Method         = "OPTIONS"                ; Section 9.2
   557  	                    | "GET"                    ; Section 9.3
   558  	                    | "HEAD"                   ; Section 9.4
   559  	                    | "POST"                   ; Section 9.5
   560  	                    | "PUT"                    ; Section 9.6
   561  	                    | "DELETE"                 ; Section 9.7
   562  	                    | "TRACE"                  ; Section 9.8
   563  	                    | "CONNECT"                ; Section 9.9
   564  	                    | extension-method
   565  	   extension-method = token
   566  	     token          = 1*<any CHAR except CTLs or separators>
   567  	*/
   568  	return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
   569  }
   570  
   571  // NewRequest returns a new Request given a method, URL, and optional body.
   572  //
   573  // If the provided body is also an io.Closer, the returned
   574  // Request.Body is set to body and will be closed by the Client
   575  // methods Do, Post, and PostForm, and Transport.RoundTrip.
   576  //
   577  // NewRequest returns a Request suitable for use with Client.Do or
   578  // Transport.RoundTrip.
   579  // To create a request for use with testing a Server Handler use either
   580  // ReadRequest or manually update the Request fields. See the Request
   581  // type's documentation for the difference between inbound and outbound
   582  // request fields.
   583  func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
   584  	if method == "" {
   585  		// We document that "" means "GET" for Request.Method, and people have
   586  		// relied on that from NewRequest, so keep that working.
   587  		// We still enforce validMethod for non-empty methods.
   588  		method = "GET"
   589  	}
   590  	if !validMethod(method) {
   591  		return nil, fmt.Errorf("net/http: invalid method %q", method)
   592  	}
   593  	u, err := url.Parse(urlStr)
   594  	if err != nil {
   595  		return nil, err
   596  	}
   597  	rc, ok := body.(io.ReadCloser)
   598  	if !ok && body != nil {
   599  		rc = ioutil.NopCloser(body)
   600  	}
   601  	req := &Request{
   602  		Method:     method,
   603  		URL:        u,
   604  		Proto:      "HTTP/1.1",
   605  		ProtoMajor: 1,
   606  		ProtoMinor: 1,
   607  		Header:     make(Header),
   608  		Body:       rc,
   609  		Host:       u.Host,
   610  	}
   611  	if body != nil {
   612  		switch v := body.(type) {
   613  		case *bytes.Buffer:
   614  			req.ContentLength = int64(v.Len())
   615  		case *bytes.Reader:
   616  			req.ContentLength = int64(v.Len())
   617  		case *strings.Reader:
   618  			req.ContentLength = int64(v.Len())
   619  		}
   620  	}
   621  
   622  	return req, nil
   623  }
   624  
   625  // BasicAuth returns the username and password provided in the request's
   626  // Authorization header, if the request uses HTTP Basic Authentication.
   627  // See RFC 2617, Section 2.
   628  func (r *Request) BasicAuth() (username, password string, ok bool) {
   629  	auth := r.Header.Get("Authorization")
   630  	if auth == "" {
   631  		return
   632  	}
   633  	return parseBasicAuth(auth)
   634  }
   635  
   636  // parseBasicAuth parses an HTTP Basic Authentication string.
   637  // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
   638  func parseBasicAuth(auth string) (username, password string, ok bool) {
   639  	const prefix = "Basic "
   640  	if !strings.HasPrefix(auth, prefix) {
   641  		return
   642  	}
   643  	c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
   644  	if err != nil {
   645  		return
   646  	}
   647  	cs := string(c)
   648  	s := strings.IndexByte(cs, ':')
   649  	if s < 0 {
   650  		return
   651  	}
   652  	return cs[:s], cs[s+1:], true
   653  }
   654  
   655  // SetBasicAuth sets the request's Authorization header to use HTTP
   656  // Basic Authentication with the provided username and password.
   657  //
   658  // With HTTP Basic Authentication the provided username and password
   659  // are not encrypted.
   660  func (r *Request) SetBasicAuth(username, password string) {
   661  	r.Header.Set("Authorization", "Basic "+basicAuth(username, password))
   662  }
   663  
   664  // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
   665  func parseRequestLine(line string) (method, requestURI, proto string, ok bool) {
   666  	s1 := strings.Index(line, " ")
   667  	s2 := strings.Index(line[s1+1:], " ")
   668  	if s1 < 0 || s2 < 0 {
   669  		return
   670  	}
   671  	s2 += s1 + 1
   672  	return line[:s1], line[s1+1 : s2], line[s2+1:], true
   673  }
   674  
   675  var textprotoReaderPool sync.Pool
   676  
   677  func newTextprotoReader(br *bufio.Reader) *textproto.Reader {
   678  	if v := textprotoReaderPool.Get(); v != nil {
   679  		tr := v.(*textproto.Reader)
   680  		tr.R = br
   681  		return tr
   682  	}
   683  	return textproto.NewReader(br)
   684  }
   685  
   686  func putTextprotoReader(r *textproto.Reader) {
   687  	r.R = nil
   688  	textprotoReaderPool.Put(r)
   689  }
   690  
   691  // ReadRequest reads and parses an incoming request from b.
   692  func ReadRequest(b *bufio.Reader) (req *Request, err error) { return readRequest(b, true) }
   693  
   694  func readRequest(b *bufio.Reader, deleteHostHeader bool) (req *Request, err error) {
   695  	tp := newTextprotoReader(b)
   696  	req = new(Request)
   697  
   698  	// First line: GET /index.html HTTP/1.0
   699  	var s string
   700  	if s, err = tp.ReadLine(); err != nil {
   701  		return nil, err
   702  	}
   703  	defer func() {
   704  		putTextprotoReader(tp)
   705  		if err == io.EOF {
   706  			err = io.ErrUnexpectedEOF
   707  		}
   708  	}()
   709  
   710  	var ok bool
   711  	req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s)
   712  	if !ok {
   713  		return nil, &badStringError{"malformed HTTP request", s}
   714  	}
   715  	rawurl := req.RequestURI
   716  	if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok {
   717  		return nil, &badStringError{"malformed HTTP version", req.Proto}
   718  	}
   719  
   720  	// CONNECT requests are used two different ways, and neither uses a full URL:
   721  	// The standard use is to tunnel HTTPS through an HTTP proxy.
   722  	// It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is
   723  	// just the authority section of a URL. This information should go in req.URL.Host.
   724  	//
   725  	// The net/rpc package also uses CONNECT, but there the parameter is a path
   726  	// that starts with a slash. It can be parsed with the regular URL parser,
   727  	// and the path will end up in req.URL.Path, where it needs to be in order for
   728  	// RPC to work.
   729  	justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/")
   730  	if justAuthority {
   731  		rawurl = "http://" + rawurl
   732  	}
   733  
   734  	if req.URL, err = url.ParseRequestURI(rawurl); err != nil {
   735  		return nil, err
   736  	}
   737  
   738  	if justAuthority {
   739  		// Strip the bogus "http://" back off.
   740  		req.URL.Scheme = ""
   741  	}
   742  
   743  	// Subsequent lines: Key: value.
   744  	mimeHeader, err := tp.ReadMIMEHeader()
   745  	if err != nil {
   746  		return nil, err
   747  	}
   748  	req.Header = Header(mimeHeader)
   749  
   750  	// RFC2616: Must treat
   751  	//	GET /index.html HTTP/1.1
   752  	//	Host: www.google.com
   753  	// and
   754  	//	GET http://www.google.com/index.html HTTP/1.1
   755  	//	Host: doesntmatter
   756  	// the same.  In the second case, any Host line is ignored.
   757  	req.Host = req.URL.Host
   758  	if req.Host == "" {
   759  		req.Host = req.Header.get("Host")
   760  	}
   761  	if deleteHostHeader {
   762  		delete(req.Header, "Host")
   763  	}
   764  
   765  	fixPragmaCacheControl(req.Header)
   766  
   767  	req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header, false)
   768  
   769  	err = readTransfer(req, b)
   770  	if err != nil {
   771  		return nil, err
   772  	}
   773  
   774  	return req, nil
   775  }
   776  
   777  // MaxBytesReader is similar to io.LimitReader but is intended for
   778  // limiting the size of incoming request bodies. In contrast to
   779  // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a
   780  // non-EOF error for a Read beyond the limit, and closes the
   781  // underlying reader when its Close method is called.
   782  //
   783  // MaxBytesReader prevents clients from accidentally or maliciously
   784  // sending a large request and wasting server resources.
   785  func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser {
   786  	return &maxBytesReader{w: w, r: r, n: n}
   787  }
   788  
   789  type maxBytesReader struct {
   790  	w       ResponseWriter
   791  	r       io.ReadCloser // underlying reader
   792  	n       int64         // max bytes remaining
   793  	stopped bool
   794  	sawEOF  bool
   795  }
   796  
   797  func (l *maxBytesReader) tooLarge() (n int, err error) {
   798  	if !l.stopped {
   799  		l.stopped = true
   800  		if res, ok := l.w.(*response); ok {
   801  			res.requestTooLarge()
   802  		}
   803  	}
   804  	return 0, errors.New("http: request body too large")
   805  }
   806  
   807  func (l *maxBytesReader) Read(p []byte) (n int, err error) {
   808  	toRead := l.n
   809  	if l.n == 0 {
   810  		if l.sawEOF {
   811  			return l.tooLarge()
   812  		}
   813  		// The underlying io.Reader may not return (0, io.EOF)
   814  		// at EOF if the requested size is 0, so read 1 byte
   815  		// instead. The io.Reader docs are a bit ambiguous
   816  		// about the return value of Read when 0 bytes are
   817  		// requested, and {bytes,strings}.Reader gets it wrong
   818  		// too (it returns (0, nil) even at EOF).
   819  		toRead = 1
   820  	}
   821  	if int64(len(p)) > toRead {
   822  		p = p[:toRead]
   823  	}
   824  	n, err = l.r.Read(p)
   825  	if err == io.EOF {
   826  		l.sawEOF = true
   827  	}
   828  	if l.n == 0 {
   829  		// If we had zero bytes to read remaining (but hadn't seen EOF)
   830  		// and we get a byte here, that means we went over our limit.
   831  		if n > 0 {
   832  			return l.tooLarge()
   833  		}
   834  		return 0, err
   835  	}
   836  	l.n -= int64(n)
   837  	if l.n < 0 {
   838  		l.n = 0
   839  	}
   840  	return
   841  }
   842  
   843  func (l *maxBytesReader) Close() error {
   844  	return l.r.Close()
   845  }
   846  
   847  func copyValues(dst, src url.Values) {
   848  	for k, vs := range src {
   849  		for _, value := range vs {
   850  			dst.Add(k, value)
   851  		}
   852  	}
   853  }
   854  
   855  func parsePostForm(r *Request) (vs url.Values, err error) {
   856  	if r.Body == nil {
   857  		err = errors.New("missing form body")
   858  		return
   859  	}
   860  	ct := r.Header.Get("Content-Type")
   861  	// RFC 2616, section 7.2.1 - empty type
   862  	//   SHOULD be treated as application/octet-stream
   863  	if ct == "" {
   864  		ct = "application/octet-stream"
   865  	}
   866  	ct, _, err = mime.ParseMediaType(ct)
   867  	switch {
   868  	case ct == "application/x-www-form-urlencoded":
   869  		var reader io.Reader = r.Body
   870  		maxFormSize := int64(1<<63 - 1)
   871  		if _, ok := r.Body.(*maxBytesReader); !ok {
   872  			maxFormSize = int64(10 << 20) // 10 MB is a lot of text.
   873  			reader = io.LimitReader(r.Body, maxFormSize+1)
   874  		}
   875  		b, e := ioutil.ReadAll(reader)
   876  		if e != nil {
   877  			if err == nil {
   878  				err = e
   879  			}
   880  			break
   881  		}
   882  		if int64(len(b)) > maxFormSize {
   883  			err = errors.New("http: POST too large")
   884  			return
   885  		}
   886  		vs, e = url.ParseQuery(string(b))
   887  		if err == nil {
   888  			err = e
   889  		}
   890  	case ct == "multipart/form-data":
   891  		// handled by ParseMultipartForm (which is calling us, or should be)
   892  		// TODO(bradfitz): there are too many possible
   893  		// orders to call too many functions here.
   894  		// Clean this up and write more tests.
   895  		// request_test.go contains the start of this,
   896  		// in TestParseMultipartFormOrder and others.
   897  	}
   898  	return
   899  }
   900  
   901  // ParseForm parses the raw query from the URL and updates r.Form.
   902  //
   903  // For POST or PUT requests, it also parses the request body as a form and
   904  // put the results into both r.PostForm and r.Form.
   905  // POST and PUT body parameters take precedence over URL query string values
   906  // in r.Form.
   907  //
   908  // If the request Body's size has not already been limited by MaxBytesReader,
   909  // the size is capped at 10MB.
   910  //
   911  // ParseMultipartForm calls ParseForm automatically.
   912  // It is idempotent.
   913  func (r *Request) ParseForm() error {
   914  	var err error
   915  	if r.PostForm == nil {
   916  		if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
   917  			r.PostForm, err = parsePostForm(r)
   918  		}
   919  		if r.PostForm == nil {
   920  			r.PostForm = make(url.Values)
   921  		}
   922  	}
   923  	if r.Form == nil {
   924  		if len(r.PostForm) > 0 {
   925  			r.Form = make(url.Values)
   926  			copyValues(r.Form, r.PostForm)
   927  		}
   928  		var newValues url.Values
   929  		if r.URL != nil {
   930  			var e error
   931  			newValues, e = url.ParseQuery(r.URL.RawQuery)
   932  			if err == nil {
   933  				err = e
   934  			}
   935  		}
   936  		if newValues == nil {
   937  			newValues = make(url.Values)
   938  		}
   939  		if r.Form == nil {
   940  			r.Form = newValues
   941  		} else {
   942  			copyValues(r.Form, newValues)
   943  		}
   944  	}
   945  	return err
   946  }
   947  
   948  // ParseMultipartForm parses a request body as multipart/form-data.
   949  // The whole request body is parsed and up to a total of maxMemory bytes of
   950  // its file parts are stored in memory, with the remainder stored on
   951  // disk in temporary files.
   952  // ParseMultipartForm calls ParseForm if necessary.
   953  // After one call to ParseMultipartForm, subsequent calls have no effect.
   954  func (r *Request) ParseMultipartForm(maxMemory int64) error {
   955  	if r.MultipartForm == multipartByReader {
   956  		return errors.New("http: multipart handled by MultipartReader")
   957  	}
   958  	if r.Form == nil {
   959  		err := r.ParseForm()
   960  		if err != nil {
   961  			return err
   962  		}
   963  	}
   964  	if r.MultipartForm != nil {
   965  		return nil
   966  	}
   967  
   968  	mr, err := r.multipartReader()
   969  	if err != nil {
   970  		return err
   971  	}
   972  
   973  	f, err := mr.ReadForm(maxMemory)
   974  	if err != nil {
   975  		return err
   976  	}
   977  	for k, v := range f.Value {
   978  		r.Form[k] = append(r.Form[k], v...)
   979  	}
   980  	r.MultipartForm = f
   981  
   982  	return nil
   983  }
   984  
   985  // FormValue returns the first value for the named component of the query.
   986  // POST and PUT body parameters take precedence over URL query string values.
   987  // FormValue calls ParseMultipartForm and ParseForm if necessary and ignores
   988  // any errors returned by these functions.
   989  // If key is not present, FormValue returns the empty string.
   990  // To access multiple values of the same key, call ParseForm and
   991  // then inspect Request.Form directly.
   992  func (r *Request) FormValue(key string) string {
   993  	if r.Form == nil {
   994  		r.ParseMultipartForm(defaultMaxMemory)
   995  	}
   996  	if vs := r.Form[key]; len(vs) > 0 {
   997  		return vs[0]
   998  	}
   999  	return ""
  1000  }
  1001  
  1002  // PostFormValue returns the first value for the named component of the POST
  1003  // or PUT request body. URL query parameters are ignored.
  1004  // PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores
  1005  // any errors returned by these functions.
  1006  // If key is not present, PostFormValue returns the empty string.
  1007  func (r *Request) PostFormValue(key string) string {
  1008  	if r.PostForm == nil {
  1009  		r.ParseMultipartForm(defaultMaxMemory)
  1010  	}
  1011  	if vs := r.PostForm[key]; len(vs) > 0 {
  1012  		return vs[0]
  1013  	}
  1014  	return ""
  1015  }
  1016  
  1017  // FormFile returns the first file for the provided form key.
  1018  // FormFile calls ParseMultipartForm and ParseForm if necessary.
  1019  func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
  1020  	if r.MultipartForm == multipartByReader {
  1021  		return nil, nil, errors.New("http: multipart handled by MultipartReader")
  1022  	}
  1023  	if r.MultipartForm == nil {
  1024  		err := r.ParseMultipartForm(defaultMaxMemory)
  1025  		if err != nil {
  1026  			return nil, nil, err
  1027  		}
  1028  	}
  1029  	if r.MultipartForm != nil && r.MultipartForm.File != nil {
  1030  		if fhs := r.MultipartForm.File[key]; len(fhs) > 0 {
  1031  			f, err := fhs[0].Open()
  1032  			return f, fhs[0], err
  1033  		}
  1034  	}
  1035  	return nil, nil, ErrMissingFile
  1036  }
  1037  
  1038  func (r *Request) expectsContinue() bool {
  1039  	return hasToken(r.Header.get("Expect"), "100-continue")
  1040  }
  1041  
  1042  func (r *Request) wantsHttp10KeepAlive() bool {
  1043  	if r.ProtoMajor != 1 || r.ProtoMinor != 0 {
  1044  		return false
  1045  	}
  1046  	return hasToken(r.Header.get("Connection"), "keep-alive")
  1047  }
  1048  
  1049  func (r *Request) wantsClose() bool {
  1050  	return hasToken(r.Header.get("Connection"), "close")
  1051  }
  1052  
  1053  func (r *Request) closeBody() {
  1054  	if r.Body != nil {
  1055  		r.Body.Close()
  1056  	}
  1057  }
  1058  
  1059  func (r *Request) isReplayable() bool {
  1060  	if r.Body == nil {
  1061  		switch valueOrDefault(r.Method, "GET") {
  1062  		case "GET", "HEAD", "OPTIONS", "TRACE":
  1063  			return true
  1064  		}
  1065  	}
  1066  	return false
  1067  }
  1068  
  1069  func validHostHeader(h string) bool {
  1070  	// The latests spec is actually this:
  1071  	//
  1072  	// http://tools.ietf.org/html/rfc7230#section-5.4
  1073  	//     Host = uri-host [ ":" port ]
  1074  	//
  1075  	// Where uri-host is:
  1076  	//     http://tools.ietf.org/html/rfc3986#section-3.2.2
  1077  	//
  1078  	// But we're going to be much more lenient for now and just
  1079  	// search for any byte that's not a valid byte in any of those
  1080  	// expressions.
  1081  	for i := 0; i < len(h); i++ {
  1082  		if !validHostByte[h[i]] {
  1083  			return false
  1084  		}
  1085  	}
  1086  	return true
  1087  }
  1088  
  1089  // See the validHostHeader comment.
  1090  var validHostByte = [256]bool{
  1091  	'0': true, '1': true, '2': true, '3': true, '4': true, '5': true, '6': true, '7': true,
  1092  	'8': true, '9': true,
  1093  
  1094  	'a': true, 'b': true, 'c': true, 'd': true, 'e': true, 'f': true, 'g': true, 'h': true,
  1095  	'i': true, 'j': true, 'k': true, 'l': true, 'm': true, 'n': true, 'o': true, 'p': true,
  1096  	'q': true, 'r': true, 's': true, 't': true, 'u': true, 'v': true, 'w': true, 'x': true,
  1097  	'y': true, 'z': true,
  1098  
  1099  	'A': true, 'B': true, 'C': true, 'D': true, 'E': true, 'F': true, 'G': true, 'H': true,
  1100  	'I': true, 'J': true, 'K': true, 'L': true, 'M': true, 'N': true, 'O': true, 'P': true,
  1101  	'Q': true, 'R': true, 'S': true, 'T': true, 'U': true, 'V': true, 'W': true, 'X': true,
  1102  	'Y': true, 'Z': true,
  1103  
  1104  	'!':  true, // sub-delims
  1105  	'$':  true, // sub-delims
  1106  	'%':  true, // pct-encoded (and used in IPv6 zones)
  1107  	'&':  true, // sub-delims
  1108  	'(':  true, // sub-delims
  1109  	')':  true, // sub-delims
  1110  	'*':  true, // sub-delims
  1111  	'+':  true, // sub-delims
  1112  	',':  true, // sub-delims
  1113  	'-':  true, // unreserved
  1114  	'.':  true, // unreserved
  1115  	':':  true, // IPv6address + Host expression's optional port
  1116  	';':  true, // sub-delims
  1117  	'=':  true, // sub-delims
  1118  	'[':  true,
  1119  	'\'': true, // sub-delims
  1120  	']':  true,
  1121  	'_':  true, // unreserved
  1122  	'~':  true, // unreserved
  1123  }
  1124  
  1125  func validHeaderName(v string) bool {
  1126  	if len(v) == 0 {
  1127  		return false
  1128  	}
  1129  	return strings.IndexFunc(v, isNotToken) == -1
  1130  }
  1131  
  1132  func validHeaderValue(v string) bool {
  1133  	for i := 0; i < len(v); i++ {
  1134  		b := v[i]
  1135  		if b == '\t' {
  1136  			continue
  1137  		}
  1138  		if ' ' <= b && b <= '~' {
  1139  			continue
  1140  		}
  1141  		return false
  1142  	}
  1143  	return true
  1144  }