author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 20 Apr 2010 18:25:56 +0200 | |
changeset 5360 | 96893296772f |
parent 5155 | 1dea6e0fdfc1 |
child 5423 | e15abfdcce38 |
permissions | -rw-r--r-- |
0 | 1 |
"""Twisted request handler for CubicWeb |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
10 |
from datetime import datetime |
0 | 11 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
12 |
from twisted.web import http |
0 | 13 |
|
14 |
from cubicweb.web import DirectResponse |
|
15 |
from cubicweb.web.request import CubicWebRequestBase |
|
16 |
from cubicweb.web.httpcache import GMTOFFSET |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
17 |
from cubicweb.web.http_headers import Headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
18 |
from cubicweb.etwist.http import not_modified_response |
0 | 19 |
|
20 |
||
21 |
class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
22 |
def __init__(self, req, vreg, https, base_url): |
|
23 |
self._twreq = req |
|
24 |
self._base_url = base_url |
|
25 |
super(CubicWebTwistedRequestAdapter, self).__init__(vreg, https, req.args) |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
26 |
for key, (name, stream) in req.files.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
27 |
if name is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
28 |
self.form[key] = (name, stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
29 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
30 |
self.form[key] = (unicode(name, self.encoding), stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
31 |
# XXX can't we keep received_headers? |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
32 |
self._headers_in = Headers() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
33 |
for k, v in req.received_headers.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
34 |
self._headers_in.addRawHeader(k, v) |
0 | 35 |
|
36 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
37 |
"""return the root url of the instance""" |
0 | 38 |
return self._base_url |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
39 |
|
0 | 40 |
def http_method(self): |
41 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
42 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
43 |
|
0 | 44 |
def relative_path(self, includeparams=True): |
45 |
"""return the normalized path of the request (ie at least relative |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
to the instance's root, but some other normalization may be needed |
0 | 47 |
so that the returned path may be used to compare to generated urls |
48 |
||
49 |
:param includeparams: |
|
50 |
boolean indicating if GET form parameters should be kept in the path |
|
51 |
""" |
|
52 |
path = self._twreq.uri[1:] # remove the root '/' |
|
53 |
if not includeparams: |
|
54 |
path = path.split('?', 1)[0] |
|
55 |
return path |
|
56 |
||
57 |
def get_header(self, header, default=None, raw=True): |
|
58 |
"""return the value associated with the given input header, |
|
59 |
raise KeyError if the header is not set |
|
60 |
""" |
|
61 |
if raw: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
62 |
return self._headers_in.getRawHeaders(header, [default])[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
63 |
return self._headers_in.getHeader(header, default) |
0 | 64 |
|
65 |
def _validate_cache(self): |
|
66 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
67 |
exists and is still usable |
|
68 |
""" |
|
69 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
70 |
# Expires header seems to be required by IE7 |
|
71 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
72 |
return |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
73 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
74 |
# when using both 'Last-Modified' and 'ETag' response headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
75 |
# (i.e. using respectively If-Modified-Since and If-None-Match request |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
76 |
# headers, see |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
77 |
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4 for |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
78 |
# reference |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
79 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
80 |
cached_because_not_modified_since = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
81 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
82 |
last_modified = self.headers_out.getHeader('last-modified') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
83 |
if last_modified is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
84 |
cached_because_not_modified_since = (self._twreq.setLastModified(last_modified) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
85 |
== http.CACHED) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
86 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
87 |
if not cached_because_not_modified_since: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
88 |
return |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
89 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
90 |
cached_because_etag_is_same = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
91 |
etag = self.headers_out.getRawHeaders('etag') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
92 |
if etag is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
93 |
cached_because_etag_is_same = self._twreq.setETag(etag[0]) == http.CACHED |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
94 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
95 |
if cached_because_etag_is_same: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
96 |
response = not_modified_response(self._twreq, self._headers_in) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
97 |
raise DirectResponse(response) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
98 |
|
0 | 99 |
# Expires header seems to be required by IE7 |
100 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
101 |
||
102 |
def header_accept_language(self): |
|
103 |
"""returns an ordered list of preferred languages""" |
|
104 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
105 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
106 |
reverse=True): |
|
107 |
lang = lang.split('-')[0] |
|
108 |
yield lang |
|
109 |
||
110 |
def header_if_modified_since(self): |
|
111 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 112 |
date time value (GMT), else return None |
0 | 113 |
""" |
114 |
mtime = self.get_header('If-modified-since', raw=False) |
|
115 |
if mtime: |
|
116 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
117 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 118 |
return None |