diff --git a/actix_cors/enum.CorsError.html b/actix_cors/enum.CorsError.html index 16e3b3c18..226dc0d46 100644 --- a/actix_cors/enum.CorsError.html +++ b/actix_cors/enum.CorsError.html @@ -6,7 +6,7 @@ logo

CorsError

logo
#[non_exhaustive]
+    

Enum actix_cors::CorsError

source · []
#[non_exhaustive]
 pub enum CorsError {
     WildcardOrigin,
     MissingOrigin,
@@ -25,16 +25,16 @@ pub enum CorsError {
 

OriginNotAllowed

Origin is not allowed to make this request.

MethodNotAllowed

Request method is not allowed.

HeadersNotAllowed

One or more request headers are not allowed.

-

Trait Implementations

Returns a copy of the value. Read more

+

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

-

Formats the value using the given formatter. Read more

-

Formats the value using the given formatter. Read more

-

The lower-level source of this error, if any. Read more

+

Formats the value using the given formatter. Read more

+

Formats the value using the given formatter. Read more

+

The lower-level source of this error, if any. Read more

🔬 This is a nightly-only experimental API. (backtrace)

Returns a stack backtrace, if available, of where this error occurred. Read more

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

-

Returns appropriate status code for error. Read more

-

Creates full response for error. Read more

+

Returns appropriate status code for error. Read more

+

Creates full response for error. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

diff --git a/src/actix_cors/error.rs.html b/src/actix_cors/error.rs.html index f247f1790..2e1fd8e2b 100644 --- a/src/actix_cors/error.rs.html +++ b/src/actix_cors/error.rs.html @@ -55,16 +55,10 @@ 48 49 50 -51 -52 -53 -54
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
 
 use derive_more::{Display, Error};
 
-use crate::inner::add_vary_header;
-
 /// Errors that can occur when processing CORS guarded requests.
 #[derive(Debug, Clone, Display, Error)]
 #[non_exhaustive]
@@ -108,9 +102,7 @@
     }
 
     fn error_response(&self) -> HttpResponse {
-        let mut res = HttpResponse::with_body(self.status_code(), self.to_string());
-        add_vary_header(res.headers_mut());
-        res.map_into_boxed_body()
+        HttpResponse::with_body(self.status_code(), self.to_string()).map_into_boxed_body()
     }
 }
 
diff --git a/src/actix_cors/middleware.rs.html b/src/actix_cors/middleware.rs.html index 4fd280f9d..a8bb7111c 100644 --- a/src/actix_cors/middleware.rs.html +++ b/src/actix_cors/middleware.rs.html @@ -254,6 +254,21 @@ 247 248 249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264
use std::{collections::HashSet, rc::Rc};
 
 use actix_utils::future::ok;
@@ -287,6 +302,7 @@
 }
 
 impl<S> CorsMiddleware<S> {
+    /// Returns true if request is `OPTIONS` and contains an `Access-Control-Request-Method` header.
     fn is_request_preflight(req: &ServiceRequest) -> bool {
         // check request method is OPTIONS
         if req.method() != Method::OPTIONS {
@@ -306,7 +322,15 @@
         true
     }
 
-    fn handle_preflight(inner: &Inner, req: ServiceRequest) -> ServiceResponse {
+    /// Validates preflight request headers against configuration and constructs preflight response.
+    ///
+    /// Checks:
+    /// - `Origin` header is acceptable;
+    /// - `Access-Control-Request-Method` header is acceptable;
+    /// - `Access-Control-Request-Headers` header is acceptable.
+    fn handle_preflight(&self, req: ServiceRequest) -> ServiceResponse {
+        let inner = Rc::clone(&self.inner);
+
         if let Err(err) = inner
             .validate_origin(req.head())
             .and_then(|_| inner.validate_allowed_method(req.head()))
@@ -347,7 +371,10 @@
         }
 
         let mut res = res.finish();
-        add_vary_header(res.headers_mut());
+
+        if inner.vary_header {
+            add_vary_header(res.headers_mut());
+        }
 
         req.into_response(res)
     }
@@ -418,31 +445,35 @@
     forward_ready!(service);
 
     fn call(&self, req: ServiceRequest) -> Self::Future {
+        let origin = req.headers().get(header::ORIGIN);
+
+        // handle preflight requests
         if self.inner.preflight && Self::is_request_preflight(&req) {
-            let inner = Rc::clone(&self.inner);
-            let res = Self::handle_preflight(&inner, req);
+            let res = self.handle_preflight(req);
             return ok(res.map_into_right_body()).boxed_local();
         }
 
-        let origin = req.headers().get(header::ORIGIN).cloned();
-
+        // only check actual requests with a origin header
         if origin.is_some() {
-            // Only check requests with a origin header.
             if let Err(err) = self.inner.validate_origin(req.head()) {
                 debug!("origin validation failed; inner service is not called");
-                return ok(req.error_response(err).map_into_right_body()).boxed_local();
+                let mut res = req.error_response(err);
+
+                if self.inner.vary_header {
+                    add_vary_header(res.headers_mut());
+                }
+
+                return ok(res.map_into_right_body()).boxed_local();
             }
         }
 
         let inner = Rc::clone(&self.inner);
         let fut = self.service.call(req);
 
-        async move {
+        Box::pin(async move {
             let res = fut.await;
-
             Ok(Self::augment_response(&inner, res?).map_into_left_body())
-        }
-        .boxed_local()
+        })
     }
 }
 
@@ -472,7 +503,6 @@
             .allow_any_origin()
             .allowed_origin_fn(|origin, req_head| {
                 assert_eq!(&origin, req_head.headers.get(header::ORIGIN).unwrap());
-
                 req_head.headers().contains_key(header::DNT)
             })
             .new_transform(test::ok_service())