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

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Jul 20 16:17:58 BST 2010


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

------------------------------------------------------------
revno: 5062
revision-id: v.ladeuil+lp at free.fr-20100720151758-1hr1ehcrwgosxu2i
parent: pqm at pqm.ubuntu.com-20100719104420-so90w8it48g3x58h
fixes bug(s): https://launchpad.net/bugs/568421
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 2.2-568421-http-error-length
timestamp: Tue 2010-07-20 17:17:58 +0200
message:
  Set a Content-Length header on errors for HTTP/1.1
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2010-07-19 10:44:20 +0000
+++ b/NEWS	2010-07-20 15:17:58 +0000
@@ -38,6 +38,9 @@
 * Don't traceback trying to unversion children files of an already
   unversioned directory.  (Vincent Ladeuil, #494221)
 
+* ``HTTP/1.1` test servers now set a ``Content-Length`` header to comply
+  with pedantic ``HTTP/1.1`` clients. (Vincent Ladeuil, #568421)
+
 * Progress bars prefer to truncate the text message rather than the
   counters.  The spinner is shown between the network transfer indicator
   and the progress message.  (Martin Pool)

=== 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 15:17:58 +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