diff options
-rw-r--r-- | src/scadere/listen.py | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/src/scadere/listen.py b/src/scadere/listen.py index 3c44407..1ae7281 100644 --- a/src/scadere/listen.py +++ b/src/scadere/listen.py @@ -69,27 +69,24 @@ def supported_http_version(version): return False -async def write_status(writer, http_version, status): +def write_status(writer, http_version, status): """Write the given HTTP status line.""" status = f'HTTP/{http_version} {status.value} {status.phrase}\r\n' writer.write(status.encode()) - await writer.drain() -async def write_content_type(writer, content_type): +def write_content_type(writer, content_type): """Write the given HTTP content type.""" writer.write(f'Content-Type: {content_type}\r\n'.encode()) - await writer.drain() -async def describe_status(writer, status, http_version='1.1'): +def describe_status(writer, status, http_version='1.1'): """Write a HTTP/1.1 response including status description.""" - await write_status(writer, http_version, status) + write_status(writer, http_version, status) content = f'{status.description}\n'.encode() - await write_content_type(writer, 'text/plain') + write_content_type(writer, 'text/plain') writer.write(f'Content-Length: {len(content)}\r\n\r\n'.encode()) writer.write(content) - await writer.drain() def body(not_before, not_after, hostname, port, number, string): @@ -217,20 +214,18 @@ def unparsed_page(*args): xml_declaration=True, default_namespace=None) -async def write_xml(writer, http_version, application, func, *args): +def write_xml(writer, http_version, application, func, *args): """Write given document as XML.""" try: content = func(*args).encode() except Exception: # pragma: no cover - await describe_status(writer, HTTPStatus.INTERNAL_SERVER_ERROR, - http_version) + describe_status(writer, HTTPStatus.INTERNAL_SERVER_ERROR, http_version) raise else: - await write_status(writer, http_version, HTTPStatus.OK) - await write_content_type(writer, f'application/{application}+xml') + write_status(writer, http_version, HTTPStatus.OK) + write_content_type(writer, f'application/{application}+xml') writer.write(f'Content-Length: {len(content)}\r\n\r\n'.encode()) writer.write(content) - await writer.drain() async def handle(certs, base_url, reader, writer, title=None): @@ -239,11 +234,11 @@ async def handle(certs, base_url, reader, writer, title=None): try: request = await reader.readuntil(b'\r\n') except Exception: - await describe_status(writer, HTTPStatus.BAD_REQUEST) + describe_status(writer, HTTPStatus.BAD_REQUEST) return if not request.startswith(b'GET '): - await describe_status(writer, HTTPStatus.METHOD_NOT_ALLOWED) + describe_status(writer, HTTPStatus.METHOD_NOT_ALLOWED) return try: @@ -253,8 +248,7 @@ async def handle(certs, base_url, reader, writer, title=None): if not supported_http_version(http_version): raise ValueError except ValueError: - await describe_status(writer, - HTTPStatus.HTTP_VERSION_NOT_SUPPORTED) + describe_status(writer, HTTPStatus.HTTP_VERSION_NOT_SUPPORTED) return try: @@ -270,18 +264,18 @@ async def handle(certs, base_url, reader, writer, title=None): if title is None: title = certs.name except Exception: # pragma: no cover - await describe_status(writer, HTTPStatus.INTERNAL_SERVER_ERROR, - http_version) + describe_status(writer, HTTPStatus.INTERNAL_SERVER_ERROR, + http_version) raise if url_parts.path == urlsplit(base_url).path: # Atom feed - await write_xml(writer, http_version, 'atom', unparsed_feed, - base_url, title, mtime, summaries, domains) + write_xml(writer, http_version, 'atom', unparsed_feed, + base_url, title, mtime, summaries, domains) elif url_parts.path in lookup: # accessible Atom entry's link/ID - await write_xml(writer, http_version, 'xhtml', unparsed_page, - lookup.get(url_parts.path)) + write_xml(writer, http_version, 'xhtml', unparsed_page, + lookup.get(url_parts.path)) else: - await describe_status(writer, HTTPStatus.NOT_FOUND, http_version) + describe_status(writer, HTTPStatus.NOT_FOUND, http_version) finally: assert writer.can_write_eof() writer.write_eof() |