Rev 5355: Set a Content-Length header on errors for HTTP/1.1. in file:///home/vila/src/bzr/bugs/568421-http-error-length/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Jul 20 15:36:42 BST 2010


At file:///home/vila/src/bzr/bugs/568421-http-error-length/

------------------------------------------------------------
revno: 5355
revision-id: v.ladeuil+lp at free.fr-20100720143642-u3x5uvkgny6iie2p
parent: pqm at pqm.ubuntu.com-20100720121136-r4c42ke7mjobuuo4
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 568421-http-error-length
timestamp: Tue 2010-07-20 16:36:42 +0200
message:
  Set a Content-Length header on errors for HTTP/1.1.
-------------- next part --------------
=== 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