Skip to content

Commit

Permalink
fix: Report 413 instead of 500 if request too large. #977
Browse files Browse the repository at this point in the history
  • Loading branch information
lo5 committed Aug 25, 2021
1 parent 304784b commit 006e282
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
v, err := readRequestWithLimit(w, r.Body, c.maxRequestSize)
if err != nil {
echo(Log{"t": "read cache request body", "error": err.Error()})
if isRequestTooLarge(err) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
Expand Down
4 changes: 4 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := readRequestWithLimit(w, r.Body, p.maxRequestSize)
if err != nil {
echo(Log{"t": "read proxy request body", "error": err.Error()})
if isRequestTooLarge(err) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
Expand Down
7 changes: 7 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func readRequestWithLimit(w http.ResponseWriter, r io.ReadCloser, n int64) ([]by
return ioutil.ReadAll(http.MaxBytesReader(w, r, n))
}

func isRequestTooLarge(err error) bool {
// HACK: net/http does not export the error
// https://github.com/golang/go/issues/30715
// https://github.com/golang/go/issues/41493
return err != nil && err.Error() == "http: request body too large"
}

func readWithLimit(r io.Reader, n int64) ([]byte, error) {
return ioutil.ReadAll(io.LimitReader(r, n))
}
8 changes: 8 additions & 0 deletions web_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (s *WebServer) patch(w http.ResponseWriter, r *http.Request) {
data, err := readRequestWithLimit(w, r.Body, s.maxRequestSize)
if err != nil {
echo(Log{"t": "read patch request body", "error": err.Error()})
if isRequestTooLarge(err) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -115,6 +119,10 @@ func (s *WebServer) post(w http.ResponseWriter, r *http.Request) {
b, err := readRequestWithLimit(w, r.Body, s.maxRequestSize)
if err != nil {
echo(Log{"t": "read post request body", "error": err.Error()})
if isRequestTooLarge(err) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 006e282

Please sign in to comment.