@@ -378,8 +378,29 @@ def headers_received(
378378 # TODO: either make context an official part of the
379379 # HTTPConnection interface or figure out some other way to do this.
380380 self .connection .context ._apply_xheaders (headers ) # type: ignore
381+ start_line , headers = self .apply_forwarded_context (start_line , headers )
382+
381383 return self .delegate .headers_received (start_line , headers )
382384
385+ def apply_forwarded_context (
386+ self ,
387+ start_line : Union [httputil .RequestStartLine , httputil .ResponseStartLine ],
388+ headers : httputil .HTTPHeaders ,
389+ ) -> Tuple [
390+ Union [httputil .RequestStartLine , httputil .ResponseStartLine ],
391+ httputil .HTTPHeaders ,
392+ ]:
393+ """Apply X-Forwarded-Context header to requested uri"""
394+ if isinstance (start_line , httputil .RequestStartLine ):
395+ # get path from X-Forwarded-Context
396+ proxy_path = headers .get ("X-Forwarded-Context" , None )
397+ if proxy_path :
398+ # preserve only the path part
399+ path = proxy_path .split ("?" , 1 )[0 ]
400+ start_line = start_line ._replace (path = path )
401+
402+ return start_line , headers
403+
383404 def data_received (self , chunk : bytes ) -> Optional [Awaitable [None ]]:
384405 return self .delegate .data_received (chunk )
385406
0 commit comments