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

fmt with new width

This commit is contained in:
Rob Ede
2021-08-30 23:27:44 +01:00
parent c6f579790f
commit e10937103e
18 changed files with 54 additions and 144 deletions

View File

@ -150,8 +150,7 @@ impl Cors {
Ok(_) => {
if cors.allowed_origins.is_all() {
cors.allowed_origins =
AllOrSome::Some(HashSet::with_capacity(8));
cors.allowed_origins = AllOrSome::Some(HashSet::with_capacity(8));
}
if let Some(origins) = cors.allowed_origins.as_mut() {
@ -257,8 +256,7 @@ impl Cors {
match header.try_into() {
Ok(method) => {
if cors.allowed_headers.is_all() {
cors.allowed_headers =
AllOrSome::Some(HashSet::with_capacity(8));
cors.allowed_headers = AllOrSome::Some(HashSet::with_capacity(8));
}
if let AllOrSome::Some(ref mut headers) = cors.allowed_headers {
@ -294,8 +292,7 @@ impl Cors {
match h.try_into() {
Ok(method) => {
if cors.allowed_headers.is_all() {
cors.allowed_headers =
AllOrSome::Some(HashSet::with_capacity(8));
cors.allowed_headers = AllOrSome::Some(HashSet::with_capacity(8));
}
if let AllOrSome::Some(ref mut headers) = cors.allowed_headers {
@ -342,8 +339,7 @@ impl Cors {
Ok(header) => {
if let Some(cors) = cors(&mut self.inner, &self.error) {
if cors.expose_headers.is_all() {
cors.expose_headers =
AllOrSome::Some(HashSet::with_capacity(8));
cors.expose_headers = AllOrSome::Some(HashSet::with_capacity(8));
}
if let AllOrSome::Some(ref mut headers) = cors.expose_headers {
headers.insert(header);
@ -506,12 +502,11 @@ where
let mut inner = Rc::clone(&self.inner);
if inner.supports_credentials
&& inner.send_wildcard
&& inner.allowed_origins.is_all()
{
error!("Illegal combination of CORS options: credentials can not be supported when all \
origins are allowed and `send_wildcard` is enabled.");
if inner.supports_credentials && inner.send_wildcard && inner.allowed_origins.is_all() {
error!(
"Illegal combination of CORS options: credentials can not be supported when all \
origins are allowed and `send_wildcard` is enabled."
);
return future::err(());
}
@ -519,8 +514,7 @@ where
match inner.allowed_headers.as_ref() {
Some(header_set) if !header_set.is_empty() => {
let allowed_headers_str = intersperse_header_values(header_set);
Rc::make_mut(&mut inner).allowed_headers_baked =
Some(allowed_headers_str);
Rc::make_mut(&mut inner).allowed_headers_baked = Some(allowed_headers_str);
}
_ => {}
}

View File

@ -15,21 +15,15 @@ pub enum CorsError {
MissingOrigin,
/// Request header `Access-Control-Request-Method` is required but is missing.
#[display(
fmt = "Request header `Access-Control-Request-Method` is required but is missing."
)]
#[display(fmt = "Request header `Access-Control-Request-Method` is required but is missing.")]
MissingRequestMethod,
/// Request header `Access-Control-Request-Method` has an invalid value.
#[display(
fmt = "Request header `Access-Control-Request-Method` has an invalid value."
)]
#[display(fmt = "Request header `Access-Control-Request-Method` has an invalid value.")]
BadRequestMethod,
/// Request header `Access-Control-Request-Headers` has an invalid value.
#[display(
fmt = "Request header `Access-Control-Request-Headers` has an invalid value."
)]
#[display(fmt = "Request header `Access-Control-Request-Headers` has an invalid value.")]
BadRequestHeaders,
/// Origin is not allowed to make this request.

View File

@ -78,9 +78,7 @@ impl Inner {
match req.headers().get(header::ORIGIN) {
// origin header exists and is a string
Some(origin) => {
if allowed_origins.contains(origin)
|| self.validate_origin_fns(origin, req)
{
if allowed_origins.contains(origin) || self.validate_origin_fns(origin, req) {
Ok(())
} else {
Err(CorsError::OriginNotAllowed)
@ -102,10 +100,7 @@ impl Inner {
}
/// Only called if origin exists and always after it's validated.
pub(crate) fn access_control_allow_origin(
&self,
req: &RequestHead,
) -> Option<HeaderValue> {
pub(crate) fn access_control_allow_origin(&self, req: &RequestHead) -> Option<HeaderValue> {
let origin = req.headers().get(header::ORIGIN);
match self.allowed_origins {
@ -129,10 +124,7 @@ impl Inner {
/// Use in preflight checks and therefore operates on header list in
/// `Access-Control-Request-Headers` not the actual header set.
pub(crate) fn validate_allowed_method(
&self,
req: &RequestHead,
) -> Result<(), CorsError> {
pub(crate) fn validate_allowed_method(&self, req: &RequestHead) -> Result<(), CorsError> {
// extract access control header and try to parse as method
let request_method = req
.headers()
@ -154,10 +146,7 @@ impl Inner {
}
}
pub(crate) fn validate_allowed_headers(
&self,
req: &RequestHead,
) -> Result<(), CorsError> {
pub(crate) fn validate_allowed_headers(&self, req: &RequestHead) -> Result<(), CorsError> {
// return early if all headers are allowed or get ref to allowed origins set
#[allow(clippy::mutable_key_type)]
let allowed_headers = match &self.allowed_headers {

View File

@ -10,9 +10,7 @@ use actix_web::{
},
HttpResponse,
};
use futures_util::future::{
ok, Either, FutureExt as _, LocalBoxFuture, Ready, TryFutureExt as _,
};
use futures_util::future::{ok, Either, FutureExt as _, LocalBoxFuture, Ready, TryFutureExt as _};
use log::debug;
use crate::Inner;
@ -53,9 +51,7 @@ impl<S> CorsMiddleware<S> {
if let Some(ref headers) = inner.allowed_headers_baked {
res.insert_header((header::ACCESS_CONTROL_ALLOW_HEADERS, headers.clone()));
} else if let Some(headers) =
req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS)
{
} else if let Some(headers) = req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS) {
// all headers allowed, return
res.insert_header((header::ACCESS_CONTROL_ALLOW_HEADERS, headers.clone()));
}
@ -75,10 +71,7 @@ impl<S> CorsMiddleware<S> {
req.into_response(res)
}
fn augment_response<B>(
inner: &Inner,
mut res: ServiceResponse<B>,
) -> ServiceResponse<B> {
fn augment_response<B>(inner: &Inner, mut res: ServiceResponse<B>) -> ServiceResponse<B> {
if let Some(origin) = inner.access_control_allow_origin(res.request().head()) {
res.headers_mut()
.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, origin);