mirror of https://github.com/fafhrd91/actix-web
header extractor
This commit is contained in:
parent
db2b062c13
commit
ca71b8ce59
|
@ -231,45 +231,6 @@ impl FromRequest for () {
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! header_from_req {
|
||||
( $( $header:ident ),* ) => {
|
||||
$(
|
||||
impl FromRequest for crate::http::header::$header {
|
||||
type Error = actix_http::error::ParseError;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
let header = crate::http::header::Header::parse(req);
|
||||
ready(header)
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
header_from_req! {
|
||||
IfMatch,
|
||||
IfNoneMatch,
|
||||
IfRange,
|
||||
Accept,
|
||||
AcceptCharset,
|
||||
AcceptLanguage,
|
||||
Allow,
|
||||
CacheControl,
|
||||
ContentDisposition,
|
||||
ContentLanguage,
|
||||
ContentRange,
|
||||
ContentType,
|
||||
Date,
|
||||
ETag,
|
||||
Expires,
|
||||
IfModifiedSince,
|
||||
IfUnmodifiedSince,
|
||||
LastModified
|
||||
}
|
||||
|
||||
macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||
|
||||
// This module is a trick to get around the inability of
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
use std::{fmt, ops};
|
||||
|
||||
use futures_util::future::{err, ok, Ready};
|
||||
|
||||
use crate::dev::Payload;
|
||||
use crate::error::ParseError;
|
||||
use crate::extract::FromRequest;
|
||||
use crate::http::header;
|
||||
use crate::HttpRequest;
|
||||
|
||||
/// Header extractor and responder.
|
||||
pub struct Header<T>(pub T);
|
||||
|
||||
impl<T> Header<T> {
|
||||
/// Unwrap into inner `T` value.
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ops::Deref for Header<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ops::DerefMut for Header<T> {
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for Header<T>
|
||||
where
|
||||
T: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Header: {:?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for Header<T>
|
||||
where
|
||||
T: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// See [here](#extractor) for example of usage as an extractor.
|
||||
impl<T> FromRequest for Header<T>
|
||||
where
|
||||
T: header::Header,
|
||||
{
|
||||
type Error = ParseError;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
match header::Header::parse(req) {
|
||||
Ok(header) => ok(Header(header)),
|
||||
Err(e) => err(e),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
// TODO: review visibility
|
||||
mod either;
|
||||
pub(crate) mod form;
|
||||
mod header;
|
||||
pub(crate) mod json;
|
||||
mod path;
|
||||
pub(crate) mod payload;
|
||||
|
@ -11,6 +12,7 @@ pub(crate) mod readlines;
|
|||
|
||||
pub use self::either::{Either, EitherExtractError};
|
||||
pub use self::form::{Form, FormConfig};
|
||||
pub use self::header::Header;
|
||||
pub use self::json::{Json, JsonConfig};
|
||||
pub use self::path::{Path, PathConfig};
|
||||
pub use self::payload::{Payload, PayloadConfig};
|
||||
|
|
Loading…
Reference in New Issue