Skip to content

Commit cbbc296

Browse files
authored
fix: properly handle /bytes/0 (#115)
Fixes #112. BREAKING CHANGE: Requests for zero bytes now actually return zero bytes. Requests for negative numbers of bytes are now rejected.
1 parent 8d82c0d commit cbbc296

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

httpbin/handlers.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,20 @@ func handleBytes(w http.ResponseWriter, r *http.Request, streaming bool) {
785785
return
786786
}
787787

788-
if numBytes < 1 {
789-
numBytes = 1
790-
} else if numBytes > 100*1024 {
788+
if numBytes < 0 {
789+
http.Error(w, "Bad Request", http.StatusBadRequest)
790+
return
791+
}
792+
793+
// Special case 0 bytes and exit early, since streaming & chunk size do not
794+
// matter here.
795+
if numBytes == 0 {
796+
w.Header().Set("Content-Length", "0")
797+
w.WriteHeader(http.StatusOK)
798+
return
799+
}
800+
801+
if numBytes > 100*1024 {
791802
numBytes = 100 * 1024
792803
}
793804

httpbin/handlers_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,8 @@ func TestBytes(t *testing.T) {
24552455
url string
24562456
expectedContentLength int
24572457
}{
2458-
{"/bytes/-1", 1},
2458+
{"/bytes/0", 0},
2459+
{"/bytes/1", 1},
24592460
{"/bytes/99999999", 100 * 1024},
24602461

24612462
// negative seed allowed
@@ -2480,6 +2481,8 @@ func TestBytes(t *testing.T) {
24802481
url string
24812482
expectedStatus int
24822483
}{
2484+
{"/bytes/-1", http.StatusBadRequest},
2485+
24832486
{"/bytes", http.StatusNotFound},
24842487
{"/bytes/16/foo", http.StatusNotFound},
24852488

0 commit comments

Comments
 (0)