author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 26 Mar 2010 08:30:25 +0100 | |
branch | stable |
changeset 5031 | 60c4dea96afa |
parent 4425 | b9913205d91e |
child 5155 | 1dea6e0fdfc1 |
child 5421 | 8167de96c523 |
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 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
12 |
from twisted.web2 import http, http_headers |
0 | 13 |
|
14 |
from cubicweb.web import DirectResponse |
|
15 |
from cubicweb.web.request import CubicWebRequestBase |
|
16 |
from cubicweb.web.httpcache import GMTOFFSET |
|
17 |
||
18 |
def cleanup_files(dct, encoding): |
|
19 |
d = {} |
|
20 |
for k, infos in dct.items(): |
|
21 |
for (filename, mt, stream) in infos: |
|
22 |
if filename: |
|
23 |
# XXX: suppose that no file submitted <-> no filename |
|
24 |
filename = unicode(filename, encoding) |
|
25 |
mt = u'%s/%s' % (mt.mediaType, mt.mediaSubtype) |
|
26 |
d[k] = (filename, mt, stream) |
|
27 |
return d |
|
28 |
||
29 |
||
30 |
class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
31 |
def __init__(self, req, vreg, https, base_url): |
|
32 |
self._twreq = req |
|
33 |
self._base_url = base_url |
|
34 |
super(CubicWebTwistedRequestAdapter, self).__init__(vreg, https, req.args) |
|
35 |
self.form.update(cleanup_files(req.files, self.encoding)) |
|
36 |
# prepare output headers |
|
37 |
self.headers_out = http_headers.Headers() |
|
38 |
self._headers = req.headers |
|
39 |
||
40 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
41 |
"""return the root url of the instance""" |
0 | 42 |
return self._base_url |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
43 |
|
0 | 44 |
def http_method(self): |
45 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
46 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
47 |
|
0 | 48 |
def relative_path(self, includeparams=True): |
49 |
"""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
|
50 |
to the instance's root, but some other normalization may be needed |
0 | 51 |
so that the returned path may be used to compare to generated urls |
52 |
||
53 |
:param includeparams: |
|
54 |
boolean indicating if GET form parameters should be kept in the path |
|
55 |
""" |
|
56 |
path = self._twreq.uri[1:] # remove the root '/' |
|
57 |
if not includeparams: |
|
58 |
path = path.split('?', 1)[0] |
|
59 |
return path |
|
60 |
||
61 |
def get_header(self, header, default=None, raw=True): |
|
62 |
"""return the value associated with the given input header, |
|
63 |
raise KeyError if the header is not set |
|
64 |
""" |
|
65 |
if raw: |
|
66 |
return self._twreq.headers.getRawHeaders(header, [default])[0] |
|
67 |
return self._twreq.headers.getHeader(header, default) |
|
68 |
||
69 |
def set_header(self, header, value, raw=True): |
|
70 |
"""set an output HTTP header""" |
|
71 |
if raw: |
|
72 |
# adding encoded header is important, else page content |
|
73 |
# will be reconverted back to unicode and apart unefficiency, this |
|
74 |
# may cause decoding problem (e.g. when downloading a file) |
|
75 |
self.headers_out.setRawHeaders(header, [str(value)]) |
|
76 |
else: |
|
77 |
self.headers_out.setHeader(header, value) |
|
78 |
||
79 |
def add_header(self, header, value): |
|
80 |
"""add an output HTTP header""" |
|
81 |
# adding encoded header is important, else page content |
|
82 |
# will be reconverted back to unicode and apart unefficiency, this |
|
83 |
# may cause decoding problem (e.g. when downloading a file) |
|
84 |
self.headers_out.addRawHeader(header, str(value)) |
|
85 |
||
86 |
def remove_header(self, header): |
|
87 |
"""remove an output HTTP header""" |
|
88 |
self.headers_out.removeHeader(header) |
|
89 |
||
90 |
def _validate_cache(self): |
|
91 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
92 |
exists and is still usable |
|
93 |
""" |
|
94 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
95 |
# Expires header seems to be required by IE7 |
|
96 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
97 |
return |
|
98 |
try: |
|
99 |
http.checkPreconditions(self._twreq, _PreResponse(self)) |
|
100 |
except http.HTTPError, ex: |
|
101 |
self.info('valid http cache, no actual rendering') |
|
102 |
raise DirectResponse(ex.response) |
|
103 |
# Expires header seems to be required by IE7 |
|
104 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
105 |
||
106 |
def header_accept_language(self): |
|
107 |
"""returns an ordered list of preferred languages""" |
|
108 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
109 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
110 |
reverse=True): |
|
111 |
lang = lang.split('-')[0] |
|
112 |
yield lang |
|
113 |
||
114 |
def header_if_modified_since(self): |
|
115 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 116 |
date time value (GMT), else return None |
0 | 117 |
""" |
118 |
mtime = self.get_header('If-modified-since', raw=False) |
|
119 |
if mtime: |
|
120 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
121 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 122 |
return None |
123 |
||
124 |
||
125 |
class _PreResponse(object): |
|
126 |
def __init__(self, request): |
|
127 |
self.headers = request.headers_out |
|
128 |
self.code = 200 |