Skip to content

Commit 8fa70b6

Browse files
committed
Preparing for returning persistent connection responses
1 parent 802d7fd commit 8fa70b6

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

adafruit_httpserver/response.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def _send(self) -> None:
102102

103103
self._send_headers(len(encoded_body), self._content_type)
104104
self._send_bytes(self._request.connection, encoded_body)
105+
self._close_connection()
105106

106107
def _send_bytes(
107108
self,
@@ -122,6 +123,12 @@ def _send_bytes(
122123
raise
123124
self._size += bytes_sent
124125

126+
def _close_connection(self) -> None:
127+
try:
128+
self._request.connection.close()
129+
except (BrokenPipeError, OSError):
130+
pass
131+
125132

126133
class FileResponse(Response): # pylint: disable=too-few-public-methods
127134
"""
@@ -246,6 +253,7 @@ def _send(self) -> None:
246253
with open(self._full_file_path, "rb") as file:
247254
while bytes_read := file.read(self._buffer_size):
248255
self._send_bytes(self._request.connection, bytes_read)
256+
self._close_connection()
249257

250258

251259
class ChunkedResponse(Response): # pylint: disable=too-few-public-methods
@@ -309,6 +317,7 @@ def _send(self) -> None:
309317

310318
# Empty chunk to indicate end of response
311319
self._send_chunk()
320+
self._close_connection()
312321

313322

314323
class JSONResponse(Response): # pylint: disable=too-few-public-methods
@@ -352,6 +361,7 @@ def _send(self) -> None:
352361

353362
self._send_headers(len(encoded_data), "application/json")
354363
self._send_bytes(self._request.connection, encoded_data)
364+
self._close_connection()
355365

356366

357367
class Redirect(Response): # pylint: disable=too-few-public-methods
@@ -391,3 +401,5 @@ def __init__(
391401

392402
def _send(self) -> None:
393403
self._send_headers()
404+
self._close_connection()
405+

adafruit_httpserver/server.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,29 +334,30 @@ def poll(self):
334334

335335
try:
336336
conn, client_address = self._sock.accept()
337-
with conn:
338-
conn.settimeout(self._timeout)
337+
conn.settimeout(self._timeout)
339338

340-
# Receive the whole request
341-
if (request := self._receive_request(conn, client_address)) is None:
342-
return
339+
# Receive the whole request
340+
if (request := self._receive_request(conn, client_address)) is None:
341+
conn.close()
342+
return
343343

344-
# Find a handler for the route
345-
handler = self._routes.find_handler(
346-
_Route(request.path, request.method)
347-
)
344+
# Find a handler for the route
345+
handler = self._routes.find_handler(Route(request.path, request.method))
348346

349-
# Handle the request
350-
response = self._handle_request(request, handler)
347+
# Handle the request
348+
response = self._handle_request(request, handler)
351349

352-
if response is None:
353-
return
350+
if response is None:
351+
conn.close()
352+
return
353+
354+
# Send the response
355+
response._send() # pylint: disable=protected-access
354356

355-
# Send the response
356-
response._send() # pylint: disable=protected-access
357+
if self.debug:
358+
_debug_response_sent(response)
357359

358-
if self.debug:
359-
_debug_response_sent(response)
360+
return
360361

361362
except Exception as error: # pylint: disable=broad-except
362363
if isinstance(error, OSError):
@@ -370,6 +371,7 @@ def poll(self):
370371
if self.debug:
371372
_debug_exception_in_handler(error)
372373

374+
conn.close()
373375
raise error # Raise the exception again to be handled by the user.
374376

375377
def require_authentication(self, auths: List[Union[Basic, Bearer]]) -> None:

0 commit comments

Comments
 (0)