Skip to content

Commit

Permalink
optimize(http1): return 413 status code if request body is too large (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever authored Dec 7, 2022
1 parent 9b9ec92 commit a470f4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -379,8 +380,11 @@ func TestNotEnoughBodySize(t *testing.T) {
r.ParseForm()
r.Form.Add("xxxxxx", "xxx")
body := strings.NewReader(r.Form.Encode())
resp, _ := http.Post("http://127.0.0.1:8889/test", "application/x-www-form-urlencoded", body)
assert.DeepEqual(t, 400, resp.StatusCode)
resp, err := http.Post("http://127.0.0.1:8889/test", "application/x-www-form-urlencoded", body)
assert.Nil(t, err)
assert.DeepEqual(t, 413, resp.StatusCode)
bodyBytes, _ := ioutil.ReadAll(resp.Body)
assert.DeepEqual(t, "Request Entity Too Large", string(bodyBytes))
}

func TestEnoughBodySize(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/protocol/http1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ func writeResponse(ctx *app.RequestContext, w network.Writer) error {
func defaultErrorHandler(ctx *app.RequestContext, err error) {
if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
ctx.AbortWithMsg("Request timeout", consts.StatusRequestTimeout)
} else if errors.Is(err, errs.ErrBodyTooLarge) {
ctx.AbortWithMsg("Request Entity Too Large", consts.StatusRequestEntityTooLarge)
} else {
ctx.AbortWithMsg("Error when parsing request", consts.StatusBadRequest)
}
Expand Down

0 comments on commit a470f4a

Please sign in to comment.