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