def DELETE(self): if self.method != 'DELETE': return NoVars('Not a DELETE request') content_type = self.content_type if content_type not in ('', 'application/x-www-form-urlencoded', 'multipart/form-data'): # Not an HTML form submission return NoVars('Not an HTML delete submission (Content-Type: %s)' % content_type) self._check_charset() if self.is_body_seekable: self.body_file_raw.seek(0) fs_environ = self.environ.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = FieldStorage(fp=self.body_file, environ=fs_environ, keep_blank_values=True) delete_values = MultiDict.from_fieldstorage(fs) ctype = self._content_type_raw or 'application/x-www-form-urlencoded' f = FakeCGIBody(delete_values, ctype) self.body_file = BufferedReader(f) return delete_values
def DELETE(self): if self.method != 'DELETE': return NoVars('Not a DELETE request') content_type = self.content_type if content_type in ( 'application/x-www-form-urlencoded', 'multipart/form-data'): self._check_charset() if self.is_body_seekable: self.body_file_raw.seek(0) fs_environ = self.environ.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = FieldStorage( fp=self.body_file, environ=fs_environ, keep_blank_values=True) delete_values = MultiDict.from_fieldstorage(fs) ctype = self._content_type_raw or 'application/x-www-form-urlencoded' f = FakeCGIBody(delete_values, ctype) self.body_file = BufferedReader(f) return delete_values else: data = parse_qsl_text(self.environ.get('QUERY_STRING', '')) return MultiDict(data)
def _str_POST(self): env = self.environ if self.method not in ('POST', 'PUT'): return NoVars('Not a form request') if 'webob._parsed_post_vars' in env: vars, body_file = env['webob._parsed_post_vars'] if body_file is self.body_file_raw: return vars content_type = self.content_type if ((self.method == 'PUT' and not content_type) or content_type not in ('', 'application/x-www-form-urlencoded', 'multipart/form-data')): # Not an HTML form submission return NoVars('Not an HTML form submission (Content-Type: %s)' % content_type) if self.is_body_seekable: self.body_file.seek(0) fs_environ = env.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = cgi.FieldStorage(fp=self.body_file_raw, environ=fs_environ, keep_blank_values=True) vars = MultiDict.from_fieldstorage(fs) #ctype = self.content_type or 'application/x-www-form-urlencoded' ctype = env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded') self.body_file = FakeCGIBody(vars, ctype) env['webob._parsed_post_vars'] = (vars, self.body_file_raw) return vars
def test_from_fieldstorage_with_quoted_printable_encoding(self): from cgi import FieldStorage from webob.request import BaseRequest from webob.multidict import MultiDict multipart_type = "multipart/form-data; boundary=foobar" from io import BytesIO body = ( b"--foobar\r\n" b'Content-Disposition: form-data; name="title"\r\n' b'Content-type: text/plain; charset="ISO-2022-JP"\r\n' b"Content-Transfer-Encoding: quoted-printable\r\n" b"\r\n" b"=1B$B$3$s$K$A$O=1B(B" b"\r\n" b"--foobar--" ) multipart_body = BytesIO(body) environ = BaseRequest.blank("/").environ environ.update(CONTENT_TYPE=multipart_type) environ.update(REQUEST_METHOD="POST") environ.update(CONTENT_LENGTH=len(body)) fs = FieldStorage(multipart_body, environ=environ) vars = MultiDict.from_fieldstorage(fs) self.assertEqual(vars["title"].encode("utf8"), text_("こんにちは", "utf8").encode("utf8"))
def test_from_fieldstorage_with_quoted_printable_encoding(self): from cgi import FieldStorage from webob.multidict import MultiDict from webob.request import BaseRequest multipart_type = "multipart/form-data; boundary=foobar" from io import BytesIO body = (b"--foobar\r\n" b'Content-Disposition: form-data; name="title"\r\n' b'Content-type: text/plain; charset="ISO-2022-JP"\r\n' b"Content-Transfer-Encoding: quoted-printable\r\n" b"\r\n" b"=1B$B$3$s$K$A$O=1B(B" b"\r\n" b"--foobar--") multipart_body = BytesIO(body) environ = BaseRequest.blank("/").environ environ.update(CONTENT_TYPE=multipart_type) environ.update(REQUEST_METHOD="POST") environ.update(CONTENT_LENGTH=len(body)) fs = FieldStorage(multipart_body, environ=environ) vars = MultiDict.from_fieldstorage(fs) self.assertEqual(vars["title"].encode("utf8"), text_("こんにちは", "utf8").encode("utf8"))
def _str_POST(self): env = self.environ if self.method not in ("POST", "PUT"): return NoVars("Not a form request") if "webob._parsed_post_vars" in env: vars, body_file = env["webob._parsed_post_vars"] if body_file is self.body_file_raw: return vars content_type = self.content_type if (self.method == "PUT" and not content_type) or content_type not in ( "", "application/x-www-form-urlencoded", "multipart/form-data", ): # Not an HTML form submission return NoVars("Not an HTML form submission (Content-Type: %s)" % content_type) if self.is_body_seekable: self.body_file.seek(0) fs_environ = env.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault("CONTENT_LENGTH", "0") fs_environ["QUERY_STRING"] = "" fs = cgi.FieldStorage(fp=self.body_file, environ=fs_environ, keep_blank_values=True) vars = MultiDict.from_fieldstorage(fs) # ctype = self.content_type or 'application/x-www-form-urlencoded' ctype = env.get("CONTENT_TYPE", "application/x-www-form-urlencoded") self.body_file = FakeCGIBody(vars, ctype) env["webob._parsed_post_vars"] = (vars, self.body_file_raw) return vars
def __call__(self, resource, request): # We can't use request.body_file because that's a socket that's # already had its data read off of. IOW, if we use that directly, # we'll block here. field_storage = cgi.FieldStorage( fp=StringIO(request.body), # Yes, lie about the method so cgi will do the right thing. environ=dict(REQUEST_METHOD='POST'), keep_blank_values=True) request.PATCH = MultiDict.from_fieldstorage(field_storage) return self.func(resource, request)
def _load_post_data(self): content_type = self.request.content_type if content_type == 'application/json': self.data = simplejson.loads(self.request.body) elif content_type == 'application/x-www-form-urlencoded': # LOOK INTO THIS fs = cgi.FieldStorage(fp=self.request.body_file, environ=self.request.environ.copy(), keep_blank_values=True) self.data = MultiDict.from_fieldstorage(fs) else: raise "Unsupported content type: %s" % content_type
def POST(self): """ A MultiDict containing all the variables from a form request. An empty dict-like object for non-form requests. Form requests are typically POST requests, however PUT requests with an appropriate Content-Type are also supported. All keys and values are unicode. """ env = self.environ if self.method not in ('POST', 'PUT'): return NoVars('Not a form request') if 'webob._parsed_post_vars' in env: vars, body_file = env['webob._parsed_post_vars'] if body_file is self.body_file_raw: return vars content_type = self.content_type if ((self.method == 'PUT' and not content_type) or content_type not in ('', 'application/x-www-form-urlencoded', 'multipart/form-data') ): # Not an HTML form submission return NoVars('Not an HTML form submission (Content-Type: %s)' % content_type) if self.is_body_seekable: self.body_file_raw.seek(0) fs_environ = env.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = cgi.FieldStorage(fp=self.body_file, environ=fs_environ, keep_blank_values=True) vars = MultiDict.from_fieldstorage(fs) #ctype = self.content_type or 'application/x-www-form-urlencoded' ctype = env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded') self.body_file = io.BufferedReader(FakeCGIBody(vars, ctype)) vars = UnicodeMultiDict(vars, encoding=self.charset, errors=self.unicode_errors) env['webob._parsed_post_vars'] = (vars, self.body_file_raw) return vars
def str_POST(self): """ Return a MultiDict containing all the variables from a form request. Returns an empty dict-like object for non-form requests. Form requests are typically POST requests, however PUT requests with an appropriate Content-Type are also supported. """ env = self.environ if self.method not in ('POST', 'PUT'): return NoVars('Not a form request') if 'webob._parsed_post_vars' in env: vars, body_file = env['webob._parsed_post_vars'] if body_file is self.body_file: return vars # Paste compatibility: if 'paste.parsed_formvars' in env: # from paste.request.parse_formvars vars, body_file = env['paste.parsed_formvars'] if body_file is self.body_file: # FIXME: is it okay that this isn't *our* MultiDict? return vars content_type = self.content_type if ';' in content_type: content_type = content_type.split(';', 1)[0] if (self.method == 'PUT' and not content_type) or \ content_type not in ('', 'application/x-www-form-urlencoded', 'multipart/form-data'): # Not an HTML form submission return NoVars('Not an HTML form submission (Content-Type: %s)' % content_type) fs_environ = env.copy() # FieldStorage assumes a default CONTENT_LENGTH of -1, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = cgi.FieldStorage(fp=self.body_file, environ=fs_environ, keep_blank_values=True) vars = MultiDict.from_fieldstorage(fs) FakeCGIBody.update_environ(env, vars) env['webob._parsed_post_vars'] = (vars, self.body_file) return vars
def POST(self): """ A MultiDict containing all the variables from a form request. An empty dict-like object for non-form requests. Form requests are typically POST requests, however PUT requests with an appropriate Content-Type are also supported. All keys and values are unicode. """ env = self.environ if self.method not in ('POST', 'PUT'): return NoVars('Not a form request') if 'webob._parsed_post_vars' in env: vars, body_file = env['webob._parsed_post_vars'] if body_file is self.body_file_raw: return vars content_type = self.content_type if ((self.method == 'PUT' and not content_type) or content_type not in ('', 'application/x-www-form-urlencoded', 'multipart/form-data')): # Not an HTML form submission return NoVars('Not an HTML form submission (Content-Type: %s)' % content_type) if self.is_body_seekable: self.body_file_raw.seek(0) fs_environ = env.copy() # FieldStorage assumes a missing CONTENT_LENGTH, but a # default of 0 is better: fs_environ.setdefault('CONTENT_LENGTH', '0') fs_environ['QUERY_STRING'] = '' fs = cgi.FieldStorage(fp=self.body_file, environ=fs_environ, keep_blank_values=True) vars = MultiDict.from_fieldstorage(fs) #ctype = self.content_type or 'application/x-www-form-urlencoded' ctype = env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded') self.body_file = io.BufferedReader(FakeCGIBody(vars, ctype)) vars = UnicodeMultiDict(vars, encoding=self.charset, errors=self.unicode_errors) env['webob._parsed_post_vars'] = (vars, self.body_file_raw) return vars
def test_from_fieldstorage_with_charset(self): from cgi import FieldStorage from webob.request import BaseRequest from webob.multidict import MultiDict multipart_type = 'multipart/form-data; boundary=foobar' from io import BytesIO body = (b'--foobar\r\n' b'Content-Disposition: form-data; name="title"\r\n' b'Content-type: text/plain; charset="ISO-2022-JP"\r\n' b'\r\n' b'\x1b$B$3$s$K$A$O\x1b(B' b'\r\n' b'--foobar--') multipart_body = BytesIO(body) environ = BaseRequest.blank('/').environ environ.update(CONTENT_TYPE=multipart_type) environ.update(REQUEST_METHOD='POST') environ.update(CONTENT_LENGTH=len(body)) fs = FieldStorage(multipart_body, environ=environ) vars = MultiDict.from_fieldstorage(fs) self.assertEqual(vars['title'].encode('utf8'), text_('こんにちは', 'utf8').encode('utf8'))
def test_from_fieldstorage_with_charset(self): from cgi import FieldStorage from webob.request import BaseRequest from webob.multidict import MultiDict multipart_type = 'multipart/form-data; boundary=foobar' from io import BytesIO body = ( b'--foobar\r\n' b'Content-Disposition: form-data; name="title"\r\n' b'Content-type: text/plain; charset="ISO-2022-JP"\r\n' b'\r\n' b'\x1b$B$3$s$K$A$O\x1b(B' b'\r\n' b'--foobar--') multipart_body = BytesIO(body) environ = BaseRequest.blank('/').environ environ.update(CONTENT_TYPE=multipart_type) environ.update(REQUEST_METHOD='POST') environ.update(CONTENT_LENGTH=len(body)) fs = FieldStorage(multipart_body, environ=environ) vars = MultiDict.from_fieldstorage(fs) self.assertEqual(vars['title'].encode('utf8'), text_('こんにちは', 'utf8').encode('utf8'))
def test_from_fieldstorage_without_filename(self): from webob.multidict import MultiDict d = MultiDict() fs = DummyFieldStorage('a', '1') self.assertEqual(d.from_fieldstorage(fs), MultiDict({'a':'1'}))
def set_variables(self, method): request = self.request fs = cgi.FieldStorage(fp=request.body_file, environ=request.environ.copy(), keep_blank_values=True) setattr(self.request, method, MultiDict.from_fieldstorage(fs))
def test_from_fieldstorage_without_filename(self): from webob.multidict import MultiDict d = MultiDict() fs = DummyFieldStorage("a", "1") self.assertEqual(d.from_fieldstorage(fs), MultiDict({"a": "1"}))