author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 26 Oct 2011 17:58:21 +0200 | |
branch | stable |
changeset 8025 | 11caeed5ae80 |
parent 7879 | 9aae456abab5 |
child 8309 | 48ef505aa9f9 |
permissions | -rw-r--r-- |
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
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
|
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/>. |
5443
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
18 |
"""Twisted request handler for CubicWeb""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
22 |
from datetime import datetime |
0 | 23 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
24 |
from twisted.web import http |
0 | 25 |
|
26 |
from cubicweb.web import DirectResponse |
|
27 |
from cubicweb.web.request import CubicWebRequestBase |
|
28 |
from cubicweb.web.httpcache import GMTOFFSET |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
29 |
from cubicweb.web.http_headers import Headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
30 |
from cubicweb.etwist.http import not_modified_response |
0 | 31 |
|
32 |
||
33 |
class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
34 |
def __init__(self, req, vreg, https, base_url): |
|
35 |
self._twreq = req |
|
36 |
self._base_url = base_url |
|
37 |
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
|
38 |
for key, (name, stream) in req.files.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
39 |
if name is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
40 |
self.form[key] = (name, stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
41 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
42 |
self.form[key] = (unicode(name, self.encoding), stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
43 |
# XXX can't we keep received_headers? |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
44 |
self._headers_in = Headers() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
45 |
for k, v in req.received_headers.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
46 |
self._headers_in.addRawHeader(k, v) |
0 | 47 |
|
48 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
49 |
"""return the root url of the instance""" |
0 | 50 |
return self._base_url |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
51 |
|
0 | 52 |
def http_method(self): |
53 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
54 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
55 |
|
0 | 56 |
def relative_path(self, includeparams=True): |
5443
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
57 |
"""return the normalized path of the request (ie at least relative to |
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
58 |
the instance's root, but some other normalization may be needed so that |
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
59 |
the returned path may be used to compare to generated urls |
0 | 60 |
|
61 |
:param includeparams: |
|
62 |
boolean indicating if GET form parameters should be kept in the path |
|
63 |
""" |
|
64 |
path = self._twreq.uri[1:] # remove the root '/' |
|
65 |
if not includeparams: |
|
66 |
path = path.split('?', 1)[0] |
|
67 |
return path |
|
68 |
||
69 |
def get_header(self, header, default=None, raw=True): |
|
5443
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
70 |
"""return the value associated with the given input header, raise |
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
71 |
KeyError if the header is not set |
0 | 72 |
""" |
73 |
if raw: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
74 |
return self._headers_in.getRawHeaders(header, [default])[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
75 |
return self._headers_in.getHeader(header, default) |
0 | 76 |
|
77 |
def _validate_cache(self): |
|
78 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
79 |
exists and is still usable |
|
80 |
""" |
|
81 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
82 |
# Expires header seems to be required by IE7 |
|
83 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
84 |
return |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
85 |
# 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
|
86 |
# (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
|
87 |
# headers, see |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
88 |
# 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
|
89 |
# reference |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
90 |
last_modified = self.headers_out.getHeader('last-modified') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
91 |
if last_modified is not None: |
5867 | 92 |
status = self._twreq.setLastModified(last_modified) |
93 |
if status != http.CACHED: |
|
94 |
return |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
95 |
etag = self.headers_out.getRawHeaders('etag') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
96 |
if etag is not None: |
5867 | 97 |
status = self._twreq.setETag(etag[0]) |
98 |
if status == http.CACHED: |
|
99 |
response = not_modified_response(self._twreq, self._headers_in) |
|
100 |
raise DirectResponse(response) |
|
0 | 101 |
# Expires header seems to be required by IE7 |
102 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
103 |
||
104 |
def header_accept_language(self): |
|
105 |
"""returns an ordered list of preferred languages""" |
|
106 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
107 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
108 |
reverse=True): |
|
109 |
lang = lang.split('-')[0] |
|
110 |
yield lang |
|
111 |
||
112 |
def header_if_modified_since(self): |
|
113 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 114 |
date time value (GMT), else return None |
0 | 115 |
""" |
116 |
mtime = self.get_header('If-modified-since', raw=False) |
|
117 |
if mtime: |
|
118 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
119 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 120 |
return None |