Skip to content

Commit

Permalink
Update to latest master Rocket version (#89) (#114)
Browse files Browse the repository at this point in the history
* Update to latest master Rocket version (#89)

* Fix "useless `vec!`"

* Fix docs

---------

Co-authored-by: Yong Wen Chua <lawliet89@users.noreply.github.com>
  • Loading branch information
j03-dev and lawliet89 authored Nov 17, 2023
1 parent 985098d commit 6b5205c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serialization = ["serde", "serde_derive", "unicase_serde"]

[dependencies]
regex = "1.7.2"
rocket = { version = "0.5.0-rc.3", default-features = false }
rocket = { version = "0.5.0-rc.4", default-features = false }
log = "0.4"
unicase = "2.6"
url = "2.3.1"
Expand Down
2 changes: 1 addition & 1 deletion src/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl rocket::route::Handler for FairingErrorRoute {
500
});
let status = Status::from_code(status).unwrap_or(Status::InternalServerError);
Outcome::Failure(status)
Outcome::Error(status)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ impl Origin {
match request.headers().get_one("Origin") {
Some(origin) => match Self::from_str(origin) {
Ok(origin) => Outcome::Success(origin),
Err(e) => Outcome::Failure((Status::BadRequest, e)),
Err(e) => Outcome::Error((Status::BadRequest, e)),
},
None => Outcome::Forward(()),
None => Outcome::Forward(Status::default()),
}
}
}
Expand Down Expand Up @@ -164,9 +164,9 @@ impl 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(_) => Outcome::Failure((Status::BadRequest, crate::Error::BadRequestMethod)),
Err(_) => Outcome::Error((Status::BadRequest, crate::Error::BadRequestMethod)),
},
None => Outcome::Forward(()),
None => Outcome::Forward(Status::default()),
}
}
}
Expand Down Expand Up @@ -214,7 +214,7 @@ impl AccessControlRequestHeaders {
unreachable!("`AccessControlRequestHeaders::from_str` should never fail")
}
},
None => Outcome::Forward(()),
None => Outcome::Forward(Status::default()),
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ Rocket's [managed state](https://rocket.rs/guide/state/#managed-state).
verb.
- Then in all the routes you want to enforce CORS on, add a
[Request Guard](https://rocket.rs/guide/requests/#request-guards) for the
[`Guard`](Guard) struct in the route arguments. You should not wrap this in an
[`Guard`] struct in the route arguments. You should not wrap this in an
`Option` or `Result` because the guard will let non-CORS requests through and will take over
error handling in case of errors.
- In your routes, to add CORS headers to your responses, use the appropriate functions on the
[`Guard`](Guard) for a `Response` or a `Responder`.
[`Guard`] for a `Response` or a `Responder`.
Refer to the [example](https://github.com/lawliet89/rocket_cors/blob/master/examples/guard.rs).
Expand Down Expand Up @@ -1515,13 +1515,13 @@ impl<'r> FromRequest<'r> for Guard<'r> {
Outcome::Success(options) => options,
_ => {
let error = Error::MissingCorsInRocketState;
return Outcome::Failure((error.status(), error));
return Outcome::Error((error.status(), error));
}
};

match Response::validate_and_build(options, request) {
Ok(response) => Outcome::Success(Self::new(response)),
Err(error) => Outcome::Failure((error.status(), error)),
Err(error) => Outcome::Error((error.status(), error)),
}
}
}
Expand Down Expand Up @@ -1757,27 +1757,27 @@ fn validate_allowed_headers(
/// Gets the `Origin` request header from the request
fn origin(request: &Request<'_>) -> Result<Option<Origin>, Error> {
match Origin::from_request_sync(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Forward(_) => Ok(None),
Outcome::Success(origin) => Ok(Some(origin)),
Outcome::Failure((_, err)) => Err(err),
Outcome::Error((_, err)) => Err(err),
}
}

/// Gets the `Access-Control-Request-Method` request header from the request
fn request_method(request: &Request<'_>) -> Result<Option<AccessControlRequestMethod>, Error> {
match AccessControlRequestMethod::from_request_sync(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Forward(_) => Ok(None),
Outcome::Success(method) => Ok(Some(method)),
Outcome::Failure((_, err)) => Err(err),
Outcome::Error((_, err)) => Err(err),
}
}

/// Gets the `Access-Control-Request-Headers` request header from the request
fn request_headers(request: &Request<'_>) -> Result<Option<AccessControlRequestHeaders>, Error> {
match AccessControlRequestHeaders::from_request_sync(request) {
Outcome::Forward(()) => Ok(None),
Outcome::Forward(_) => Ok(None),
Outcome::Success(geaders) => Ok(Some(geaders)),
Outcome::Failure((_, err)) => Err(err),
Outcome::Error((_, err)) => Err(err),
}
}

Expand Down Expand Up @@ -1997,8 +1997,8 @@ impl rocket::route::Handler for CatchAllOptionsRouteHandler {
) -> rocket::route::Outcome<'r> {
let guard: Guard<'_> = match request.guard().await {
Outcome::Success(guard) => guard,
Outcome::Failure((status, _)) => return rocket::route::Outcome::failure(status),
Outcome::Forward(()) => unreachable!("Should not be reachable"),
Outcome::Error((status, _)) => return rocket::route::Outcome::Error(status),
Outcome::Forward(_) => unreachable!("Should not be reachable"),
};

info_!(
Expand Down Expand Up @@ -2461,7 +2461,7 @@ mod tests {
#[test]
fn all_allowed_headers_are_validated_correctly() {
let allowed_headers = AllOrSome::All;
let requested_headers = vec!["Bar", "Foo"];
let requested_headers = ["Bar", "Foo"];

not_err!(validate_allowed_headers(
&FromStr::from_str(&requested_headers.join(",")).unwrap(),
Expand All @@ -2473,8 +2473,8 @@ mod tests {
/// echoes back the list that is actually requested for and not the whole list
#[test]
fn allowed_headers_are_validated_correctly() {
let allowed_headers = vec!["Bar", "Baz", "Foo"];
let requested_headers = vec!["Bar", "Foo"];
let allowed_headers = ["Bar", "Baz", "Foo"];
let requested_headers = ["Bar", "Foo"];

not_err!(validate_allowed_headers(
&FromStr::from_str(&requested_headers.join(",")).unwrap(),
Expand All @@ -2490,8 +2490,8 @@ mod tests {
#[test]
#[should_panic(expected = "HeadersNotAllowed")]
fn allowed_headers_errors_on_non_subset() {
let allowed_headers = vec!["Bar", "Baz", "Foo"];
let requested_headers = vec!["Bar", "Foo", "Unknown"];
let allowed_headers = ["Bar", "Baz", "Foo"];
let requested_headers = ["Bar", "Foo", "Unknown"];

validate_allowed_headers(
&FromStr::from_str(&requested_headers.join(",")).unwrap(),
Expand Down

0 comments on commit 6b5205c

Please sign in to comment.