mirror of
https://github.com/storytold/voder-proxy.git
synced 2026-10-18 07:33:13 +00:00
don't build a new response, modify the old one
This commit is contained in:
+7
-27
@@ -75,7 +75,7 @@ pub async fn speak_proxy_with_retry(
|
|||||||
match result {
|
match result {
|
||||||
Ok(good_response) => {
|
Ok(good_response) => {
|
||||||
// All we want to do is add our special header
|
// All we want to do is add our special header
|
||||||
return modify_response(good_response, proxy_hostname, attempt);
|
return Ok(modify_response(good_response, proxy_hostname, attempt));
|
||||||
},
|
},
|
||||||
Err(_) => {},
|
Err(_) => {},
|
||||||
}
|
}
|
||||||
@@ -129,13 +129,10 @@ async fn speak_proxy(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn modify_response(response: Response<Body>, proxy_hostname: &str, retry_count: u8)
|
fn modify_response(mut response: Response<Body>, proxy_hostname: &str, retry_count: u8)
|
||||||
-> Result<Response<Body>, ErrorResponse>
|
-> Response<Body>
|
||||||
{
|
{
|
||||||
let status = response.status();
|
let headers = response.headers_mut();
|
||||||
|
|
||||||
let response_headers = {
|
|
||||||
let mut response_headers = response.headers().clone();
|
|
||||||
|
|
||||||
let proxy_hostname = HeaderValue::from_str(proxy_hostname)
|
let proxy_hostname = HeaderValue::from_str(proxy_hostname)
|
||||||
.ok()
|
.ok()
|
||||||
@@ -145,25 +142,8 @@ fn modify_response(response: Response<Body>, proxy_hostname: &str, retry_count:
|
|||||||
.ok()
|
.ok()
|
||||||
.unwrap_or(HeaderValue::from_static("unknown"));
|
.unwrap_or(HeaderValue::from_static("unknown"));
|
||||||
|
|
||||||
response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname);
|
headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname);
|
||||||
response_headers.append(HeaderName::from_static("x-retry-count"), retry_count);
|
headers.append(HeaderName::from_static("x-retry-count"), retry_count);
|
||||||
response_headers
|
|
||||||
};
|
|
||||||
|
|
||||||
let (_parts, body) = response.into_parts();
|
response
|
||||||
|
|
||||||
let mut response_builder = Response::builder()
|
|
||||||
.status(status);
|
|
||||||
|
|
||||||
for header in response_headers {
|
|
||||||
if let Some(header_name) = header.0.as_ref() {
|
|
||||||
response_builder = response_builder.header(header_name, header.1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
response_builder.body(body)
|
|
||||||
.map_err(|_| ErrorResponse {
|
|
||||||
error_type: ErrorType::ProxyError,
|
|
||||||
error_description: "could not modify response".to_string()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user