Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A workaround for handling big response body? #1948

Open
osxtest opened this issue Feb 7, 2025 · 1 comment
Open

A workaround for handling big response body? #1948

osxtest opened this issue Feb 7, 2025 · 1 comment

Comments

@osxtest
Copy link

osxtest commented Feb 7, 2025

Hello,

While reviewing PR #1414, I found that large response bodies can be streamed and partially read (based on how you use bodyStream) by setting both StreamResponseBody and MaxResponseBodySize:

fasthttp/http_test.go

Lines 3017 to 3037 in d2dc36f

t.Run("limit response body size", func(t *testing.T) {
t.Parallel()
client := Client{StreamResponseBody: true, MaxResponseBodySize: 20}
resp := AcquireResponse()
request := AcquireRequest()
request.SetRequestURI(server.URL)
if err := client.Do(request, resp); err != nil {
t.Fatal(err)
}
stream := resp.BodyStream()
defer func() {
if err := resp.CloseBodyStream(); err != nil {
t.Fatalf("close stream err: %v", err)
}
}()
content, _ := io.ReadAll(stream)
if string(content) != "hello world" {
t.Fatalf("unexpected body content, got: %#v, want: %#v", string(content), "hello world")
}
})

When these two fields are set, the response body exceeding maxBodySize is returned as a stream:

fasthttp/http.go

Lines 1477 to 1481 in d2dc36f

bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
if err == ErrBodyTooLarge && resp.StreamBody {
resp.bodyStream = acquireRequestStream(bodyBuf, r, &resp.Header)
err = nil
}

If this is a feasible workaround, should we update the doc or add examples for this specific use case?

Related doc and issues:

@erikdubbelboer
Copy link
Collaborator

Pull requests with extra documentation and examples is always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants