Commit 7f4c2cae authored by Petar Maymounkov's avatar Petar Maymounkov Committed by Russ Cox

http: add ParseQuery

R=rsc
CC=golang-dev
https://golang.org/cl/238041
parent 354679d9
......@@ -571,7 +571,7 @@ func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) {
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)
for _, kv := range strings.Split(query, "&", 0) {
kvPair := strings.Split(kv, "=", 2)
......@@ -594,6 +594,7 @@ func parseForm(m map[string][]string, query string) (err os.Error) {
vec.Push(value)
}
m = make(map[string][]string)
for k, vec := range data {
m[k] = vec.Data()
}
......@@ -607,7 +608,6 @@ func (r *Request) ParseForm() (err os.Error) {
if r.Form != nil {
return
}
r.Form = make(map[string][]string)
var query string
switch r.Method {
......@@ -615,6 +615,7 @@ func (r *Request) ParseForm() (err os.Error) {
query = r.URL.RawQuery
case "POST":
if r.Body == nil {
r.Form = make(map[string][]string)
return os.ErrorString("missing form body")
}
ct, _ := r.Header["Content-Type"]
......@@ -622,15 +623,18 @@ func (r *Request) ParseForm() (err os.Error) {
case "text/plain", "application/x-www-form-urlencoded", "":
var b []byte
if b, err = ioutil.ReadAll(r.Body); err != nil {
r.Form = make(map[string][]string)
return err
}
query = string(b)
// TODO(dsymonds): Handle multipart/form-data
default:
r.Form = make(map[string][]string)
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.
......
......@@ -33,8 +33,7 @@ var parseTests = []parseTest{
func TestParseForm(t *testing.T) {
for i, test := range parseTests {
form := make(map[string][]string)
err := parseForm(form, test.query)
form, err := ParseQuery(test.query)
if err != nil {
t.Errorf("test %d: Unexpected error: %v", i, err)
continue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment