Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1576

Show
Ignore:
Timestamp:
12/28/06 16:16:54
Author:
dowski
Message:

Backport of [1187] to 2.x branch. (Age header in cached responses). See #567.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cherrypy-2.x/cherrypy/filters/cachefilter.py

    r1524 r1576  
    118118         
    119119        request = cherrypy.request 
     120        response = cherrypy.response 
    120121         
    121122        # POST, PUT, DELETE should invalidate (delete) the cached copy. 
     
    130131            # found a hit! check the if-modified-since request header 
    131132            expirationTime, lastModified, obj = cacheData 
    132             s, h, b = obj 
     133            s, h, b, create_time = obj 
    133134            modifiedSince = request.headers.get('If-Modified-Since', None) 
    134135            if modifiedSince is not None and modifiedSince == lastModified: 
    135136                cherrypy._cache.totNonModified += 1 
    136                 cherrypy.response.status = "304 Not Modified" 
    137                 ct = h.get("Content-Type"
     137                response.status = "304 Not Modified" 
     138                ct = h.get('Content-Type', None
    138139                if ct: 
    139                     cherrypy.response.header_list["Content-Type"] = ct 
    140                 cherrypy.response.body = None 
     140                    response.headers['Content-Type'] = ct 
     141                response.body = None 
    141142            else: 
    142143                # serve it & get out from the request 
    143144                response = cherrypy.response 
    144                 response.status, response.header_list, response.body = s, h, b 
    145             raise cherrypy.RequestHandled() 
     145                response.status, response.headers, response.body = s, h, b 
     146            response.headers['Age'] = str(int(time.time() - create_time)) 
     147            request.execute_main = False 
    146148        else: 
    147149            request.cacheable = True 
     
    169171            # save the cache data 
    170172            body = ''.join([chunk for chunk in response._cachefilter_tee]) 
     173            create_time = time.time() 
    171174            cherrypy._cache.put(lastModified, (response.status, 
    172                                                response.header_list, 
    173                                                body)) 
     175                                               response.headers, 
     176                                               body, 
     177                                               create_time)) 
    174178 
    175179 
  • branches/cherrypy-2.x/cherrypy/test/test_cache_filter.py

    r1506 r1576  
    44import cherrypy 
    55from cherrypy.filters.cachefilter import expires 
     6from cherrypy.lib.httptools import HTTPDate 
    67 
    78 
     
    1617            return msg 
    1718        index.exposed = True 
    18      
     19         
     20        def textplain(self): 
     21            cherrypy.response.headers['Content-type'] = 'text/plain' 
     22            cherrypy.response.headers['Last-Modified'] = HTTPDate() 
     23            return self.index() 
     24        textplain.exposed = True 
     25         
    1926    class UnCached(object): 
    2027         
     
    6067     
    6168    def testCaching(self): 
     69        elapsed = 0.0 
    6270        for trial in xrange(10): 
    6371            self.getPage("/") 
    64             # The response should be the same every time! 
     72            # The response should be the same every time, 
     73            # except for the Age response header. 
    6574            self.assertBody('visit #1') 
     75            if trial != 0: 
     76                age = int(self.assertHeader("Age")) 
     77                self.assert_(age >= elapsed) 
     78                elapsed = age 
    6679         
    6780        # POST, PUT, DELETE should not be cached. 
     
    8194        self.getPage("/", method="GET") 
    8295        self.assertBody('visit #5') 
    83      
     96 
     97        # make sure that custom set Content-types get passed through on 304s 
     98        self.getPage("/textplain") 
     99        self.assertHeader("Content-type", "text/plain") 
     100        self.assertStatus("200 OK") 
     101        self.assertBody('visit #6') 
     102        date = self.assertHeader("Last-Modified") 
     103        self.getPage("/textplain", [("If-Modified-Since", date)]) 
     104        self.assertHeader("Content-type", "text/plain") 
     105        self.assertStatus("304 Not Modified") 
     106         
    84107    def testExpiresTool(self): 
    85108         

Hosted by WebFaction

Log in as guest/cpguest to create tickets