From e0c62dbcb67fccd8db1535a75d1b06f8964672b7 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sun, 12 Jan 2020 17:15:19 -0500 Subject: [PATCH] Return 59 bad request if URL cannot be parsed --- jetforce.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/jetforce.py b/jetforce.py index 68788f9..0e4371e 100755 --- a/jetforce.py +++ b/jetforce.py @@ -82,8 +82,15 @@ class Request: self.url = environ["GEMINI_URL"] url_parts = urllib.parse.urlparse(self.url) - # If scheme is missing, infer it as gemini:// - self.scheme = url_parts.scheme or "gemini" + if not url_parts.hostname: + raise ValueError("URL must contain a `hostname` part") + + if not url_parts.scheme: + # If scheme is missing, infer it to be gemini:// + self.scheme = "gemini" + else: + self.scheme = url_parts.scheme + self.hostname = url_parts.hostname self.port = url_parts.port self.path = url_parts.path @@ -146,7 +153,12 @@ class JetforceApplication: def __call__( self, environ: dict, send_status: typing.Callable ) -> typing.Iterator[bytes]: - request = Request(environ) + try: + request = Request(environ) + except Exception: + send_status(Status.BAD_REQUEST, "Unrecognized URL format") + return + for route_pattern, callback in self.routes[::-1]: if route_pattern.match(request): response = callback(request) @@ -160,7 +172,7 @@ class JetforceApplication: yield from response.body break else: - send_status(Status.PERMANENT_FAILURE, "Unrecognized URL") + send_status(Status.PERMANENT_FAILURE, "Not Found") def route( self,