author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Mon, 27 Feb 2012 15:24:14 +0100 | |
changeset 8309 | 48ef505aa9f9 |
parent 7879 | 9aae456abab5 |
child 8314 | cfd6ab461849 |
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): |
|
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7879
diff
changeset
|
34 |
def __init__(self, req, vreg, https): |
0 | 35 |
self._twreq = req |
36 |
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
|
37 |
for key, (name, stream) in req.files.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
38 |
if name is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
39 |
self.form[key] = (name, stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
40 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
41 |
self.form[key] = (unicode(name, self.encoding), stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
42 |
# XXX can't we keep received_headers? |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
43 |
self._headers_in = Headers() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
44 |
for k, v in req.received_headers.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
45 |
self._headers_in.addRawHeader(k, v) |
0 | 46 |
|
47 |
def http_method(self): |
|
48 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
49 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
50 |
|
0 | 51 |
def relative_path(self, includeparams=True): |
5443
f299ee54d7e0
[webconfig] cleanup base_url management
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
52 |
"""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
|
53 |
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
|
54 |
the returned path may be used to compare to generated urls |
0 | 55 |
|
56 |
:param includeparams: |
|
57 |
boolean indicating if GET form parameters should be kept in the path |
|
58 |
""" |
|
59 |
path = self._twreq.uri[1:] # remove the root '/' |
|
60 |
if not includeparams: |
|
61 |
path = path.split('?', 1)[0] |
|
62 |
return path |
|
63 |
||
64 |
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
|
65 |
"""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
|
66 |
KeyError if the header is not set |
0 | 67 |
""" |
68 |
if raw: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
69 |
return self._headers_in.getRawHeaders(header, [default])[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
70 |
return self._headers_in.getHeader(header, default) |
0 | 71 |
|
72 |
def _validate_cache(self): |
|
73 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
74 |
exists and is still usable |
|
75 |
""" |
|
76 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
77 |
# Expires header seems to be required by IE7 |
|
78 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
79 |
return |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
80 |
# 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
|
81 |
# (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
|
82 |
# headers, see |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
83 |
# 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
|
84 |
# reference |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
85 |
last_modified = self.headers_out.getHeader('last-modified') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
86 |
if last_modified is not None: |
5867 | 87 |
status = self._twreq.setLastModified(last_modified) |
88 |
if status != http.CACHED: |
|
89 |
return |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
90 |
etag = self.headers_out.getRawHeaders('etag') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
91 |
if etag is not None: |
5867 | 92 |
status = self._twreq.setETag(etag[0]) |
93 |
if status == http.CACHED: |
|
94 |
response = not_modified_response(self._twreq, self._headers_in) |
|
95 |
raise DirectResponse(response) |
|
0 | 96 |
# Expires header seems to be required by IE7 |
97 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
98 |
||
99 |
def header_accept_language(self): |
|
100 |
"""returns an ordered list of preferred languages""" |
|
101 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
102 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
103 |
reverse=True): |
|
104 |
lang = lang.split('-')[0] |
|
105 |
yield lang |
|
106 |
||
107 |
def header_if_modified_since(self): |
|
108 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 109 |
date time value (GMT), else return None |
0 | 110 |
""" |
111 |
mtime = self.get_header('If-modified-since', raw=False) |
|
112 |
if mtime: |
|
113 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
114 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 115 |
return None |