diff options
author | Philipp Hörist <philipp@hoerist.com> | 2023-05-21 10:35:16 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2023-05-21 10:37:41 +0300 |
commit | 5afb2b4a14e62b3b5f9b0d1c2843ba9575fd5c50 (patch) | |
tree | fe3aff8c2734ce506869e2287c56948b9a314d14 | |
parent | 1de40d394f1d9aa96f47db811698b31ce72ae3e3 (diff) |
cfix: HTTP: Use cancel() to abort download in got-body-data callback
Fixes #144
-rw-r--r-- | nbxmpp/const.py | 3 | ||||
-rw-r--r-- | nbxmpp/http.py | 23 |
2 files changed, 14 insertions, 12 deletions
diff --git a/nbxmpp/const.py b/nbxmpp/const.py index b3fd201..efd4a08 100644 --- a/nbxmpp/const.py +++ b/nbxmpp/const.py @@ -462,6 +462,9 @@ class HTTPRequestError(IntEnum): CONTENT_OVERFLOW = 4 TIMEOUT = 5 + def __str__(self) -> str: + return self.name + MOODS = [ 'afraid', diff --git a/nbxmpp/http.py b/nbxmpp/http.py index 2261618..316ba63 100644 --- a/nbxmpp/http.py +++ b/nbxmpp/http.py @@ -305,6 +305,8 @@ class HTTPRequest(GObject.GObject): def _on_timeout(self) -> None: self._timeout_reached = True + self._timeout_id = None + self._set_error(HTTPRequestError.TIMEOUT) self.cancel() def _on_response(self, @@ -312,9 +314,6 @@ class HTTPRequest(GObject.GObject): result: Gio.AsyncResult ) -> None: - if self._is_finished: - return - self._log.info('Request response received') try: self._input_stream = session.send_finish(result) @@ -351,9 +350,6 @@ class HTTPRequest(GObject.GObject): input_stream: Gio.InputStream, result: Gio.AsyncResult) -> None: - if self._is_finished: - return - try: data = input_stream.read_bytes_finish(result) except GLib.Error as error: @@ -463,7 +459,8 @@ class HTTPRequest(GObject.GObject): def _check_content_overflow(self) -> None: if self._received_size > self._response_content_length: - self._finish_read(HTTPRequestError.CONTENT_OVERFLOW) + self._set_error(HTTPRequestError.CONTENT_OVERFLOW) + self.cancel() def _on_restarted(self, _message: Soup.Message) -> None: self._log.info('Restarted') @@ -503,14 +500,16 @@ class HTTPRequest(GObject.GObject): self._set_complete() + def _set_error(self, error: HTTPRequestError) -> None: + self._log.info('Set Error: %s', error) + self._error = error + def _set_failed(self, error: HTTPRequestError) -> None: self._log.info('Set Failed: %s', error) self._is_finished = True - if self._timeout_reached: - self._timeout_id = None - self._error = HTTPRequestError.TIMEOUT - else: - self._error = error + + if self._error is None: + self._set_error(error) self._close_all_streams() self.emit('finished') |