Skip to content

Commit

Permalink
Update to rocket 05rc3 (#113)
Browse files Browse the repository at this point in the history
* Update rocket dependency to v0.5.0-rc.3

* Update dependencies

* Fix clippy lint

* Fix clippy

* Fix elided lifetime

* Remove spurious lines

* Fix format

---------

Co-authored-by: Yong Wen Chua <[email protected]>
  • Loading branch information
flosse and lawliet89 authored Mar 25, 2023
1 parent c17e814 commit c3acfbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ default = ["serialization"]
serialization = ["serde", "serde_derive", "unicase_serde"]

[dependencies]
regex = "1.5.5"
rocket = { version = "0.5.0-rc.2", default-features = false }
regex = "1.7.2"
rocket = { version = "0.5.0-rc.3", default-features = false }
log = "0.4"
unicase = "2.0"
url = "2.1.0"
unicase = "2.6"
url = "2.3.1"
http = "0.2"

# Optional dependencies that are activated by the various features
Expand Down
42 changes: 17 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ See the [example](https://github.com/lawliet89/rocket_cors/blob/master/examples/
*/

#![deny(
const_err,
dead_code,
deprecated,
arithmetic_overflow,
Expand Down Expand Up @@ -434,19 +433,15 @@ impl From<regex::Error> for Error {
/// ["Externally tagged"](https://serde.rs/enum-representations.html)
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
#[derive(Default)]
pub enum AllOrSome<T> {
/// Everything is allowed. Usually equivalent to the "*" value.
#[default]
All,
/// Only some of `T` is allowed
Some(T),
}

impl<T> Default for AllOrSome<T> {
fn default() -> Self {
AllOrSome::All
}
}

impl<T> AllOrSome<T> {
/// Returns whether this is an `All` variant
pub fn is_all(&self) -> bool {
Expand Down Expand Up @@ -1468,10 +1463,7 @@ impl Response {
}

/// Validate and create a new CORS Response from a request and settings
pub fn validate_and_build<'a, 'r>(
options: &'a Cors,
request: &'a Request<'r>,
) -> Result<Self, Error> {
pub fn validate_and_build<'a>(options: &'a Cors, request: &'a Request) -> Result<Self, Error> {
validate_and_build(options, request)
}
}
Expand Down Expand Up @@ -1735,7 +1727,7 @@ fn validate_allowed_method(
method: &AccessControlRequestMethod,
allowed_methods: &AllowedMethods,
) -> Result<(), Error> {
let &AccessControlRequestMethod(ref request_method) = method;
let AccessControlRequestMethod(request_method) = method;
if !allowed_methods.iter().any(|m| m == request_method) {
return Err(Error::MethodNotAllowed(method.0.to_string()));
}
Expand All @@ -1749,7 +1741,7 @@ fn validate_allowed_headers(
headers: &AccessControlRequestHeaders,
allowed_headers: &AllowedHeaders,
) -> Result<(), Error> {
let &AccessControlRequestHeaders(ref headers) = headers;
let AccessControlRequestHeaders(headers) = headers;

match *allowed_headers {
AllOrSome::All => Ok(()),
Expand Down Expand Up @@ -1897,7 +1889,7 @@ fn preflight_response(

// We do not do anything special with simple headers
if let Some(headers) = headers {
let &AccessControlRequestHeaders(ref headers) = headers;
let AccessControlRequestHeaders(headers) = headers;
response.headers(
headers
.iter()
Expand Down Expand Up @@ -2242,7 +2234,7 @@ mod tests {
#[test]
fn validate_origin_allows_all_origins() {
let url = "https://www.example.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
let allowed_origins = AllOrSome::All;

not_err!(validate_origin(&origin, &allowed_origins));
Expand All @@ -2251,7 +2243,7 @@ mod tests {
#[test]
fn validate_origin_allows_origin() {
let url = "https://www.example.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
let allowed_origins = not_err!(parse_allowed_origins(&AllowedOrigins::some_exact(&[
"https://www.example.com"
])));
Expand All @@ -2270,7 +2262,7 @@ mod tests {
];

for (url, allowed_origin) in cases {
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
let allowed_origins = not_err!(parse_allowed_origins(&AllowedOrigins::some_exact(&[
allowed_origin
])));
Expand All @@ -2287,18 +2279,18 @@ mod tests {
])));

let url = "https://www.example-something.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
not_err!(validate_origin(&origin, &allowed_origins));

let url = "https://subdomain.acme.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
not_err!(validate_origin(&origin, &allowed_origins));
}

#[test]
fn validate_origin_validates_opaque_origins() {
let url = "moz-extension://8c7c4444-e29f-…cb8-1ade813dbd12/js/content.js:505";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
let allowed_origins = not_err!(parse_allowed_origins(&AllowedOrigins::some_regex(&[
"moz-extension://.*"
])));
Expand All @@ -2314,19 +2306,19 @@ mod tests {
)));

let url = "https://www.example-something123.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
not_err!(validate_origin(&origin, &allowed_origins));

let url = "https://www.acme.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
not_err!(validate_origin(&origin, &allowed_origins));
}

#[test]
#[should_panic(expected = "OriginNotAllowed")]
fn validate_origin_rejects_invalid_origin() {
let url = "https://www.acme.com";
let origin = not_err!(to_parsed_origin(&url));
let origin = not_err!(to_parsed_origin(url));
let allowed_origins = not_err!(parse_allowed_origins(&AllowedOrigins::some_exact(&[
"https://www.example.com"
])));
Expand Down Expand Up @@ -2489,7 +2481,7 @@ mod tests {
&AllOrSome::Some(
allowed_headers
.iter()
.map(|s| FromStr::from_str(*s).unwrap())
.map(|s| FromStr::from_str(s).unwrap())
.collect(),
),
));
Expand All @@ -2506,7 +2498,7 @@ mod tests {
&AllOrSome::Some(
allowed_headers
.iter()
.map(|s| FromStr::from_str(*s).unwrap())
.map(|s| FromStr::from_str(s).unwrap())
.collect(),
),
)
Expand Down

0 comments on commit c3acfbf

Please sign in to comment.