diff --git a/tina4_python/Webserver.py b/tina4_python/Webserver.py index ebb795e..bc8953a 100644 --- a/tina4_python/Webserver.py +++ b/tina4_python/Webserver.py @@ -317,7 +317,17 @@ async def get_response(self, method: str, scope ,reader: asyncio.StreamReader, w self.send_header("Access-Control-Allow-Credentials", "true", headers) headers_bytes = await self.get_headers(headers, self.response_protocol, HTTP_OK) - return headers_bytes if not asgi_response else (None, headers) + class _Tina4Response: + def __init__(self, content: str = "", http_code: int = HTTP_OK, content_type: str = "text/plain", headers: dict | None = None): + self.content = content + self.http_code = http_code + self.content_type = content_type + self.headers = headers or {} + + if not asgi_response: + return headers_bytes + + return _Tina4Response(content="", http_code=HTTP_OK, content_type="text/plain", headers={}), headers # ------------------------------------------------------------------ # Query string parsing with nested support @@ -592,4 +602,4 @@ def server_close(self): """ if self.server: self.server.close() - self.running = False \ No newline at end of file + self.running = False diff --git a/tina4_python/__init__.py b/tina4_python/__init__.py index 7ad2552..052ea40 100644 --- a/tina4_python/__init__.py +++ b/tina4_python/__init__.py @@ -413,8 +413,19 @@ async def app(scope, receive, send): if message["type"] != "websocket": response_headers = [] for header in tina4_headers: - header = header.split(":") - response_headers.append([header[0].strip().encode(), header[1].strip().encode()]) + # ensure header is a str + if isinstance(header, bytes): + header = header.decode() + else: + header = str(header) + + # split only on the first ':' to preserve ':' in values (e.g. URLs with ports) + name, sep, value = header.partition(":") + if not sep: + # malformed header, skip it + continue + + response_headers.append([name.strip().encode(), value.lstrip().encode()]) await send({ 'type': 'http.response.start',