88import socket
99from asyncio import Lock
1010from copy import copy
11- from html import escape
1211from tempfile import mkdtemp
1312from urllib .parse import quote , urlparse , urlunparse
1413
@@ -324,14 +323,11 @@ async def proxy(self, host, port, proxied_path):
324323 """
325324
326325 if not self ._check_host_allowlist (host ):
327- self .set_status (403 )
328- self .write (
329- "Host '{host}' is not allowed. "
330- "See https://jupyter-server-proxy.readthedocs.io/en/latest/arbitrary-ports-hosts.html for info." .format (
331- host = escape (host )
332- )
326+ raise web .HTTPError (
327+ 403 ,
328+ f"Host '{ host } ' is not allowed. "
329+ "See https://jupyter-server-proxy.readthedocs.io/en/latest/arbitrary-ports-hosts.html for info." ,
333330 )
334- return
335331
336332 # Remove hop-by-hop headers that don't necessarily apply to the request we are making
337333 # to the backend. See https://github.com/jupyterhub/jupyter-server-proxy/pull/328
@@ -392,9 +388,7 @@ async def proxy(self, host, port, proxied_path):
392388 # Ref: https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.AsyncHTTPClient.fetch
393389 if err .code == 599 :
394390 self ._record_activity ()
395- self .set_status (599 )
396- self .write (escape (str (err )))
397- return
391+ raise web .HTTPError (599 , str (err ))
398392 else :
399393 raise
400394
@@ -403,8 +397,7 @@ async def proxy(self, host, port, proxied_path):
403397
404398 # For all non http errors...
405399 if response .error and type (response .error ) is not httpclient .HTTPError :
406- self .set_status (500 )
407- self .write (escape (str (response .error )))
400+ raise web .HTTPError (500 , str (response .error ))
408401 else :
409402 # Represent the original response as a RewritableResponse object.
410403 original_response = RewritableResponse (orig_response = response )
0 commit comments