Skip to content

Allow sending raw bytes in the response #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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: 9 additions & 5 deletions adafruit_httpserver/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def send(
if self._response_already_sent:
raise RuntimeError("Response was already sent")

encoded_response_message_body = body.encode("utf-8")
if getattr(body, "encode", None):
encoded_response_message_body = body.encode("utf-8")
else:
encoded_response_message_body = body

self._send_headers(
content_type=content_type or self.content_type,
Expand Down Expand Up @@ -206,11 +209,12 @@ def send_chunk(self, chunk: str = "") -> None:

:param str chunk: String data to be sent.
"""
hex_length = hex(len(chunk))[2:] # removing 0x
if getattr(chunk, "encode", None):
chunk = chunk.encode("utf-8")

self._send_bytes(
self.request.connection, f"{hex_length}\r\n{chunk}\r\n".encode("utf-8")
)
self._send_bytes(self.request.connection, b"%x\r\n" % len(chunk))
self._send_bytes(self.request.connection, chunk)
self._send_bytes(self.request.connection, b"\r\n")

def __enter__(self):
if self.chunked:
Expand Down