-
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
TypedHeader shouldn't reject missing headers by default #1781
Comments
Seems reasonable! |
Workaround for tokio-rs/axum#1781.
axum just calls |
Seems like it, that optimization (?) of returning |
Traced it down through git blame to hyperium/headers@1150ca8. Looks like the original signatures of those extension methods were returning Not sure why - maybe a refactoring oversight, or maybe indeed an optimisation, but either way I don't think that |
That makes sense. Do you wanna try and make a PR to headers? Or at least an issue? |
Yeah I can, I suppose it would have to a feature request for a new method though since |
cc @seanmonstar |
Created upstream feature request at hyperium/headers#133. |
Should we close this? Or is there something we should do in axum? |
Well, we still need to use the new functionality from the |
It should be pretty easy to do this even without waiting for headers crate update FWIW. If that's something you're open to, I'm happy to make a PR. |
Sure! Would it be a breaking change? Or does that depend on the specific |
Yeah, this. Also worth noting it's breaking in the sense that "something that used to reject the request before now might succeed", which might be potentially problematic in some scenarios where someone was counting on that error, but seems low-risk. |
Right. Let's merge it into the 0.7.0 branch for now then. Hyper 1.0 hopefully isn't too far off anyway. |
Okay. I guess if we can do breaking changes, then I can also remove the |
Yeah we could make other changes as well, though I'm not quite sure what the implications of that would be. We obviously want to avoid panics. |
Oh yeah I'm just thinking whether it's worth the extra effort to continue reporting missing headers as a special case... I guess let me prototype it in a PR and we can discuss further over there. |
Reports a missing header rejection only if the header's decoder didn't return a default value. Closes tokio-rs#1781.
Ok opened a PR with a quick fix. In retrospect, I'm tempted to say this is not a breaking change after all, but rather a bugfix (with the usual caveat that someone might have started relying on a bug, but I don't think that constitutes a breaking change?). Up to you though. |
Workaround for tokio-rs/axum#1781.
Hmm, this change now means that |
FWIW, |
Hm yeah I'd say you'd then want Option itself to implement FromRequest differently (check presence first, parse later). That said, in pretty much all real-world scenarios you shouldn't distinguish between the header's default value and the missing header if the header is specified in such a way that missing header is equivalent to some default value. |
Feature Request
TypedHeader should allow specific Header type to handle missing headers instead of immediately rejecting the request.
Motivation
Header
trait already allows to decode headers from arbitrary iterators, including iterators with zero, one or more header value repetitions.Some
Header
implementations require their header value to be always present, in which caseHeader::decode
will return an error anyway, while many other headers might be optional by design, in which case theirHeader::decode
will correctly handle empty iterator and return some default parsed value (e.g. empty collection).However, right now axum's
TypedHeader
checks for header name's presence in the request and, if it's not there, immediately rejects the request, not giving thoseHeader::decode
implementation a chance to gracefully handle the empty case.Proposal
Forward empty iterator to the
<H as Header>::decode
and use its result instead of immediately rejecting the request. This will correctly handle both mandatory headers (as those will still return an error) as well as optional ones.Alternatives
If this is considered undesirable for some reason, another alternative could be to implement a custom extractor for
Option<TypedHeader<H>>
. It won't be ideal as the user will have to manually invokeH::decode
again to get the correct default value, but might be less of a breaking change.The text was updated successfully, but these errors were encountered: