author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 10:06:01 +0200 | |
branch | stable |
changeset 5421 | 8167de96c523 |
parent 4425 | b9913205d91e |
child 5423 | e15abfdcce38 |
child 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
10 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
11 |
# logilab-common is distributed in the hope that it will be useful, but WITHOUT |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4425
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Twisted request handler for CubicWeb |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
23 |
from datetime import datetime |
0 | 24 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
25 |
from twisted.web2 import http, http_headers |
0 | 26 |
|
27 |
from cubicweb.web import DirectResponse |
|
28 |
from cubicweb.web.request import CubicWebRequestBase |
|
29 |
from cubicweb.web.httpcache import GMTOFFSET |
|
30 |
||
31 |
def cleanup_files(dct, encoding): |
|
32 |
d = {} |
|
33 |
for k, infos in dct.items(): |
|
34 |
for (filename, mt, stream) in infos: |
|
35 |
if filename: |
|
36 |
# XXX: suppose that no file submitted <-> no filename |
|
37 |
filename = unicode(filename, encoding) |
|
38 |
mt = u'%s/%s' % (mt.mediaType, mt.mediaSubtype) |
|
39 |
d[k] = (filename, mt, stream) |
|
40 |
return d |
|
41 |
||
42 |
||
43 |
class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
44 |
def __init__(self, req, vreg, https, base_url): |
|
45 |
self._twreq = req |
|
46 |
self._base_url = base_url |
|
47 |
super(CubicWebTwistedRequestAdapter, self).__init__(vreg, https, req.args) |
|
48 |
self.form.update(cleanup_files(req.files, self.encoding)) |
|
49 |
# prepare output headers |
|
50 |
self.headers_out = http_headers.Headers() |
|
51 |
self._headers = req.headers |
|
52 |
||
53 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
"""return the root url of the instance""" |
0 | 55 |
return self._base_url |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
56 |
|
0 | 57 |
def http_method(self): |
58 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
59 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
60 |
|
0 | 61 |
def relative_path(self, includeparams=True): |
62 |
"""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
|
63 |
to the instance's root, but some other normalization may be needed |
0 | 64 |
so that the returned path may be used to compare to generated urls |
65 |
||
66 |
:param includeparams: |
|
67 |
boolean indicating if GET form parameters should be kept in the path |
|
68 |
""" |
|
69 |
path = self._twreq.uri[1:] # remove the root '/' |
|
70 |
if not includeparams: |
|
71 |
path = path.split('?', 1)[0] |
|
72 |
return path |
|
73 |
||
74 |
def get_header(self, header, default=None, raw=True): |
|
75 |
"""return the value associated with the given input header, |
|
76 |
raise KeyError if the header is not set |
|
77 |
""" |
|
78 |
if raw: |
|
79 |
return self._twreq.headers.getRawHeaders(header, [default])[0] |
|
80 |
return self._twreq.headers.getHeader(header, default) |
|
81 |
||
82 |
def set_header(self, header, value, raw=True): |
|
83 |
"""set an output HTTP header""" |
|
84 |
if raw: |
|
85 |
# adding encoded header is important, else page content |
|
86 |
# will be reconverted back to unicode and apart unefficiency, this |
|
87 |
# may cause decoding problem (e.g. when downloading a file) |
|
88 |
self.headers_out.setRawHeaders(header, [str(value)]) |
|
89 |
else: |
|
90 |
self.headers_out.setHeader(header, value) |
|
91 |
||
92 |
def add_header(self, header, value): |
|
93 |
"""add an output HTTP header""" |
|
94 |
# adding encoded header is important, else page content |
|
95 |
# will be reconverted back to unicode and apart unefficiency, this |
|
96 |
# may cause decoding problem (e.g. when downloading a file) |
|
97 |
self.headers_out.addRawHeader(header, str(value)) |
|
98 |
||
99 |
def remove_header(self, header): |
|
100 |
"""remove an output HTTP header""" |
|
101 |
self.headers_out.removeHeader(header) |
|
102 |
||
103 |
def _validate_cache(self): |
|
104 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
105 |
exists and is still usable |
|
106 |
""" |
|
107 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
108 |
# Expires header seems to be required by IE7 |
|
109 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
110 |
return |
|
111 |
try: |
|
112 |
http.checkPreconditions(self._twreq, _PreResponse(self)) |
|
113 |
except http.HTTPError, ex: |
|
114 |
self.info('valid http cache, no actual rendering') |
|
115 |
raise DirectResponse(ex.response) |
|
116 |
# Expires header seems to be required by IE7 |
|
117 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
118 |
||
119 |
def header_accept_language(self): |
|
120 |
"""returns an ordered list of preferred languages""" |
|
121 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
122 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
123 |
reverse=True): |
|
124 |
lang = lang.split('-')[0] |
|
125 |
yield lang |
|
126 |
||
127 |
def header_if_modified_since(self): |
|
128 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 129 |
date time value (GMT), else return None |
0 | 130 |
""" |
131 |
mtime = self.get_header('If-modified-since', raw=False) |
|
132 |
if mtime: |
|
133 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
134 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 135 |
return None |
136 |
||
137 |
||
138 |
class _PreResponse(object): |
|
139 |
def __init__(self, request): |
|
140 |
self.headers = request.headers_out |
|
141 |
self.code = 200 |