-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix: avoid setting content-length before middleware #2897
Conversation
Axum currently sets content-length automatically inside Route. However, if this occurs before middleware is run then should the middleware add a body to the request, Axum will avoid overwriting the content-length and so the user is stuck with an incorrect content-length, leading to panics in Hyper.
cb69cde
to
8615867
Compare
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.
I didn't dive into the details here, but nothing stands out as wrong and I'm pretty sure we have sufficient tests to cover this change (especially with you adding another one for the previous bug).
Do you want to add a changelog entry though? |
Yup, good point – added. |
Are you interested in backporting this to v0.7? I've tried, but failed. Maybe it makes sense to first backport #2894, which touched the same code 🤷🏼 |
I can try. I’ll be able to get to it hopefully in a couple days. |
Thanks! There's a |
BeforeAfterwas this change intended? 🤔 (this is from rust-lang/crates.io#9969) |
Chunked response bodies must not have any content length, right?
|
Yes, it's either content length or chunked bodies (or closing the connection after the request). But if the length is known we should use that so if we started using chunked transfer encoding instead of content length for a given request after the update, then that was not an intentional change. I would guess we're not setting the length properly in some case. |
Are we sure this needs to be in a minor release? We got some tests failing after moving to axum 0.7.9 that were dependant on having the Also it seems that the E.g. the following (pseudo-)code fails for me:
If I inject a |
I’m not goïng to comment on whether it’s a breaking change or not; I don’t have that much of an opinion.
This is the intended behaviour of the change. First of all, axum never changes My idea is that if you want to check the content length from middleware, what you should do is call |
Thanks. Yes, I agree that the new approach is more logical. But anyway I'd vote for such changes not to be backported between "major" versions. |
I get that. If it makes you feel better, this is something I considered as well. But given that this fixed a bug that could pretty badly affect services after a change like adding compression support, I decided that backporting it to v0.7 would be better, rather than rush out v0.8 and tell everybody to upgrade (there's still a bunch of things I'd like to get done before releasing v0.8). |
@jplatte Thanks, yes, probably that was worth some breakage :) |
I finally managed to get my hands on this and it seems that using Middleware is simply something like:
Before this PR extracting the
In Tower-like tests this works fine e.g.
Any idea what I did wrong here? Thanks in advance. |
Could you please open a new discussion and post a full reproducing example (repo including a cargo lockfile)? |
Motivation
Axum currently sets content-length automatically inside Route. However, if this occurs before middleware is run then should the middleware add a body to the request, Axum will avoid overwriting the content-length and so the user is stuck with an incorrect content-length, leading to panics in Hyper.
Solution
Only set content-length for top-level
Route
s.(there are a couple miscellaneous changes as well; one is a one-line change in
route.rs
to avoid double-boxing the body for middleware, another removes the unused enumRouteFutureKind
)