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