🐛 Fix exception on log download upon file change

Closes #3731
This commit is contained in:
Gina Häußge committed 2020-09-16 15:06:43 +02:00
1 parent 9e5d1e9ad6
commit a5a3b9f8ee
2 files changed
+41 -3

No files matched your search

+2 -1
View File
@@ -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
+39 -2
View File
@@ -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: