From: Fredrik Tolf Date: Sun, 8 Nov 2015 02:52:43 +0000 (+0100) Subject: Allow formdata to handle errors better. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=3350ac8c0b5bdc6c098732931a59b7982beb760b;p=wrw.git Allow formdata to handle errors better. --- diff --git a/wrw/form.py b/wrw/form.py index e89901d..7905dc6 100644 --- a/wrw/form.py +++ b/wrw/form.py @@ -7,9 +7,12 @@ 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) + 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") + return ValueError("x-www-form-urlencoded data is absurdly long") buf.update(urllib.parse.parse_qsl(rbody.decode("latin1"))) return buf @@ -161,5 +164,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