author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 22 Jul 2013 11:09:28 +0200 | |
branch | stable |
changeset 9170 | e6fe77dbcfdf |
parent 7815 | 2a164a9cf81c |
child 9735 | b71158815bc8 |
permissions | -rw-r--r-- |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
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 |
"""This package contains all WSGI specific code for cubicweb |
19 |
||
20 |
NOTE: this package borrows a lot of code to Django |
|
21 |
(http://www.djangoproject.com) and to the wsgiref module |
|
22 |
of the python2.5's stdlib. |
|
23 |
||
24 |
WSGI corresponding PEP: http://www.python.org/dev/peps/pep-0333/ |
|
25 |
||
26 |
""" |
|
27 |
__docformat__ = "restructuredtext en" |
|
28 |
||
29 |
from email import message, message_from_string |
|
30 |
from Cookie import SimpleCookie |
|
31 |
from StringIO import StringIO |
|
32 |
from cgi import parse_header, parse_qsl |
|
33 |
from pprint import pformat as _pformat |
|
34 |
||
35 |
||
36 |
def pformat(obj): |
|
37 |
"""pretty prints `obj` if possible""" |
|
38 |
try: |
|
39 |
return _pformat(obj) |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
40 |
except Exception: |
0 | 41 |
return u'<could not parse>' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
42 |
|
0 | 43 |
def qs2dict(qs): |
44 |
"""transforms a query string into a regular python dict""" |
|
45 |
result = {} |
|
46 |
for key, value in parse_qsl(qs, True): |
|
47 |
result.setdefault(key, []).append(value) |
|
48 |
return result |
|
49 |
||
50 |
def normalize_header(header): |
|
51 |
"""returns a normalized header name |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
52 |
|
0 | 53 |
>>> normalize_header('User_Agent') |
54 |
'User-agent' |
|
55 |
""" |
|
56 |
return header.replace('_', '-').capitalize() |
|
57 |
||
58 |
def safe_copyfileobj(fsrc, fdst, length=16*1024, size=0): |
|
59 |
""" |
|
60 |
THIS COMES FROM DJANGO |
|
61 |
A version of shutil.copyfileobj that will not read more than 'size' bytes. |
|
62 |
This makes it safe from clients sending more than CONTENT_LENGTH bytes of |
|
63 |
data in the body. |
|
64 |
""" |
|
65 |
if not size: |
|
66 |
return |
|
67 |
while size > 0: |
|
68 |
buf = fsrc.read(min(length, size)) |
|
69 |
if not buf: |
|
70 |
break |
|
71 |
fdst.write(buf) |
|
72 |
size -= len(buf) |
|
73 |
||
74 |
def parse_file_upload(header_dict, post_data): |
|
75 |
"""This is adapted FROM DJANGO""" |
|
76 |
raw_message = '\r\n'.join('%s:%s' % pair for pair in header_dict.iteritems()) |
|
77 |
raw_message += '\r\n\r\n' + post_data |
|
78 |
msg = message_from_string(raw_message) |
|
79 |
post, files = {}, {} |
|
80 |
for submessage in msg.get_payload(): |
|
81 |
name_dict = parse_header(submessage['Content-Disposition'])[1] |
|
82 |
key = name_dict['name'] |
|
83 |
# name_dict is something like {'name': 'file', 'filename': 'test.txt'} for file uploads |
|
84 |
# or {'name': 'blah'} for POST fields |
|
85 |
# We assume all uploaded files have a 'filename' set. |
|
86 |
if 'filename' in name_dict: |
|
87 |
assert type([]) != type(submessage.get_payload()), "Nested MIME messages are not supported" |
|
88 |
if not name_dict['filename'].strip(): |
|
89 |
continue |
|
90 |
# IE submits the full path, so trim everything but the basename. |
|
91 |
# (We can't use os.path.basename because that uses the server's |
|
92 |
# directory separator, which may not be the same as the |
|
93 |
# client's one.) |
|
94 |
filename = name_dict['filename'][name_dict['filename'].rfind("\\")+1:] |
|
95 |
mimetype = 'Content-Type' in submessage and submessage['Content-Type'] or None |
|
96 |
content = StringIO(submessage.get_payload()) |
|
97 |
files[key] = [filename, mimetype, content] |
|
98 |
else: |
|
99 |
post.setdefault(key, []).append(submessage.get_payload()) |
|
100 |
return post, files |