etwist/request.py
changeset 8316 d5b1b75805dd
parent 8314 cfd6ab461849
child 8752 e19f4bba89cd
equal deleted inserted replaced
8315:166e6d5d8e17 8316:d5b1b75805dd
    25 
    25 
    26 from cubicweb.web import DirectResponse
    26 from cubicweb.web import DirectResponse
    27 from cubicweb.web.request import CubicWebRequestBase
    27 from cubicweb.web.request import CubicWebRequestBase
    28 from cubicweb.web.httpcache import GMTOFFSET
    28 from cubicweb.web.httpcache import GMTOFFSET
    29 from cubicweb.web.http_headers import Headers
    29 from cubicweb.web.http_headers import Headers
    30 from cubicweb.etwist.http import not_modified_response
       
    31 
    30 
    32 
    31 
    33 class CubicWebTwistedRequestAdapter(CubicWebRequestBase):
    32 class CubicWebTwistedRequestAdapter(CubicWebRequestBase):
    34     def __init__(self, req, vreg, https):
    33     def __init__(self, req, vreg, https):
    35         self._twreq = req
    34         self._twreq = req
    55         """
    54         """
    56         path = self._twreq.uri[1:] # remove the root '/'
    55         path = self._twreq.uri[1:] # remove the root '/'
    57         if not includeparams:
    56         if not includeparams:
    58             path = path.split('?', 1)[0]
    57             path = path.split('?', 1)[0]
    59         return path
    58         return path
    60 
       
    61     def _validate_cache(self):
       
    62         """raise a `DirectResponse` exception if a cached page along the way
       
    63         exists and is still usable
       
    64         """
       
    65         if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'):
       
    66             # Expires header seems to be required by IE7
       
    67             self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
       
    68             return
       
    69         # when using both 'Last-Modified' and 'ETag' response headers
       
    70         # (i.e. using respectively If-Modified-Since and If-None-Match request
       
    71         # headers, see
       
    72         # http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4 for
       
    73         # reference
       
    74         last_modified = self.headers_out.getHeader('last-modified')
       
    75         if last_modified is not None:
       
    76             status = self._twreq.setLastModified(last_modified)
       
    77             if status != http.CACHED:
       
    78                 return
       
    79         etag = self.headers_out.getRawHeaders('etag')
       
    80         if etag is not None:
       
    81             status = self._twreq.setETag(etag[0])
       
    82             if status == http.CACHED:
       
    83                 response = not_modified_response(self._twreq, self._headers_in)
       
    84                 raise DirectResponse(response)
       
    85         # Expires header seems to be required by IE7
       
    86         self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')