]> granicus.if.org Git - python/commitdiff
#1492: allow overriding BaseHTTPServer's content type for error messages.
authorGeorg Brandl <georg@python.org>
Sat, 23 Feb 2008 15:02:28 +0000 (15:02 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 23 Feb 2008 15:02:28 +0000 (15:02 +0000)
Doc/library/basehttpserver.rst
Lib/BaseHTTPServer.py
Misc/NEWS

index 2e8d6a380683da7d5d5d4f68c371896d1bc6e130..0f058a18e1657e4920633c77246f8bcf671cbeb8 100644 (file)
@@ -122,6 +122,15 @@ to a handler.  Code to create and run the server looks like this::
    class variable.
 
 
+.. attribute:: BaseHTTPRequestHandler.error_content_type
+
+   Specifies the Content-Type HTTP header of error responses sent to the client.
+   The default value is ``'text/html'``.
+
+   .. versionadded:: 2.6
+      Previously, the content type was always ``'text/html'``.
+
+
 .. attribute:: BaseHTTPRequestHandler.protocol_version
 
    This specifies the HTTP protocol version used in responses.  If set to
index e4e1a148b8cccbf52222adb5f053d1a46385a56d..97d800cb390b5ab914f2e13274833e4b80403214 100644 (file)
@@ -76,7 +76,7 @@ import socket # For gethostbyaddr()
 import mimetools
 import SocketServer
 
-# Default error message
+# Default error message template
 DEFAULT_ERROR_MESSAGE = """\
 <head>
 <title>Error response</title>
@@ -89,6 +89,8 @@ DEFAULT_ERROR_MESSAGE = """\
 </body>
 """
 
+DEFAULT_ERROR_CONTENT_TYPE = "text/html"
+
 def _quote_html(html):
     return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
 
@@ -342,13 +344,14 @@ class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
         content = (self.error_message_format %
                    {'code': code, 'message': _quote_html(message), 'explain': explain})
         self.send_response(code, message)
-        self.send_header("Content-Type", "text/html")
+        self.send_header("Content-Type", self.error_content_type)
         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)
 
     error_message_format = DEFAULT_ERROR_MESSAGE
+    error_content_type = DEFAULT_ERROR_CONTENT_TYPE
 
     def send_response(self, code, message=None):
         """Send the response header and log the response code.
index 055f7689651313419f170e01e3f6ee98acb7b766..180d25ae6ec13ce2b9793bb5638625e80f36db0d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -436,6 +436,9 @@ Core and builtins
 Library
 -------
 
+- #1492: The content type of BaseHTTPServer error messages can now be
+  overridden.
+
 - Issue 1781: ConfigParser now does not let you add the "default" section
   (ignore-case)