From: Michael Tremer Date: Sun, 26 Jan 2025 12:20:38 +0000 (+0000) Subject: jobs: Send log files with decompressing them if client supports it X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb621a2cab77a96d74a9e0d8ed7b2a2648fb2717;p=pbs.git jobs: Send log files with decompressing them if client supports it If the browser supports gzip, we simply send the raw file as there is no need to decompress it first and then have the reverse proxy compress it again. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index 38a5a164..be647643 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -956,7 +956,7 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Open Log - async def open_log(self): + async def open_log(self, decode=True): """ Opens the log file, and returns an open file handle """ @@ -964,16 +964,16 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): 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 diff --git a/src/web/base.py b/src/web/base.py index c353bca3..bd96a4e1 100644 --- a/src/web/base.py +++ b/src/web/base.py @@ -229,6 +229,18 @@ class BaseHandler(tornado.web.RequestHandler): """ 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) diff --git a/src/web/jobs.py b/src/web/jobs.py index 178b5a8d..f503e193 100644 --- a/src/web/jobs.py +++ b/src/web/jobs.py @@ -193,8 +193,17 @@ class LogHandler(base.BaseHandler): # 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)