Rev 5355: (vila) Set a Content-Length header on errors for HTTP/1.1 (Vincent Ladeuil) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Jul 20 21:11:01 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5355 [merge]
revision-id: pqm at pqm.ubuntu.com-20100720201059-y4nvvahwp5vq4rpk
parent: pqm at pqm.ubuntu.com-20100720121136-r4c42ke7mjobuuo4
parent: v.ladeuil+lp at free.fr-20100720143642-u3x5uvkgny6iie2p
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2010-07-20 21:10:59 +0100
message:
  (vila) Set a Content-Length header on errors for HTTP/1.1 (Vincent Ladeuil)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/http_server.py    httpserver.py-20061012142527-m1yxdj1xazsf8d7s-1
=== modified file 'NEWS'
--- a/NEWS	2010-07-20 10:50:44 +0000
+++ b/NEWS	2010-07-20 14:36:42 +0000
@@ -34,6 +34,9 @@
 * Don't print internal object name when print an invalid revision spec
   error.  (Neil Martinsen-Burrell, #598701)
 
+* ``HTTP/1.1` test servers now set a ``Content-Length`` header to comply
+  with pedantic ``HTTP/1.1`` clients. (Vincent Ladeuil, #568421)
+
 * `PathNotChild` should not give a traceback.
   (Martin Pool, #98735)
 

=== modified file 'bzrlib/tests/http_server.py'
--- a/bzrlib/tests/http_server.py	2010-02-23 07:43:11 +0000
+++ b/bzrlib/tests/http_server.py	2010-07-20 14:36:42 +0000
@@ -89,6 +89,41 @@
                                      errno.ECONNABORTED, errno.EBADF)):
                 raise
 
+    error_content_type = 'text/plain'
+    error_message_format = '''\
+Error code: %(code)s.
+Message: %(message)s.
+'''
+
+    def send_error(self, code, message=None):
+        """Send and log an error reply.
+
+        We redefine the python-provided version to be able to set a 
+        ``Content-Length`` header as some http/1.1 clients complain otherwise
+        (see bug #568421).
+
+        :param code: The HTTP error code.
+
+        :param message: The explanation of the error code, Defaults to a short
+             entry.
+        """
+
+        if message is None:
+            try:
+                message = self.responses[code][0]
+            except KeyError:
+                message = '???'
+        self.log_error("code %d, message %s", code, message)
+        content = (self.error_message_format %
+                   {'code': code, 'message': message})
+        self.send_response(code, message)
+        self.send_header("Content-Type", self.error_content_type)
+        self.send_header("Content-Length", "%d" % len(content))
+        self.send_header('Connection', 'close')
+        self.end_headers()
+        if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
+            self.wfile.write(content)
+
     _range_regexp = re.compile(r'^(?P<start>\d+)-(?P<end>\d+)$')
     _tail_regexp = re.compile(r'^-(?P<tail>\d+)$')
 




More information about the bazaar-commits mailing list