github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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 "errors" 14 "fmt" 15 "io" 16 "io/ioutil" 17 "mime" 18 "mime/multipart" 19 "net/textproto" 20 "net/url" 21 "strconv" 22 "strings" 23 ) 24 25 const ( 26 maxValueLength = 4096 27 maxHeaderLines = 1024 28 chunkSize = 4 << 10 // 4 KB chunks 29 defaultMaxMemory = 32 << 20 // 32 MB 30 ) 31 32 // ErrMissingFile is returned by FormFile when the provided file field name 33 // is either not present in the request or not a file field. 34 var ErrMissingFile = errors.New("http: no such file") 35 36 // HTTP request parsing errors. 37 type ProtocolError struct { 38 ErrorString string 39 } 40 41 func (err *ProtocolError) Error() string { return err.ErrorString } 42 43 var ( 44 ErrHeaderTooLong = &ProtocolError{"header too long"} 45 ErrShortBody = &ProtocolError{"entity body too short"} 46 ErrNotSupported = &ProtocolError{"feature not supported"} 47 ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"} 48 ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"} 49 ErrNotMultipart = &ProtocolError{"request Content-Type isn't multipart/form-data"} 50 ErrMissingBoundary = &ProtocolError{"no multipart boundary param in Content-Type"} 51 ) 52 53 type badStringError struct { 54 what string 55 str string 56 } 57 58 func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 59 60 // Headers that Request.Write handles itself and should be skipped. 61 var reqWriteExcludeHeader = map[string]bool{ 62 "Host": true, // not in Header map anyway 63 "User-Agent": true, 64 "Content-Length": true, 65 "Transfer-Encoding": true, 66 "Trailer": true, 67 } 68 69 // A Request represents an HTTP request received by a server 70 // or to be sent by a client. 71 type Request struct { 72 Method string // GET, POST, PUT, etc. 73 74 // URL is created from the URI supplied on the Request-Line 75 // as stored in RequestURI. 76 // 77 // For most requests, fields other than Path and RawQuery 78 // will be empty. (See RFC 2616, Section 5.1.2) 79 URL *url.URL 80 81 // The protocol version for incoming requests. 82 // Outgoing requests always use HTTP/1.1. 83 Proto string // "HTTP/1.0" 84 ProtoMajor int // 1 85 ProtoMinor int // 0 86 87 // A header maps request lines to their values. 88 // If the header says 89 // 90 // accept-encoding: gzip, deflate 91 // Accept-Language: en-us 92 // Connection: keep-alive 93 // 94 // then 95 // 96 // Header = map[string][]string{ 97 // "Accept-Encoding": {"gzip, deflate"}, 98 // "Accept-Language": {"en-us"}, 99 // "Connection": {"keep-alive"}, 100 // } 101 // 102 // HTTP defines that header names are case-insensitive. 103 // The request parser implements this by canonicalizing the 104 // name, making the first character and any characters 105 // following a hyphen uppercase and the rest lowercase. 106 Header Header 107 108 // Body is the request's body. 109 // 110 // For client requests, a nil body means the request has no 111 // body, such as a GET request. The HTTP Client's Transport 112 // is responsible for calling the Close method. 113 // 114 // For server requests, the Request Body is always non-nil 115 // but will return EOF immediately when no body is present. 116 // The Server will close the request body. The ServeHTTP 117 // Handler does not need to. 118 Body io.ReadCloser 119 120 // ContentLength records the length of the associated content. 121 // The value -1 indicates that the length is unknown. 122 // Values >= 0 indicate that the given number of bytes may 123 // be read from Body. 124 // For outgoing requests, a value of 0 means unknown if Body is not nil. 125 ContentLength int64 126 127 // TransferEncoding lists the transfer encodings from outermost to 128 // innermost. An empty list denotes the "identity" encoding. 129 // TransferEncoding can usually be ignored; chunked encoding is 130 // automatically added and removed as necessary when sending and 131 // receiving requests. 132 TransferEncoding []string 133 134 // Close indicates whether to close the connection after 135 // replying to this request. 136 Close bool 137 138 // The host on which the URL is sought. 139 // Per RFC 2616, this is either the value of the Host: header 140 // or the host name given in the URL itself. 141 // It may be of the form "host:port". 142 Host string 143 144 // Form contains the parsed form data, including both the URL 145 // field's query parameters and the POST or PUT form data. 146 // This field is only available after ParseForm is called. 147 // The HTTP client ignores Form and uses Body instead. 148 Form url.Values 149 150 // PostForm contains the parsed form data from POST or PUT 151 // body parameters. 152 // This field is only available after ParseForm is called. 153 // The HTTP client ignores PostForm and uses Body instead. 154 PostForm url.Values 155 156 // MultipartForm is the parsed multipart form, including file uploads. 157 // This field is only available after ParseMultipartForm is called. 158 // The HTTP client ignores MultipartForm and uses Body instead. 159 MultipartForm *multipart.Form 160 161 // Trailer maps trailer keys to values. Like for Header, if the 162 // response has multiple trailer lines with the same key, they will be 163 // concatenated, delimited by commas. 164 // For server requests, Trailer is only populated after Body has been 165 // closed or fully consumed. 166 // Trailer support is only partially complete. 167 Trailer Header 168 169 // RemoteAddr allows HTTP servers and other software to record 170 // the network address that sent the request, usually for 171 // logging. This field is not filled in by ReadRequest and 172 // has no defined format. The HTTP server in this package 173 // sets RemoteAddr to an "IP:port" address before invoking a 174 // handler. 175 // This field is ignored by the HTTP client. 176 RemoteAddr string 177 178 // RequestURI is the unmodified Request-URI of the 179 // Request-Line (RFC 2616, Section 5.1) as sent by the client 180 // to a server. Usually the URL field should be used instead. 181 // It is an error to set this field in an HTTP client request. 182 RequestURI string 183 184 // TLS allows HTTP servers and other software to record 185 // information about the TLS connection on which the request 186 // was received. This field is not filled in by ReadRequest. 187 // The HTTP server in this package sets the field for 188 // TLS-enabled connections before invoking a handler; 189 // otherwise it leaves the field nil. 190 // This field is ignored by the HTTP client. 191 TLS *tls.ConnectionState 192 } 193 194 // ProtoAtLeast reports whether the HTTP protocol used 195 // in the request is at least major.minor. 196 func (r *Request) ProtoAtLeast(major, minor int) bool { 197 return r.ProtoMajor > major || 198 r.ProtoMajor == major && r.ProtoMinor >= minor 199 } 200 201 // UserAgent returns the client's User-Agent, if sent in the request. 202 func (r *Request) UserAgent() string { 203 return r.Header.Get("User-Agent") 204 } 205 206 // Cookies parses and returns the HTTP cookies sent with the request. 207 func (r *Request) Cookies() []*Cookie { 208 return readCookies(r.Header, "") 209 } 210 211 var ErrNoCookie = errors.New("http: named cookie not present") 212 213 // Cookie returns the named cookie provided in the request or 214 // ErrNoCookie if not found. 215 func (r *Request) Cookie(name string) (*Cookie, error) { 216 for _, c := range readCookies(r.Header, name) { 217 return c, nil 218 } 219 return nil, ErrNoCookie 220 } 221 222 // AddCookie adds a cookie to the request. Per RFC 6265 section 5.4, 223 // AddCookie does not attach more than one Cookie header field. That 224 // means all cookies, if any, are written into the same line, 225 // separated by semicolon. 226 func (r *Request) AddCookie(c *Cookie) { 227 s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value)) 228 if c := r.Header.Get("Cookie"); c != "" { 229 r.Header.Set("Cookie", c+"; "+s) 230 } else { 231 r.Header.Set("Cookie", s) 232 } 233 } 234 235 // Referer returns the referring URL, if sent in the request. 236 // 237 // Referer is misspelled as in the request itself, a mistake from the 238 // earliest days of HTTP. This value can also be fetched from the 239 // Header map as Header["Referer"]; the benefit of making it available 240 // as a method is that the compiler can diagnose programs that use the 241 // alternate (correct English) spelling req.Referrer() but cannot 242 // diagnose programs that use Header["Referrer"]. 243 func (r *Request) Referer() string { 244 return r.Header.Get("Referer") 245 } 246 247 // multipartByReader is a sentinel value. 248 // Its presence in Request.MultipartForm indicates that parsing of the request 249 // body has been handed off to a MultipartReader instead of ParseMultipartFrom. 250 var multipartByReader = &multipart.Form{ 251 Value: make(map[string][]string), 252 File: make(map[string][]*multipart.FileHeader), 253 } 254 255 // MultipartReader returns a MIME multipart reader if this is a 256 // multipart/form-data POST request, else returns nil and an error. 257 // Use this function instead of ParseMultipartForm to 258 // process the request body as a stream. 259 func (r *Request) MultipartReader() (*multipart.Reader, error) { 260 if r.MultipartForm == multipartByReader { 261 return nil, errors.New("http: MultipartReader called twice") 262 } 263 if r.MultipartForm != nil { 264 return nil, errors.New("http: multipart handled by ParseMultipartForm") 265 } 266 r.MultipartForm = multipartByReader 267 return r.multipartReader() 268 } 269 270 func (r *Request) multipartReader() (*multipart.Reader, error) { 271 v := r.Header.Get("Content-Type") 272 if v == "" { 273 return nil, ErrNotMultipart 274 } 275 d, params, err := mime.ParseMediaType(v) 276 if err != nil || d != "multipart/form-data" { 277 return nil, ErrNotMultipart 278 } 279 boundary, ok := params["boundary"] 280 if !ok { 281 return nil, ErrMissingBoundary 282 } 283 return multipart.NewReader(r.Body, boundary), nil 284 } 285 286 // Return value if nonempty, def otherwise. 287 func valueOrDefault(value, def string) string { 288 if value != "" { 289 return value 290 } 291 return def 292 } 293 294 const defaultUserAgent = "Go 1.1 package http" 295 296 // Write writes an HTTP/1.1 request -- header and body -- in wire format. 297 // This method consults the following fields of the request: 298 // Host 299 // URL 300 // Method (defaults to "GET") 301 // Header 302 // ContentLength 303 // TransferEncoding 304 // Body 305 // 306 // If Body is present, Content-Length is <= 0 and TransferEncoding 307 // hasn't been set to "identity", Write adds "Transfer-Encoding: 308 // chunked" to the header. Body is closed after it is sent. 309 func (r *Request) Write(w io.Writer) error { 310 return r.write(w, false, nil) 311 } 312 313 // WriteProxy is like Write but writes the request in the form 314 // expected by an HTTP proxy. In particular, WriteProxy writes the 315 // initial Request-URI line of the request with an absolute URI, per 316 // section 5.1.2 of RFC 2616, including the scheme and host. 317 // In either case, WriteProxy also writes a Host header, using 318 // either r.Host or r.URL.Host. 319 func (r *Request) WriteProxy(w io.Writer) error { 320 return r.write(w, true, nil) 321 } 322 323 // extraHeaders may be nil 324 func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header) error { 325 host := req.Host 326 if host == "" { 327 if req.URL == nil { 328 return errors.New("http: Request.Write on Request with no Host or URL set") 329 } 330 host = req.URL.Host 331 } 332 333 ruri := req.URL.RequestURI() 334 if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" { 335 ruri = req.URL.Scheme + "://" + host + ruri 336 } else if req.Method == "CONNECT" && req.URL.Path == "" { 337 // CONNECT requests normally give just the host and port, not a full URL. 338 ruri = host 339 } 340 // TODO(bradfitz): escape at least newlines in ruri? 341 342 // Wrap the writer in a bufio Writer if it's not already buffered. 343 // Don't always call NewWriter, as that forces a bytes.Buffer 344 // and other small bufio Writers to have a minimum 4k buffer 345 // size. 346 var bw *bufio.Writer 347 if _, ok := w.(io.ByteWriter); !ok { 348 bw = bufio.NewWriter(w) 349 w = bw 350 } 351 352 fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri) 353 354 // Header lines 355 fmt.Fprintf(w, "Host: %s\r\n", host) 356 357 // Use the defaultUserAgent unless the Header contains one, which 358 // may be blank to not send the header. 359 userAgent := defaultUserAgent 360 if req.Header != nil { 361 if ua := req.Header["User-Agent"]; len(ua) > 0 { 362 userAgent = ua[0] 363 } 364 } 365 if userAgent != "" { 366 fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent) 367 } 368 369 // Process Body,ContentLength,Close,Trailer 370 tw, err := newTransferWriter(req) 371 if err != nil { 372 return err 373 } 374 err = tw.WriteHeader(w) 375 if err != nil { 376 return err 377 } 378 379 // TODO: split long values? (If so, should share code with Conn.Write) 380 err = req.Header.WriteSubset(w, reqWriteExcludeHeader) 381 if err != nil { 382 return err 383 } 384 385 if extraHeaders != nil { 386 err = extraHeaders.Write(w) 387 if err != nil { 388 return err 389 } 390 } 391 392 io.WriteString(w, "\r\n") 393 394 // Write body and trailer 395 err = tw.WriteBody(w) 396 if err != nil { 397 return err 398 } 399 400 if bw != nil { 401 return bw.Flush() 402 } 403 return nil 404 } 405 406 // ParseHTTPVersion parses a HTTP version string. 407 // "HTTP/1.0" returns (1, 0, true). 408 func ParseHTTPVersion(vers string) (major, minor int, ok bool) { 409 const Big = 1000000 // arbitrary upper bound 410 switch vers { 411 case "HTTP/1.1": 412 return 1, 1, true 413 case "HTTP/1.0": 414 return 1, 0, true 415 } 416 if !strings.HasPrefix(vers, "HTTP/") { 417 return 0, 0, false 418 } 419 dot := strings.Index(vers, ".") 420 if dot < 0 { 421 return 0, 0, false 422 } 423 major, err := strconv.Atoi(vers[5:dot]) 424 if err != nil || major < 0 || major > Big { 425 return 0, 0, false 426 } 427 minor, err = strconv.Atoi(vers[dot+1:]) 428 if err != nil || minor < 0 || minor > Big { 429 return 0, 0, false 430 } 431 return major, minor, true 432 } 433 434 // NewRequest returns a new Request given a method, URL, and optional body. 435 // 436 // If the provided body is also an io.Closer, the returned 437 // Request.Body is set to body and will be closed by the Client 438 // methods Do, Post, and PostForm, and Transport.RoundTrip. 439 func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { 440 u, err := url.Parse(urlStr) 441 if err != nil { 442 return nil, err 443 } 444 rc, ok := body.(io.ReadCloser) 445 if !ok && body != nil { 446 rc = ioutil.NopCloser(body) 447 } 448 req := &Request{ 449 Method: method, 450 URL: u, 451 Proto: "HTTP/1.1", 452 ProtoMajor: 1, 453 ProtoMinor: 1, 454 Header: make(Header), 455 Body: rc, 456 Host: u.Host, 457 } 458 if body != nil { 459 switch v := body.(type) { 460 case *bytes.Buffer: 461 req.ContentLength = int64(v.Len()) 462 case *bytes.Reader: 463 req.ContentLength = int64(v.Len()) 464 case *strings.Reader: 465 req.ContentLength = int64(v.Len()) 466 } 467 } 468 469 return req, nil 470 } 471 472 // SetBasicAuth sets the request's Authorization header to use HTTP 473 // Basic Authentication with the provided username and password. 474 // 475 // With HTTP Basic Authentication the provided username and password 476 // are not encrypted. 477 func (r *Request) SetBasicAuth(username, password string) { 478 r.Header.Set("Authorization", "Basic "+basicAuth(username, password)) 479 } 480 481 // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. 482 func parseRequestLine(line string) (method, requestURI, proto string, ok bool) { 483 s1 := strings.Index(line, " ") 484 s2 := strings.Index(line[s1+1:], " ") 485 if s1 < 0 || s2 < 0 { 486 return 487 } 488 s2 += s1 + 1 489 return line[:s1], line[s1+1 : s2], line[s2+1:], true 490 } 491 492 // TODO(bradfitz): use a sync.Cache when available 493 var textprotoReaderCache = make(chan *textproto.Reader, 4) 494 495 func newTextprotoReader(br *bufio.Reader) *textproto.Reader { 496 select { 497 case r := <-textprotoReaderCache: 498 r.R = br 499 return r 500 default: 501 return textproto.NewReader(br) 502 } 503 } 504 505 func putTextprotoReader(r *textproto.Reader) { 506 r.R = nil 507 select { 508 case textprotoReaderCache <- r: 509 default: 510 } 511 } 512 513 // ReadRequest reads and parses a request from b. 514 func ReadRequest(b *bufio.Reader) (req *Request, err error) { 515 516 tp := newTextprotoReader(b) 517 req = new(Request) 518 519 // First line: GET /index.html HTTP/1.0 520 var s string 521 if s, err = tp.ReadLine(); err != nil { 522 return nil, err 523 } 524 defer func() { 525 putTextprotoReader(tp) 526 if err == io.EOF { 527 err = io.ErrUnexpectedEOF 528 } 529 }() 530 531 var ok bool 532 req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s) 533 if !ok { 534 return nil, &badStringError{"malformed HTTP request", s} 535 } 536 rawurl := req.RequestURI 537 if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok { 538 return nil, &badStringError{"malformed HTTP version", req.Proto} 539 } 540 541 // CONNECT requests are used two different ways, and neither uses a full URL: 542 // The standard use is to tunnel HTTPS through an HTTP proxy. 543 // It looks like "CONNECT www.google.com:443 HTTP/1.1", and the parameter is 544 // just the authority section of a URL. This information should go in req.URL.Host. 545 // 546 // The net/rpc package also uses CONNECT, but there the parameter is a path 547 // that starts with a slash. It can be parsed with the regular URL parser, 548 // and the path will end up in req.URL.Path, where it needs to be in order for 549 // RPC to work. 550 justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/") 551 if justAuthority { 552 rawurl = "http://" + rawurl 553 } 554 555 if req.URL, err = url.ParseRequestURI(rawurl); err != nil { 556 return nil, err 557 } 558 559 if justAuthority { 560 // Strip the bogus "http://" back off. 561 req.URL.Scheme = "" 562 } 563 564 // Subsequent lines: Key: value. 565 mimeHeader, err := tp.ReadMIMEHeader() 566 if err != nil { 567 return nil, err 568 } 569 req.Header = Header(mimeHeader) 570 571 // RFC2616: Must treat 572 // GET /index.html HTTP/1.1 573 // Host: www.google.com 574 // and 575 // GET http://www.google.com/index.html HTTP/1.1 576 // Host: doesntmatter 577 // the same. In the second case, any Host line is ignored. 578 req.Host = req.URL.Host 579 if req.Host == "" { 580 req.Host = req.Header.get("Host") 581 } 582 delete(req.Header, "Host") 583 584 fixPragmaCacheControl(req.Header) 585 586 // TODO: Parse specific header values: 587 // Accept 588 // Accept-Encoding 589 // Accept-Language 590 // Authorization 591 // Cache-Control 592 // Connection 593 // Date 594 // Expect 595 // From 596 // If-Match 597 // If-Modified-Since 598 // If-None-Match 599 // If-Range 600 // If-Unmodified-Since 601 // Max-Forwards 602 // Proxy-Authorization 603 // Referer [sic] 604 // TE (transfer-codings) 605 // Trailer 606 // Transfer-Encoding 607 // Upgrade 608 // User-Agent 609 // Via 610 // Warning 611 612 err = readTransfer(req, b) 613 if err != nil { 614 return nil, err 615 } 616 617 return req, nil 618 } 619 620 // MaxBytesReader is similar to io.LimitReader but is intended for 621 // limiting the size of incoming request bodies. In contrast to 622 // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a 623 // non-EOF error for a Read beyond the limit, and Closes the 624 // underlying reader when its Close method is called. 625 // 626 // MaxBytesReader prevents clients from accidentally or maliciously 627 // sending a large request and wasting server resources. 628 func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser { 629 return &maxBytesReader{w: w, r: r, n: n} 630 } 631 632 type maxBytesReader struct { 633 w ResponseWriter 634 r io.ReadCloser // underlying reader 635 n int64 // max bytes remaining 636 stopped bool 637 } 638 639 func (l *maxBytesReader) Read(p []byte) (n int, err error) { 640 if l.n <= 0 { 641 if !l.stopped { 642 l.stopped = true 643 if res, ok := l.w.(*response); ok { 644 res.requestTooLarge() 645 } 646 } 647 return 0, errors.New("http: request body too large") 648 } 649 if int64(len(p)) > l.n { 650 p = p[:l.n] 651 } 652 n, err = l.r.Read(p) 653 l.n -= int64(n) 654 return 655 } 656 657 func (l *maxBytesReader) Close() error { 658 return l.r.Close() 659 } 660 661 func copyValues(dst, src url.Values) { 662 for k, vs := range src { 663 for _, value := range vs { 664 dst.Add(k, value) 665 } 666 } 667 } 668 669 func parsePostForm(r *Request) (vs url.Values, err error) { 670 if r.Body == nil { 671 err = errors.New("missing form body") 672 return 673 } 674 ct := r.Header.Get("Content-Type") 675 ct, _, err = mime.ParseMediaType(ct) 676 switch { 677 case ct == "application/x-www-form-urlencoded": 678 var reader io.Reader = r.Body 679 maxFormSize := int64(1<<63 - 1) 680 if _, ok := r.Body.(*maxBytesReader); !ok { 681 maxFormSize = int64(10 << 20) // 10 MB is a lot of text. 682 reader = io.LimitReader(r.Body, maxFormSize+1) 683 } 684 b, e := ioutil.ReadAll(reader) 685 if e != nil { 686 if err == nil { 687 err = e 688 } 689 break 690 } 691 if int64(len(b)) > maxFormSize { 692 err = errors.New("http: POST too large") 693 return 694 } 695 vs, e = url.ParseQuery(string(b)) 696 if err == nil { 697 err = e 698 } 699 case ct == "multipart/form-data": 700 // handled by ParseMultipartForm (which is calling us, or should be) 701 // TODO(bradfitz): there are too many possible 702 // orders to call too many functions here. 703 // Clean this up and write more tests. 704 // request_test.go contains the start of this, 705 // in TestRequestMultipartCallOrder. 706 } 707 return 708 } 709 710 // ParseForm parses the raw query from the URL and updates r.Form. 711 // 712 // For POST or PUT requests, it also parses the request body as a form and 713 // put the results into both r.PostForm and r.Form. 714 // POST and PUT body parameters take precedence over URL query string values 715 // in r.Form. 716 // 717 // If the request Body's size has not already been limited by MaxBytesReader, 718 // the size is capped at 10MB. 719 // 720 // ParseMultipartForm calls ParseForm automatically. 721 // It is idempotent. 722 func (r *Request) ParseForm() error { 723 var err error 724 if r.PostForm == nil { 725 if r.Method == "POST" || r.Method == "PUT" { 726 r.PostForm, err = parsePostForm(r) 727 } 728 if r.PostForm == nil { 729 r.PostForm = make(url.Values) 730 } 731 } 732 if r.Form == nil { 733 if len(r.PostForm) > 0 { 734 r.Form = make(url.Values) 735 copyValues(r.Form, r.PostForm) 736 } 737 var newValues url.Values 738 if r.URL != nil { 739 var e error 740 newValues, e = url.ParseQuery(r.URL.RawQuery) 741 if err == nil { 742 err = e 743 } 744 } 745 if newValues == nil { 746 newValues = make(url.Values) 747 } 748 if r.Form == nil { 749 r.Form = newValues 750 } else { 751 copyValues(r.Form, newValues) 752 } 753 } 754 return err 755 } 756 757 // ParseMultipartForm parses a request body as multipart/form-data. 758 // The whole request body is parsed and up to a total of maxMemory bytes of 759 // its file parts are stored in memory, with the remainder stored on 760 // disk in temporary files. 761 // ParseMultipartForm calls ParseForm if necessary. 762 // After one call to ParseMultipartForm, subsequent calls have no effect. 763 func (r *Request) ParseMultipartForm(maxMemory int64) error { 764 if r.MultipartForm == multipartByReader { 765 return errors.New("http: multipart handled by MultipartReader") 766 } 767 if r.Form == nil { 768 err := r.ParseForm() 769 if err != nil { 770 return err 771 } 772 } 773 if r.MultipartForm != nil { 774 return nil 775 } 776 777 mr, err := r.multipartReader() 778 if err == ErrNotMultipart { 779 return nil 780 } else if err != nil { 781 return err 782 } 783 784 f, err := mr.ReadForm(maxMemory) 785 if err != nil { 786 return err 787 } 788 for k, v := range f.Value { 789 r.Form[k] = append(r.Form[k], v...) 790 } 791 r.MultipartForm = f 792 793 return nil 794 } 795 796 // FormValue returns the first value for the named component of the query. 797 // POST and PUT body parameters take precedence over URL query string values. 798 // FormValue calls ParseMultipartForm and ParseForm if necessary. 799 // To access multiple values of the same key use ParseForm. 800 func (r *Request) FormValue(key string) string { 801 if r.Form == nil { 802 r.ParseMultipartForm(defaultMaxMemory) 803 } 804 if vs := r.Form[key]; len(vs) > 0 { 805 return vs[0] 806 } 807 return "" 808 } 809 810 // PostFormValue returns the first value for the named component of the POST 811 // or PUT request body. URL query parameters are ignored. 812 // PostFormValue calls ParseMultipartForm and ParseForm if necessary. 813 func (r *Request) PostFormValue(key string) string { 814 if r.PostForm == nil { 815 r.ParseMultipartForm(defaultMaxMemory) 816 } 817 if vs := r.PostForm[key]; len(vs) > 0 { 818 return vs[0] 819 } 820 return "" 821 } 822 823 // FormFile returns the first file for the provided form key. 824 // FormFile calls ParseMultipartForm and ParseForm if necessary. 825 func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) { 826 if r.MultipartForm == multipartByReader { 827 return nil, nil, errors.New("http: multipart handled by MultipartReader") 828 } 829 if r.MultipartForm == nil { 830 err := r.ParseMultipartForm(defaultMaxMemory) 831 if err != nil { 832 return nil, nil, err 833 } 834 } 835 if r.MultipartForm != nil && r.MultipartForm.File != nil { 836 if fhs := r.MultipartForm.File[key]; len(fhs) > 0 { 837 f, err := fhs[0].Open() 838 return f, fhs[0], err 839 } 840 } 841 return nil, nil, ErrMissingFile 842 } 843 844 func (r *Request) expectsContinue() bool { 845 return hasToken(r.Header.get("Expect"), "100-continue") 846 } 847 848 func (r *Request) wantsHttp10KeepAlive() bool { 849 if r.ProtoMajor != 1 || r.ProtoMinor != 0 { 850 return false 851 } 852 return hasToken(r.Header.get("Connection"), "keep-alive") 853 } 854 855 func (r *Request) wantsClose() bool { 856 return hasToken(r.Header.get("Connection"), "close") 857 }