author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:24:20 +0200 | |
branch | stable |
changeset 5426 | 0d4853a6e5ee |
parent 5423 | e15abfdcce38 |
parent 5424 | 8ecbcbff9777 |
child 5443 | f299ee54d7e0 |
child 5867 | 7ef636d24ab2 |
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 |
# |
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/>. |
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 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
25 |
from twisted.web import http |
0 | 26 |
|
27 |
from cubicweb.web import DirectResponse |
|
28 |
from cubicweb.web.request import CubicWebRequestBase |
|
29 |
from cubicweb.web.httpcache import GMTOFFSET |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
30 |
from cubicweb.web.http_headers import Headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
31 |
from cubicweb.etwist.http import not_modified_response |
0 | 32 |
|
33 |
||
34 |
class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
35 |
def __init__(self, req, vreg, https, base_url): |
|
36 |
self._twreq = req |
|
37 |
self._base_url = base_url |
|
38 |
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
|
39 |
for key, (name, stream) in req.files.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
40 |
if name is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
41 |
self.form[key] = (name, stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
42 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
43 |
self.form[key] = (unicode(name, self.encoding), stream) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
44 |
# XXX can't we keep received_headers? |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
45 |
self._headers_in = Headers() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
46 |
for k, v in req.received_headers.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
47 |
self._headers_in.addRawHeader(k, v) |
0 | 48 |
|
49 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
50 |
"""return the root url of the instance""" |
0 | 51 |
return self._base_url |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
52 |
|
0 | 53 |
def http_method(self): |
54 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
55 |
return self._twreq.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
56 |
|
0 | 57 |
def relative_path(self, includeparams=True): |
58 |
"""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
|
59 |
to the instance's root, but some other normalization may be needed |
0 | 60 |
so that the returned path may be used to compare to generated urls |
61 |
||
62 |
:param includeparams: |
|
63 |
boolean indicating if GET form parameters should be kept in the path |
|
64 |
""" |
|
65 |
path = self._twreq.uri[1:] # remove the root '/' |
|
66 |
if not includeparams: |
|
67 |
path = path.split('?', 1)[0] |
|
68 |
return path |
|
69 |
||
70 |
def get_header(self, header, default=None, raw=True): |
|
71 |
"""return the value associated with the given input header, |
|
72 |
raise KeyError if the header is not set |
|
73 |
""" |
|
74 |
if raw: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
75 |
return self._headers_in.getRawHeaders(header, [default])[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
76 |
return self._headers_in.getHeader(header, default) |
0 | 77 |
|
78 |
def _validate_cache(self): |
|
79 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
80 |
exists and is still usable |
|
81 |
""" |
|
82 |
if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
83 |
# Expires header seems to be required by IE7 |
|
84 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
85 |
return |
|
5155
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 |
# 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
|
88 |
# (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
|
89 |
# headers, see |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
90 |
# 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
|
91 |
# reference |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
92 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
93 |
cached_because_not_modified_since = False |
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 |
last_modified = self.headers_out.getHeader('last-modified') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
96 |
if last_modified is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
97 |
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
|
98 |
== http.CACHED) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
99 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
100 |
if not cached_because_not_modified_since: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
101 |
return |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
102 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
103 |
cached_because_etag_is_same = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
104 |
etag = self.headers_out.getRawHeaders('etag') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
105 |
if etag is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
106 |
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
|
107 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
108 |
if cached_because_etag_is_same: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
109 |
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
|
110 |
raise DirectResponse(response) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4425
diff
changeset
|
111 |
|
0 | 112 |
# Expires header seems to be required by IE7 |
113 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
114 |
||
115 |
def header_accept_language(self): |
|
116 |
"""returns an ordered list of preferred languages""" |
|
117 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
|
118 |
for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1], |
|
119 |
reverse=True): |
|
120 |
lang = lang.split('-')[0] |
|
121 |
yield lang |
|
122 |
||
123 |
def header_if_modified_since(self): |
|
124 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
4425 | 125 |
date time value (GMT), else return None |
0 | 126 |
""" |
127 |
mtime = self.get_header('If-modified-since', raw=False) |
|
128 |
if mtime: |
|
129 |
# :/ twisted is returned a localized time stamp |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
130 |
return datetime.fromtimestamp(mtime) + GMTOFFSET |
0 | 131 |
return None |