X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Fform.py;h=85114680d59d271100994fa6f9d4d97fa05e2cc5;hp=e89901d0221c95432740b20251630b1b45c13918;hb=HEAD;hpb=d9d1f25883905067ace7a28319f429d167bbaa62 diff --git a/wrw/form.py b/wrw/form.py index e89901d..8511468 100644 --- a/wrw/form.py +++ b/wrw/form.py @@ -5,15 +5,19 @@ __all__ = ["formdata"] def formparse(req): buf = {} - buf.update(urllib.parse.parse_qsl(req.query)) - if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded": - rbody = req.input.read(2 ** 20) + buf.update(urllib.parse.parse_qsl(req.query, keep_blank_values=True)) + ctype, ctpars = proto.pmimehead(req.ihead.get("Content-Type", "")) + if ctype == "application/x-www-form-urlencoded": + try: + rbody = req.input.read(2 ** 20) + except IOError as exc: + return exc if len(rbody) >= 2 ** 20: - raise ValueError("x-www-form-urlencoded data is absurdly long") - buf.update(urllib.parse.parse_qsl(rbody.decode("latin1"))) + return ValueError("x-www-form-urlencoded data is absurdly long") + buf.update(urllib.parse.parse_qsl(rbody.decode("latin1"), encoding=ctpars.get("charset", "utf-8"), keep_blank_values=True)) return buf -class badmultipart(Exception): +class badmultipart(IOError): pass class formpart(object): @@ -161,5 +165,10 @@ class multipart(object): self.lastpart.parsehead(self.headcs) return self.lastpart -def formdata(req): - return req.item(formparse) +def formdata(req, onerror=Exception): + data = req.item(formparse) + if isinstance(data, Exception): + if onerror is Exception: + raise data + return onerror + return data