1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

I don't know how I ended up with spaces

This commit is contained in:
Dominic 2020-05-16 14:26:21 +02:00
parent dc26e9a02e
commit 955715eea6
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
5 changed files with 26 additions and 26 deletions

View file

@ -1,8 +1,8 @@
use crate::matcher::AccessControlRequestMethodMatcher; use crate::matcher::AccessControlRequestMethodMatcher;
use gotham::{ use gotham::{
handler::HandlerFuture, handler::HandlerFuture,
helpers::http::response::create_empty_response, helpers::http::response::create_empty_response,
hyper::{ hyper::{
header::{ header::{
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, 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, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_METHOD, ORIGIN, VARY,
@ -10,10 +10,10 @@ use gotham::{
}, },
Body, Method, Response, StatusCode Body, Method, Response, StatusCode
}, },
middleware::Middleware, middleware::Middleware,
pipeline::chain::PipelineHandleChain, pipeline::chain::PipelineHandleChain,
router::builder::*, router::builder::*,
state::{FromState, State}, state::{FromState, State},
}; };
use itertools::Itertools; use itertools::Itertools;
use std::{ use std::{
@ -30,11 +30,11 @@ allowed to make the request.
pub enum Origin pub enum Origin
{ {
/// Do not send any `Access-Control-Allow-Origin` headers. /// Do not send any `Access-Control-Allow-Origin` headers.
None, None,
/// Send `Access-Control-Allow-Origin: *`. Note that browser will not send credentials. /// 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. /// 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 the `Origin` header into the `Access-Control-Allow-Origin` header.
Copy Copy
} }
@ -75,7 +75,7 @@ To change settings, you need to put this type into gotham's [`State`]:
# use gotham_restful::*; # use gotham_restful::*;
fn main() { fn main() {
let cors = CorsConfig { let cors = CorsConfig {
origin: Origin::Star, origin: Origin::Star,
..Default::default() ..Default::default()
}; };
let (chain, pipelines) = single_pipeline(new_pipeline().add(cors).build()); let (chain, pipelines) = single_pipeline(new_pipeline().add(cors).build());
@ -140,13 +140,13 @@ pub struct CorsConfig
impl Middleware for CorsConfig impl Middleware for CorsConfig
{ {
fn call<Chain>(self, mut state : State, chain : Chain) -> Pin<Box<HandlerFuture>> fn call<Chain>(self, mut state : State, chain : Chain) -> Pin<Box<HandlerFuture>>
where where
Chain : FnOnce(State) -> Pin<Box<HandlerFuture>> Chain : FnOnce(State) -> Pin<Box<HandlerFuture>>
{ {
state.put(self); state.put(self);
chain(state) chain(state)
} }
} }
/** /**
@ -166,7 +166,7 @@ pub fn handle_cors(state : &State, res : &mut Response<Body>)
let config = CorsConfig::try_borrow_from(state); let config = CorsConfig::try_borrow_from(state);
let headers = res.headers_mut(); 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)) if let Some(header) = config.and_then(|cfg| cfg.origin.header_value(state))
{ {
headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, header); headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, header);
@ -207,7 +207,7 @@ pub fn handle_cors(state : &State, res : &mut Response<Body>)
pub trait CorsRoute<C, P> pub trait CorsRoute<C, P>
where where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static, C : PipelineHandleChain<P> + 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 /// Handle a preflight request on `path` for `method`. To configure the behaviour, use
/// [`CorsConfig`](struct.CorsConfig.html). /// [`CorsConfig`](struct.CorsConfig.html).
@ -252,14 +252,14 @@ fn cors_preflight_handler(state : State) -> (State, Response<Body>)
impl<D, C, P> CorsRoute<C, P> for D impl<D, C, P> CorsRoute<C, P> for D
where where
D : DrawRoutes<C, P>, D : DrawRoutes<C, P>,
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static, C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static P : RefUnwindSafe + Send + Sync + 'static
{ {
fn cors(&mut self, path : &str, method : Method) fn cors(&mut self, path : &str, method : Method)
{ {
let matcher = AccessControlRequestMethodMatcher::new(method); let matcher = AccessControlRequestMethodMatcher::new(method);
self.options(path) self.options(path)
.extend_route_matcher(matcher) .extend_route_matcher(matcher)
.to(cors_preflight_handler); .to(cors_preflight_handler);
} }
} }

View file

@ -76,7 +76,7 @@ let matcher = AcceptHeaderMatcher::new(types);
# build_simple_router(|route| { # build_simple_router(|route| {
// use the matcher for your request // use the matcher for your request
route.post("/foo") route.post("/foo")
.extend_route_matcher(matcher) .extend_route_matcher(matcher)
.to(|state| { .to(|state| {
// we know that the client is a modern browser and can handle webp images // we know that the client is a modern browser and can handle webp images
# let IMAGE_WEBP : mime::Mime = "image/webp".parse().unwrap(); # let IMAGE_WEBP : mime::Mime = "image/webp".parse().unwrap();

View file

@ -48,7 +48,7 @@ impl RouteMatcher for AccessControlRequestMethodMatcher
{ {
match HeaderMap::borrow_from(state).get(ACCESS_CONTROL_REQUEST_METHOD) match HeaderMap::borrow_from(state).get(ACCESS_CONTROL_REQUEST_METHOD)
.and_then(|value| value.to_str().ok()) .and_then(|value| value.to_str().ok())
.and_then(|str| str.parse::<Method>().ok()) .and_then(|str| str.parse::<Method>().ok())
{ {
Some(m) if m == self.method => Ok(()), Some(m) if m == self.method => Ok(()),
_ => Err(RouteNonMatch::new(StatusCode::NOT_FOUND)) _ => Err(RouteNonMatch::new(StatusCode::NOT_FOUND))

View file

@ -26,7 +26,7 @@ let matcher = ContentTypeMatcher::new(types)
# build_simple_router(|route| { # build_simple_router(|route| {
// use the matcher for your request // use the matcher for your request
route.post("/foo") route.post("/foo")
.extend_route_matcher(matcher) .extend_route_matcher(matcher)
.to(|state| { .to(|state| {
let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, "Correct Content Type!"); let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, "Correct Content Type!");
(state, res) (state, res)

View file

@ -50,8 +50,8 @@ fn test_preflight(server : &TestServer, method : &str, origin : Option<&str>, va
{ {
let res = server.client().options("http://example.org/foo") let res = server.client().options("http://example.org/foo")
.with_header(ACCESS_CONTROL_REQUEST_METHOD, method.parse().unwrap()) .with_header(ACCESS_CONTROL_REQUEST_METHOD, method.parse().unwrap())
.with_header(ORIGIN, "http://example.org".parse().unwrap()) .with_header(ORIGIN, "http://example.org".parse().unwrap())
.perform().unwrap(); .perform().unwrap();
assert_eq!(res.status(), StatusCode::NO_CONTENT); assert_eq!(res.status(), StatusCode::NO_CONTENT);
let headers = res.headers(); let headers = res.headers();
println!("{}", headers.keys().join(",")); println!("{}", headers.keys().join(","));