From a5a3b9f8ee84392c416dac034924a4dfa8bb352d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Sep 2020 15:06:43 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20exception=20on=20log=20dow?= =?UTF-8?q?nload=20upon=20file=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3731 --- src/octoprint/server/__init__.py | 3 +- src/octoprint/server/util/tornado.py | 41 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 55fc789f6..659e434d1 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -677,7 +677,8 @@ class Server(object): no_hidden_files_validator, additional_mime_types)), (r"/downloads/logs/([^/]*)", util.tornado.LargeResponseHandler, joined_dict(dict(path=self._settings.getBaseFolder("logs"), - mime_type_guesser=lambda *args, **kwargs: "text/plain"), + mime_type_guesser=lambda *args, **kwargs: "text/plain", + stream_body=True), download_handler_kwargs, log_permission_validator)), # camera snapshot diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index 94b2b31c6..2c699f1a8 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -990,7 +990,7 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin, def initialize(self, path, default_filename=None, as_attachment=False, allow_client_caching=True, access_validation=None, path_validation=None, etag_generator=None, name_generator=None, - mime_type_guesser=None, is_pre_compressed=False): + mime_type_guesser=None, is_pre_compressed=False, stream_body=False): tornado.web.StaticFileHandler.initialize(self, os.path.abspath(path), default_filename) self._as_attachment = as_attachment self._allow_client_caching = allow_client_caching @@ -1000,6 +1000,7 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin, self._name_generator = name_generator self._mime_type_guesser = mime_type_guesser self._is_pre_compressed = is_pre_compressed + self._stream_body = stream_body def should_use_precompressed(self): return (self._is_pre_compressed @@ -1022,7 +1023,43 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin, logging.getLogger(__name__).warning("Precompressed assets expected but {}.gz does not exist " "in {}, using plain file instead.".format(path, self.root)) - return tornado.web.StaticFileHandler.get(self, path, include_body=include_body) + if self._stream_body: + return self.streamed_get(path, include_body=include_body) + else: + return tornado.web.StaticFileHandler.get(self, path, include_body=include_body) + + @tornado.gen.coroutine + def streamed_get(self, path, include_body=True): + """ + Version of StaticFileHandler.get that doesn't support ranges or ETag but streams the content. Helpful for files + that might still change while being transmitted (e.g. log files) + """ + + # Set up our path instance variables. + self.path = self.parse_url_path(path) + del path # make sure we don't refer to path instead of self.path again + absolute_path = self.get_absolute_path(self.root, self.path) + self.absolute_path = self.validate_absolute_path(self.root, absolute_path) + if self.absolute_path is None: + return + + content_type = self.get_content_type() + if content_type: + self.set_header("Content-Type", content_type) + self.set_extra_headers(self.path) + + if include_body: + content = self.get_content(self.absolute_path) + if isinstance(content, bytes): + content = [content] + for chunk in content: + try: + self.write(chunk) + yield self.flush() + except tornado.iostream.StreamClosedError: + return + else: + assert self.request.method == "HEAD" def set_extra_headers(self, path): if self._as_attachment: