From: Fredrik Tolf Date: Wed, 7 May 2014 16:13:40 +0000 (+0200) Subject: Merge branch 'master' into python2 X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=commitdiff_plain;h=0d4c1b8b8da9e20c4bc9fb41ff1136283821b98e;hp=-c Merge branch 'master' into python2 --- 0d4c1b8b8da9e20c4bc9fb41ff1136283821b98e diff --combined wrw/form.py index de40092,53fc82d..da13a4a --- a/wrw/form.py +++ b/wrw/form.py @@@ -1,16 -1,16 +1,16 @@@ -import urllib.parse -from . import proto +import urlparse +import proto __all__ = ["formdata"] def formparse(req): buf = {} - buf.update(urllib.parse.parse_qsl(req.query)) + buf.update(urlparse.parse_qsl(req.query)) if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded": - rbody = req.input(2 ** 20) + rbody = req.input.read(2 ** 20) if len(rbody) >= 2 ** 20: raise ValueError("x-www-form-urlencoded data is absurdly long") - buf.update(urllib.parse.parse_qsl(rbody.decode("latin1"))) + buf.update(urlparse.parse_qsl(rbody)) return buf class badmultipart(Exception): @@@ -19,7 -19,7 +19,7 @@@ class formpart(object): def __init__(self, form): self.form = form - self.buf = b"" + self.buf = "" self.eof = False self.head = {} @@@ -28,8 -28,8 +28,8 @@@ def fillbuf(self, sz): req = self.form.req - mboundary = b"\r\n--" + self.form.boundary + b"\r\n" - lboundary = b"\r\n--" + self.form.boundary + b"--\r\n" + mboundary = "\r\n--" + self.form.boundary + "\r\n" + lboundary = "\r\n--" + self.form.boundary + "--\r\n" while not self.eof: p = self.form.buf.find(mboundary) if p >= 0: @@@ -67,7 -67,7 +67,7 @@@ def readline(self, limit=-1): last = 0 while True: - p = self.buf.find(b'\n', last) + p = self.buf.find('\n', last) if p < 0: if self.eof: ret = self.buf @@@ -90,12 -90,15 +90,12 @@@ self.close() return False - def parsehead(self, charset): + def parsehead(self): def headline(): ln = self.readline(256) - if ln[-1] != ord(b'\n'): + if ln[-1] != '\n': raise badmultipart("Too long header line in part") - try: - return ln.decode(charset).rstrip() - except UnicodeError: - raise badmultipart("Form part header is not in assumed charset") + return ln.rstrip() ln = headline() while True: @@@ -127,29 -130,33 +127,29 @@@ raise badmultipart("Form part uses unexpected transfer encoding: %r" % encoding) class multipart(object): - def __init__(self, req, charset): + def __init__(self, req): val, par = proto.pmimehead(req.ihead.get("Content-Type", "")) if req.method != "POST" or val != "multipart/form-data": raise badmultipart("Request is not a multipart form") if "boundary" not in par: raise badmultipart("Multipart form lacks boundary") - try: - self.boundary = par["boundary"].encode("us-ascii") - except UnicodeError: - raise badmultipart("Multipart boundary must be ASCII string") + self.boundary = par["boundary"] self.req = req - self.buf = b"\r\n" + self.buf = "\r\n" self.eof = False - self.headcs = charset self.lastpart = formpart(self) self.lastpart.close() def __iter__(self): return self - def __next__(self): + def next(self): if not self.lastpart.eof: raise RuntimeError("All form parts must be read entirely") if self.eof: raise StopIteration() self.lastpart = formpart(self) - self.lastpart.parsehead(self.headcs) + self.lastpart.parsehead() return self.lastpart def formdata(req):