diff options
author | Russ Cox <rsc@golang.org> | 2010-03-30 10:51:11 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2010-03-30 10:51:11 -0700 |
commit | 0bb18c1baaaea84c30e88af4103e207ee73ea733 (patch) | |
tree | 8b338a7affb56cf4142d66f8a8cd691366fdf7d1 /src/pkg/http | |
parent | 03c9611be0830da3cdb670cf59239eafd8bb4395 (diff) | |
download | go-0bb18c1baaaea84c30e88af4103e207ee73ea733.tar.gz |
simplify various code using new map index rule
R=r
CC=golang-dev
http://codereview.appspot.com/833044
Diffstat (limited to 'src/pkg/http')
-rw-r--r-- | src/pkg/http/request.go | 53 | ||||
-rw-r--r-- | src/pkg/http/response.go | 17 | ||||
-rw-r--r-- | src/pkg/http/transfer.go | 7 |
3 files changed, 30 insertions, 47 deletions
diff --git a/src/pkg/http/request.go b/src/pkg/http/request.go index 33c12c024..83a335bec 100644 --- a/src/pkg/http/request.go +++ b/src/pkg/http/request.go @@ -49,13 +49,13 @@ type badStringError struct { func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e.str) } -var reqExcludeHeader = map[string]int{ - "Host": 0, - "User-Agent": 0, - "Referer": 0, - "Content-Length": 0, - "Transfer-Encoding": 0, - "Trailer": 0, +var reqExcludeHeader = map[string]bool{ + "Host": true, + "User-Agent": true, + "Referer": true, + "Content-Length": true, + "Transfer-Encoding": true, + "Trailer": true, } // A Request represents a parsed HTTP request header. @@ -518,24 +518,19 @@ func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) { // Host: doesntmatter // the same. In the second case, any Host line is ignored. req.Host = req.URL.Host - if v, present := req.Header["Host"]; present { - if req.Host == "" { - req.Host = v - } - req.Header["Host"] = "", false + if req.Host == "" { + req.Host = req.Header["Host"] } + req.Header["Host"] = "", false fixPragmaCacheControl(req.Header) // Pull out useful fields as a convenience to clients. - if v, present := req.Header["Referer"]; present { - req.Referer = v - req.Header["Referer"] = "", false - } - if v, present := req.Header["User-Agent"]; present { - req.UserAgent = v - req.Header["User-Agent"] = "", false - } + req.Referer = req.Header["Referer"] + req.Header["Referer"] = "", false + + req.UserAgent = req.Header["User-Agent"] + req.Header["User-Agent"] = "", false // TODO: Parse specific header values: // Accept @@ -572,7 +567,7 @@ func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) { } func ParseQuery(query string) (m map[string][]string, err os.Error) { - data := make(map[string]*vector.StringVector) + m = make(map[string][]string) for _, kv := range strings.Split(query, "&", 0) { kvPair := strings.Split(kv, "=", 2) @@ -586,17 +581,9 @@ func ParseQuery(query string) (m map[string][]string, err os.Error) { err = e } - vec, ok := data[key] - if !ok { - vec = new(vector.StringVector) - data[key] = vec - } + vec := vector.StringVector(m[key]) vec.Push(value) - } - - m = make(map[string][]string) - for k, vec := range data { - m[k] = vec.Data() + m[key] = vec } return @@ -618,7 +605,7 @@ func (r *Request) ParseForm() (err os.Error) { r.Form = make(map[string][]string) return os.ErrorString("missing form body") } - ct, _ := r.Header["Content-Type"] + ct := r.Header["Content-Type"] switch strings.Split(ct, ";", 2)[0] { case "text/plain", "application/x-www-form-urlencoded", "": var b []byte @@ -643,7 +630,7 @@ func (r *Request) FormValue(key string) string { if r.Form == nil { r.ParseForm() } - if vs, ok := r.Form[key]; ok && len(vs) > 0 { + if vs := r.Form[key]; len(vs) > 0 { return vs[0] } return "" diff --git a/src/pkg/http/response.go b/src/pkg/http/response.go index 3a4637576..6a209c9f8 100644 --- a/src/pkg/http/response.go +++ b/src/pkg/http/response.go @@ -16,10 +16,10 @@ import ( "strings" ) -var respExcludeHeader = map[string]int{ - "Content-Length": 0, - "Transfer-Encoding": 0, - "Trailer": 0, +var respExcludeHeader = map[string]bool{ + "Content-Length": true, + "Transfer-Encoding": true, + "Trailer": true, } // Response represents the response from an HTTP request. @@ -133,7 +133,7 @@ func ReadResponse(r *bufio.Reader, requestMethod string) (resp *Response, err os // like // Cache-Control: no-cache func fixPragmaCacheControl(header map[string]string) { - if v, present := header["Pragma"]; present && v == "no-cache" { + if header["Pragma"] == "no-cache" { if _, presentcc := header["Cache-Control"]; !presentcc { header["Cache-Control"] = "no-cache" } @@ -157,8 +157,7 @@ func (r *Response) AddHeader(key, value string) { // with a comma delimiter. If there were no response headers with the given // key, GetHeader returns an empty string. Keys are not case sensitive. func (r *Response) GetHeader(key string) (value string) { - value, _ = r.Header[CanonicalHeaderKey(key)] - return + return r.Header[CanonicalHeaderKey(key)] } // ProtoAtLeast returns whether the HTTP protocol used @@ -228,11 +227,11 @@ func (resp *Response) Write(w io.Writer) os.Error { return nil } -func writeSortedKeyValue(w io.Writer, kvm map[string]string, exclude map[string]int) os.Error { +func writeSortedKeyValue(w io.Writer, kvm map[string]string, exclude map[string]bool) os.Error { kva := make([]string, len(kvm)) i := 0 for k, v := range kvm { - if _, exc := exclude[k]; !exc { + if !exclude[k] { kva[i] = fmt.Sprint(k + ": " + v + "\r\n") i++ } diff --git a/src/pkg/http/transfer.go b/src/pkg/http/transfer.go index 017077a99..26266cbca 100644 --- a/src/pkg/http/transfer.go +++ b/src/pkg/http/transfer.go @@ -340,11 +340,8 @@ func fixLength(status int, requestMethod string, header map[string]string, te [] // Logic based on media type. The purpose of the following code is just // to detect whether the unsupported "multipart/byteranges" is being // used. A proper Content-Type parser is needed in the future. - if ct, present := header["Content-Type"]; present { - ct = strings.ToLower(ct) - if strings.Index(ct, "multipart/byteranges") >= 0 { - return -1, ErrNotSupported - } + if strings.Index(strings.ToLower(header["Content-Type"]), "multipart/byteranges") >= 0 { + return -1, ErrNotSupported } // Body-EOF logic based on other methods (like closing, or chunked coding) |