1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00
actix-extras/src/pred.rs

292 lines
8.8 KiB
Rust
Raw Normal View History

2017-12-04 22:32:05 +01:00
//! Route match predicates
#![allow(non_snake_case)]
use std::marker::PhantomData;
use http;
use http::{header, HttpTryFrom};
use httpmessage::HttpMessage;
2017-12-04 22:32:05 +01:00
use httprequest::HttpRequest;
/// Trait defines resource route predicate.
/// Predicate can modify request object. It is also possible to
/// to store extra attributes on request by using `.extensions()` method.
pub trait Predicate<S> {
/// Check if request matches predicate
fn check(&self, &mut HttpRequest<S>) -> bool;
}
/// Return predicate that matches if any of supplied predicate matches.
2017-12-20 22:23:50 +01:00
///
/// ```rust
/// # extern crate actix_web;
2018-03-31 09:16:55 +02:00
/// use actix_web::{pred, App, HttpResponse};
2017-12-20 22:23:50 +01:00
///
/// fn main() {
2018-03-31 09:16:55 +02:00
/// App::new()
2017-12-20 22:23:50 +01:00
/// .resource("/index.html", |r| r.route()
2018-03-02 03:32:31 +01:00
/// .filter(pred::Any(pred::Get()).or(pred::Post()))
/// .f(|r| HttpResponse::MethodNotAllowed()));
2017-12-20 22:23:50 +01:00
/// }
/// ```
pub fn Any<S: 'static, P: Predicate<S> + 'static>(pred: P) -> AnyPredicate<S>
2017-12-04 22:32:05 +01:00
{
2017-12-20 22:23:50 +01:00
AnyPredicate(vec![Box::new(pred)])
2017-12-04 22:32:05 +01:00
}
2017-12-20 22:23:50 +01:00
/// Matches if any of supplied predicate matches.
pub struct AnyPredicate<S>(Vec<Box<Predicate<S>>>);
impl<S> AnyPredicate<S> {
/// Add new predicate to list of predicates to check
pub fn or<P: Predicate<S> + 'static>(mut self, pred: P) -> Self {
self.0.push(Box::new(pred));
self
}
}
2017-12-04 22:32:05 +01:00
impl<S: 'static> Predicate<S> for AnyPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
for p in &self.0 {
if p.check(req) {
return true
}
}
false
}
}
/// Return predicate that matches if all of supplied predicate matches.
2017-12-20 22:23:50 +01:00
///
/// ```rust
/// # extern crate actix_web;
/// use actix_web::{pred, Application, HttpResponse};
2017-12-20 22:23:50 +01:00
///
/// fn main() {
/// Application::new()
/// .resource("/index.html", |r| r.route()
2018-03-02 03:32:31 +01:00
/// .filter(pred::All(pred::Get())
2017-12-20 22:23:50 +01:00
/// .and(pred::Header("content-type", "plain/text")))
/// .f(|_| HttpResponse::MethodNotAllowed()));
2017-12-20 22:23:50 +01:00
/// }
/// ```
pub fn All<S: 'static, P: Predicate<S> + 'static>(pred: P) -> AllPredicate<S> {
AllPredicate(vec![Box::new(pred)])
2017-12-04 22:32:05 +01:00
}
2017-12-20 22:23:50 +01:00
/// Matches if all of supplied predicate matches.
pub struct AllPredicate<S>(Vec<Box<Predicate<S>>>);
impl<S> AllPredicate<S> {
/// Add new predicate to list of predicates to check
pub fn and<P: Predicate<S> + 'static>(mut self, pred: P) -> Self {
self.0.push(Box::new(pred));
self
}
}
2017-12-04 22:32:05 +01:00
impl<S: 'static> Predicate<S> for AllPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
for p in &self.0 {
if !p.check(req) {
return false
}
}
true
}
}
/// Return predicate that matches if supplied predicate does not match.
2017-12-20 22:23:50 +01:00
pub fn Not<S: 'static, P: Predicate<S> + 'static>(pred: P) -> NotPredicate<S>
2017-12-04 22:32:05 +01:00
{
2017-12-20 22:23:50 +01:00
NotPredicate(Box::new(pred))
2017-12-04 22:32:05 +01:00
}
2017-12-20 22:23:50 +01:00
#[doc(hidden)]
pub struct NotPredicate<S>(Box<Predicate<S>>);
2017-12-04 22:32:05 +01:00
impl<S: 'static> Predicate<S> for NotPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
!self.0.check(req)
}
}
/// Http method predicate
2017-12-20 22:23:50 +01:00
#[doc(hidden)]
pub struct MethodPredicate<S>(http::Method, PhantomData<S>);
2017-12-04 22:32:05 +01:00
impl<S: 'static> Predicate<S> for MethodPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
*req.method() == self.0
}
}
/// Predicate to match *GET* http method
2017-12-20 22:23:50 +01:00
pub fn Get<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::GET, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *POST* http method
2017-12-20 22:23:50 +01:00
pub fn Post<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::POST, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *PUT* http method
2017-12-20 22:23:50 +01:00
pub fn Put<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::PUT, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *DELETE* http method
2017-12-20 22:23:50 +01:00
pub fn Delete<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::DELETE, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *HEAD* http method
2017-12-20 22:23:50 +01:00
pub fn Head<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::HEAD, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *OPTIONS* http method
2017-12-20 22:23:50 +01:00
pub fn Options<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::OPTIONS, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *CONNECT* http method
2017-12-20 22:23:50 +01:00
pub fn Connect<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::CONNECT, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *PATCH* http method
2017-12-20 22:23:50 +01:00
pub fn Patch<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::PATCH, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match *TRACE* http method
2017-12-20 22:23:50 +01:00
pub fn Trace<S: 'static>() -> MethodPredicate<S> {
MethodPredicate(http::Method::TRACE, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Predicate to match specified http method
2017-12-20 22:23:50 +01:00
pub fn Method<S: 'static>(method: http::Method) -> MethodPredicate<S> {
MethodPredicate(method, PhantomData)
2017-12-04 22:32:05 +01:00
}
/// Return predicate that matches if request contains specified header and value.
2017-12-20 22:23:50 +01:00
pub fn Header<S: 'static>(name: &'static str, value: &'static str) -> HeaderPredicate<S>
2017-12-04 22:32:05 +01:00
{
2017-12-20 22:23:50 +01:00
HeaderPredicate(header::HeaderName::try_from(name).unwrap(),
header::HeaderValue::from_static(value),
PhantomData)
2017-12-04 22:32:05 +01:00
}
2017-12-20 22:23:50 +01:00
#[doc(hidden)]
pub struct HeaderPredicate<S>(header::HeaderName, header::HeaderValue, PhantomData<S>);
2017-12-04 22:32:05 +01:00
impl<S: 'static> Predicate<S> for HeaderPredicate<S> {
fn check(&self, req: &mut HttpRequest<S>) -> bool {
if let Some(val) = req.headers().get(&self.0) {
return val == self.1
}
false
}
}
2017-12-08 21:51:44 +01:00
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use http::{Uri, Version, Method};
use http::header::{self, HeaderMap};
#[test]
fn test_header() {
let mut headers = HeaderMap::new();
headers.insert(header::TRANSFER_ENCODING,
header::HeaderValue::from_static("chunked"));
let mut req = HttpRequest::new(
2017-12-13 17:00:25 +01:00
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
2017-12-08 21:51:44 +01:00
let pred = Header("transfer-encoding", "chunked");
assert!(pred.check(&mut req));
let pred = Header("transfer-encoding", "other");
assert!(!pred.check(&mut req));
2017-12-09 00:25:37 +01:00
let pred = Header("content-type", "other");
2017-12-09 00:25:37 +01:00
assert!(!pred.check(&mut req));
2017-12-08 21:51:44 +01:00
}
#[test]
fn test_methods() {
let mut req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
let mut req2 = HttpRequest::new(
Method::POST, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Get().check(&mut req));
assert!(!Get().check(&mut req2));
assert!(Post().check(&mut req2));
assert!(!Post().check(&mut req));
let mut r = HttpRequest::new(
Method::PUT, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Put().check(&mut r));
assert!(!Put().check(&mut req));
let mut r = HttpRequest::new(
Method::DELETE, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Delete().check(&mut r));
assert!(!Delete().check(&mut req));
let mut r = HttpRequest::new(
Method::HEAD, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Head().check(&mut r));
assert!(!Head().check(&mut req));
let mut r = HttpRequest::new(
Method::OPTIONS, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Options().check(&mut r));
assert!(!Options().check(&mut req));
let mut r = HttpRequest::new(
Method::CONNECT, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Connect().check(&mut r));
assert!(!Connect().check(&mut req));
let mut r = HttpRequest::new(
Method::PATCH, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Patch().check(&mut r));
assert!(!Patch().check(&mut req));
let mut r = HttpRequest::new(
Method::TRACE, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Trace().check(&mut r));
assert!(!Trace().check(&mut req));
}
#[test]
fn test_preds() {
let mut r = HttpRequest::new(
Method::TRACE, Uri::from_str("/").unwrap(),
2017-12-13 17:00:25 +01:00
Version::HTTP_11, HeaderMap::new(), None);
2017-12-08 21:51:44 +01:00
assert!(Not(Get()).check(&mut r));
assert!(!Not(Trace()).check(&mut r));
2017-12-20 22:23:50 +01:00
assert!(All(Trace()).and(Trace()).check(&mut r));
assert!(!All(Get()).and(Trace()).check(&mut r));
2017-12-08 21:51:44 +01:00
2017-12-20 22:23:50 +01:00
assert!(Any(Get()).or(Trace()).check(&mut r));
assert!(!Any(Get()).or(Get()).check(&mut r));
2017-12-08 21:51:44 +01:00
}
}