Skip to content

Commit 4a69848

Browse files
committed
fix: fixed route bug, fix incorrect headers parsing, added errors
Signed-off-by: Martin <martin@hotmail.com.br>
1 parent dff4aff commit 4a69848

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/infrastructure/server_impl/server.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ pub fn get_router() -> &'static Regex {
2020
ROUTER.get_or_init(|| Regex::new(r#"clientes/(\d)/(transacoes|extrato)"#).unwrap())
2121
}
2222

23-
pub fn match_routes(server_data: &ServerData, request: Request) -> Either<Response, Response> {
23+
pub async fn match_routes(
24+
server_data: &ServerData,
25+
request: Request<'_>,
26+
) -> Either<Response, Response> {
2427
let Some(route) = get_router().captures(request.resource) else {
25-
return Either::Right(StatusCode::NotFound.into());
28+
return Either::Right(Response::from_status_code(
29+
StatusCode::NotFound,
30+
"route error".to_string(),
31+
));
2632
};
2733
let client_id = route
2834
.get(1)
@@ -31,36 +37,45 @@ pub fn match_routes(server_data: &ServerData, request: Request) -> Either<Respon
3137

3238
// the fastest router in existence!
3339
let response = match route.get(2).map(|c| c.as_str()).unwrap() {
34-
"extrato" => statement_route(server_data, request, client_id),
35-
"transacao" => transaction_route(server_data, request, client_id),
36-
_ => unreachable!(),
40+
"extrato" => statement_route(server_data, request, client_id).await,
41+
"transacoes" => transaction_route(server_data, request, client_id).await,
42+
_ => {
43+
return Either::Right(Response::from_status_code(
44+
StatusCode::NotFound,
45+
"route not found".to_string(),
46+
))
47+
}
3748
};
3849

3950
Either::Left(response.unwrap())
4051
}
4152

4253
#[allow(clippy::upper_case_acronyms, non_camel_case_types)]
4354
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Enum)]
55+
#[non_exhaustive]
4456
pub enum Header {
45-
HOST,
46-
USER_AGENT,
4757
ACCEPT,
48-
CONTENT_TYPE,
58+
ACCEPT_ENCODING,
4959
CONTENT_LENGTH,
60+
CONTENT_TYPE,
61+
CONNECTION,
62+
HOST,
5063
KEEP_ALIVE,
64+
USER_AGENT,
5165
}
5266

5367
impl FromStr for Header {
5468
type Err = ();
5569

5670
fn from_str(s: &str) -> Result<Self, Self::Err> {
57-
let res = match s {
58-
"Host" => Self::HOST,
59-
"User-Agent" => Self::USER_AGENT,
60-
"Accept" => Self::ACCEPT,
61-
"Content-Type" => Self::CONTENT_TYPE,
62-
"Content-Length" => Self::CONTENT_LENGTH,
63-
"Keep-Alive" => Self::KEEP_ALIVE,
71+
let res = match s.to_lowercase().as_str() {
72+
"accept" => Self::ACCEPT,
73+
"accept-encoding" => Self::ACCEPT_ENCODING,
74+
"content-length" => Self::CONTENT_LENGTH,
75+
"content-type" => Self::CONTENT_TYPE,
76+
"host" => Self::HOST,
77+
"connection" => Self::CONNECTION,
78+
"user-agent" => Self::USER_AGENT,
6479
_ => return Err(()),
6580
};
6681

0 commit comments

Comments
 (0)