aboutsummaryrefslogtreecommitdiff
path: root/src/https.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-05-06 12:21:15 +0200
committerAlex Auvolat <alex@adnab.me>2022-05-06 12:21:15 +0200
commit8c6114c3d306acebca908f37861e2c03d8562032 (patch)
tree41431ed388b7eac30e56dfdca829a9f9d2fef03a /src/https.rs
parent6383d9877282b98fa058ea4101ca6a8578c7514e (diff)
downloadtricot-8c6114c3d306acebca908f37861e2c03d8562032.tar.gz
tricot-8c6114c3d306acebca908f37861e2c03d8562032.zip
Try to clean up code and to fix WebSocket problemsdocker-38
Diffstat (limited to 'src/https.rs')
-rw-r--r--src/https.rs39
1 files changed, 21 insertions, 18 deletions
diff --git a/src/https.rs b/src/https.rs
index 83eca7c..7dcf051 100644
--- a/src/https.rs
+++ b/src/https.rs
@@ -194,25 +194,24 @@ async fn handle(
handle_error(reverse_proxy::call(remote_addr.ip(), &to_addr, req).await)
};
- // Do further processing (compression, additionnal headers) only for 2xx responses
- if !response.status().is_success() {
- return Ok(response);
+ if response.status().is_success() {
+ // (TODO: maybe we want to add these headers even if it's not a success?)
+ for (header, value) in proxy_to.add_headers.iter() {
+ response.headers_mut().insert(
+ HeaderName::from_bytes(header.as_bytes())?,
+ HeaderValue::from_str(value)?,
+ );
+ }
}
- for (header, value) in proxy_to.add_headers.iter() {
- response.headers_mut().insert(
- HeaderName::from_bytes(header.as_bytes())?,
- HeaderValue::from_str(value)?,
- );
- }
- trace!("Response: {:?}", response);
- info!("{} {} {}", method, response.status().as_u16(), uri);
-
if https_config.enable_compression {
- try_compress(response, method, accept_encoding, &https_config).await
- } else {
- Ok(response)
- }
+ response =
+ try_compress(response, method.clone(), accept_encoding, &https_config).await?
+ };
+
+ trace!("Final response: {:?}", response);
+ info!("{} {} {}", method, response.status().as_u16(), uri);
+ Ok(response)
} else {
debug!("{}{} -> NOT FOUND", host, path);
info!("{} 404 {}", method, uri);
@@ -240,10 +239,14 @@ async fn try_compress(
https_config: &HttpsConfig,
) -> Result<Response<Body>> {
// Don't bother compressing successfull responses for HEAD and PUT (they should have an empty body)
- // Don't compress partial content, that would be wierd
- // If already compressed, return as is
+ // Don't compress partial content as it causes issues
+ // Don't bother compressing non-2xx results
+ // Don't compress Upgrade responses (e.g. websockets)
+ // Don't compress responses that are already compressed
if (response.status().is_success() && (method == Method::HEAD || method == Method::PUT))
|| response.status() == StatusCode::PARTIAL_CONTENT
+ || !response.status().is_success()
+ || response.headers().get(header::CONNECTION) == Some(&HeaderValue::from_static("Upgrade"))
|| response.headers().get(header::CONTENT_ENCODING).is_some()
{
return Ok(response);