Commits

Robert Brewer committed 1f23f5b

Eliminated 'default_content_type' config entry by moving tools.response_headers to on_start_resource. If anyone still needs response headers to be set late, they can call tools.response_headers later, either in code or via a hook declared in config.

  • Participants
  • Parent commits d3a14f0
  • Branches cherrypy

Comments (0)

Files changed (6)

 class Response(object):
     """An HTTP Response."""
     
+    # Class attributes for dev-time introspection.
     status = None
     header_list = None
     headers = http.HeaderMap()
         self.time = time.time()
         
         self.headers = http.HeaderMap()
-        content_type = cherrypy.config.get('default_content_type', 'text/html')
         # Since we know all our keys are titled strings, we can
         # bypass HeaderMap.update and get a big speed boost.
         dict.update(self.headers, {
-            "Content-Type": content_type,
+            "Content-Type": 'text/html',
             "Server": "CherryPy/" + cherrypy.__version__,
             "Date": http.HTTPDate(self.time),
-            "Set-Cookie": [],
-            "Content-Length": None
         })
         self.simple_cookie = Cookie.SimpleCookie()
     
 default_toolbox = Toolbox()
 default_toolbox.session_auth = MainTool(cptools.session_auth)
 default_toolbox.proxy = Tool('before_request_body', cptools.proxy)
-default_toolbox.response_headers = Tool('before_finalize', cptools.response_headers)
+default_toolbox.response_headers = Tool('on_start_resource', cptools.response_headers)
 # We can't call virtual_host in on_start_resource,
 # because it's failsafe and the redirect would be swallowed.
 default_toolbox.virtual_host = Tool('before_request_body', cptools.virtual_host)
             secs = (86400 * secs.days) + secs.seconds
         
         if secs == 0:
-            cptools.response_headers([("Pragma", "no-cache")], force)
+            if force or "Pragma" not in cherrypy.response.headers:
+                cherrypy.response.headers["Pragma"] = "no-cache"
             if cherrypy.request.version >= (1, 1):
-                cptools.response_headers([("Cache-Control", "no-cache")], force)
+                if force or "Cache-Control" not in cherrypy.response.headers:
+                    cherrypy.response.headers["Cache-Control"] = "no-cache"
         
         expiry = http.HTTPDate(cherrypy.response.time + secs)
-        cptools.response_headers([("Expires", expiry)], force)
+        if force or "Expires" not in cherrypy.response.headers:
+            cherrypy.response.headers["Expires"] = expiry
             request.remote_host = xff
 
 
-def response_headers(headers=None, force=True):
+def response_headers(headers=None):
     """Set headers on the response."""
     for name, value in (headers or []):
-        if force or (name not in cherrypy.response.headers):
-            cherrypy.response.headers[name] = value
+        cherrypy.response.headers[name] = value
 
 
 _login_screen = """<html><body>

test/test_core.py

         
         def defct(self, newct):
             newct = "text/%s" % newct
-            cherrypy.config.update({'default_content_type': newct})
+            cherrypy.config.update({'tools.response_headers.on': True,
+                                    'tools.response_headers.headers':
+                                    [('Content-Type', newct)]})
         defct.exposed = True
         
         def upload(self, file):

test/test_response_headers.py

         other.exposed = True
         other._cp_config = {
             'tools.response_headers.on': True,
-            'tools.response_headers.force': False,
             'tools.response_headers.headers': [("Content-Language", "fr"),
                                                ('Content-Type', 'text/plain')],
             }
     def testResponseHeaders(self):
         self.getPage('/other')
         self.assertHeader("Content-Language", "fr")
-        # Since 'force' is False, the tool should only change headers
-        # that have not been set yet.
-        # Content-Type should have been set when the response object
-        # was created (default to text/html)
-        self.assertHeader('Content-Type', 'text/html')
+        self.assertHeader('Content-Type', 'text/plain')
 
 if __name__ == "__main__":
     setup_server()