Skip to content

Commit

Permalink
handle Method::GET variant to get proxy_route
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Jun 30, 2024
1 parent efce9fa commit 1d752a0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ impl AppConfig {
route_index.map(|index| &self.proxy_routes[index])
}

#[allow(dead_code)]
pub(crate) fn get_proxy_route_by_uri_inbound(
&self,
uri: &mut Uri,
Expand Down
74 changes: 50 additions & 24 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) async fn insert_jwt_to_http_header(

pub(crate) async fn http_handler(
cfg: &AppConfig,
req: Request<Body>,
mut req: Request<Body>,
remote_addr: SocketAddr,
) -> GenericResult<Response<Body>> {
let req_uri = req.uri().clone();
Expand Down Expand Up @@ -93,29 +93,55 @@ pub(crate) async fn http_handler(
return handle_preflight();
}

// TODO instead of let inbound_route = match req.method() use get_proxy_route_by_uri_inbound fnc for GET method
// for other methods use get_proxy_route_by_inbound

let inbound_route = match req.method() {
// should take the second element as path string starts with a delimiter
&Method::GET => req.uri().path().split('/').nth(1).unwrap_or("").to_string(),
_ => req.uri().path().to_string(),
};

// create proxy_route before payload, as we need proxy_type from it for payload generation
let proxy_route = match cfg.get_proxy_route_by_inbound(&inbound_route) {
Some(proxy_route) => proxy_route,
None => {
log::warn!(
"{}",
log_format!(
remote_addr.ip(),
String::from("-"),
req.uri(),
"Proxy route not found, returning 404."
)
);
return response_by_status(StatusCode::NOT_FOUND);
let proxy_route = match req.method() {
&Method::GET => {
// Use get_proxy_route_by_uri_inbound for GET requests
match cfg.get_proxy_route_by_uri_inbound(req.uri_mut()) {
Ok(Some(route)) => route,
Ok(None) => {
log::warn!(
"{}",
log_format!(
remote_addr.ip(),
String::from("-"),
req_uri,
"Proxy route not found for GET request, returning 404."
)
);
return response_by_status(StatusCode::NOT_FOUND);
}
Err(e) => {
log::error!(
"{}",
log_format!(
remote_addr.ip(),
String::from("-"),
req_uri,
"Error finding proxy route for GET request: {}, returning 500.",
e
)
);
return response_by_status(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
_ => {
// Use get_proxy_route_by_inbound for other methods
match cfg.get_proxy_route_by_inbound(req.uri().path()) {
Some(proxy_route) => proxy_route,
None => {
log::warn!(
"{}",
log_format!(
remote_addr.ip(),
String::from("-"),
req_uri,
"Proxy route not found for non-GET request, returning 404."
)
);
return response_by_status(StatusCode::NOT_FOUND);
}
}
}
};

Expand Down

0 comments on commit 1d752a0

Please sign in to comment.