Skip to content

Commit

Permalink
Move reverse proxy case into match block
Browse files Browse the repository at this point in the history
  • Loading branch information
mbr committed Jan 6, 2024
1 parent bebd3ea commit 4101850
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions src/reverse_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,44 @@ async fn route_request(
routing_table.get_destination_uri_from_request(&request)
};

let dest = match dest_uri {
Destination::ReverseProxied(dest) => dest,
match dest_uri {
Destination::ReverseProxied(dest) => {
trace!(%dest, "reverse proxying");

// Note: `reqwest` and `axum` currently use different versions of `http`
let method = request.method().to_string().parse().map_err(|_| {
AppError::AssertionFailed("method http version mismatch workaround failed")
})?;
let response = rp.client.request(method, dest.to_string()).send().await;

match response {
Ok(response) => {
let mut bld = Response::builder().status(response.status().as_u16());
for (key, value) in response.headers() {
if HOP_BY_HOP.contains(key) {
continue;
}

let key_string = key.to_string();
let value_str = value.to_str().map_err(|_| AppError::NonUtf8Header)?;

bld = bld.header(key_string, value_str);
}
Ok(bld.body(Body::from(response.bytes().await?)).map_err(|_| {
AppError::AssertionFailed("should not fail to construct response")
})?)
}
Err(err) => {
warn!(%err, %dest, "failed request");
Ok(Response::builder()
.status(500)
.body(Body::empty())
.map_err(|_| {
AppError::AssertionFailed("should not fail to construct error response")
})?)
}
}
}
Destination::Internal(uri) => {
let method = request.method().clone();
// Note: The auth functionality has been lifted from `registry`. It may need to be
Expand Down Expand Up @@ -321,7 +357,7 @@ async fn route_request(
.get()
.ok_or_else(|| AppError::AssertionFailed("no orchestrator configured"))?;

return match method {
match method {
Method::GET => {
let config = orchestrator
.load_config(&manifest_reference)
Expand All @@ -342,47 +378,9 @@ async fn route_request(
Ok(stored.into_response())
}
_ => Err(AppError::InternalUrlInvalid),
};
}
Destination::NotFound => {
return Err(AppError::NoSuchContainer);
}
};
trace!(%dest, "reverse proxying");

// Note: `reqwest` and `axum` currently use different versions of `http`
let method =
request.method().to_string().parse().map_err(|_| {
AppError::AssertionFailed("method http version mismatch workaround failed")
})?;
let response = rp.client.request(method, dest.to_string()).send().await;

match response {
Ok(response) => {
let mut bld = Response::builder().status(response.status().as_u16());
for (key, value) in response.headers() {
if HOP_BY_HOP.contains(key) {
continue;
}

let key_string = key.to_string();
let value_str = value.to_str().map_err(|_| AppError::NonUtf8Header)?;

bld = bld.header(key_string, value_str);
}
Ok(bld
.body(Body::from(response.bytes().await?))
.map_err(|_| AppError::AssertionFailed("should not fail to construct response"))?)
}
Err(err) => {
warn!(%err, %dest, "failed request");
Ok(Response::builder()
.status(500)
.body(Body::empty())
.map_err(|_| {
AppError::AssertionFailed("should not fail to construct error response")
})?)
}
Destination::NotFound => Err(AppError::NoSuchContainer),
}
}

Expand Down

0 comments on commit 4101850

Please sign in to comment.