diff --git a/actix_cors/all.html b/actix_cors/all.html index 7bf5913a0..7943316c6 100644 --- a/actix_cors/all.html +++ b/actix_cors/all.html @@ -1,4 +1,4 @@ -List of all items in this crate

List of all items[] +List of all items in this crate

List of all items[]

Structs

Enums

diff --git a/actix_cors/index.html b/actix_cors/index.html index 44e91c612..db5bcc026 100644 --- a/actix_cors/index.html +++ b/actix_cors/index.html @@ -1,4 +1,4 @@ -actix_cors - Rust

Crate actix_cors[][src]

Expand description

Cross-Origin Resource Sharing (CORS) controls for Actix Web.

+actix_cors - Rust

Crate actix_cors[][src]

Expand description

Cross-Origin Resource Sharing (CORS) controls for Actix Web.

This middleware can be applied to both applications and resources. Once built, a Cors builder can be used as an argument for Actix Web’s App::wrap(), Scope::wrap(), or Resource::wrap() methods.

diff --git a/actix_cors/struct.Cors.html b/actix_cors/struct.Cors.html index 2b0cd9a47..8863933d8 100644 --- a/actix_cors/struct.Cors.html +++ b/actix_cors/struct.Cors.html @@ -20,12 +20,12 @@ server will fail to start up or serve requests.

.max_age(3600); // `cors` can now be used in `App::wrap`.
-

Implementations

A very permissive set of default for quick development. Not recommended for production use.

+

Implementations

A very permissive set of default for quick development. Not recommended for production use.

All origins, methods, request headers and exposed headers allowed. Credentials supported. Max age 1 hour. Does not send wildcard.

-

Resets allowed origin list to a state where any origin is accepted.

+

Resets allowed origin list to a state where any origin is accepted.

See Cors::allowed_origin for more info on allowed origins.

-

Add an origin that is allowed to make requests.

+

Add an origin that is allowed to make requests.

By default, requests from all origins are accepted by CORS logic. This method allows to specify a finite set of origins to verify the value of the Origin request header.

These are origin-or-null types in the Fetch Standard.

