1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
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 pprint import pformat as _pformat |
|
31 |
|
32 from six.moves.http_cookies import SimpleCookie |
|
33 |
|
34 def pformat(obj): |
|
35 """pretty prints `obj` if possible""" |
|
36 try: |
|
37 return _pformat(obj) |
|
38 except Exception: |
|
39 return u'<could not parse>' |
|
40 |
|
41 def normalize_header(header): |
|
42 """returns a normalized header name |
|
43 |
|
44 >>> normalize_header('User_Agent') |
|
45 'User-agent' |
|
46 """ |
|
47 return header.replace('_', '-').capitalize() |
|
48 |
|
49 def safe_copyfileobj(fsrc, fdst, length=16*1024, size=0): |
|
50 """ |
|
51 THIS COMES FROM DJANGO |
|
52 A version of shutil.copyfileobj that will not read more than 'size' bytes. |
|
53 This makes it safe from clients sending more than CONTENT_LENGTH bytes of |
|
54 data in the body. |
|
55 """ |
|
56 if not size: |
|
57 return |
|
58 while size > 0: |
|
59 buf = fsrc.read(min(length, size)) |
|
60 if not buf: |
|
61 break |
|
62 fdst.write(buf) |
|
63 size -= len(buf) |
|