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

Automatically handle http_body::LengthLimitError #1048

Merged
merged 9 commits into from
Jun 8, 2022

Conversation

davidpdrsn
Copy link
Member

@davidpdrsn davidpdrsn commented May 22, 2022

This adds special handling of http_body::LengthLimitError to FailedToBufferBody such that this just works and returns 413 Payload Too Large if the limit is exceeded.

tower-http's new RequestBodyLimitLayer uses that so limiting both requests with a known length and streaming is just:

use tower_http::limit::RequestBodyLimitLayer;

Router::new()
    .route("/", post(|body: Bytes| { ... }))
    .layer(RequestBodyLimitLayer::new(1024));

I think changing ContentLengthLimit to use http_body::Limited would be good but we cannot do that without breaking changes since it requires T: FromRequest<B> but we have to change that to T: FromRequest<Limited<B>>.

EDIT: Actually I don't think we can do that since you cannot change the body type in a RequestParts with only an &mut RequestParts. You'd need an owned value 🤔

@davidpdrsn davidpdrsn marked this pull request as ready for review June 6, 2022 18:56
@davidpdrsn davidpdrsn requested a review from jplatte June 6, 2022 18:56
axum-core/src/extract/rejection.rs Outdated Show resolved Hide resolved
axum-core/src/extract/rejection.rs Outdated Show resolved Hide resolved
axum-core/src/extract/rejection.rs Outdated Show resolved Hide resolved
axum-core/src/extract/rejection.rs Outdated Show resolved Hide resolved
axum-core/src/extract/rejection.rs Outdated Show resolved Hide resolved
@davidpdrsn davidpdrsn requested a review from jplatte June 8, 2022 13:21
@davidpdrsn davidpdrsn enabled auto-merge (squash) June 8, 2022 13:22
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@davidpdrsn davidpdrsn merged commit ef75079 into main Jun 8, 2022
@davidpdrsn davidpdrsn deleted the handle-length-limit-error branch June 8, 2022 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants