-
Notifications
You must be signed in to change notification settings - Fork 166
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
feat: add layer that limits body size #271
Conversation
Without having looked at the implementation or considering any previous disussion around this, this seems like an obvious 'Yes' to me. |
Yes I think we should. Except we can't do that unless the request has a |
If you take a look at how this is set up right now, in combination with |
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: ```rust use http_body::Limited; use tower_http::map_request_body::MapRequestBodyLayer; Router::new() .route("/", post(|body: Bytes| { ... })) .layer(MapRequestBodyLayer::new(|body| { Limited::new(body, 1024) })); ``` This is a draft until tower-rs/tower-http#271 is merged because I wanna make sure that works as well. The use case I have in mind is: ```rust use http_body::Limited; use tower_http::limit::RequestBodyLimitLayer; Router::new() .route("/", post(|body: Bytes| { ... })) .layer(RequestBodyLimitLayer::new(1024)); ```
This removes the `StdError + 'static` bound on the service error. Also sets the read limit for a given message to the lesser of `Content-Length` or the configured limit.
I made some changes based on the feedback. Now the future doesn't attempt to interpret the error returned by the underlying service. This removes the This now also interprets the Doc examples have been updated as well. |
Amazing! Thank you for working on this. I'll review it this week :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neoeinstein I think it looks good! I pushed a bunch of nit picks but had one question I'd like to hear what you think about.
@neoeinstein Thanks for working on this! |
Motivation
To provide a tower layer that limits the allowable length of the request body to a certain amount.
Solution
Add a new tower layer that wraps the incoming body in
http_body::Limited<B>
.A couple of decisions: