Update to latest master Rocket version (#89)

* Change to catch up with latest rocket master

- rocket::handler -> rocket::route
- request.get_param() -> request.param
- Change attach -> ignite and ignite -> build
- Update lifetime signature of FromRequest

* Remove unused import

* Remove colliding route in catch_all_options_routes()

In rocket 0.5 "/<catch_all_options_route..>" should match path "/" too hence the collision.

* Revert "Remove unused import"

This reverts commit 69db6d53

* add #[allow(unused_imports)] to log

* lock Rocket to specific commit

* apply rustfmt
This commit is contained in:
thanadolps 2021-04-26 14:36:37 +07:00 committed by GitHub
parent 305971023d
commit c0d7b36877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 54 additions and 57 deletions

View File

@ -22,7 +22,7 @@ serialization = ["serde", "serde_derive", "unicase_serde"]
[dependencies]
regex = "1.1"
rocket = { branch = "master", git = "https://github.com/SergioBenitez/Rocket.git", default-features = false }
rocket = { rev="801e04bd5369eb39e126c75f6d11e1e9597304d8", git = "https://github.com/SergioBenitez/Rocket.git", default-features = false }
log = "0.4"
unicase = "2.0"
url = "2.1.0"

View File

@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
.to_cors()?;
rocket::ignite()
rocket::build()
.mount("/", routes![cors])
.attach(cors)
.launch()

View File

@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
.to_cors()?;
rocket::ignite()
rocket::build()
.mount("/", routes![responder, response])
// Mount the routes to catch all the OPTIONS pre-flight requests
.mount("/", rocket_cors::catch_all_options_routes())

View File

@ -71,7 +71,7 @@ fn cors_options() -> CorsOptions {
#[rocket::main]
async fn main() -> Result<(), Error> {
rocket::ignite()
rocket::build()
.mount("/", routes![borrowed, response, owned, owned_options,])
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options().to_cors().expect("To not fail"))

View File

@ -56,7 +56,7 @@ fn cors_options_all() -> CorsOptions {
#[rocket::main]
async fn main() -> Result<(), Error> {
rocket::ignite()
rocket::build()
.mount("/", routes![app, ping, ping_options,])
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options().to_cors().expect("To not fail"))

View File

@ -1,8 +1,9 @@
//! Fairing implementation
#[allow(unused_imports)]
use ::log::{error, info};
use rocket::http::{self, uri::Origin, Status};
use rocket::{self, error_, info_, log_, outcome::Outcome, Request};
use rocket::{self, error_, info_, outcome::Outcome, Request};
use crate::{
actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error,
@ -19,14 +20,14 @@ enum CorsValidation {
struct FairingErrorRoute {}
#[rocket::async_trait]
impl rocket::handler::Handler for FairingErrorRoute {
async fn handle<'r, 's: 'r>(
&'s self,
impl rocket::route::Handler for FairingErrorRoute {
async fn handle<'r>(
&self,
request: &'r Request<'_>,
_: rocket::Data,
) -> rocket::handler::Outcome<'r> {
) -> rocket::route::Outcome<'r> {
let status = request
.get_param::<u16>(0)
.param::<u16>(0)
.unwrap_or(Ok(0))
.unwrap_or_else(|e| {
error_!("Fairing Error Handling Route error: {:?}", e);
@ -102,13 +103,13 @@ impl rocket::fairing::Fairing for Cors {
fn info(&self) -> rocket::fairing::Info {
rocket::fairing::Info {
name: "CORS",
kind: rocket::fairing::Kind::Attach
kind: rocket::fairing::Kind::Ignite
| rocket::fairing::Kind::Request
| rocket::fairing::Kind::Response,
}
}
async fn on_attach(&self, rocket: rocket::Rocket) -> Result<rocket::Rocket, rocket::Rocket> {
async fn on_ignite(&self, rocket: rocket::Rocket<rocket::Build>) -> rocket::fairing::Result {
Ok(rocket.mount(
&self.fairing_route_base,
vec![fairing_route(self.fairing_route_rank)],
@ -164,8 +165,8 @@ mod tests {
.expect("Not to fail")
}
fn rocket(fairing: Cors) -> Rocket {
Rocket::ignite().attach(fairing)
fn rocket(fairing: Cors) -> Rocket<rocket::Build> {
Rocket::build().attach(fairing)
}
#[test]
@ -187,8 +188,11 @@ mod tests {
}
#[rocket::async_test]
async fn error_route_is_mounted_on_attach() {
let rocket = rocket(make_cors_options());
async fn error_route_is_mounted_on_ignite() {
let rocket = rocket(make_cors_options())
.ignite()
.await
.expect("to ignite");
let expected_uri = format!("{}/<status>", CORS_ROOT);
let error_route = rocket

View File

@ -134,11 +134,11 @@ impl fmt::Display for Origin {
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for Origin {
impl<'r> FromRequest<'r> for Origin {
type Error = crate::Error;
async fn from_request(
request: &'a rocket::Request<'r>,
request: &'r rocket::Request<'_>,
) -> request::Outcome<Self, crate::Error> {
Origin::from_request_sync(request)
}
@ -180,11 +180,11 @@ impl FromStr for AccessControlRequestMethod {
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for AccessControlRequestMethod {
impl<'r> FromRequest<'r> for AccessControlRequestMethod {
type Error = crate::Error;
async fn from_request(
request: &'a rocket::Request<'r>,
request: &'r rocket::Request<'_>,
) -> request::Outcome<Self, crate::Error> {
AccessControlRequestMethod::from_request_sync(request)
}
@ -238,11 +238,11 @@ impl FromStr for AccessControlRequestHeaders {
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for AccessControlRequestHeaders {
impl<'r> FromRequest<'r> for AccessControlRequestHeaders {
type Error = crate::Error;
async fn from_request(
request: &'a rocket::Request<'r>,
request: &'r rocket::Request<'_>,
) -> request::Outcome<Self, crate::Error> {
AccessControlRequestHeaders::from_request_sync(request)
}
@ -266,7 +266,7 @@ mod tests {
/// Make a client with no routes for unit testing
fn make_client() -> Client {
let rocket = rocket::ignite();
let rocket = rocket::build();
Client::tracked(rocket).expect("valid rocket instance")
}

View File

@ -280,12 +280,13 @@ use std::marker::PhantomData;
use std::ops::Deref;
use std::str::FromStr;
#[allow(unused_imports)]
use ::log::{debug, error, info};
use regex::RegexSet;
use rocket::http::{self, Status};
use rocket::request::{FromRequest, Request};
use rocket::response;
use rocket::{debug_, error_, info_, log_, outcome::Outcome, State};
use rocket::{debug_, error_, info_, outcome::Outcome, State};
#[cfg(feature = "serialization")]
use serde_derive::{Deserialize, Serialize};
@ -1516,10 +1517,10 @@ impl<'r, 'o: 'r> Guard<'r> {
}
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for Guard<'r> {
impl<'r> FromRequest<'r> for Guard<'r> {
type Error = Error;
async fn from_request(request: &'a Request<'r>) -> rocket::request::Outcome<Self, Self::Error> {
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
let options = match request.guard::<State<'_, Cors>>().await {
Outcome::Success(options) => options,
_ => {
@ -1988,20 +1989,12 @@ fn actual_request_response(options: &Cors, origin: &str) -> Response {
///
/// See the documentation at the [crate root](index.html) for usage information.
pub fn catch_all_options_routes() -> Vec<rocket::Route> {
vec![
rocket::Route::ranked(
isize::max_value(),
http::Method::Options,
"/",
CatchAllOptionsRouteHandler {},
),
rocket::Route::ranked(
isize::max_value(),
vec![rocket::Route::ranked(
isize::MAX,
http::Method::Options,
"/<catch_all_options_route..>",
CatchAllOptionsRouteHandler {},
),
]
)]
}
/// Handler for the "catch all options route"
@ -2009,15 +2002,15 @@ pub fn catch_all_options_routes() -> Vec<rocket::Route> {
struct CatchAllOptionsRouteHandler {}
#[rocket::async_trait]
impl rocket::handler::Handler for CatchAllOptionsRouteHandler {
async fn handle<'r, 's: 'r>(
&'s self,
impl rocket::route::Handler for CatchAllOptionsRouteHandler {
async fn handle<'r>(
&self,
request: &'r Request<'_>,
_: rocket::Data,
) -> rocket::handler::Outcome<'r> {
) -> rocket::route::Outcome<'r> {
let guard: Guard<'_> = match request.guard().await {
Outcome::Success(guard) => guard,
Outcome::Failure((status, _)) => return rocket::handler::Outcome::failure(status),
Outcome::Failure((status, _)) => return rocket::route::Outcome::failure(status),
Outcome::Forward(()) => unreachable!("Should not be reachable"),
};
@ -2026,7 +2019,7 @@ impl rocket::handler::Handler for CatchAllOptionsRouteHandler {
request
);
rocket::handler::Outcome::from(request, guard.responder(()))
rocket::route::Outcome::from(request, guard.responder(()))
}
}
@ -2080,7 +2073,7 @@ mod tests {
/// Make a client with no routes for unit testing
fn make_client() -> Client {
let rocket = rocket::ignite();
let rocket = rocket::build();
Client::tracked(rocket).expect("valid rocket instance")
}

View File

@ -36,8 +36,8 @@ fn make_cors() -> Cors {
.expect("To not fail")
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build()
.mount("/", routes![cors, panicking_route])
.attach(make_cors())
}

View File

@ -78,8 +78,8 @@ fn make_cors() -> cors::Cors {
.expect("To not fail")
}
fn make_rocket() -> rocket::Rocket {
rocket::ignite()
fn make_rocket() -> rocket::Rocket<rocket::Build> {
rocket::build()
.mount("/", routes![cors_responder, panicking_route])
.mount(
"/",

View File

@ -32,7 +32,7 @@ fn request_headers(
/// Tests that all the request headers are parsed correcly in a HTTP request
#[test]
fn request_headers_round_trip_smoke_test() {
let rocket = rocket::ignite().mount("/", routes![request_headers]);
let rocket = rocket::build().mount("/", routes![request_headers]);
let client = Client::tracked(rocket).expect("A valid Rocket client");
let origin_header = Header::new(ORIGIN.as_str(), "https://foo.bar.xyz");

View File

@ -95,8 +95,8 @@ fn make_different_cors_options() -> CorsOptions {
}
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build()
.mount("/", routes![cors, panicking_route])
.mount("/", routes![owned, owned_options])
.mount("/", catch_all_options_routes()) // mount the catch all routes

View File

@ -61,8 +61,8 @@ fn cors_options_all() -> CorsOptions {
Default::default()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build()
.mount("/", routes![app, ping, ping_options,])
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options().to_cors().expect("Not to fail"))