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