Return 59 bad request if URL cannot be parsed
This commit is contained in:
parent
d97eda001e
commit
e0c62dbcb6
18
jetforce.py
18
jetforce.py
|
@ -82,8 +82,15 @@ class Request:
|
||||||
self.url = environ["GEMINI_URL"]
|
self.url = environ["GEMINI_URL"]
|
||||||
|
|
||||||
url_parts = urllib.parse.urlparse(self.url)
|
url_parts = urllib.parse.urlparse(self.url)
|
||||||
# If scheme is missing, infer it as gemini://
|
if not url_parts.hostname:
|
||||||
self.scheme = url_parts.scheme or "gemini"
|
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.hostname = url_parts.hostname
|
||||||
self.port = url_parts.port
|
self.port = url_parts.port
|
||||||
self.path = url_parts.path
|
self.path = url_parts.path
|
||||||
|
@ -146,7 +153,12 @@ class JetforceApplication:
|
||||||
def __call__(
|
def __call__(
|
||||||
self, environ: dict, send_status: typing.Callable
|
self, environ: dict, send_status: typing.Callable
|
||||||
) -> typing.Iterator[bytes]:
|
) -> typing.Iterator[bytes]:
|
||||||
|
try:
|
||||||
request = Request(environ)
|
request = Request(environ)
|
||||||
|
except Exception:
|
||||||
|
send_status(Status.BAD_REQUEST, "Unrecognized URL format")
|
||||||
|
return
|
||||||
|
|
||||||
for route_pattern, callback in self.routes[::-1]:
|
for route_pattern, callback in self.routes[::-1]:
|
||||||
if route_pattern.match(request):
|
if route_pattern.match(request):
|
||||||
response = callback(request)
|
response = callback(request)
|
||||||
|
@ -160,7 +172,7 @@ class JetforceApplication:
|
||||||
yield from response.body
|
yield from response.body
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
send_status(Status.PERMANENT_FAILURE, "Unrecognized URL")
|
send_status(Status.PERMANENT_FAILURE, "Not Found")
|
||||||
|
|
||||||
def route(
|
def route(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in New Issue