1
0
mirror of https://github.com/golang/go synced 2024-11-22 07:04:40 -07:00

http: add ParseQuery

R=rsc
CC=golang-dev
https://golang.org/cl/238041
This commit is contained in:
Petar Maymounkov 2010-03-16 13:57:16 -07:00 committed by Russ Cox
parent 354679d910
commit 7f4c2caea5
2 changed files with 8 additions and 5 deletions

View File

@ -571,7 +571,7 @@ func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) {
return req, nil return req, nil
} }
func parseForm(m map[string][]string, query string) (err os.Error) { func ParseQuery(query string) (m map[string][]string, err os.Error) {
data := make(map[string]*vector.StringVector) data := make(map[string]*vector.StringVector)
for _, kv := range strings.Split(query, "&", 0) { for _, kv := range strings.Split(query, "&", 0) {
kvPair := strings.Split(kv, "=", 2) kvPair := strings.Split(kv, "=", 2)
@ -594,6 +594,7 @@ func parseForm(m map[string][]string, query string) (err os.Error) {
vec.Push(value) vec.Push(value)
} }
m = make(map[string][]string)
for k, vec := range data { for k, vec := range data {
m[k] = vec.Data() m[k] = vec.Data()
} }
@ -607,7 +608,6 @@ func (r *Request) ParseForm() (err os.Error) {
if r.Form != nil { if r.Form != nil {
return return
} }
r.Form = make(map[string][]string)
var query string var query string
switch r.Method { switch r.Method {
@ -615,6 +615,7 @@ func (r *Request) ParseForm() (err os.Error) {
query = r.URL.RawQuery query = r.URL.RawQuery
case "POST": case "POST":
if r.Body == nil { if r.Body == nil {
r.Form = make(map[string][]string)
return os.ErrorString("missing form body") return os.ErrorString("missing form body")
} }
ct, _ := r.Header["Content-Type"] ct, _ := r.Header["Content-Type"]
@ -622,15 +623,18 @@ func (r *Request) ParseForm() (err os.Error) {
case "text/plain", "application/x-www-form-urlencoded", "": case "text/plain", "application/x-www-form-urlencoded", "":
var b []byte var b []byte
if b, err = ioutil.ReadAll(r.Body); err != nil { if b, err = ioutil.ReadAll(r.Body); err != nil {
r.Form = make(map[string][]string)
return err return err
} }
query = string(b) query = string(b)
// TODO(dsymonds): Handle multipart/form-data // TODO(dsymonds): Handle multipart/form-data
default: default:
r.Form = make(map[string][]string)
return &badStringError{"unknown Content-Type", ct} return &badStringError{"unknown Content-Type", ct}
} }
} }
return parseForm(r.Form, query) r.Form, err = ParseQuery(query)
return
} }
// FormValue returns the first value for the named component of the query. // FormValue returns the first value for the named component of the query.

View File

@ -33,8 +33,7 @@ var parseTests = []parseTest{
func TestParseForm(t *testing.T) { func TestParseForm(t *testing.T) {
for i, test := range parseTests { for i, test := range parseTests {
form := make(map[string][]string) form, err := ParseQuery(test.query)
err := parseForm(form, test.query)
if err != nil { if err != nil {
t.Errorf("test %d: Unexpected error: %v", i, err) t.Errorf("test %d: Unexpected error: %v", i, err)
continue continue