-
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
Change HeaderMap
extractor to clone the headers
#698
Conversation
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 like the various code simplifications! I think we should be consistent and do the same thing for extensions, as well as adding some warnings about the mostly-unnecessary cloning (of headers you're not interested in) to the affected FromRequest
impls.
Would there be the possibility of having |
Hm not sure if it's worth it since it's only the |
I believe most session implementations would depend on cookies or JWT obtained from the HeaderMap, and rarely if ever mutate the headers. As it stands, would this make every API call that uses sessions more expensive? |
Like I just wrote, no it won't. Only using If you need a single header for a handler you should use If you need a header in your own extractor you just call |
Thanks for the clarification! |
* Change `HeaderMap` extractor to clone the headers * fix docs * changelog * inline variable * also add changelog item to axum * don't list types from axum in axum-core's changelog * document that `HeaderMap::from_request` clones the headers * fix typo * a few more typos
* Change `HeaderMap` extractor to clone the headers * fix docs * changelog * inline variable * also add changelog item to axum * don't list types from axum in axum-core's changelog * document that `HeaderMap::from_request` clones the headers * fix typo * a few more typos
Since #698 this section about `HeaderMap` removing the headers from the request is no longer true.
Since #698 this section about `HeaderMap` removing the headers from the request is no longer true.
Since #698 this section about `HeaderMap` removing the headers from the request is no longer true.
Since #698 this section about `HeaderMap` removing the headers from the request is no longer true.
We often hear from users who are hit by
Headers taken by other extractor
errors because they usedHeaderMap
as an extractor combined withForm<T>
, or some other extractor that happens to use the headers.The reason
RequestParts::take_headers
exists in the first place is for performance reasons. But I think I've realized that it was probably a mistake because it causes issues like this. So I think we should change it!This PR removes
RequestParts::take_headers
and changes theHeaderMap
extractor to clone the headers. We could also consider other workarounds such as recording which extractor removed and which requires the headers, but I think this solution is the least surprising, and worth the small perf hit.If someone is really concerned about cloning the headers you can still write an extractor that does
request_parts.headers_mut().take()
which would leave an emptyHeaderMap
in place. I don't think axum should provide such an extractor though --- then the problem is just gonna stay.This PR doesn't touch "extensions already taken by another extractor" which is basically the same problem. I'm not very concerned about that since it feels more niche but probably worth looking into as well.
TODO