From 0b737736923dc6b639fddf5314b54432fa9b4c0b Mon Sep 17 00:00:00 2001 From: Eric Dattore Date: Tue, 30 Oct 2018 20:25:10 -0600 Subject: [PATCH] Update library to 0.4 and support Rocket 0.4 (#46) * Update Cargo.toml for Rocket `master` * Prepare library for Rocket 0.4 TODO: check line 62 in `fairing.rs` to find better way than unwrapping * Bump minimum nightly for Rocket 0.4 --- .travis.yml | 2 +- Cargo.toml | 5 ++--- examples/fairing.rs | 5 ++--- examples/guard.rs | 5 ++--- examples/json.rs | 1 + examples/manual.rs | 5 ++--- examples/mix.rs | 5 ++--- src/fairing.rs | 10 ++++++---- src/headers.rs | 4 ++-- src/lib.rs | 37 +++++++++++++------------------------ tests/fairing.rs | 6 ++---- tests/guard.rs | 6 ++---- tests/headers.rs | 5 ++--- tests/manual.rs | 6 ++---- tests/mix.rs | 6 ++---- 15 files changed, 43 insertions(+), 65 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2c3a1bc..ac5d79b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: rust rust: - nightly # Minimum Rust set by Rocket - - nightly-2018-08-24 + - nightly-2018-10-06 branches: only: - master diff --git a/Cargo.toml b/Cargo.toml index ca9d6e7..be6f7c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rocket_cors" -version = "0.3.0" +version = "0.4.0" license = "MIT/Apache-2.0" authors = ["Yong Wen Chua "] description = "Cross-origin resource sharing (CORS) for Rocket.rs applications" @@ -20,8 +20,8 @@ default = ["serialization"] serialization = ["serde", "serde_derive", "unicase_serde", "url_serde"] [dependencies] +rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" } log = "0.3" -rocket = "0.3.16" unicase = "2.0" url = "1.5.1" @@ -33,7 +33,6 @@ url_serde = { version = "0.2.0", optional = true } [dev-dependencies] hyper = "0.10" -rocket_codegen = "0.3.16" serde_json = "1.0" serde_test = "1.0" diff --git a/examples/fairing.rs b/examples/fairing.rs index 3e08779..5d493af 100644 --- a/examples/fairing.rs +++ b/examples/fairing.rs @@ -1,6 +1,5 @@ -#![feature(plugin)] -#![plugin(rocket_codegen)] -extern crate rocket; +#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; extern crate rocket_cors; use rocket::http::Method; diff --git a/examples/guard.rs b/examples/guard.rs index 3a8e59d..ff59fbe 100644 --- a/examples/guard.rs +++ b/examples/guard.rs @@ -1,6 +1,5 @@ -#![feature(plugin)] -#![plugin(rocket_codegen)] -extern crate rocket; +#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::io::Cursor; diff --git a/examples/json.rs b/examples/json.rs index 8f9e5d7..b19bb95 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -1,6 +1,7 @@ //! This example is to demonstrate the JSON serialization and deserialization of the Cors settings //! //! Note: This requires the `serialization` feature which is enabled by default. +#![feature(proc_macro_hygiene, decl_macro)] extern crate rocket; extern crate rocket_cors as cors; extern crate serde_json; diff --git a/examples/manual.rs b/examples/manual.rs index 4998cc3..07d67a7 100644 --- a/examples/manual.rs +++ b/examples/manual.rs @@ -1,6 +1,5 @@ -#![feature(plugin)] -#![plugin(rocket_codegen)] -extern crate rocket; +#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::io::Cursor; diff --git a/examples/mix.rs b/examples/mix.rs index 6b53fcb..d17539a 100644 --- a/examples/mix.rs +++ b/examples/mix.rs @@ -3,9 +3,8 @@ //! In this example, you typically have an application wide `Cors` struct except for one specific //! `ping` route that you want to allow all Origins to access. -#![feature(plugin)] -#![plugin(rocket_codegen)] -extern crate rocket; +#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; extern crate rocket_cors; use rocket::http::Method; diff --git a/src/fairing.rs b/src/fairing.rs index dac9985..7fdb33a 100644 --- a/src/fairing.rs +++ b/src/fairing.rs @@ -1,7 +1,7 @@ //! Fairing implementation use std::str::FromStr; -use rocket::http::{self, Header, Status}; +use rocket::http::{self, Header, Status, uri::Origin}; use rocket::{self, Outcome, Request}; use {actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error}; @@ -44,7 +44,7 @@ pub(crate) fn fairing_error_route<'r>( request: &'r Request, _: rocket::Data, ) -> rocket::handler::Outcome<'r> { - let status = request.get_param::(0).unwrap_or_else(|e| { + let status = request.get_param::(0).unwrap_or(Ok(0)).unwrap_or_else(|e| { error_!("Fairing Error Handling Route error: {:?}", e); 500 }); @@ -59,8 +59,10 @@ fn fairing_route(rank: isize) -> rocket::Route { /// Modifies a `Request` to route to Fairing error handler fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request) { + let origin = Origin::parse_owned(format!("{}/{}", options.fairing_route_base, status)).unwrap(); + request.set_method(http::Method::Get); - request.set_uri(format!("{}/{}", options.fairing_route_base, status)); + request.set_uri(origin); } /// Inject a header into the Request with result @@ -217,7 +219,7 @@ mod tests { let expected_uri = format!("{}/", CORS_ROOT); let error_route = rocket .routes() - .find(|r| r.method == Method::Get && r.uri.as_str() == expected_uri); + .find(|r| r.method == Method::Get && r.uri.to_string() == expected_uri); assert!(error_route.is_some()); } diff --git a/src/headers.rs b/src/headers.rs index cb48f80..4f21b9b 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -120,7 +120,7 @@ pub type Origin = Url; pub struct AccessControlRequestMethod(pub ::Method); impl FromStr for AccessControlRequestMethod { - type Err = rocket::Error; + type Err = (); fn from_str(method: &str) -> Result { Ok(AccessControlRequestMethod(::Method::from_str(method)?)) @@ -134,7 +134,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for AccessControlRequestMethod { match request.headers().get_one("Access-Control-Request-Method") { Some(request_method) => match Self::from_str(request_method) { Ok(request_method) => Outcome::Success(request_method), - Err(e) => Outcome::Failure((Status::BadRequest, ::Error::BadRequestMethod(e))), + Err(_) => Outcome::Failure((Status::BadRequest, ::Error::BadRequestMethod)), }, None => Outcome::Forward(()), } diff --git a/src/lib.rs b/src/lib.rs index 56fca20..3a73ab1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,9 +101,8 @@ //! [`attach`](https://api.rocket.rs/rocket/struct.Rocket.html#method.attach) it to Rocket. //! //! ```rust,no_run -//! #![feature(plugin, custom_derive)] -//! #![plugin(rocket_codegen)] -//! extern crate rocket; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! #[macro_use] extern crate rocket; //! extern crate rocket_cors; //! //! use rocket::http::Method; @@ -176,9 +175,8 @@ //! [`Guard`](Guard) for a `Response` or a `Responder`. //! //! ```rust,no_run -//! #![feature(plugin)] -//! #![plugin(rocket_codegen)] -//! extern crate rocket; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! #[macro_use] extern crate rocket; //! extern crate rocket_cors; //! //! use std::io::Cursor; @@ -295,9 +293,8 @@ //! (which you might have put in Rocket's state). //! //! ```rust,no_run -//! #![feature(plugin)] -//! #![plugin(rocket_codegen)] -//! extern crate rocket; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! #[macro_use] extern crate rocket; //! extern crate rocket_cors; //! //! use rocket::http::Method; @@ -352,9 +349,8 @@ //! special handling, you might want to use the Guard method instead which has less hassle. //! //! ```rust,no_run -//! #![feature(plugin)] -//! #![plugin(rocket_codegen)] -//! extern crate rocket; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! #[macro_use] extern crate rocket; //! extern crate rocket_cors; //! //! use std::io::Cursor; @@ -421,9 +417,8 @@ //! You can run the example code below with `cargo run --example mix`. //! //! ```rust,no_run -//! #![feature(plugin)] -//! #![plugin(rocket_codegen)] -//! extern crate rocket; +//! #![feature(proc_macro_hygiene, decl_macro)] +//! #[macro_use] extern crate rocket; //! extern crate rocket_cors; //! //! use rocket::http::Method; @@ -516,8 +511,6 @@ overflowing_literals, path_statements, plugin_as_library, - private_no_mangle_fns, - private_no_mangle_statics, stable_features, trivial_casts, trivial_numeric_casts, @@ -538,7 +531,6 @@ unused_parens, unused_results, unused_unsafe, - unused_variables, variant_size_differences, warnings, while_true @@ -551,8 +543,6 @@ unsafe_code, intra_doc_link_resolution_failure )] -#![cfg_attr(test, feature(plugin))] -#![cfg_attr(test, plugin(rocket_codegen))] #![doc(test(attr(allow(unused_variables), deny(warnings))))] #[macro_use] @@ -622,7 +612,7 @@ pub enum Error { /// The request header `Access-Control-Request-Method` is required but is missing MissingRequestMethod, /// The request header `Access-Control-Request-Method` has an invalid value - BadRequestMethod(rocket::Error), + BadRequestMethod, /// The request header `Access-Control-Request-Headers` is required but is missing. MissingRequestHeaders, /// Origin is not allowed to make this request @@ -672,7 +662,7 @@ impl error::Error for Error { "The request header `Access-Control-Request-Method` \ is required but is missing" } - Error::BadRequestMethod(_) => { + Error::BadRequestMethod => { "The request header `Access-Control-Request-Method` has an invalid value" } Error::MissingRequestHeaders => { @@ -712,7 +702,6 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::BadOrigin(ref e) => fmt::Display::fmt(e, f), - Error::BadRequestMethod(ref e) => fmt::Debug::fmt(e, f), _ => write!(f, "{}", error::Error::description(self)), } } @@ -780,7 +769,7 @@ impl AllOrSome> { pub struct Method(http::Method); impl FromStr for Method { - type Err = rocket::Error; + type Err = (); fn from_str(s: &str) -> Result { let method = http::Method::from_str(s)?; diff --git a/tests/fairing.rs b/tests/fairing.rs index 5c99029..859188f 100644 --- a/tests/fairing.rs +++ b/tests/fairing.rs @@ -1,9 +1,7 @@ //! This crate tests using `rocket_cors` using Fairings - -#![feature(plugin)] -#![plugin(rocket_codegen)] +#![feature(proc_macro_hygiene, decl_macro)] extern crate hyper; -extern crate rocket; +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::str::FromStr; diff --git a/tests/guard.rs b/tests/guard.rs index fc25f7a..18e99fb 100644 --- a/tests/guard.rs +++ b/tests/guard.rs @@ -1,9 +1,7 @@ //! This crate tests using `rocket_cors` using the per-route handling with request guard - -#![feature(plugin)] -#![plugin(rocket_codegen)] +#![feature(proc_macro_hygiene, decl_macro)] extern crate hyper; -extern crate rocket; +#[macro_use] extern crate rocket; extern crate rocket_cors as cors; use std::str::FromStr; diff --git a/tests/headers.rs b/tests/headers.rs index d857d83..4479d11 100644 --- a/tests/headers.rs +++ b/tests/headers.rs @@ -1,8 +1,7 @@ //! This crate tests that all the request headers are parsed correctly in the round trip -#![feature(plugin)] -#![plugin(rocket_codegen)] +#![feature(proc_macro_hygiene, decl_macro)] extern crate hyper; -extern crate rocket; +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::ops::Deref; diff --git a/tests/manual.rs b/tests/manual.rs index c12cc7a..bdb3df5 100644 --- a/tests/manual.rs +++ b/tests/manual.rs @@ -1,9 +1,7 @@ //! This crate tests using `rocket_cors` using manual mode - -#![feature(plugin)] -#![plugin(rocket_codegen)] +#![feature(proc_macro_hygiene, decl_macro)] extern crate hyper; -extern crate rocket; +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::str::FromStr; diff --git a/tests/mix.rs b/tests/mix.rs index 81b6cae..7cfe6e8 100644 --- a/tests/mix.rs +++ b/tests/mix.rs @@ -2,11 +2,9 @@ //! //! In this example, you typically have an application wide `Cors` struct except for one specific //! `ping` route that you want to allow all Origins to access. - -#![feature(plugin)] -#![plugin(rocket_codegen)] +#![feature(proc_macro_hygiene, decl_macro)] extern crate hyper; -extern crate rocket; +#[macro_use] extern crate rocket; extern crate rocket_cors; use std::str::FromStr;