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
This commit is contained in:
Eric Dattore 2018-10-30 20:25:10 -06:00 committed by Yong Wen Chua
parent 351f0f945f
commit 0b73773692
15 changed files with 43 additions and 65 deletions

View File

@ -3,7 +3,7 @@ language: rust
rust: rust:
- nightly - nightly
# Minimum Rust set by Rocket # Minimum Rust set by Rocket
- nightly-2018-08-24 - nightly-2018-10-06
branches: branches:
only: only:
- master - master

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rocket_cors" name = "rocket_cors"
version = "0.3.0" version = "0.4.0"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
authors = ["Yong Wen Chua <me@yongwen.xyz>"] authors = ["Yong Wen Chua <me@yongwen.xyz>"]
description = "Cross-origin resource sharing (CORS) for Rocket.rs applications" description = "Cross-origin resource sharing (CORS) for Rocket.rs applications"
@ -20,8 +20,8 @@ default = ["serialization"]
serialization = ["serde", "serde_derive", "unicase_serde", "url_serde"] serialization = ["serde", "serde_derive", "unicase_serde", "url_serde"]
[dependencies] [dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" }
log = "0.3" log = "0.3"
rocket = "0.3.16"
unicase = "2.0" unicase = "2.0"
url = "1.5.1" url = "1.5.1"
@ -33,7 +33,6 @@ url_serde = { version = "0.2.0", optional = true }
[dev-dependencies] [dev-dependencies]
hyper = "0.10" hyper = "0.10"
rocket_codegen = "0.3.16"
serde_json = "1.0" serde_json = "1.0"
serde_test = "1.0" serde_test = "1.0"

View File

@ -1,6 +1,5 @@
#![feature(plugin)] #![feature(proc_macro_hygiene, decl_macro)]
#![plugin(rocket_codegen)] #[macro_use] extern crate rocket;
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use rocket::http::Method; use rocket::http::Method;

View File

@ -1,6 +1,5 @@
#![feature(plugin)] #![feature(proc_macro_hygiene, decl_macro)]
#![plugin(rocket_codegen)] #[macro_use] extern crate rocket;
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::io::Cursor; use std::io::Cursor;

View File

@ -1,6 +1,7 @@
//! This example is to demonstrate the JSON serialization and deserialization of the Cors settings //! 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. //! Note: This requires the `serialization` feature which is enabled by default.
#![feature(proc_macro_hygiene, decl_macro)]
extern crate rocket; extern crate rocket;
extern crate rocket_cors as cors; extern crate rocket_cors as cors;
extern crate serde_json; extern crate serde_json;

View File

@ -1,6 +1,5 @@
#![feature(plugin)] #![feature(proc_macro_hygiene, decl_macro)]
#![plugin(rocket_codegen)] #[macro_use] extern crate rocket;
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::io::Cursor; use std::io::Cursor;

View File

@ -3,9 +3,8 @@
//! In this example, you typically have an application wide `Cors` struct except for one specific //! 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. //! `ping` route that you want to allow all Origins to access.
#![feature(plugin)] #![feature(proc_macro_hygiene, decl_macro)]
#![plugin(rocket_codegen)] #[macro_use] extern crate rocket;
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use rocket::http::Method; use rocket::http::Method;

View File

@ -1,7 +1,7 @@
//! Fairing implementation //! Fairing implementation
use std::str::FromStr; use std::str::FromStr;
use rocket::http::{self, Header, Status}; use rocket::http::{self, Header, Status, uri::Origin};
use rocket::{self, Outcome, Request}; use rocket::{self, Outcome, Request};
use {actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error}; 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, request: &'r Request,
_: rocket::Data, _: rocket::Data,
) -> rocket::handler::Outcome<'r> { ) -> rocket::handler::Outcome<'r> {
let status = request.get_param::<u16>(0).unwrap_or_else(|e| { let status = request.get_param::<u16>(0).unwrap_or(Ok(0)).unwrap_or_else(|e| {
error_!("Fairing Error Handling Route error: {:?}", e); error_!("Fairing Error Handling Route error: {:?}", e);
500 500
}); });
@ -59,8 +59,10 @@ fn fairing_route(rank: isize) -> rocket::Route {
/// Modifies a `Request` to route to Fairing error handler /// Modifies a `Request` to route to Fairing error handler
fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request) { 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_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 /// Inject a header into the Request with result
@ -217,7 +219,7 @@ mod tests {
let expected_uri = format!("{}/<status>", CORS_ROOT); let expected_uri = format!("{}/<status>", CORS_ROOT);
let error_route = rocket let error_route = rocket
.routes() .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()); assert!(error_route.is_some());
} }

View File

@ -120,7 +120,7 @@ pub type Origin = Url;
pub struct AccessControlRequestMethod(pub ::Method); pub struct AccessControlRequestMethod(pub ::Method);
impl FromStr for AccessControlRequestMethod { impl FromStr for AccessControlRequestMethod {
type Err = rocket::Error; type Err = ();
fn from_str(method: &str) -> Result<Self, Self::Err> { fn from_str(method: &str) -> Result<Self, Self::Err> {
Ok(AccessControlRequestMethod(::Method::from_str(method)?)) 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") { match request.headers().get_one("Access-Control-Request-Method") {
Some(request_method) => match Self::from_str(request_method) { Some(request_method) => match Self::from_str(request_method) {
Ok(request_method) => Outcome::Success(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(()), None => Outcome::Forward(()),
} }

View File

@ -101,9 +101,8 @@
//! [`attach`](https://api.rocket.rs/rocket/struct.Rocket.html#method.attach) it to Rocket. //! [`attach`](https://api.rocket.rs/rocket/struct.Rocket.html#method.attach) it to Rocket.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! #![feature(plugin, custom_derive)] //! #![feature(proc_macro_hygiene, decl_macro)]
//! #![plugin(rocket_codegen)] //! #[macro_use] extern crate rocket;
//! extern crate rocket;
//! extern crate rocket_cors; //! extern crate rocket_cors;
//! //!
//! use rocket::http::Method; //! use rocket::http::Method;
@ -176,9 +175,8 @@
//! [`Guard`](Guard) for a `Response` or a `Responder`. //! [`Guard`](Guard) for a `Response` or a `Responder`.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! #![feature(plugin)] //! #![feature(proc_macro_hygiene, decl_macro)]
//! #![plugin(rocket_codegen)] //! #[macro_use] extern crate rocket;
//! extern crate rocket;
//! extern crate rocket_cors; //! extern crate rocket_cors;
//! //!
//! use std::io::Cursor; //! use std::io::Cursor;
@ -295,9 +293,8 @@
//! (which you might have put in Rocket's state). //! (which you might have put in Rocket's state).
//! //!
//! ```rust,no_run //! ```rust,no_run
//! #![feature(plugin)] //! #![feature(proc_macro_hygiene, decl_macro)]
//! #![plugin(rocket_codegen)] //! #[macro_use] extern crate rocket;
//! extern crate rocket;
//! extern crate rocket_cors; //! extern crate rocket_cors;
//! //!
//! use rocket::http::Method; //! use rocket::http::Method;
@ -352,9 +349,8 @@
//! special handling, you might want to use the Guard method instead which has less hassle. //! special handling, you might want to use the Guard method instead which has less hassle.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! #![feature(plugin)] //! #![feature(proc_macro_hygiene, decl_macro)]
//! #![plugin(rocket_codegen)] //! #[macro_use] extern crate rocket;
//! extern crate rocket;
//! extern crate rocket_cors; //! extern crate rocket_cors;
//! //!
//! use std::io::Cursor; //! use std::io::Cursor;
@ -421,9 +417,8 @@
//! You can run the example code below with `cargo run --example mix`. //! You can run the example code below with `cargo run --example mix`.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! #![feature(plugin)] //! #![feature(proc_macro_hygiene, decl_macro)]
//! #![plugin(rocket_codegen)] //! #[macro_use] extern crate rocket;
//! extern crate rocket;
//! extern crate rocket_cors; //! extern crate rocket_cors;
//! //!
//! use rocket::http::Method; //! use rocket::http::Method;
@ -516,8 +511,6 @@
overflowing_literals, overflowing_literals,
path_statements, path_statements,
plugin_as_library, plugin_as_library,
private_no_mangle_fns,
private_no_mangle_statics,
stable_features, stable_features,
trivial_casts, trivial_casts,
trivial_numeric_casts, trivial_numeric_casts,
@ -538,7 +531,6 @@
unused_parens, unused_parens,
unused_results, unused_results,
unused_unsafe, unused_unsafe,
unused_variables,
variant_size_differences, variant_size_differences,
warnings, warnings,
while_true while_true
@ -551,8 +543,6 @@
unsafe_code, unsafe_code,
intra_doc_link_resolution_failure intra_doc_link_resolution_failure
)] )]
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(rocket_codegen))]
#![doc(test(attr(allow(unused_variables), deny(warnings))))] #![doc(test(attr(allow(unused_variables), deny(warnings))))]
#[macro_use] #[macro_use]
@ -622,7 +612,7 @@ pub enum Error {
/// The request header `Access-Control-Request-Method` is required but is missing /// The request header `Access-Control-Request-Method` is required but is missing
MissingRequestMethod, MissingRequestMethod,
/// The request header `Access-Control-Request-Method` has an invalid value /// 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. /// The request header `Access-Control-Request-Headers` is required but is missing.
MissingRequestHeaders, MissingRequestHeaders,
/// Origin is not allowed to make this request /// Origin is not allowed to make this request
@ -672,7 +662,7 @@ impl error::Error for Error {
"The request header `Access-Control-Request-Method` \ "The request header `Access-Control-Request-Method` \
is required but is missing" is required but is missing"
} }
Error::BadRequestMethod(_) => { Error::BadRequestMethod => {
"The request header `Access-Control-Request-Method` has an invalid value" "The request header `Access-Control-Request-Method` has an invalid value"
} }
Error::MissingRequestHeaders => { Error::MissingRequestHeaders => {
@ -712,7 +702,6 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Error::BadOrigin(ref e) => fmt::Display::fmt(e, f), Error::BadOrigin(ref e) => fmt::Display::fmt(e, f),
Error::BadRequestMethod(ref e) => fmt::Debug::fmt(e, f),
_ => write!(f, "{}", error::Error::description(self)), _ => write!(f, "{}", error::Error::description(self)),
} }
} }
@ -780,7 +769,7 @@ impl AllOrSome<HashSet<Url>> {
pub struct Method(http::Method); pub struct Method(http::Method);
impl FromStr for Method { impl FromStr for Method {
type Err = rocket::Error; type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let method = http::Method::from_str(s)?; let method = http::Method::from_str(s)?;

View File

@ -1,9 +1,7 @@
//! This crate tests using `rocket_cors` using Fairings //! This crate tests using `rocket_cors` using Fairings
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate hyper; extern crate hyper;
extern crate rocket; #[macro_use] extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;

View File

@ -1,9 +1,7 @@
//! This crate tests using `rocket_cors` using the per-route handling with request guard //! This crate tests using `rocket_cors` using the per-route handling with request guard
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate hyper; extern crate hyper;
extern crate rocket; #[macro_use] extern crate rocket;
extern crate rocket_cors as cors; extern crate rocket_cors as cors;
use std::str::FromStr; use std::str::FromStr;

View File

@ -1,8 +1,7 @@
//! This crate tests that all the request headers are parsed correctly in the round trip //! This crate tests that all the request headers are parsed correctly in the round trip
#![feature(plugin)] #![feature(proc_macro_hygiene, decl_macro)]
#![plugin(rocket_codegen)]
extern crate hyper; extern crate hyper;
extern crate rocket; #[macro_use] extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::ops::Deref; use std::ops::Deref;

View File

@ -1,9 +1,7 @@
//! This crate tests using `rocket_cors` using manual mode //! This crate tests using `rocket_cors` using manual mode
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate hyper; extern crate hyper;
extern crate rocket; #[macro_use] extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;

View File

@ -2,11 +2,9 @@
//! //!
//! In this example, you typically have an application wide `Cors` struct except for one specific //! 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. //! `ping` route that you want to allow all Origins to access.
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate hyper; extern crate hyper;
extern crate rocket; #[macro_use] extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;