-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New Module For Retrieving Headers #1283
Comments
Although this example is useful as-is and makes sense in a utility module or crate, I think for inclusion in A feature like this could also be a really nice application of const generics (still quite a ways away from being stable, I fear): const CONTENT_LENGTH: &str = "Content-Length";
const CONTENT_TYPE: &str = "Content-Type";
struct Header<'a, const NAME: &str>(value: &'a str);
impl<'a, 'r, const NAME> FromRequest<'a, 'r> for Header<'a, NAME> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
request.headers().get_one(NAME).expect("TODO: example")
}
}
#[get("/")]
fn index(ct: Header<CONTENT_TYPE>) -> String {
ct.0.to_string()
} One upside of an approach like this is that |
I agree that using constant generics would be nice. I suppose you could close this issue or let things settle with the async migration and go from there? Thanks for the response. |
I know Rust 1.51 is still a ways out but it sounds like the goal is to get the |
Unfortunately not as far as that design: I am still in favor of this idea in general. #1067 already tracks the conversion from a typed header library to Rocket's header type which is most useful for responses. I think we should either 1) keep this issue open for implementing |
Closing in favor of #1067. |
Idea
While working through a small application, I wanted to fetch a request header to do some inspection and respond accordingly. I looked through existing issues and comments in this repository saying to use the
FromRequest
trait for the desired header. This was pretty straightforward and I was wondering if it would be useful to have a new module incontrib
that implemented this as a macro for various common headers? This way, developers could use them as request guards to fetch headers that they want to use.Here is how I used it to fetch the user agent. In this case, I failed if the header wasn't there but in the implementation, we can use an
Option
and always succeed.Implementation Idea
This is roughly what I think it could look like. Keep in mind this won't compile as is.
The text was updated successfully, but these errors were encountered: