@@ -86,6 +86,11 @@ pub async fn proxy_handler(req: Request<Body>) -> impl IntoResponse {
8686 let uri = req. uri ( ) . to_string ( ) ;
8787 tracing:: debug!( "🔗 Received request for URI: {}" , uri) ;
8888
89+ tracing:: debug!( "🔎 Incoming request headers:" ) ;
90+ for ( k, v) in req. headers ( ) . iter ( ) {
91+ tracing:: debug!( " {}: {:?}" , k, v) ;
92+ }
93+
8994 // Increment total request counter for each URI
9095 counter ! ( "cachebolt_proxy_requests_total" , "uri" => uri. clone( ) ) . increment ( 1 ) ;
9196
@@ -173,9 +178,11 @@ pub async fn proxy_handler(req: Request<Body>) -> impl IntoResponse {
173178 }
174179
175180 // Split response into parts
176- let ( parts, body) = resp. into_parts ( ) ;
181+ let ( mut parts, body) = resp. into_parts ( ) ;
177182 let body_bytes = hyper:: body:: to_bytes ( body) . await . unwrap_or_default ( ) ;
178183
184+ parts. headers . remove ( "content-length" ) ;
185+
179186 let headers_vec = parts
180187 . headers
181188 . iter ( )
@@ -214,7 +221,10 @@ pub async fn proxy_handler(req: Request<Body>) -> impl IntoResponse {
214221 ) ;
215222 }
216223 } else {
217- tracing:: info!( "⏩ Cache bypass activated for '{}' due to client header" , uri) ;
224+ tracing:: info!(
225+ "⏩ Cache bypass activated for '{}' due to client header" ,
226+ uri
227+ ) ;
218228 }
219229
220230 Response :: from_parts ( parts, Body :: from ( body_bytes) )
@@ -308,11 +318,22 @@ pub fn hash_uri(uri: &str) -> String {
308318}
309319
310320/// Sends an outbound GET request to the downstream backend
321+ /// Sends an outbound GET request to the downstream backend, forwarding all headers except 'accept-encoding'.
322+ /// This prevents curl: (52) Empty reply from server errors caused by unsupported encodings.
323+ ///
324+ /// # Arguments
325+ /// - `uri`: The path to append to the downstream base URL.
326+ /// - `original_req`: The incoming Axum request, from which headers are forwarded.
327+ ///
328+ /// # Returns
329+ /// - `Ok(Response)` with the downstream response if successful.
330+ /// - `Err(())` if the downstream call fails or the request could not be built.
311331pub async fn forward_request ( uri : & str , original_req : Request < Body > ) -> Result < Response < Body > , ( ) > {
332+ // Get the config and build the downstream full URL
312333 let cfg = CONFIG . get ( ) . unwrap ( ) ;
313334 let full_url = format ! ( "{}{}" , cfg. downstream_base_url, uri) ;
314335
315- // Log scheme/ host/path para debug (opcional, pero muy útil)
336+ // Debug: Log the scheme, host, and path of the downstream URL
316337 if let Ok ( parsed_url) = url:: Url :: parse ( & full_url) {
317338 tracing:: info!(
318339 "🌐 Downstream request: scheme='{}' host='{}' path='{}'" ,
@@ -322,14 +343,33 @@ pub async fn forward_request(uri: &str, original_req: Request<Body>) -> Result<R
322343 ) ;
323344 }
324345
346+ // Parse downstream_base_url to extract the host (domain)
347+ let downstream_host = url:: Url :: parse ( & cfg. downstream_base_url )
348+ . ok ( )
349+ . and_then ( |u| u. host_str ( ) . map ( |s| s. to_string ( ) ) )
350+ . unwrap_or_else ( || "" . to_string ( ) ) ;
351+
352+ // Build the request, starting with the URL and GET method
325353 let mut builder = Request :: builder ( ) . uri ( full_url. clone ( ) ) . method ( "GET" ) ;
326354
327- // Copia todos los headers originales
355+ // Copy all headers from the incoming request,
356+ // except for 'accept-encoding' and 'host'
357+ // (We want to control the Host header for SNI/proxying, and avoid content-encoding issues.)
328358 for ( key, value) in original_req. headers ( ) . iter ( ) {
359+ if key. as_str ( ) . eq_ignore_ascii_case ( "accept-encoding" )
360+ || key. as_str ( ) . eq_ignore_ascii_case ( "host" )
361+ {
362+ continue ;
363+ }
329364 builder = builder. header ( key, value) ;
330365 }
331366
332- // Construye el request
367+ // Inject the Host header, if it was successfully extracted from the downstream_base_url
368+ if !downstream_host. is_empty ( ) {
369+ builder = builder. header ( "Host" , downstream_host) ;
370+ }
371+
372+ // Build the final request object with empty body
333373 let req = match builder. body ( Body :: empty ( ) ) {
334374 Ok ( req) => req,
335375 Err ( e) => {
@@ -338,7 +378,7 @@ pub async fn forward_request(uri: &str, original_req: Request<Body>) -> Result<R
338378 }
339379 } ;
340380
341- // Ejecuta la request, maneja errores con logs detallados
381+ // Send the HTTP request to the downstream service
342382 match HTTP_CLIENT . request ( req) . await {
343383 Ok ( resp) => Ok ( resp) ,
344384 Err ( e) => {
0 commit comments