generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
routing/http: feat: limit the resp body payload
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type ResponseBodyLimitedTransport struct { | ||
http.RoundTripper | ||
LimitBytes int64 | ||
} | ||
|
||
func (r *ResponseBodyLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := r.RoundTripper.RoundTrip(req) | ||
if resp != nil && resp.Body != nil { | ||
resp.Body = &limitReadCloser{ | ||
limit: r.LimitBytes, | ||
ReadCloser: resp.Body, | ||
} | ||
} | ||
return resp, err | ||
} | ||
|
||
type limitReadCloser struct { | ||
limit int64 | ||
bytesRead int64 | ||
io.ReadCloser | ||
} | ||
|
||
func (l *limitReadCloser) Read(p []byte) (int, error) { | ||
n, err := l.ReadCloser.Read(p) | ||
l.bytesRead += int64(n) | ||
if l.bytesRead > l.limit { | ||
return 0, fmt.Errorf("reached read limit of %d bytes after reading %d bytes", l.limit, l.bytesRead) | ||
} | ||
return n, err | ||
} |
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,44 @@ | ||
package client | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testServer struct { | ||
bytesToWrite int | ||
} | ||
|
||
func (s *testServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
bytes := make([]byte, s.bytesToWrite) | ||
for i := 0; i < s.bytesToWrite; i++ { | ||
bytes[i] = 'a' | ||
} | ||
_, err := w.Write(bytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestResponseBodyLimitedTransport(t *testing.T) { | ||
server := httptest.NewServer(&testServer{bytesToWrite: 1 << 21}) | ||
t.Cleanup(server.Close) | ||
|
||
client := server.Client() | ||
client.Transport = &ResponseBodyLimitedTransport{ | ||
LimitBytes: 1 << 20, | ||
RoundTripper: client.Transport, | ||
} | ||
|
||
resp, err := client.Get(server.URL) | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = io.ReadAll(resp.Body) | ||
|
||
assert.Contains(t, err.Error(), "reached read limit of 1048576 bytes after reading") | ||
} |