30 from StringIO import StringIO |
30 from StringIO import StringIO |
31 from urllib import quote |
31 from urllib import quote |
32 from urlparse import parse_qs |
32 from urlparse import parse_qs |
33 from warnings import warn |
33 from warnings import warn |
34 |
34 |
35 from cubicweb.multipart import copy_file, parse_form_data |
35 from cubicweb.multipart import ( |
|
36 copy_file, parse_form_data, MultipartError, parse_options_header) |
|
37 from cubicweb.web import RequestError |
36 from cubicweb.web.request import CubicWebRequestBase |
38 from cubicweb.web.request import CubicWebRequestBase |
37 from cubicweb.wsgi import pformat, normalize_header |
39 from cubicweb.wsgi import pformat, normalize_header |
38 |
40 |
39 |
41 |
40 class CubicWebWsgiRequest(CubicWebRequestBase): |
42 class CubicWebWsgiRequest(CubicWebRequestBase): |
79 super(CubicWebWsgiRequest, self).__init__(vreg, https, post, |
81 super(CubicWebWsgiRequest, self).__init__(vreg, https, post, |
80 headers= headers_in) |
82 headers= headers_in) |
81 self.content = environ['wsgi.input'] |
83 self.content = environ['wsgi.input'] |
82 if files is not None: |
84 if files is not None: |
83 for key, part in files.iteritems(): |
85 for key, part in files.iteritems(): |
84 name = None |
86 self.form[key] = (part.filename, part.file) |
85 if part.filename is not None: |
|
86 name = unicode(part.filename, self.encoding) |
|
87 self.form[key] = (name, part.file) |
|
88 |
87 |
89 def __repr__(self): |
88 def __repr__(self): |
90 # Since this is called as part of error handling, we need to be very |
89 # Since this is called as part of error handling, we need to be very |
91 # robust against potentially malformed input. |
90 # robust against potentially malformed input. |
92 form = pformat(self.form) |
91 form = pformat(self.form) |
125 def get_posted_data(self): |
124 def get_posted_data(self): |
126 # The WSGI spec says 'QUERY_STRING' may be absent. |
125 # The WSGI spec says 'QUERY_STRING' may be absent. |
127 post = parse_qs(self.environ.get('QUERY_STRING', '')) |
126 post = parse_qs(self.environ.get('QUERY_STRING', '')) |
128 files = None |
127 files = None |
129 if self.method == 'POST': |
128 if self.method == 'POST': |
130 forms, files = parse_form_data(self.environ, strict=True, |
129 content_type = self.environ.get('CONTENT_TYPE') |
131 mem_limit=self.vreg.config['max-post-length']) |
130 if not content_type: |
132 post.update(forms.dict) |
131 raise RequestError("Missing Content-Type") |
|
132 content_type, options = parse_options_header(content_type) |
|
133 if content_type in ( |
|
134 'multipart/form-data', |
|
135 'application/x-www-form-urlencoded', |
|
136 'application/x-url-encoded'): |
|
137 forms, files = parse_form_data( |
|
138 self.environ, strict=True, |
|
139 mem_limit=self.vreg.config['max-post-length']) |
|
140 post.update(forms.dict) |
133 self.content.seek(0, 0) |
141 self.content.seek(0, 0) |
134 return post, files |
142 return post, files |
135 |
143 |
136 def setup_params(self, params): |
144 def setup_params(self, params): |
137 # This is a copy of CubicWebRequestBase.setup_params, but without |
145 # This is a copy of CubicWebRequestBase.setup_params, but without |