mirror of
https://github.com/actix/actix-web.git
synced 2024-12-24 00:50:36 +00:00
add Host predicate
This commit is contained in:
parent
e3cd0fdd13
commit
42f9e1034b
2 changed files with 62 additions and 1 deletions
|
@ -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();
|
||||||
/// # }
|
/// # }
|
||||||
|
|
61
src/pred.rs
61
src/pred.rs
|
@ -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(
|
||||||
|
|
Loading…
Reference in a new issue