OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // HTTP Request reading and parsing. | 5 // HTTP Request reading and parsing. |
6 | 6 |
7 // Package http implements parsing of HTTP requests, replies, and URLs and | 7 // Package http implements parsing of HTTP requests, replies, and URLs and |
8 // provides an extensible HTTP server and a basic HTTP client. | 8 // provides an extensible HTTP server and a basic HTTP client. |
9 package http | 9 package http |
10 | 10 |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 // First line: GET /index.html HTTP/1.0 | 536 // First line: GET /index.html HTTP/1.0 |
537 var s string | 537 var s string |
538 if s, err = tp.ReadLine(); err != nil { | 538 if s, err = tp.ReadLine(); err != nil { |
539 if err == os.EOF { | 539 if err == os.EOF { |
540 err = io.ErrUnexpectedEOF | 540 err = io.ErrUnexpectedEOF |
541 } | 541 } |
542 return nil, err | 542 return nil, err |
543 } | 543 } |
544 | 544 |
545 var f []string | 545 var f []string |
546 » if f = strings.Split(s, " ", 3); len(f) < 3 { | 546 » if f = strings.SplitN(s, " ", 3); len(f) < 3 { |
547 return nil, &badStringError{"malformed HTTP request", s} | 547 return nil, &badStringError{"malformed HTTP request", s} |
548 } | 548 } |
549 req.Method, req.RawURL, req.Proto = f[0], f[1], f[2] | 549 req.Method, req.RawURL, req.Proto = f[0], f[1], f[2] |
550 var ok bool | 550 var ok bool |
551 if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok
{ | 551 if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok
{ |
552 return nil, &badStringError{"malformed HTTP version", req.Proto} | 552 return nil, &badStringError{"malformed HTTP version", req.Proto} |
553 } | 553 } |
554 | 554 |
555 if req.URL, err = ParseRequestURL(req.RawURL); err != nil { | 555 if req.URL, err = ParseRequestURL(req.RawURL); err != nil { |
556 return nil, err | 556 return nil, err |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 // ParseQuery always returns a non-nil map containing all the | 655 // ParseQuery always returns a non-nil map containing all the |
656 // valid query parameters found; err describes the first decoding error | 656 // valid query parameters found; err describes the first decoding error |
657 // encountered, if any. | 657 // encountered, if any. |
658 func ParseQuery(query string) (m Values, err os.Error) { | 658 func ParseQuery(query string) (m Values, err os.Error) { |
659 m = make(Values) | 659 m = make(Values) |
660 err = parseQuery(m, query) | 660 err = parseQuery(m, query) |
661 return | 661 return |
662 } | 662 } |
663 | 663 |
664 func parseQuery(m Values, query string) (err os.Error) { | 664 func parseQuery(m Values, query string) (err os.Error) { |
665 » for _, kv := range strings.Split(query, "&", -1) { | 665 » for _, kv := range strings.Split(query, "&") { |
666 if len(kv) == 0 { | 666 if len(kv) == 0 { |
667 continue | 667 continue |
668 } | 668 } |
669 » » kvPair := strings.Split(kv, "=", 2) | 669 » » kvPair := strings.SplitN(kv, "=", 2) |
670 | 670 |
671 var key, value string | 671 var key, value string |
672 var e os.Error | 672 var e os.Error |
673 key, e = URLUnescape(kvPair[0]) | 673 key, e = URLUnescape(kvPair[0]) |
674 if e == nil && len(kvPair) > 1 { | 674 if e == nil && len(kvPair) > 1 { |
675 value, e = URLUnescape(kvPair[1]) | 675 value, e = URLUnescape(kvPair[1]) |
676 } | 676 } |
677 if e != nil { | 677 if e != nil { |
678 err = e | 678 err = e |
679 continue | 679 continue |
(...skipping 16 matching lines...) Expand all Loading... |
696 | 696 |
697 r.Form = make(Values) | 697 r.Form = make(Values) |
698 if r.URL != nil { | 698 if r.URL != nil { |
699 err = parseQuery(r.Form, r.URL.RawQuery) | 699 err = parseQuery(r.Form, r.URL.RawQuery) |
700 } | 700 } |
701 if r.Method == "POST" { | 701 if r.Method == "POST" { |
702 if r.Body == nil { | 702 if r.Body == nil { |
703 return os.NewError("missing form body") | 703 return os.NewError("missing form body") |
704 } | 704 } |
705 ct := r.Header.Get("Content-Type") | 705 ct := r.Header.Get("Content-Type") |
706 » » switch strings.Split(ct, ";", 2)[0] { | 706 » » switch strings.SplitN(ct, ";", 2)[0] { |
707 case "text/plain", "application/x-www-form-urlencoded", "": | 707 case "text/plain", "application/x-www-form-urlencoded", "": |
708 const maxFormSize = int64(10 << 20) // 10 MB is a lot of
text. | 708 const maxFormSize = int64(10 << 20) // 10 MB is a lot of
text. |
709 b, e := ioutil.ReadAll(io.LimitReader(r.Body, maxFormSiz
e+1)) | 709 b, e := ioutil.ReadAll(io.LimitReader(r.Body, maxFormSiz
e+1)) |
710 if e != nil { | 710 if e != nil { |
711 if err == nil { | 711 if err == nil { |
712 err = e | 712 err = e |
713 } | 713 } |
714 break | 714 break |
715 } | 715 } |
716 if int64(len(b)) > maxFormSize { | 716 if int64(len(b)) > maxFormSize { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 func (r *Request) expectsContinue() bool { | 804 func (r *Request) expectsContinue() bool { |
805 return strings.ToLower(r.Header.Get("Expect")) == "100-continue" | 805 return strings.ToLower(r.Header.Get("Expect")) == "100-continue" |
806 } | 806 } |
807 | 807 |
808 func (r *Request) wantsHttp10KeepAlive() bool { | 808 func (r *Request) wantsHttp10KeepAlive() bool { |
809 if r.ProtoMajor != 1 || r.ProtoMinor != 0 { | 809 if r.ProtoMajor != 1 || r.ProtoMinor != 0 { |
810 return false | 810 return false |
811 } | 811 } |
812 return strings.Contains(strings.ToLower(r.Header.Get("Connection")), "ke
ep-alive") | 812 return strings.Contains(strings.ToLower(r.Header.Get("Connection")), "ke
ep-alive") |
813 } | 813 } |
OLD | NEW |