From e55e975183ab3079ceaee9399131f33cbc515b8a Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Wed, 28 Feb 2024 08:04:35 +0100 Subject: [PATCH 1/4] close(): fix exception and enable fast-close --- adafruit_requests.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 771bfcc..a64fd3d 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -226,22 +226,23 @@ def _throw_away(self, nbytes: int) -> None: while to_read > 0: to_read -= self._recv_into(buf, to_read) - def close(self) -> None: + def close(self, fast: bool = False) -> None: """Drain the remaining ESP socket buffers. We assume we already got what we wanted.""" if not self.socket: return # Make sure we've read all of our response. - if self._cached is None: + if self._cached is None and not fast: if self._remaining and self._remaining > 0: self._throw_away(self._remaining) elif self._chunked: while True: chunk_header = bytes(self._readto(b"\r\n")).split(b";", 1)[0] + if not chunk_header: + break chunk_size = int(bytes(chunk_header), 16) if chunk_size == 0: break self._throw_away(chunk_size + 2) - self._parse_headers() if self._session: # pylint: disable=protected-access self._session._connection_manager.free_socket(self.socket) From b062ab95b2b7a4488ee42bf5464d7d9570ccc187 Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Tue, 5 Mar 2024 10:20:21 +0100 Subject: [PATCH 2/4] added fast_close as kw-arg to Session --- adafruit_requests.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index a64fd3d..be4dca7 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -86,11 +86,12 @@ class Response: encoding = None - def __init__(self, sock: SocketType, session: "Session") -> None: + def __init__(self, sock: SocketType, session: "Session", fast_close: bool = False) -> None: self.socket = sock self.encoding = "utf-8" self._cached = None self._headers = {} + self._fast_close = fast_close # _start_index and _receive_buffer are used when parsing headers. # _receive_buffer will grow by 32 bytes everytime it is too small. @@ -226,12 +227,12 @@ def _throw_away(self, nbytes: int) -> None: while to_read > 0: to_read -= self._recv_into(buf, to_read) - def close(self, fast: bool = False) -> None: + def close(self) -> None: """Drain the remaining ESP socket buffers. We assume we already got what we wanted.""" if not self.socket: return # Make sure we've read all of our response. - if self._cached is None and not fast: + if self._cached is None and not self._fast_close: if self._remaining and self._remaining > 0: self._throw_away(self._remaining) elif self._chunked: @@ -362,11 +363,13 @@ def __init__( socket_pool: SocketpoolModuleType, ssl_context: Optional[SSLContextType] = None, session_id: Optional[str] = None, + fast_close: Optional[Bool] = False, ) -> None: self._connection_manager = get_connection_manager(socket_pool) self._ssl_context = ssl_context self._session_id = session_id self._last_response = None + self._fast_close = fast_close @staticmethod def _check_headers(headers: Dict[str, str]): @@ -561,7 +564,7 @@ def request( if not socket: raise OutOfRetries("Repeated socket failures") from last_exc - resp = Response(socket, self) # our response + resp = Response(socket, self, fast_close=self._fast_close) # our response if allow_redirects: if "location" in resp.headers and 300 <= resp.status_code <= 399: # a naive handler for redirects From 46b0a6fb0c2db9c529c533c3ab44627b22deddad Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Tue, 5 Mar 2024 10:30:39 +0100 Subject: [PATCH 3/4] fixed spelling --- adafruit_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index be4dca7..856b3b7 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -363,7 +363,7 @@ def __init__( socket_pool: SocketpoolModuleType, ssl_context: Optional[SSLContextType] = None, session_id: Optional[str] = None, - fast_close: Optional[Bool] = False, + fast_close: Optional[bool] = False, ) -> None: self._connection_manager = get_connection_manager(socket_pool) self._ssl_context = ssl_context From 1d8cd63d8c56f3f08e9f8dfcc43679b60f226956 Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Tue, 5 Mar 2024 10:39:18 +0100 Subject: [PATCH 4/4] blackified --- adafruit_requests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 856b3b7..d6730c9 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -86,7 +86,9 @@ class Response: encoding = None - def __init__(self, sock: SocketType, session: "Session", fast_close: bool = False) -> None: + def __init__( + self, sock: SocketType, session: "Session", fast_close: bool = False + ) -> None: self.socket = sock self.encoding = "utf-8" self._cached = None