[issue15991] BaseHTTPServer with ThreadingMixIn serving wrong data sometimes

Antoine Pitrou report at bugs.python.org
Mon Sep 24 02:13:42 CEST 2012


Antoine Pitrou added the comment:

Note that forcing a content length on error responses also seems to make wget happy:


diff --git a/Lib/BaseHTTPServer.py b/Lib/BaseHTTPServer.py
--- a/Lib/BaseHTTPServer.py
+++ b/Lib/BaseHTTPServer.py
@@ -362,14 +362,19 @@
             message = short
         explain = long
         self.log_error("code %d, message %s", code, message)
-        # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
-        content = (self.error_message_format %
-                   {'code': code, 'message': _quote_html(message), 'explain': explain})
+        if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
+            # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
+            content = (self.error_message_format %
+                       {'code': code, 'message': _quote_html(message), 'explain': explain})
+        else:
+            content = None
         self.send_response(code, message)
         self.send_header("Content-Type", self.error_content_type)
+        if content is not None:
+            self.send_header("Content-Length", str(len(content)))
         self.send_header('Connection', 'close')
         self.end_headers()
-        if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
+        if content is not None:
             self.wfile.write(content)
 
     error_message_format = DEFAULT_ERROR_MESSAGE

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15991>
_______________________________________


More information about the Python-bugs-list mailing list