From 955715eea67aa2cc55d58b88b7993c20ad5dea82 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sat, 16 May 2020 14:26:21 +0200 Subject: [PATCH] I don't know how I ended up with spaces --- src/cors.rs | 42 ++++++++++---------- src/matcher/accept.rs | 2 +- src/matcher/access_control_request_method.rs | 2 +- src/matcher/content_type.rs | 2 +- tests/cors_handling.rs | 4 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/cors.rs b/src/cors.rs index f9f8170..46af65c 100644 --- a/src/cors.rs +++ b/src/cors.rs @@ -1,8 +1,8 @@ use crate::matcher::AccessControlRequestMethodMatcher; use gotham::{ - handler::HandlerFuture, + handler::HandlerFuture, helpers::http::response::create_empty_response, - hyper::{ + hyper::{ header::{ ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_METHOD, ORIGIN, VARY, @@ -10,10 +10,10 @@ use gotham::{ }, Body, Method, Response, StatusCode }, - middleware::Middleware, + middleware::Middleware, pipeline::chain::PipelineHandleChain, router::builder::*, - state::{FromState, State}, + state::{FromState, State}, }; use itertools::Itertools; use std::{ @@ -30,11 +30,11 @@ allowed to make the request. pub enum Origin { /// Do not send any `Access-Control-Allow-Origin` headers. - None, + None, /// Send `Access-Control-Allow-Origin: *`. Note that browser will not send credentials. - Star, + Star, /// Set the `Access-Control-Allow-Origin` header to a single origin. - Single(String), + Single(String), /// Copy the `Origin` header into the `Access-Control-Allow-Origin` header. Copy } @@ -75,7 +75,7 @@ To change settings, you need to put this type into gotham's [`State`]: # use gotham_restful::*; fn main() { let cors = CorsConfig { - origin: Origin::Star, + origin: Origin::Star, ..Default::default() }; let (chain, pipelines) = single_pipeline(new_pipeline().add(cors).build()); @@ -140,13 +140,13 @@ pub struct CorsConfig impl Middleware for CorsConfig { - fn call(self, mut state : State, chain : Chain) -> Pin> - where - Chain : FnOnce(State) -> Pin> - { - state.put(self); - chain(state) - } + fn call(self, mut state : State, chain : Chain) -> Pin> + where + Chain : FnOnce(State) -> Pin> + { + state.put(self); + chain(state) + } } /** @@ -166,7 +166,7 @@ pub fn handle_cors(state : &State, res : &mut Response) let config = CorsConfig::try_borrow_from(state); let headers = res.headers_mut(); - // non-preflight requests require the Access-Control-Allow-Origin header + // non-preflight requests require the Access-Control-Allow-Origin header if let Some(header) = config.and_then(|cfg| cfg.origin.header_value(state)) { headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, header); @@ -207,7 +207,7 @@ pub fn handle_cors(state : &State, res : &mut Response) pub trait CorsRoute where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, - P : RefUnwindSafe + Send + Sync + 'static + P : RefUnwindSafe + Send + Sync + 'static { /// Handle a preflight request on `path` for `method`. To configure the behaviour, use /// [`CorsConfig`](struct.CorsConfig.html). @@ -252,14 +252,14 @@ fn cors_preflight_handler(state : State) -> (State, Response) impl CorsRoute for D where D : DrawRoutes, - C : PipelineHandleChain

+ Copy + Send + Sync + 'static, - P : RefUnwindSafe + Send + Sync + 'static + C : PipelineHandleChain

+ Copy + Send + Sync + 'static, + P : RefUnwindSafe + Send + Sync + 'static { fn cors(&mut self, path : &str, method : Method) { let matcher = AccessControlRequestMethodMatcher::new(method); self.options(path) - .extend_route_matcher(matcher) - .to(cors_preflight_handler); + .extend_route_matcher(matcher) + .to(cors_preflight_handler); } } diff --git a/src/matcher/accept.rs b/src/matcher/accept.rs index 2e89ac2..9aa1d91 100644 --- a/src/matcher/accept.rs +++ b/src/matcher/accept.rs @@ -76,7 +76,7 @@ let matcher = AcceptHeaderMatcher::new(types); # build_simple_router(|route| { // use the matcher for your request route.post("/foo") - .extend_route_matcher(matcher) + .extend_route_matcher(matcher) .to(|state| { // we know that the client is a modern browser and can handle webp images # let IMAGE_WEBP : mime::Mime = "image/webp".parse().unwrap(); diff --git a/src/matcher/access_control_request_method.rs b/src/matcher/access_control_request_method.rs index 6c912cc..e356c0a 100644 --- a/src/matcher/access_control_request_method.rs +++ b/src/matcher/access_control_request_method.rs @@ -48,7 +48,7 @@ impl RouteMatcher for AccessControlRequestMethodMatcher { match HeaderMap::borrow_from(state).get(ACCESS_CONTROL_REQUEST_METHOD) .and_then(|value| value.to_str().ok()) - .and_then(|str| str.parse::().ok()) + .and_then(|str| str.parse::().ok()) { Some(m) if m == self.method => Ok(()), _ => Err(RouteNonMatch::new(StatusCode::NOT_FOUND)) diff --git a/src/matcher/content_type.rs b/src/matcher/content_type.rs index d33ed1a..aca8b9a 100644 --- a/src/matcher/content_type.rs +++ b/src/matcher/content_type.rs @@ -26,7 +26,7 @@ let matcher = ContentTypeMatcher::new(types) # build_simple_router(|route| { // use the matcher for your request route.post("/foo") - .extend_route_matcher(matcher) + .extend_route_matcher(matcher) .to(|state| { let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, "Correct Content Type!"); (state, res) diff --git a/tests/cors_handling.rs b/tests/cors_handling.rs index a9fe498..24450de 100644 --- a/tests/cors_handling.rs +++ b/tests/cors_handling.rs @@ -50,8 +50,8 @@ fn test_preflight(server : &TestServer, method : &str, origin : Option<&str>, va { let res = server.client().options("http://example.org/foo") .with_header(ACCESS_CONTROL_REQUEST_METHOD, method.parse().unwrap()) - .with_header(ORIGIN, "http://example.org".parse().unwrap()) - .perform().unwrap(); + .with_header(ORIGIN, "http://example.org".parse().unwrap()) + .perform().unwrap(); assert_eq!(res.status(), StatusCode::NO_CONTENT); let headers = res.headers(); println!("{}", headers.keys().join(","));