Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tina4_python/Webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -592,4 +602,4 @@ def server_close(self):
"""
if self.server:
self.server.close()
self.running = False
self.running = False
15 changes: 13 additions & 2 deletions tina4_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading