X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Fform.py;h=da13a4ae7301cf08b9237387b63b1a59f9cbb7db;hp=98157c055867a9f4eb557b374f092ace4bb0f154;hb=0d4c1b8b8da9e20c4bc9fb41ff1136283821b98e;hpb=0417f41c0ae50eb2027a5109b85dbf1f103b1c80 diff --git a/wrw/form.py b/wrw/form.py index 98157c0..da13a4a 100644 --- a/wrw/form.py +++ b/wrw/form.py @@ -1,41 +1,17 @@ -import cgi +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.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 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 @@ -78,7 +54,7 @@ class formpart(object): 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) @@ -177,4 +153,4 @@ class multipart(object): return self.lastpart def formdata(req): - return req.item(formwrap) + return req.item(formparse)