# Open Log
- async def open_log(self):
+ async def open_log(self, decode=True):
"""
Opens the log file, and returns an open file handle
"""
if not self.has_log():
raise FileNotFoundError
- return await asyncio.to_thread(self._open_log, self.log_path)
+ return await asyncio.to_thread(self._open_log, self.log_path, decode=decode)
- def _open_log(self, path):
- # Open gzip-compressed files
- if path.endswith(".gz"):
- return gzip.open(path)
+ def _open_log(self, path, decode=True):
+ if decode:
+ # Open gzip-compressed files
+ if path.endswith(".gz"):
+ return gzip.open(path)
# Open uncompressed files
- else:
- return open(path)
+ return open(path, "rb")
# Tail Log
"""
return self.request.headers.get("User-Agent", None)
+ def client_accepts_encoding(self, encoding):
+ """
+ Checks if type is in Accept-Encoding: header
+ """
+ # Fetch the header
+ header = self.request.headers.get("Accept-Encoding", "")
+ if not header:
+ return False
+
+ # Check if the encoding is accepted
+ return encoding in header.split(",")
+
def format_date(self, date, relative=True, shorter=False, full_format=False):
return self.locale.format_date(date, relative=relative,
shorter=shorter, full_format=full_format)
# Done!
return
+ # Automatically decompress the log
+ decode = True
+
+ if self.client_accepts_encoding("gzip"):
+ decode = False
+
+ # Tell the client we are sending this compressed
+ self.set_header("Content-Encoding", "gzip")
+
# If we are sending the entire file we won't split it by line before
- log = await job.open_log()
+ log = await job.open_log(decode=decode)
while True:
chunk = log.read(65536)