]> git.ipfire.org Git - pbs.git/commitdiff
jobs: Send log files with decompressing them if client supports it
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 12:20:38 +0000 (12:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 12:21:59 +0000 (12:21 +0000)
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 <michael.tremer@ipfire.org>
src/buildservice/jobs.py
src/web/base.py
src/web/jobs.py

index 38a5a16427ced2cd7601a5b3a112e42958f8f789..be6476432fd016cd6b2601ade137c7e09e05a58f 100644 (file)
@@ -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
 
index c353bca361c61e5f38fae9e4bce1c9f1d8bcc182..bd96a4e1b3c471d830ed3c268d1651b2faf20c32 100644 (file)
@@ -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)
index 178b5a8d257c4a34e57a591f745a18a83e70a27e..f503e1934587ca4269394a2f7b2f5599c0fa9d5c 100644 (file)
@@ -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)