Rename Options to Cors

This commit is contained in:
Yong Wen Chua 2017-07-15 10:54:26 +08:00
parent ebd75cd0ba
commit d23c935a4a
2 changed files with 19 additions and 19 deletions

View File

@ -11,7 +11,7 @@
//! ## Requirements //! ## Requirements
//! //!
//! - Nightly Rust //! - Nightly Rust
//! - Rocket > 0.3 //! - Rocket >= 0.3
//! //!
//! ### Nightly Rust //! ### Nightly Rust
//! //!
@ -151,7 +151,7 @@ pub enum Error {
HeadersNotAllowed, HeadersNotAllowed,
/// Credentials are allowed, but the Origin is set to "*". This is not allowed by W3C /// Credentials are allowed, but the Origin is set to "*". This is not allowed by W3C
/// ///
/// This is a misconfiguration. Check the docuemntation for `Options`. /// This is a misconfiguration. Check the docuemntation for `Cors`.
CredentialsWithWildcardOrigin, CredentialsWithWildcardOrigin,
} }
@ -381,7 +381,7 @@ impl AllOrSome<HashSet<Url>> {
/// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) is implemented for this /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) is implemented for this
/// struct. The default for each field is described in the docuementation for the field. /// struct. The default for each field is described in the docuementation for the field.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Options { pub struct Cors {
/// Origins that are allowed to make requests. /// Origins that are allowed to make requests.
/// Will be verified against the `Origin` request header. /// Will be verified against the `Origin` request header.
/// ///
@ -405,7 +405,7 @@ pub struct Options {
/// [Resource Processing Model](https://www.w3.org/TR/cors/#resource-processing-model). /// [Resource Processing Model](https://www.w3.org/TR/cors/#resource-processing-model).
/// ///
/// Defaults to `[GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]` /// Defaults to `[GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]`
// #[serde(default = "Options::default_allowed_methods")] // #[serde(default = "Cors::default_allowed_methods")]
pub allowed_methods: HashSet<Method>, pub allowed_methods: HashSet<Method>,
/// The list of header field names which can be used when this resource is accessed by allowed /// The list of header field names which can be used when this resource is accessed by allowed
/// origins. /// origins.
@ -461,7 +461,7 @@ pub struct Options {
pub send_wildcard: bool, pub send_wildcard: bool,
} }
impl Default for Options { impl Default for Cors {
fn default() -> Self { fn default() -> Self {
Self { Self {
allowed_origins: Default::default(), allowed_origins: Default::default(),
@ -483,13 +483,13 @@ impl Default for Options {
/// possible to have such a reference. /// possible to have such a reference.
/// See [this PR on Rocket](https://github.com/SergioBenitez/Rocket/pull/345). /// See [this PR on Rocket](https://github.com/SergioBenitez/Rocket/pull/345).
pub fn respond<'a, 'r: 'a, R: response::Responder<'r>>( pub fn respond<'a, 'r: 'a, R: response::Responder<'r>>(
options: State<'a, Options>, options: State<'a, Cors>,
responder: R, responder: R,
) -> Responder<'a, 'r, R> { ) -> Responder<'a, 'r, R> {
options.inner().respond(responder) options.inner().respond(responder)
} }
impl Options { impl Cors {
/// Wrap any `Rocket::Response` and respond with CORS headers. /// Wrap any `Rocket::Response` and respond with CORS headers.
/// This is only used for ad-hoc route CORS response /// This is only used for ad-hoc route CORS response
fn respond<'a, 'r: 'a, R: response::Responder<'r>>( fn respond<'a, 'r: 'a, R: response::Responder<'r>>(
@ -513,7 +513,7 @@ impl Options {
} }
} }
/// A CORS Responder which will inspect the incoming requests and respond accoridingly. /// A CORS Responder which will inspect the incoming requests and respond accordingly.
/// ///
/// If the wrapped `Responder` already has the `Access-Control-Allow-Origin` header set, /// If the wrapped `Responder` already has the `Access-Control-Allow-Origin` header set,
/// this responder will leave the response untouched. /// this responder will leave the response untouched.
@ -532,12 +532,12 @@ impl Options {
#[derive(Debug)] #[derive(Debug)]
pub struct Responder<'a, 'r: 'a, R> { pub struct Responder<'a, 'r: 'a, R> {
responder: R, responder: R,
options: &'a Options, options: &'a Cors,
marker: PhantomData<response::Responder<'r>>, marker: PhantomData<response::Responder<'r>>,
} }
impl<'a, 'r: 'a, R: response::Responder<'r>> Responder<'a, 'r, R> { impl<'a, 'r: 'a, R: response::Responder<'r>> Responder<'a, 'r, R> {
fn new(responder: R, options: &'a Options) -> Self { fn new(responder: R, options: &'a Cors) -> Self {
Self { Self {
responder, responder,
options, options,
@ -627,7 +627,7 @@ impl<'a, 'r: 'a, R: response::Responder<'r>> Responder<'a, 'r, R> {
/// This implementation references the /// This implementation references the
/// [W3C recommendation](https://www.w3.org/TR/cors/#resource-preflight-requests). /// [W3C recommendation](https://www.w3.org/TR/cors/#resource-preflight-requests).
fn preflight( fn preflight(
options: &Options, options: &Cors,
origin: Origin, origin: Origin,
method: Option<AccessControlRequestMethod>, method: Option<AccessControlRequestMethod>,
headers: Option<AccessControlRequestHeaders>, headers: Option<AccessControlRequestHeaders>,
@ -717,7 +717,7 @@ impl<'a, 'r: 'a, R: response::Responder<'r>> Responder<'a, 'r, R> {
/// Respond to an actual request based on the settings. /// Respond to an actual request based on the settings.
/// If the `Origin` is not provided, then this request was not made by a browser and there is no /// If the `Origin` is not provided, then this request was not made by a browser and there is no
/// CORS enforcement. /// CORS enforcement.
fn actual_request(options: &Options, origin: Origin) -> Result<Response, Error> { fn actual_request(options: &Cors, origin: Origin) -> Result<Response, Error> {
let response = Response::new(); let response = Response::new();
// Note: All header parse failures are dealt with in the `FromRequest` trait implementation // Note: All header parse failures are dealt with in the `FromRequest` trait implementation
@ -1508,12 +1508,12 @@ mod tests {
// The following tests check that preflight checks are done properly // The following tests check that preflight checks are done properly
// fn make_cors_options() -> Options { // fn make_cors_options() -> Cors {
// let (allowed_origins, failed_origins) = // let (allowed_origins, failed_origins) =
// AllOrSome::new_from_str_list(&["https://www.acme.com"]); // AllOrSome::new_from_str_list(&["https://www.acme.com"]);
// assert!(failed_origins.is_empty()); // assert!(failed_origins.is_empty());
// Options { // Cors {
// allowed_origins: allowed_origins, // allowed_origins: allowed_origins,
// allowed_methods: [Method::Get].iter().cloned().collect(), // allowed_methods: [Method::Get].iter().cloned().collect(),
// allowed_headers: AllOrSome::Some( // allowed_headers: AllOrSome::Some(

View File

@ -15,20 +15,20 @@ use rocket::local::Client;
use rocket_cors::*; use rocket_cors::*;
#[options("/")] #[options("/")]
fn cors_options(options: State<rocket_cors::Options>) -> Responder<&str> { fn cors_options(options: State<rocket_cors::Cors>) -> Responder<&str> {
rocket_cors::respond(options, "") rocket_cors::respond(options, "")
} }
#[get("/")] #[get("/")]
fn cors(options: State<rocket_cors::Options>) -> Responder<&str> { fn cors(options: State<rocket_cors::Cors>) -> Responder<&str> {
rocket_cors::respond(options, "Hello CORS") rocket_cors::respond(options, "Hello CORS")
} }
fn make_cors_options() -> Options { fn make_cors_options() -> Cors {
let (allowed_origins, failed_origins) = AllOrSome::new_from_str_list(&["https://www.acme.com"]); let (allowed_origins, failed_origins) = AllOrSome::new_from_str_list(&["https://www.acme.com"]);
assert!(failed_origins.is_empty()); assert!(failed_origins.is_empty());
Options { Cors {
allowed_origins: allowed_origins, allowed_origins: allowed_origins,
allowed_methods: [Method::Get].iter().cloned().collect(), allowed_methods: [Method::Get].iter().cloned().collect(),
allowed_headers: AllOrSome::Some( allowed_headers: AllOrSome::Some(
@ -46,7 +46,7 @@ fn make_cors_options() -> Options {
fn smoke_test() { fn smoke_test() {
let (allowed_origins, failed_origins) = AllOrSome::new_from_str_list(&["https://www.acme.com"]); let (allowed_origins, failed_origins) = AllOrSome::new_from_str_list(&["https://www.acme.com"]);
assert!(failed_origins.is_empty()); assert!(failed_origins.is_empty());
let cors_options = rocket_cors::Options { let cors_options = rocket_cors::Cors {
allowed_origins: allowed_origins, allowed_origins: allowed_origins,
allowed_methods: [Method::Get].iter().cloned().collect(), allowed_methods: [Method::Get].iter().cloned().collect(),
allowed_headers: AllOrSome::Some( allowed_headers: AllOrSome::Some(