Skip to content

Commit

Permalink
Merge pull request roadrunner-server#113 from Alex-Bond/bugfix/urlenc…
Browse files Browse the repository at this point in the history
…oded-parsing

Fix application/x-www-form-urlencoded parsing
  • Loading branch information
wolfy-j authored Feb 2, 2019
2 parents cd9e9a9 + 01148ad commit 8a1cd5f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions service/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,64 @@ func TestHandler_FormData_POST(t *testing.T) {
assert.Equal(t, `{"arr":{"c":{"p":"l","z":""},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`, string(b))
}

func TestHandler_FormData_POST_Form_UrlEncoded_Charset(t *testing.T) {
h := &Handler{
cfg: &Config{
MaxRequest: 1024,
Uploads: &UploadsConfig{
Dir: os.TempDir(),
Forbid: []string{},
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
AllocateTimeout: 10000000,
DestroyTimeout: 10000000,
},
}),
}

assert.NoError(t, h.rr.Start())
defer h.rr.Stop()

hs := &http.Server{Addr: ":8083", Handler: h}
defer hs.Shutdown(context.Background())

go func() { hs.ListenAndServe() }()
time.Sleep(time.Millisecond * 10)

form := url.Values{}

form.Add("key", "value")
form.Add("name[]", "name1")
form.Add("name[]", "name2")
form.Add("name[]", "name3")
form.Add("arr[x][y][z]", "y")
form.Add("arr[x][y][e]", "f")
form.Add("arr[c]p", "l")
form.Add("arr[c]z", "")

req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, strings.NewReader(form.Encode()))
assert.NoError(t, err)

req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")

r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
defer r.Body.Close()

b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)

assert.NoError(t, err)
assert.Equal(t, 200, r.StatusCode)

assert.Equal(t, `{"arr":{"c":{"p":"l","z":""},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`, string(b))
}

func TestHandler_FormData_PUT(t *testing.T) {
h := &Handler{
cfg: &Config{
Expand Down
2 changes: 1 addition & 1 deletion service/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r *Request) contentType() int {
}

ct := r.Headers.Get("content-type")
if ct == "application/x-www-form-urlencoded" {
if strings.Contains(ct, "application/x-www-form-urlencoded") {
return contentFormData
}

Expand Down

0 comments on commit 8a1cd5f

Please sign in to comment.