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

Added utility function to convert body into bytes #2373

Merged
merged 6 commits into from Nov 29, 2023

Conversation

ItsEthra
Copy link
Contributor

Motivation

Sometimes it is useful to convert axum body into bytes, for that one needs to use collect, so to avoid pulling in http-body-util I want to provide this function.

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but we need to careful about making sure the body has a limit. That was a common source of vulnerabilities with hyper's to_bytes since it didn't set any default limit.

What about

pub async fn to_bytes(body: Body, limit: usize)

Then you can do to_bytes(body, usize::MAX) if you really don't want a limit.

A limit does get applied by axum when using extractors but if you were to extract the whole Request and then use to_bytes then no limit would be applied.

axum/src/body/mod.rs Outdated Show resolved Hide resolved
@ItsEthra
Copy link
Contributor Author

I see, didn't think about that. How should I implement length limiting in that case? I'm thinking about converting into data frames stream and keep joining them until they exceed length limit.

@davidpdrsn
Copy link
Member

You can use https://docs.rs/http-body-util/latest/http_body_util/struct.Limited.html. That should handle everything for you.

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

One last thing: Wanna add a line to the changelog?

@ItsEthra
Copy link
Contributor Author

Will add

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@davidpdrsn davidpdrsn merged commit 3c7cf81 into tokio-rs:main Nov 29, 2023
18 checks passed
@ItsEthra ItsEthra deleted the body-bytes-util branch November 29, 2023 17:34
rillian added a commit to brave/star-randsrv that referenced this pull request Nov 30, 2023
This release removes the re-export of hyper::body, which has moved
to the http-body crate. Worse, the `to_bytes` method we were using
wasn't stablized with the 1.0 release of that crate, and was instead
moved to a new http-body-util crate.

axum 0.7.2 will likely add a native Body::to_bytes method we can
use for this. In the meantime use a similar conversion ourselves.
See tokio-rs/axum#2373
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

Successfully merging this pull request may close these issues.

None yet

3 participants