Skip to content

Commit 7d415ca

Browse files
committed
Fix more feedback from PR
1 parent 80111bf commit 7d415ca

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

sentry/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![deny(rust_2018_idioms)]
33

44
use crate::middleware::auth;
5-
use crate::middleware::cors::{cors, CorsResult};
5+
use crate::middleware::cors::{cors, Cors};
66
use hyper::service::{make_service_fn, service_fn};
77
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
88
use primitives::adapter::Adapter;
@@ -77,7 +77,12 @@ impl<A: Adapter + 'static> Application<A> {
7777
Ok::<_, Error>(service_fn(move |req| {
7878
let adapter_config = adapter_config.clone();
7979
let redis = redis.clone();
80-
async move { Ok::<_, Error>(handle_routing(req, (&adapter_config.0, &adapter_config.1), redis).await) }
80+
async move {
81+
Ok::<_, Error>(
82+
handle_routing(req, (&adapter_config.0, &adapter_config.1), redis)
83+
.await,
84+
)
85+
}
8186
}))
8287
}
8388
});
@@ -111,10 +116,10 @@ async fn handle_routing(
111116
redis: SharedConnection,
112117
) -> Response<Body> {
113118
let headers = match cors(&req) {
114-
CorsResult::Simple(headers) => headers,
119+
Some(Cors::Simple(headers)) => headers,
115120
// if we have a Preflight, just return the response directly
116-
CorsResult::Preflight(response) => return response,
117-
CorsResult::None => Default::default(),
121+
Some(Cors::Preflight(response)) => return response,
122+
None => Default::default(),
118123
};
119124

120125
let req = match auth::for_request(req, adapter, redis.clone()).await {

sentry/src/middleware/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) async fn for_request(
2222
hv.to_str()
2323
.map(|token_str| {
2424
if token_str.starts_with(prefix) {
25-
Some(token_str.replacen(prefix, "", 1))
25+
Some(token_str[prefix.len()..].to_string())
2626
} else {
2727
None
2828
}

sentry/src/middleware/cors.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
use hyper::header::{ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_REQUEST_HEADERS};
22
use hyper::{Body, HeaderMap, Method, Request, Response, StatusCode};
33

4-
pub enum CorsResult {
4+
pub enum Cors {
55
Preflight(Response<Body>),
66
Simple(HeaderMap),
7-
None,
8-
}
9-
10-
impl CorsResult {
11-
pub fn is_none(&self) -> bool {
12-
match self {
13-
Self::None => true,
14-
_ => false,
15-
}
16-
}
177
}
188

199
/// Cross-Origin Resource Sharing request handler
2010
/// Allows all origins and methods and supports Preflighted `OPTIONS` requests.
2111
/// On Simple CORS requests sets initial `HeaderMap` to be used in the routes handlers.
2212
/// Otherwise returns a `OPTION` Response.
23-
pub(crate) fn cors(req: &Request<Body>) -> CorsResult {
13+
pub(crate) fn cors(req: &Request<Body>) -> Option<Cors> {
2414
use hyper::header::{
2515
HeaderValue, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_LENGTH,
2616
ORIGIN,
@@ -62,11 +52,11 @@ pub(crate) fn cors(req: &Request<Body>) -> CorsResult {
6252
// set the headers of the Request
6353
*response.headers_mut() = headers;
6454

65-
CorsResult::Preflight(response)
55+
Some(Cors::Preflight(response))
6656
} else if !headers.is_empty() {
67-
CorsResult::Simple(headers)
57+
Some(Cors::Simple(headers))
6858
} else {
69-
CorsResult::None
59+
None
7060
}
7161
}
7262

@@ -87,7 +77,7 @@ mod test {
8777
.unwrap();
8878

8979
let allowed_origin_headers = match cors(&cors_req) {
90-
CorsResult::Simple(headers) => headers,
80+
Some(Cors::Simple(headers)) => headers,
9181
_ => panic!("Simple CORS headers were expected"),
9282
};
9383
assert_eq!(
@@ -120,7 +110,7 @@ mod test {
120110
.unwrap();
121111

122112
let response = match cors(&cors_req) {
123-
CorsResult::Preflight(headers) => headers,
113+
Some(Cors::Preflight(headers)) => headers,
124114
_ => panic!("Preflight CORS response was expected"),
125115
};
126116

0 commit comments

Comments
 (0)