1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-25 19:11:10 +00:00

add GuardContext::header (#2569)

This commit is contained in:
Rob Ede 2022-01-05 11:47:14 +00:00 committed by GitHub
parent fe0bbfb3da
commit 4ebf16890d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View file

@ -1,10 +1,14 @@
# Changes
## Unreleased - 2021-xx-xx
### Changed
- `HttpResponse` can now be used as a `Responder` with any body type.
### Added
- `GuardContext::header` [#2569]
[#2501]: https://github.com/actix/actix-web/pull/2501
### Changed
- `HttpResponse` can now be used as a `Responder` with any body type. [#2567]
[#2567]: https://github.com/actix/actix-web/pull/2567
[#2569]: https://github.com/actix/actix-web/pull/2569
## 4.0.0-beta.19 - 2022-01-04

View file

@ -54,7 +54,7 @@ use std::{
use actix_http::{header, uri::Uri, Extensions, Method as HttpMethod, RequestHead};
use crate::service::ServiceRequest;
use crate::{http::header::Header, service::ServiceRequest};
/// Provides access to request parts that are useful during routing.
#[derive(Debug)]
@ -80,6 +80,26 @@ impl<'a> GuardContext<'a> {
pub fn req_data_mut(&self) -> RefMut<'a, Extensions> {
self.req.req_data_mut()
}
/// Extracts a typed header from the request.
///
/// Returns `None` if parsing `H` fails.
///
/// # Examples
/// ```
/// use actix_web::{guard::fn_guard, http::header};
///
/// let image_accept_guard = fn_guard(|ctx| {
/// match ctx.header::<header::Accept>() {
/// Some(hdr) => hdr.preference() == "image/*",
/// None => false,
/// }
/// });
/// ```
#[inline]
pub fn header<H: Header>(&self) -> Option<H> {
H::parse(self.req).ok()
}
}
/// Interface for routing guards.

View file

@ -2,10 +2,10 @@ use std::cmp::Ordering;
use mime::Mime;
use super::QualityItem;
use super::{common_header, QualityItem};
use crate::http::header;
crate::http::header::common_header! {
common_header! {
/// `Accept` header, defined
/// in [RFC 7231 §5.3.2](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2)
///