Merge branch 'master' into python2
[wrw.git] / wrw / form.py
index da13a4a..76abab6 100644 (file)
@@ -7,9 +7,12 @@ def formparse(req):
     buf = {}
     buf.update(urlparse.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(urlparse.parse_qsl(rbody))
     return buf
 
@@ -81,7 +84,9 @@ class formpart(object):
                 return ret
 
     def close(self):
-        self.fillbuf(-1)
+        while True:
+            if self.read(8192) == "":
+                break
 
     def __enter__(self):
         return self
@@ -152,5 +157,10 @@ class multipart(object):
         self.lastpart.parsehead()
         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