@@ -76,11 +76,16 @@ def _validated_allowed_paths(values: Sequence[str]) -> tuple[str, ...]:
7676 return normalized
7777
7878
79- def _upstream_connection (parsed : SplitResult , * , timeout_sec : float ):
79+ def _upstream_connection (
80+ parsed : SplitResult , * , timeout_sec : float
81+ ) -> http .client .HTTPConnection :
82+ host = parsed .hostname
83+ if host is None : # Defensive narrowing; _validated_upstream rejects this.
84+ raise ValueError ("upstream_base_url must include a host" )
8085 port = parsed .port
8186 if parsed .scheme == "https" :
82- return http .client .HTTPSConnection (parsed . hostname , port , timeout = timeout_sec )
83- return http .client .HTTPConnection (parsed . hostname , port , timeout = timeout_sec )
87+ return http .client .HTTPSConnection (host , port , timeout = timeout_sec )
88+ return http .client .HTTPConnection (host , port , timeout = timeout_sec )
8489
8590
8691@contextmanager
@@ -124,7 +129,7 @@ def _json_error(self, status: int, reason: str) -> None:
124129 self .end_headers ()
125130 self .wfile .write (body )
126131
127- def do_POST (self ) -> None : # noqa: N802 - BaseHTTPRequestHandler contract
132+ def do_POST (self ) -> None :
128133 requested = urlsplit (self .path )
129134 if requested .path not in admitted_paths :
130135 self ._json_error (404 , "provider_gateway_path_not_admitted" )
@@ -179,7 +184,7 @@ def do_POST(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
179184 finally :
180185 connection .close ()
181186
182- def do_GET (self ) -> None : # noqa: N802 - BaseHTTPRequestHandler contract
187+ def do_GET (self ) -> None :
183188 self ._json_error (405 , "provider_gateway_method_not_admitted" )
184189
185190 server = ThreadingHTTPServer (("127.0.0.1" , 0 ), GatewayHandler )
@@ -192,8 +197,9 @@ def do_GET(self) -> None: # noqa: N802 - BaseHTTPRequestHandler contract
192197 thread .start ()
193198 try :
194199 host , port = server .server_address [:2 ]
200+ rendered_host = host .decode ("ascii" ) if isinstance (host , bytes ) else str (host )
195201 yield RunnerOwnedProviderGateway (
196- base_url = f"http://{ host } :{ port } " ,
202+ base_url = f"http://{ rendered_host } :{ port } " ,
197203 allowed_paths = admitted_paths ,
198204 )
199205 finally :
0 commit comments