-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW max-body-size config; HTTP 413 if body exceeds max size; fixes #8299
- Loading branch information
1 parent
55d1ba6
commit cac5dbf
Showing
5 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package httpd | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
var ( | ||
errTruncated = errors.New("Read: truncated") | ||
) | ||
|
||
// truncateReader returns a Reader that reads from r | ||
// but stops with ErrTruncated after n bytes. | ||
func truncateReader(r io.Reader, n int64) io.ReadCloser { | ||
tr := &truncatedReader{r: &io.LimitedReader{R: r, N: n + 1}} | ||
|
||
if rc, ok := r.(io.Closer); ok { | ||
tr.Closer = rc | ||
} | ||
|
||
return tr | ||
} | ||
|
||
// A truncatedReader reads limits the amount of | ||
// data returned to a maximum of r.N bytes. | ||
type truncatedReader struct { | ||
r *io.LimitedReader | ||
io.Closer | ||
} | ||
|
||
func (r *truncatedReader) Read(p []byte) (n int, err error) { | ||
n, err = r.r.Read(p) | ||
if r.r.N <= 0 { | ||
return n, errTruncated | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func (r *truncatedReader) Close() error { | ||
if r.Closer != nil { | ||
return r.Closer.Close() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package httpd | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestTruncatedReader_Read(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in []byte | ||
n int64 | ||
err error | ||
}{ | ||
{"in(1000)-max(1000)", make([]byte, 1000), 1000, nil}, | ||
{"in(1000)-max(1001)", make([]byte, 1000), 1001, nil}, | ||
{"in(1001)-max(1000)", make([]byte, 1001), 1000, errTruncated}, | ||
{"in(10000)-max(1000)", make([]byte, 1e5), 1e3, errTruncated}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
b := truncateReader(bytes.NewReader(tc.in), tc.n) | ||
_, err := ioutil.ReadAll(b) | ||
if err != tc.err { | ||
t.Errorf("unexpected error; got=%v, exp=%v", err, tc.err) | ||
} | ||
}) | ||
} | ||
} |
cac5dbf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a unit test for a mis-configured negative
max-body-size
?