X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Fform.py;h=da13a4ae7301cf08b9237387b63b1a59f9cbb7db;hp=d089fd70d6a1f6321271e82c3995f0652e2fd55a;hb=0d4c1b8b8da9e20c4bc9fb41ff1136283821b98e;hpb=8b7cf2787e8cf2ee716c709d904f66c651cfed89 diff --git a/wrw/form.py b/wrw/form.py index d089fd7..da13a4a 100644 --- a/wrw/form.py +++ b/wrw/form.py @@ -1,41 +1,17 @@ -import cgi -from . import proto +import urlparse +import proto __all__ = ["formdata"] -class formwrap(object): - def __init__(self, req): - if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded": - self.cf = cgi.parse(environ = req.env, fp = req.env["wsgi.input"]) - else: - self.cf = cgi.parse(environ = req.env) - - def __getitem__(self, key): - return self.cf[key][0] - - def get(self, key, default = ""): - if key in self: - return self.cf[key][0] - return default - - def __contains__(self, key): - return key in self.cf and len(self.cf[key]) > 0 - - def __iter__(self): - return iter(self.cf) - - def items(self): - def iter(): - for key, list in self.cf.items(): - for val in list: - yield key, val - return list(iter()) - - def keys(self): - return list(self.cf.keys()) - - def values(self): - return [val for key, val in self.items()] +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) + if len(rbody) >= 2 ** 20: + raise ValueError("x-www-form-urlencoded data is absurdly long") + buf.update(urlparse.parse_qsl(rbody)) + return buf class badmultipart(Exception): pass @@ -73,12 +49,12 @@ class formpart(object): if sz >= 0 and len(self.buf) >= sz: break while len(self.form.buf) <= len(lboundary): - ret = req.env["wsgi.input"].read(8192) + ret = req.input.read(8192) if ret == "": raise badmultipart("Missing last multipart boundary") self.form.buf += ret - def read(self, limit = -1): + def read(self, limit=-1): self.fillbuf(limit) if limit >= 0: ret = self.buf[:limit] @@ -88,7 +64,7 @@ class formpart(object): self.buf = "" return ret - def readline(self, limit = -1): + def readline(self, limit=-1): last = 0 while True: p = self.buf.find('\n', last) @@ -111,6 +87,7 @@ class formpart(object): return self def __exit__(self, *excinfo): + self.close() return False def parsehead(self): @@ -176,4 +153,4 @@ class multipart(object): return self.lastpart def formdata(req): - return req.item(formwrap) + return req.item(formparse)