1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 10:27:42 +02:00

fix doctest ci (#188)

This commit is contained in:
Rob Ede
2021-06-27 07:02:38 +01:00
committed by GitHub
parent 64eec6e550
commit 20ef05c36e
23 changed files with 255 additions and 206 deletions

View File

@ -6,11 +6,9 @@ authors = [
"Rob Ede <robjtede@icloud.com>",
]
description = "Cross-Origin Resource Sharing (CORS) controls for Actix Web"
readme = "README.md"
keywords = ["actix", "cors", "web", "security", "crossorigin"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-extras.git"
documentation = "https://docs.rs/actix-cors"
repository = "https://github.com/actix/actix-extras"
license = "MIT OR Apache-2.0"
edition = "2018"
@ -19,14 +17,14 @@ name = "actix_cors"
path = "src/lib.rs"
[dependencies]
actix-web = { version = "4.0.0-beta.5", default-features = false }
actix-service = "2.0.0-beta.5"
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-service = "2.0.0"
derive_more = "0.99.5"
futures-util = { version = "0.3.7", default-features = false }
log = "0.4"
once_cell = "1"
tinyvec = { version = "1", features = ["alloc"] }
smallvec = "1.6"
[dev-dependencies]
actix-rt = "2"

View File

@ -9,7 +9,7 @@ use actix_web::{
use futures_util::future::{self, Ready};
use log::error;
use once_cell::sync::Lazy;
use tinyvec::tiny_vec;
use smallvec::smallvec;
use crate::{AllOrSome, CorsError, CorsMiddleware, Inner, OriginFn};
@ -82,7 +82,7 @@ impl Cors {
pub fn permissive() -> Self {
let inner = Inner {
allowed_origins: AllOrSome::All,
allowed_origins_fns: tiny_vec![],
allowed_origins_fns: smallvec![],
allowed_methods: ALL_METHODS_SET.clone(),
allowed_methods_baked: None,
@ -458,7 +458,7 @@ impl Default for Cors {
fn default() -> Cors {
let inner = Inner {
allowed_origins: AllOrSome::Some(HashSet::with_capacity(8)),
allowed_origins_fns: tiny_vec![],
allowed_origins_fns: smallvec![],
allowed_methods: HashSet::with_capacity(8),
allowed_methods_baked: None,
@ -483,13 +483,12 @@ impl Default for Cors {
}
}
impl<S, B> Transform<S, ServiceRequest> for Cors
impl<S> Transform<S, ServiceRequest> for Cors
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S: Service<ServiceRequest, Response = ServiceResponse, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse;
type Error = Error;
type InitError = ();
type Transform = CorsMiddleware<S>;

View File

@ -9,7 +9,7 @@ use actix_web::{
},
};
use once_cell::sync::Lazy;
use tinyvec::TinyVec;
use smallvec::SmallVec;
use crate::{AllOrSome, CorsError};
@ -42,7 +42,7 @@ fn header_value_try_into_method(hdr: &HeaderValue) -> Option<Method> {
#[derive(Debug, Clone)]
pub(crate) struct Inner {
pub(crate) allowed_origins: AllOrSome<HashSet<HeaderValue>>,
pub(crate) allowed_origins_fns: TinyVec<[OriginFn; 4]>,
pub(crate) allowed_origins_fns: SmallVec<[OriginFn; 4]>,
pub(crate) allowed_methods: HashSet<Method>,
pub(crate) allowed_methods_baked: Option<HeaderValue>,

View File

@ -1,6 +1,7 @@
use std::{convert::TryInto, rc::Rc};
use std::{convert::TryInto, error::Error as StdError, rc::Rc};
use actix_web::{
body::{AnyBody, MessageBody},
dev::{Service, ServiceRequest, ServiceResponse},
error::{Error, Result},
http::{
@ -9,7 +10,9 @@ use actix_web::{
},
HttpResponse,
};
use futures_util::future::{ok, Either, FutureExt as _, LocalBoxFuture, Ready};
use futures_util::future::{
ok, Either, FutureExt as _, LocalBoxFuture, Ready, TryFutureExt as _,
};
use log::debug;
use crate::Inner;
@ -26,7 +29,7 @@ pub struct CorsMiddleware<S> {
}
impl<S> CorsMiddleware<S> {
fn handle_preflight<B>(inner: &Inner, req: ServiceRequest) -> ServiceResponse<B> {
fn handle_preflight(inner: &Inner, req: ServiceRequest) -> ServiceResponse {
if let Err(err) = inner
.validate_origin(req.head())
.and_then(|_| inner.validate_allowed_method(req.head()))
@ -69,7 +72,6 @@ impl<S> CorsMiddleware<S> {
}
let res = res.finish();
let res = res.into_body();
req.into_response(res)
}
@ -112,20 +114,21 @@ impl<S> CorsMiddleware<S> {
}
}
type CorsMiddlewareServiceFuture<B> = Either<
Ready<Result<ServiceResponse<B>, Error>>,
LocalBoxFuture<'static, Result<ServiceResponse<B>, Error>>,
type CorsMiddlewareServiceFuture = Either<
Ready<Result<ServiceResponse, Error>>,
LocalBoxFuture<'static, Result<ServiceResponse, Error>>,
>;
impl<S, B> Service<ServiceRequest> for CorsMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
B: MessageBody + 'static,
B::Error: StdError,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse;
type Error = Error;
type Future = CorsMiddlewareServiceFuture<B>;
type Future = CorsMiddlewareServiceFuture;
actix_service::forward_ready!(service);
@ -158,6 +161,7 @@ where
res
}
}
.map_ok(|res| res.map_body(|_, body| AnyBody::from_message(body)))
.boxed_local();
Either::Right(res)