1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-24 17:10:33 +00:00

add Host predicate

This commit is contained in:
Nikolay Kim 2018-06-07 20:00:54 -07:00
parent e3cd0fdd13
commit 42f9e1034b
2 changed files with 62 additions and 1 deletions

View file

@ -306,7 +306,7 @@ where
/// # use actix_web::*; /// # use actix_web::*;
/// # fn main() { /// # fn main() {
/// App::new() /// App::new()
/// .filter(pred::Get()) /// .filter(pred::Hoat("www.rust-lang.org"))
/// .resource("/path", |r| r.f(|_| HttpResponse::Ok())) /// .resource("/path", |r| r.f(|_| HttpResponse::Ok()))
/// # .finish(); /// # .finish();
/// # } /// # }

View file

@ -192,6 +192,45 @@ impl<S: 'static> Predicate<S> for HeaderPredicate<S> {
} }
} }
/// Return predicate that matches if request contains specified Host name.
///
/// ```rust
/// # extern crate actix_web;
/// use actix_web::{pred, App, HttpResponse};
///
/// fn main() {
/// App::new().resource("/index.html", |r| {
/// r.route()
/// .filter(pred::Host("www.rust-lang.org"))
/// .f(|_| HttpResponse::MethodNotAllowed())
/// });
/// }
/// ```
pub fn Host<S: 'static, H: AsRef<str>>(host: H) -> HostPredicate<S> {
HostPredicate(host.as_ref().to_string(), None, PhantomData)
}
#[doc(hidden)]
pub struct HostPredicate<S>(String, Option<String>, PhantomData<S>);
impl<S> HostPredicate<S> {
/// Set reuest scheme to match
pub fn scheme<H: AsRef<str>>(&mut self, scheme: H) {
self.1 = Some(scheme.as_ref().to_string())
}
}
impl<S: 'static> Predicate<S> for HostPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
let info = req.connection_info();
if let Some(ref scheme) = self.1 {
self.0 == info.host() && scheme == info.scheme()
} else {
self.0 == info.host()
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -224,6 +263,28 @@ mod tests {
assert!(!pred.check(&mut req)); assert!(!pred.check(&mut req));
} }
#[test]
fn test_host() {
let mut headers = HeaderMap::new();
headers.insert(
header::HOST,
header::HeaderValue::from_static("www.rust-lang.org"),
);
let mut req = HttpRequest::new(
Method::GET,
Uri::from_str("/").unwrap(),
Version::HTTP_11,
headers,
None,
);
let pred = Host("www.rust-lang.org");
assert!(pred.check(&mut req));
let pred = Host("localhost");
assert!(!pred.check(&mut req));
}
#[test] #[test]
fn test_methods() { fn test_methods() {
let mut req = HttpRequest::new( let mut req = HttpRequest::new(