Просмотр исходного кода

Merge pull request #506 from Unrud/patch-1

Move encoding of answer into response function
Guillaume Ayoub 9 лет назад
Родитель
Сommit
e6433ec970
1 измененных файлов с 23 добавлено и 21 удалено
  1. 23 21
      radicale/__init__.py

+ 23 - 21
radicale/__init__.py

@@ -265,6 +265,28 @@ class Application:
         """Manage a request."""
         """Manage a request."""
 
 
         def response(status, headers={}, answer=None):
         def response(status, headers={}, answer=None):
+            headers = headers.copy()
+            # 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
             # Start response
             status = "%i %s" % (
             status = "%i %s" % (
                 status, client.responses.get(status, "Unknown"))
                 status, client.responses.get(status, "Unknown"))
@@ -359,31 +381,11 @@ class Application:
             self.logger.info("%s refused" % (user or "Anonymous user"))
             self.logger.info("%s refused" % (user or "Anonymous user"))
             status = client.UNAUTHORIZED
             status = client.UNAUTHORIZED
             realm = self.configuration.get("server", "realm")
             realm = self.configuration.get("server", "realm")
+            headers = headers.copy()
             headers.update ({
             headers.update ({
                 "WWW-Authenticate":
                 "WWW-Authenticate":
                 "Basic realm=\"%s\"" % realm})
                 "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)
         return response(status, headers, answer)
 
 
     def _access(self, user, path, permission, item=None):
     def _access(self, user, path, permission, item=None):