1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
763 controller = self.relative_path(False).split('/', 1)[0] |
763 controller = self.relative_path(False).split('/', 1)[0] |
764 if controller in self.vreg['controllers']: |
764 if controller in self.vreg['controllers']: |
765 return controller |
765 return controller |
766 return 'view' |
766 return 'view' |
767 |
767 |
768 def validate_cache(self): |
768 def is_client_cache_valid(self): |
769 """raise a `StatusResponse` exception if a cached page along the way |
769 """check if a client cached page exists (as specified in request |
770 exists and is still usable. |
770 headers) and is still usable. |
771 |
771 |
772 calls the client-dependant implementation of `_validate_cache` |
772 Return False if the page has to be calculated, else True. |
|
773 |
|
774 Some response cache headers may be set by this method. |
773 """ |
775 """ |
774 modified = True |
776 modified = True |
775 if self.get_header('Cache-Control') not in ('max-age=0', 'no-cache'): |
777 if self.get_header('Cache-Control') not in ('max-age=0', 'no-cache'): |
776 # Here, we search for any invalid 'not modified' condition |
778 # Here, we search for any invalid 'not modified' condition |
777 # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3 |
779 # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3 |
782 if modified: |
784 if modified: |
783 if 'Expires' not in self.headers_out: |
785 if 'Expires' not in self.headers_out: |
784 # Expires header seems to be required by IE7 -- Are you sure ? |
786 # Expires header seems to be required by IE7 -- Are you sure ? |
785 self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
787 self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
786 if self.http_method() == 'HEAD': |
788 if self.http_method() == 'HEAD': |
787 raise StatusResponse(200, '') |
789 self.status_out = 200 |
788 # /!\ no raise, the function returns and we keep processing the request) |
790 # XXX replace by True once validate_cache bw compat method is dropped |
|
791 return 200 |
|
792 # /!\ no raise, the function returns and we keep processing the request |
789 else: |
793 else: |
790 # overwrite headers_out to forge a brand new not-modified response |
794 # overwrite headers_out to forge a brand new not-modified response |
791 self.headers_out = self._forge_cached_headers() |
795 self.headers_out = self._forge_cached_headers() |
792 if self.http_method() in ('HEAD', 'GET'): |
796 if self.http_method() in ('HEAD', 'GET'): |
793 raise StatusResponse(httplib.NOT_MODIFIED) |
797 self.status_out = httplib.NOT_MODIFIED |
794 else: |
798 else: |
795 raise StatusResponse(httplib.PRECONDITION_FAILED) |
799 self.status_out = httplib.PRECONDITION_FAILED |
|
800 # XXX replace by True once validate_cache bw compat method is dropped |
|
801 return self.status_out |
|
802 # XXX replace by False once validate_cache bw compat method is dropped |
|
803 return None |
|
804 |
|
805 @deprecated('[3.18] use .is_client_cache_valid() method instead') |
|
806 def validate_cache(self): |
|
807 """raise a `StatusResponse` exception if a cached page along the way |
|
808 exists and is still usable. |
|
809 """ |
|
810 status_code = self.is_client_cache_valid() |
|
811 if status_code is not None: |
|
812 raise StatusResponse(status_code) |
796 |
813 |
797 # abstract methods to override according to the web front-end ############# |
814 # abstract methods to override according to the web front-end ############# |
798 |
815 |
799 def http_method(self): |
816 def http_method(self): |
800 """returns 'POST', 'GET', 'HEAD', etc.""" |
817 """returns 'POST', 'GET', 'HEAD', etc.""" |