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

Question regarding streaming large bodies #1647

Open
pablolagos opened this issue Nov 6, 2023 · 2 comments
Open

Question regarding streaming large bodies #1647

pablolagos opened this issue Nov 6, 2023 · 2 comments

Comments

@pablolagos
Copy link

Hello everyone. I know this has been asked several times, but it's been a while and there have been many changes in fasthttp.

Is there a way to copy a body directly from the source to the user without having to get it completely in memory?

This involves two aspects:

  1. client: Is it able to make a request and not download the body automatically?
  2. server: I have seen that there are some options, but none within the context of RequestCtx.
@pablolagos
Copy link
Author

pablolagos commented Nov 8, 2023

Hello again, We currently need this functionality and I think it could be beneficial to everyone.
Basically, the idea is:

In server

func handle(ctx *fasthttp.RequestCtx) {
     // DisableBuffering modifies fasthttp to disable headers and body buffering for this request
     ctx.DisableBuffering()
     
     // Write headers
     ctx.Response.Header.Set(.....)
     ctx.Response.Header.Set(.....)
     ctx.Response.Header.Set(.....)
     
     // Write body. Headers will be sent and cannot be modified once the body begins to be dispatched.
     // src can be any io.Reader or io.ReaderFrom or any object implementing io.WriteTo
     // The process would be unbuffered, well, actually using at most a 32KB buffer 
     // according to the current io.Copy implementation.
     // Ideal for serving large files without storing them on RAM. 
     io.Copy(ctx, src)
}

It would automatically set the Transfer-Encoding header to "chunked" and chunk the content on the fly.

I can write a PR if you think it makes sense for fasthttp

@erikdubbelboer
Copy link
Collaborator

It could make sense but I'm afraid it will be a lot harder to implement correctly than you think. If you can try to make a PR please do.

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