author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 06 Aug 2009 09:40:40 +0200 | |
branch | stable |
changeset 2714 | 4a8d93bd7c72 |
parent 1977 | 606923dff11b |
child 2476 | 1294a6bdf3bf |
permissions | -rw-r--r-- |
0 | 1 |
"""WSGI request adapter for cubicweb |
2 |
||
3 |
NOTE: each docstring tagged with ``COME FROM DJANGO`` means that |
|
4 |
the code has been taken (or adapted) from Djanco source code : |
|
5 |
http://www.djangoproject.com/ |
|
6 |
||
7 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 9 |
: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
|
10 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 11 |
""" |
12 |
||
13 |
__docformat__ = "restructuredtext en" |
|
14 |
||
15 |
from StringIO import StringIO |
|
16 |
from urllib import quote |
|
17 |
||
18 |
from logilab.common.decorators import cached |
|
19 |
||
20 |
from cubicweb.web.request import CubicWebRequestBase |
|
21 |
from cubicweb.wsgi import (pformat, qs2dict, safe_copyfileobj, parse_file_upload, |
|
22 |
normalize_header) |
|
23 |
||
24 |
||
25 |
||
26 |
class CubicWebWsgiRequest(CubicWebRequestBase): |
|
27 |
"""most of this code COMES FROM DJANO |
|
28 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
def __init__(self, environ, vreg, base_url=None): |
31 |
self.environ = environ |
|
32 |
self.path = environ['PATH_INFO'] |
|
33 |
self.method = environ['REQUEST_METHOD'].upper() |
|
34 |
self._headers = dict([(normalize_header(k[5:]), v) for k, v in self.environ.items() |
|
35 |
if k.startswith('HTTP_')]) |
|
36 |
https = environ.get("HTTPS") in ('yes', 'on', '1') |
|
37 |
self._base_url = base_url or self.application_uri() |
|
38 |
post, files = self.get_posted_data() |
|
39 |
super(CubicWebWsgiRequest, self).__init__(vreg, https, post) |
|
40 |
if files is not None: |
|
41 |
for fdef in files.itervalues(): |
|
42 |
fdef[0] = unicode(fdef[0], self.encoding) |
|
43 |
self.form.update(files) |
|
44 |
# prepare output headers |
|
45 |
self.headers_out = {} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
46 |
|
0 | 47 |
def __repr__(self): |
48 |
# Since this is called as part of error handling, we need to be very |
|
49 |
# robust against potentially malformed input. |
|
50 |
form = pformat(self.form) |
|
51 |
meta = pformat(self.environ) |
|
52 |
return '<CubicWebWsgiRequest\FORM:%s,\nMETA:%s>' % \ |
|
53 |
(form, meta) |
|
54 |
||
55 |
## cubicweb request interface ################################################ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
|
0 | 57 |
def base_url(self): |
58 |
return self._base_url |
|
59 |
||
60 |
def http_method(self): |
|
61 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
62 |
return self.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
63 |
|
0 | 64 |
def relative_path(self, includeparams=True): |
65 |
"""return the normalized path of the request (ie at least relative |
|
66 |
to the application's root, but some other normalization may be needed |
|
67 |
so that the returned path may be used to compare to generated urls |
|
68 |
||
69 |
:param includeparams: |
|
70 |
boolean indicating if GET form parameters should be kept in the path |
|
71 |
""" |
|
72 |
path = self.environ['PATH_INFO'] |
|
73 |
path = path[1:] # remove leading '/' |
|
74 |
if includeparams: |
|
75 |
qs = self.environ.get('QUERY_STRING') |
|
76 |
if qs: |
|
77 |
return '%s?%s' % (path, qs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
78 |
|
0 | 79 |
return path |
80 |
||
81 |
def get_header(self, header, default=None): |
|
82 |
"""return the value associated with the given input HTTP header, |
|
83 |
raise KeyError if the header is not set |
|
84 |
""" |
|
85 |
return self._headers.get(normalize_header(header), default) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
86 |
|
0 | 87 |
def set_header(self, header, value, raw=True): |
88 |
"""set an output HTTP header""" |
|
89 |
assert raw, "don't know anything about non-raw headers for wsgi requests" |
|
90 |
self.headers_out[header] = value |
|
91 |
||
92 |
def add_header(self, header, value): |
|
93 |
"""add an output HTTP header""" |
|
94 |
self.headers_out[header] = value |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
95 |
|
0 | 96 |
def remove_header(self, header): |
97 |
"""remove an output HTTP header""" |
|
98 |
self.headers_out.pop(header, None) |
|
99 |
||
100 |
def header_if_modified_since(self): |
|
101 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
102 |
mx date time value (GMT), else return None |
|
103 |
""" |
|
104 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
105 |
|
0 | 106 |
## wsgi request helpers ################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
107 |
|
0 | 108 |
def application_uri(self): |
109 |
"""Return the application's base URI (no PATH_INFO or QUERY_STRING) |
|
110 |
||
111 |
see python2.5's wsgiref.util.application_uri code |
|
112 |
""" |
|
113 |
environ = self.environ |
|
114 |
url = environ['wsgi.url_scheme'] + '://' |
|
115 |
if environ.get('HTTP_HOST'): |
|
116 |
url += environ['HTTP_HOST'] |
|
117 |
else: |
|
118 |
url += environ['SERVER_NAME'] |
|
119 |
if environ['wsgi.url_scheme'] == 'https': |
|
120 |
if environ['SERVER_PORT'] != '443': |
|
121 |
url += ':' + environ['SERVER_PORT'] |
|
122 |
else: |
|
123 |
if environ['SERVER_PORT'] != '80': |
|
124 |
url += ':' + environ['SERVER_PORT'] |
|
125 |
url += quote(environ.get('SCRIPT_NAME') or '/') |
|
126 |
return url |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
127 |
|
0 | 128 |
def get_full_path(self): |
129 |
return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '') |
|
130 |
||
131 |
def is_secure(self): |
|
132 |
return 'wsgi.url_scheme' in self.environ \ |
|
133 |
and self.environ['wsgi.url_scheme'] == 'https' |
|
134 |
||
135 |
def get_posted_data(self): |
|
136 |
files = None |
|
137 |
if self.method == 'POST': |
|
138 |
if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): |
|
139 |
header_dict = dict((normalize_header(k[5:]), v) |
|
140 |
for k, v in self.environ.items() |
|
141 |
if k.startswith('HTTP_')) |
|
142 |
header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '') |
|
143 |
post, files = parse_file_upload(header_dict, self.raw_post_data) |
|
144 |
else: |
|
145 |
post = qs2dict(self.raw_post_data) |
|
146 |
else: |
|
147 |
# The WSGI spec says 'QUERY_STRING' may be absent. |
|
148 |
post = qs2dict(self.environ.get('QUERY_STRING', '')) |
|
149 |
return post, files |
|
150 |
||
151 |
@property |
|
152 |
@cached |
|
153 |
def raw_post_data(self): |
|
154 |
buf = StringIO() |
|
155 |
try: |
|
156 |
# CONTENT_LENGTH might be absent if POST doesn't have content at all (lighttpd) |
|
157 |
content_length = int(self.environ.get('CONTENT_LENGTH', 0)) |
|
158 |
except ValueError: # if CONTENT_LENGTH was empty string or not an integer |
|
159 |
content_length = 0 |
|
160 |
if content_length > 0: |
|
161 |
safe_copyfileobj(self.environ['wsgi.input'], buf, |
|
162 |
size=content_length) |
|
163 |
postdata = buf.getvalue() |
|
164 |
buf.close() |
|
165 |
return postdata |
|
166 |
||
167 |
def _validate_cache(self): |
|
168 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
169 |
exists and is still usable |
|
170 |
""" |
|
171 |
# XXX |
|
172 |
# if self.get_header('Cache-Control') in ('max-age=0', 'no-cache'): |
|
173 |
# # Expires header seems to be required by IE7 |
|
174 |
# self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |
|
175 |
# return |
|
176 |
# try: |
|
177 |
# http.checkPreconditions(self._twreq, _PreResponse(self)) |
|
178 |
# except http.HTTPError, ex: |
|
179 |
# self.info('valid http cache, no actual rendering') |
|
180 |
# raise DirectResponse(ex.response) |
|
181 |
# Expires header seems to be required by IE7 |
|
182 |
self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') |