From 0b2d3e80b13d21162bd240f911e304a258ec3fb4 Mon Sep 17 00:00:00 2001 From: Eric Dattore Date: Thu, 27 May 2021 20:57:55 -0600 Subject: [PATCH] Update to latest Rocket master (#91) - Update all instances of `State` to borrow and drop anonymous lifetime - Remove examples showing returning a `Response` instead of a `Responder` since `Response` no longer implements `Responder` --- Cargo.toml | 2 +- examples/guard.rs | 13 +------------ examples/manual.rs | 22 +++------------------- src/fairing.rs | 4 ++-- src/headers.rs | 2 +- src/lib.rs | 7 +++---- tests/guard.rs | 15 +++------------ tests/manual.rs | 10 +++++----- 8 files changed, 19 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d47abe8..cbd6db4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ serialization = ["serde", "serde_derive", "unicase_serde"] [dependencies] regex = "1.1" -rocket = { rev="801e04bd5369eb39e126c75f6d11e1e9597304d8", git = "https://github.com/SergioBenitez/Rocket.git", default-features = false } +rocket = { rev="1f1976f8bf8531daf4ac596d83ea7ad589dd7368", git = "https://github.com/SergioBenitez/Rocket.git", default-features = false } log = "0.4" unicase = "2.0" url = "2.1.0" diff --git a/examples/guard.rs b/examples/guard.rs index 924b86b..ea152e2 100644 --- a/examples/guard.rs +++ b/examples/guard.rs @@ -1,8 +1,6 @@ use std::error::Error; -use std::io::Cursor; use rocket::http::Method; -use rocket::Response; use rocket::{get, options, routes}; use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder}; @@ -12,15 +10,6 @@ fn responder(cors: Guard<'_>) -> Responder<'_, '_, &str> { cors.responder("Hello CORS!") } -/// Using a `Response` instead of a `Responder`. You generally won't have to do this. -#[get("/response")] -fn response(cors: Guard<'_>) -> Response<'_> { - let mut response = Response::new(); - let body = "Hello CORS!"; - response.set_sized_body(body.len(), Cursor::new(body)); - cors.response(response) -} - /// Manually mount an OPTIONS route for your own handling #[options("/manual")] fn manual_options(cors: Guard<'_>) -> Responder<'_, '_, &str> { @@ -48,7 +37,7 @@ async fn main() -> Result<(), Box> { .to_cors()?; rocket::build() - .mount("/", routes![responder, response]) + .mount("/", routes![responder]) // Mount the routes to catch all the OPTIONS pre-flight requests .mount("/", rocket_cors::catch_all_options_routes()) // You can also manually mount an OPTIONS route that will be used instead diff --git a/examples/manual.rs b/examples/manual.rs index c87f10b..6863c96 100644 --- a/examples/manual.rs +++ b/examples/manual.rs @@ -1,9 +1,7 @@ -use std::io::Cursor; - use rocket::error::Error; use rocket::http::Method; use rocket::response::Responder; -use rocket::{get, options, routes, Response, State}; +use rocket::{get, options, routes, State}; use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions}; /// Using a borrowed Cors @@ -14,26 +12,12 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions}; /// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime /// `'r` and so does `Responder`! #[get("/")] -fn borrowed(options: State<'_, Cors>) -> impl Responder<'_, '_> { +fn borrowed(options: &State) -> impl Responder<'_, '_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS")) } -/// Using a `Response` instead of a `Responder`. You generally won't have to do this. -/// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime -/// `'r` and so does `Responder`! -#[get("/response")] -fn response(options: State<'_, Cors>) -> impl Responder<'_, '_> { - let mut response = Response::new(); - let body = "Hello CORS!"; - response.set_sized_body(body.len(), Cursor::new(body)); - - options - .inner() - .respond_borrowed(move |guard| guard.response(response)) -} - /// Create and use an ad-hoc Cors /// Note that the `'r` lifetime is needed because the compiler cannot elide anything. /// @@ -72,7 +56,7 @@ fn cors_options() -> CorsOptions { #[rocket::main] async fn main() -> Result<(), Error> { rocket::build() - .mount("/", routes![borrowed, response, owned, owned_options,]) + .mount("/", routes![borrowed, owned, owned_options,]) .mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes .manage(cors_options().to_cors().expect("To not fail")) .launch() diff --git a/src/fairing.rs b/src/fairing.rs index 0533b88..c2bd628 100644 --- a/src/fairing.rs +++ b/src/fairing.rs @@ -93,7 +93,7 @@ fn on_response_wrapper( request ); response.set_status(Status::NoContent); - let _ = response.take_body(); + let _ = response.body(); } Ok(()) } @@ -134,7 +134,7 @@ impl rocket::fairing::Fairing for Cors { if let Err(err) = on_response_wrapper(self, request, response) { error_!("Fairings on_response error: {}\nMost likely a bug", err); response.set_status(Status::InternalServerError); - let _ = response.take_body(); + let _ = response.body(); } } } diff --git a/src/headers.rs b/src/headers.rs index 45ea97b..8326ae3 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -124,7 +124,7 @@ impl FromStr for Origin { } impl fmt::Display for Origin { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Origin::Null => write!(f, "null"), Origin::Parsed(ref parsed) => write!(f, "{}", parsed.ascii_serialization()), diff --git a/src/lib.rs b/src/lib.rs index 1a841a9..7b20e86 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -353,7 +353,7 @@ impl Error { } 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 { Error::MissingOrigin => write!( f, @@ -1521,7 +1521,7 @@ impl<'r> FromRequest<'r> for Guard<'r> { type Error = Error; async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome { - let options = match request.guard::>().await { + let options = match request.guard::<&State>().await { Outcome::Success(options) => options, _ => { let error = Error::MissingCorsInRocketState; @@ -2523,8 +2523,7 @@ mod tests { let response = Response::new(); let response = response.response(response::Response::new()); - let headers: Vec<_> = response.headers().iter().collect(); - assert_eq!(headers.len(), 0); + assert_eq!(response.headers().iter().count(), 0); } #[test] diff --git a/tests/guard.rs b/tests/guard.rs index 876383d..8268c08 100644 --- a/tests/guard.rs +++ b/tests/guard.rs @@ -5,8 +5,8 @@ use rocket::http::hyper; use rocket::http::Method; use rocket::http::{Header, Status}; use rocket::local::blocking::Client; +use rocket::State; use rocket::{get, options, routes}; -use rocket::{Response, State}; static ORIGIN: hyper::HeaderName = hyper::header::ORIGIN; static ACCESS_CONTROL_REQUEST_METHOD: hyper::HeaderName = @@ -36,12 +36,6 @@ fn cors_manual(cors: cors::Guard<'_>) -> cors::Responder<'_, '_, &str> { cors.responder("Hello CORS") } -/// Using a `Response` instead of a `Responder` -#[get("/response")] -fn response(cors: cors::Guard<'_>) -> Response<'_> { - cors.response(Response::new()) -} - /// `Responder` with String #[get("/responder/string")] fn responder_string(cors: cors::Guard<'_>) -> cors::Responder<'_, 'static, String> { @@ -59,7 +53,7 @@ struct SomeState; #[get("/state")] fn state<'r, 'o: 'r>( cors: cors::Guard<'r>, - _state: State<'r, SomeState>, + _state: &State, ) -> cors::Responder<'r, 'o, &'r str> { cors.responder("hmm") } @@ -81,10 +75,7 @@ fn make_cors() -> cors::Cors { fn make_rocket() -> rocket::Rocket { rocket::build() .mount("/", routes![cors_responder, panicking_route]) - .mount( - "/", - routes![response, responder_string, responder_unit, state], - ) + .mount("/", routes![responder_string, responder_unit, state]) .mount("/", cors::catch_all_options_routes()) // mount the catch all routes .mount("/", routes![cors_manual, cors_manual_options]) // manual OPTIOONS routes .manage(make_cors()) diff --git a/tests/manual.rs b/tests/manual.rs index 3fa8f1e..3c26140 100644 --- a/tests/manual.rs +++ b/tests/manual.rs @@ -16,14 +16,14 @@ static ACCESS_CONTROL_REQUEST_HEADERS: hyper::HeaderName = /// Using a borrowed `Cors` #[get("/")] -fn cors(options: State<'_, Cors>) -> impl Responder<'_, '_> { +fn cors(options: &State) -> impl Responder<'_, '_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS")) } #[get("/panic")] -fn panicking_route(options: State<'_, Cors>) -> impl Responder<'_, '_> { +fn panicking_route(options: &State) -> impl Responder<'_, '_> { options.inner().respond_borrowed(|_| { panic!("This route will panic"); }) @@ -50,7 +50,7 @@ fn owned<'r, 'o: 'r>() -> impl Responder<'r, 'o> { /// `Responder` with String #[get("/")] #[allow(dead_code)] -fn responder_string(options: State<'_, Cors>) -> impl Responder<'_, '_> { +fn responder_string(options: &State) -> impl Responder<'_, '_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS".to_string())) @@ -61,8 +61,8 @@ struct TestState; #[get("/")] #[allow(dead_code)] fn borrow<'r, 'o: 'r>( - options: State<'r, Cors>, - test_state: State<'r, TestState>, + options: &'r State, + test_state: &'r State, ) -> impl Responder<'r, 'o> { let borrow = test_state.inner(); options.inner().respond_borrowed(move |guard| {