Skip to content

Commit aeb95a2

Browse files
committed
Encoding body only once when constructing response bytes
1 parent be20bb1 commit aeb95a2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

adafruit_httpserver/response.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
7272
) -> bytes:
7373
"""Constructs the response bytes from the given parameters."""
7474

75-
response = f"{http_version} {status.code} {status.text}\r\n"
75+
response_message_header = f"{http_version} {status.code} {status.text}\r\n"
76+
encoded_response_message_body = body.encode("utf-8")
7677

7778
headers.setdefault("Content-Type", content_type)
7879
headers.setdefault(
79-
"Content-Length", content_length or len(body.encode("utf-8"))
80+
"Content-Length", content_length or len(encoded_response_message_body)
8081
)
8182
headers.setdefault("Connection", "close")
8283

8384
for header, value in headers.items():
84-
response += f"{header}: {value}\r\n"
85+
response_message_header += f"{header}: {value}\r\n"
86+
response_message_header += "\r\n"
8587

86-
response += f"\r\n{body}"
87-
88-
return response.encode("utf-8")
88+
return response_message_header.encode("utf-8") + encoded_response_message_body
8989

9090
def send(self, conn: Union["SocketPool.Socket", "socket.socket"]) -> None:
9191
"""

0 commit comments

Comments
 (0)