author | David Douard <david.douard@logilab.fr> |
Fri, 25 Jan 2013 17:52:02 +0100 | |
branch | stable |
changeset 8685 | 8bfc0753f1da |
parent 8316 | d5b1b75805dd |
child 8752 | e19f4bba89cd |
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, |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
35 |
normalize_header) |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
36 |
from cubicweb.web.http_headers import Headers |
0 | 37 |
|
38 |
||
39 |
||
40 |
class CubicWebWsgiRequest(CubicWebRequestBase): |
|
41 |
"""most of this code COMES FROM DJANO |
|
42 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
43 |
|
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
|
44 |
def __init__(self, environ, vreg): |
0 | 45 |
self.environ = environ |
46 |
self.path = environ['PATH_INFO'] |
|
47 |
self.method = environ['REQUEST_METHOD'].upper() |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
48 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
49 |
headers_in = dict((normalize_header(k[5:]), v) for k, v in self.environ.items() |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
50 |
if k.startswith('HTTP_')) |
0 | 51 |
https = environ.get("HTTPS") in ('yes', 'on', '1') |
52 |
post, files = self.get_posted_data() |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
53 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
54 |
super(CubicWebWsgiRequest, self).__init__(vreg, https, post, |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
55 |
headers= headers_in) |
0 | 56 |
if files is not None: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4212
diff
changeset
|
57 |
for key, (name, _, stream) in files.iteritems(): |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
58 |
if name is not None: |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
59 |
name = unicode(name, self.encoding) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4212
diff
changeset
|
60 |
self.form[key] = (name, stream) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
61 |
|
0 | 62 |
def __repr__(self): |
63 |
# Since this is called as part of error handling, we need to be very |
|
64 |
# robust against potentially malformed input. |
|
65 |
form = pformat(self.form) |
|
66 |
meta = pformat(self.environ) |
|
67 |
return '<CubicWebWsgiRequest\FORM:%s,\nMETA:%s>' % \ |
|
68 |
(form, meta) |
|
69 |
||
70 |
## cubicweb request interface ################################################ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
71 |
|
0 | 72 |
def http_method(self): |
73 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
74 |
return self.method |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
75 |
|
0 | 76 |
def relative_path(self, includeparams=True): |
77 |
"""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
|
78 |
to the instance's root, but some other normalization may be needed |
0 | 79 |
so that the returned path may be used to compare to generated urls |
80 |
||
81 |
:param includeparams: |
|
82 |
boolean indicating if GET form parameters should be kept in the path |
|
83 |
""" |
|
84 |
path = self.environ['PATH_INFO'] |
|
85 |
path = path[1:] # remove leading '/' |
|
86 |
if includeparams: |
|
87 |
qs = self.environ.get('QUERY_STRING') |
|
88 |
if qs: |
|
89 |
return '%s?%s' % (path, qs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
90 |
|
0 | 91 |
return path |
92 |
||
93 |
## wsgi request helpers ################################################### |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
94 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
95 |
def instance_uri(self): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
96 |
"""Return the instance's base URI (no PATH_INFO or QUERY_STRING) |
0 | 97 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
see python2.5's wsgiref.util.instance_uri code |
0 | 99 |
""" |
100 |
environ = self.environ |
|
101 |
url = environ['wsgi.url_scheme'] + '://' |
|
102 |
if environ.get('HTTP_HOST'): |
|
103 |
url += environ['HTTP_HOST'] |
|
104 |
else: |
|
105 |
url += environ['SERVER_NAME'] |
|
106 |
if environ['wsgi.url_scheme'] == 'https': |
|
107 |
if environ['SERVER_PORT'] != '443': |
|
108 |
url += ':' + environ['SERVER_PORT'] |
|
109 |
else: |
|
110 |
if environ['SERVER_PORT'] != '80': |
|
111 |
url += ':' + environ['SERVER_PORT'] |
|
112 |
url += quote(environ.get('SCRIPT_NAME') or '/') |
|
113 |
return url |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
114 |
|
0 | 115 |
def get_full_path(self): |
116 |
return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '') |
|
117 |
||
118 |
def is_secure(self): |
|
119 |
return 'wsgi.url_scheme' in self.environ \ |
|
120 |
and self.environ['wsgi.url_scheme'] == 'https' |
|
121 |
||
122 |
def get_posted_data(self): |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
123 |
# The WSGI spec says 'QUERY_STRING' may be absent. |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
124 |
post = qs2dict(self.environ.get('QUERY_STRING', '')) |
0 | 125 |
files = None |
126 |
if self.method == 'POST': |
|
127 |
if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): |
|
128 |
header_dict = dict((normalize_header(k[5:]), v) |
|
129 |
for k, v in self.environ.items() |
|
130 |
if k.startswith('HTTP_')) |
|
131 |
header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '') |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
132 |
post_, files = parse_file_upload(header_dict, self.raw_post_data) |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
133 |
post.update(post_) |
0 | 134 |
else: |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
135 |
post.update(qs2dict(self.raw_post_data)) |
0 | 136 |
return post, files |
137 |
||
138 |
@property |
|
139 |
@cached |
|
140 |
def raw_post_data(self): |
|
141 |
buf = StringIO() |
|
142 |
try: |
|
143 |
# CONTENT_LENGTH might be absent if POST doesn't have content at all (lighttpd) |
|
144 |
content_length = int(self.environ.get('CONTENT_LENGTH', 0)) |
|
145 |
except ValueError: # if CONTENT_LENGTH was empty string or not an integer |
|
146 |
content_length = 0 |
|
147 |
if content_length > 0: |
|
148 |
safe_copyfileobj(self.environ['wsgi.input'], buf, |
|
149 |
size=content_length) |
|
150 |
postdata = buf.getvalue() |
|
151 |
buf.close() |
|
152 |
return postdata |