@@ -43,39 +43,39 @@ allowed origins.

  • If supplied origin is not valid uri
  • If supplied origin is a wildcard (*). Cors::send_wildcard should be used instead.
  • -

    Determinate allowed origins by processing requests which didn’t match any origins specified +

    Determinate allowed origins by processing requests which didn’t match any origins specified in the allowed_origin.

    The function will receive two parameters, the Origin header value, and the RequestHead of each request, which can be used to determine whether to allow the request or not.

    If the function returns true, the client’s Origin request header will be echoed back into the Access-Control-Allow-Origin response header.

    -

    Resets allowed methods list to all methods.

    +

    Resets allowed methods list to all methods.

    See Cors::allowed_methods for more info on allowed methods.

    -

    Set a list of methods which allowed origins can perform.

    +

    Set a list of methods which allowed origins can perform.

    These will be sent in the Access-Control-Allow-Methods response header as specified in the Fetch Standard CORS protocol.

    Defaults to [GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]

    -

    Resets allowed request header list to a state where any header is accepted.

    +

    Resets allowed request header list to a state where any header is accepted.

    See Cors::allowed_headers for more info on allowed request headers.

    -

    Add an allowed request header.

    +

    Add an allowed request header.

    See Cors::allowed_headers for more info on allowed request headers.

    -

    Set a list of request header field names which can be used when this resource is accessed by +

    Set a list of request header field names which can be used when this resource is accessed by allowed origins.

    If All is set, whatever is requested by the client in Access-Control-Request-Headers will be echoed back in the Access-Control-Allow-Headers header as specified in the Fetch Standard CORS protocol.

    Defaults to All.

    -

    Resets exposed response header list to a state where any header is accepted.

    +

    Resets exposed response header list to a state where any header is accepted.

    See Cors::expose_headers for more info on exposed response headers.

    -

    Set a list of headers which are safe to expose to the API of a CORS API specification. +

    Set a list of headers which are safe to expose to the API of a CORS API specification. This corresponds to the Access-Control-Expose-Headers response header as specified in the Fetch Standard CORS protocol.

    This defaults to an empty set.

    -

    Set a maximum time (in seconds) for which this CORS request maybe cached. +

    Set a maximum time (in seconds) for which this CORS request maybe cached. This value is set as the Access-Control-Max-Age header as specified in the Fetch Standard CORS protocol.

    Pass a number (of seconds) or use None to disable sending max age header.

    -

    Set to use wildcard origins.

    +

    Set to use wildcard origins.

    If send wildcard is set and the allowed_origins parameter is All, a wildcard Access-Control-Allow-Origin response header is sent, rather than the request’s Origin header.

    @@ -83,7 +83,7 @@ the Fetch Standard C allow_credentials set to true. Depending on the mode of usage, this will either result in an CorsError::CredentialsWithWildcardOrigin error during actix launch or runtime.

    Defaults to false.

    -

    Allows users to make authenticated requests

    +

    Allows users to make authenticated requests

    If true, injects the Access-Control-Allow-Credentials header in responses. This allows cookies and credentials to be submitted across domains as specified in the Fetch Standard CORS protocol.

    @@ -92,27 +92,27 @@ the Fetch Standard C

    Defaults to false.

    A server initialization error will occur if credentials are allowed, but the Origin is set to send wildcards (*); this is not allowed by the CORS protocol.

    -

    Disable Vary header support.

    +

    Disable Vary header support.

    When enabled the header Vary: Origin will be returned as per the Fetch Standard implementation guidelines.

    Setting this header when the Access-Control-Allow-Origin is dynamically generated (eg. when there is more than one allowed origin, and an Origin other than ‘*’ is returned) informs CDNs and other caches that the CORS headers are dynamic, and cannot be cached.

    By default, Vary header support is enabled.

    -

    Disable support for preflight requests.

    +

    Disable support for preflight requests.

    When enabled CORS middleware automatically handles OPTIONS requests. This is useful for application level middleware.

    By default preflight support is enabled.

    Trait Implementations

    Formats the value using the given formatter. Read more

    -

    A restrictive (security paranoid) set of defaults.

    +

    A restrictive (security paranoid) set of defaults.

    No allowed origins, methods, request headers or exposed headers. Credentials not supported. No max age (will use browser’s default).

    -

    Responses produced by the service.

    +

    Responses produced by the service.

    Errors produced by the service.

    Errors produced while building a transform service.

    The TransformService value created by this factory

    The future response value.

    -

    Creates and returns a new Transform component, asynchronously

    +

    Creates and returns a new Transform component, asynchronously

    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/builder.rs.html b/src/actix_cors/builder.rs.html index 049007e79..9336e39f4 100644 --- a/src/actix_cors/builder.rs.html +++ b/src/actix_cors/builder.rs.html @@ -650,6 +650,12 @@ 650 651 652 +653 +654 +655 +656 +657 +658
    use std::{
         collections::HashSet, convert::TryInto, error::Error as StdError, iter::FromIterator, rc::Rc,
     };
    @@ -747,6 +753,7 @@
     
                 expose_headers: AllOrSome::All,
                 expose_headers_baked: None,
    +
                 max_age: Some(3600),
                 preflight: true,
                 send_wildcard: false,
    @@ -1196,13 +1203,18 @@
     }
     
     /// Only call when values are guaranteed to be valid header values and set is not empty.
    -fn intersperse_header_values<T>(val_set: &HashSet<T>) -> HeaderValue
    +pub(crate) fn intersperse_header_values<T>(val_set: &HashSet<T>) -> HeaderValue
     where
         T: AsRef<str>,
     {
    +    debug_assert!(
    +        !val_set.is_empty(),
    +        "only call `intersperse_header_values` when set is not empty"
    +    );
    +
         val_set
             .iter()
    -        .fold(String::with_capacity(32), |mut acc, val| {
    +        .fold(String::with_capacity(64), |mut acc, val| {
                 acc.push_str(", ");
                 acc.push_str(val.as_ref());
                 acc
    diff --git a/src/actix_cors/middleware.rs.html b/src/actix_cors/middleware.rs.html
    index 2613a0811..09879a3ad 100644
    --- a/src/actix_cors/middleware.rs.html
    +++ b/src/actix_cors/middleware.rs.html
    @@ -212,7 +212,33 @@
     212
     213
     214
    -
    use std::{convert::TryInto, error::Error as StdError, rc::Rc};
    +215
    +216
    +217
    +218
    +219
    +220
    +221
    +222
    +223
    +224
    +225
    +226
    +227
    +228
    +229
    +230
    +231
    +232
    +233
    +234
    +235
    +236
    +237
    +238
    +239
    +240
    +
    use std::{collections::HashSet, convert::TryInto, error::Error as StdError, rc::Rc};
     
     use actix_web::{
         body::{AnyBody, MessageBody},
    @@ -227,7 +253,7 @@
     use futures_util::future::{ok, Either, FutureExt as _, LocalBoxFuture, Ready, TryFutureExt as _};
     use log::debug;
     
    -use crate::Inner;
    +use crate::{builder::intersperse_header_values, AllOrSome, Inner};
     
     /// Service wrapper for Cross-Origin Resource Sharing support.
     ///
    @@ -292,8 +318,34 @@
             };
     
             if let Some(ref expose) = inner.expose_headers_baked {
    +            log::trace!("exposing selected headers: {:?}", expose);
    +
                 res.headers_mut()
                     .insert(header::ACCESS_CONTROL_EXPOSE_HEADERS, expose.clone());
    +        } else if matches!(inner.expose_headers, AllOrSome::All) {
    +            // intersperse_header_values requires that argument is non-empty
    +            if !res.request().headers().is_empty() {
    +                // extract header names from request
    +                let expose_all_request_headers = res
    +                    .request()
    +                    .headers()
    +                    .keys()
    +                    .into_iter()
    +                    .map(|name| name.as_str())
    +                    .collect::<HashSet<_>>();
    +
    +                // create comma separated string of header names
    +                let expose_headers_value = intersperse_header_values(&expose_all_request_headers);
    +
    +                log::trace!(
    +                    "exposing all headers from request: {:?}",
    +                    expose_headers_value
    +                );
    +
    +                // add header names to expose response header
    +                res.headers_mut()
    +                    .insert(header::ACCESS_CONTROL_EXPOSE_HEADERS, expose_headers_value);
    +            }
             }
     
             if inner.supports_credentials {