From f7e995f9f6b9afb01a8a97b094a5d4e1a713b72b Mon Sep 17 00:00:00 2001 From: Unrud Date: Fri, 2 Sep 2016 04:10:11 +0200 Subject: [PATCH 1/2] Move encoding of answer into response function Fix #505 --- radicale/__init__.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 185d2c0..7317ad6 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -265,6 +265,27 @@ class Application: """Manage a request.""" def response(status, headers={}, answer=None): + # Set content length + if answer: + self.logger.debug("Response content:\n%s", answer) + answer = answer.encode(self.encoding) + accept_encoding = [ + encoding.strip() for encoding in + environ.get("HTTP_ACCEPT_ENCODING", "").split(",") + if encoding.strip()] + + if "gzip" in accept_encoding: + zcomp = zlib.compressobj(wbits=16 + zlib.MAX_WBITS) + answer = zcomp.compress(answer) + zcomp.flush() + headers["Content-Encoding"] = "gzip" + + headers["Content-Length"] = str(len(answer)) + + # Add extra headers set in configuration + if self.configuration.has_section("headers"): + for key in self.configuration.options("headers"): + headers[key] = self.configuration.get("headers", key) + # Start response status = "%i %s" % ( status, client.responses.get(status, "Unknown")) @@ -363,27 +384,6 @@ class Application: "WWW-Authenticate": "Basic realm=\"%s\"" % realm}) - # Set content length - if answer: - self.logger.debug("Response content:\n%s", answer) - answer = answer.encode(self.encoding) - accept_encoding = [ - encoding.strip() for encoding in - environ.get("HTTP_ACCEPT_ENCODING", "").split(",") - if encoding.strip()] - - if "gzip" in accept_encoding: - zcomp = zlib.compressobj(wbits=16 + zlib.MAX_WBITS) - answer = zcomp.compress(answer) + zcomp.flush() - headers["Content-Encoding"] = "gzip" - - headers["Content-Length"] = str(len(answer)) - - # Add extra headers set in configuration - if self.configuration.has_section("headers"): - for key in self.configuration.options("headers"): - headers[key] = self.configuration.get("headers", key) - return response(status, headers, answer) def _access(self, user, path, permission, item=None): From 20b14803994c57d323da759ba0253ab3bad27455 Mon Sep 17 00:00:00 2001 From: Unrud Date: Fri, 2 Sep 2016 04:19:19 +0200 Subject: [PATCH 2/2] Make copy of headers before mutating --- radicale/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/radicale/__init__.py b/radicale/__init__.py index 7317ad6..b1f2d97 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -265,6 +265,7 @@ class Application: """Manage a request.""" def response(status, headers={}, answer=None): + headers = headers.copy() # Set content length if answer: self.logger.debug("Response content:\n%s", answer) @@ -380,6 +381,7 @@ class Application: self.logger.info("%s refused" % (user or "Anonymous user")) status = client.UNAUTHORIZED realm = self.configuration.get("server", "realm") + headers = headers.copy() headers.update ({ "WWW-Authenticate": "Basic realm=\"%s\"" % realm})