Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix application/x-www-form-urlencoded parsing #113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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