From: Fredrik Tolf Date: Sun, 8 Nov 2015 03:01:08 +0000 (+0100) Subject: Merge branch 'master' into python2 X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=b6f62b898efdc272130d3dbdd6742c5b785e7c7d;p=wrw.git Merge branch 'master' into python2 Conflicts: wrw/auth.py wrw/form.py wrw/proto.py wrw/session.py --- b6f62b898efdc272130d3dbdd6742c5b785e7c7d diff --cc wrw/auth.py index 4ae292d,27e2a98..bb0e0cd --- a/wrw/auth.py +++ b/wrw/auth.py @@@ -1,5 -1,5 +1,5 @@@ import binascii, hashlib, threading, time - import resp -from . import resp, proto ++import resp, proto class unauthorized(resp.httperror): def __init__(self, challenge, message=None, detail=None): @@@ -31,9 -31,13 +31,9 @@@ def parsebasic(req) if mech != "basic": return None, None try: - raw = binascii.a2b_base64(data) + raw = proto.unb64(data) except binascii.Error: return None, None - try: - raw = raw.decode("utf-8") - except UnicodeError: - raw = raw.decode("latin1") p = raw.find(":") if p < 0: return None, None diff --cc wrw/form.py index da13a4a,7905dc6..76abab6 --- a/wrw/form.py +++ b/wrw/form.py @@@ -5,12 -5,15 +5,15 @@@ __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.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"))) + buf.update(urlparse.parse_qsl(rbody)) return buf class badmultipart(Exception): @@@ -81,7 -84,9 +84,9 @@@ class formpart(object) return ret def close(self): - self.fillbuf(-1) + while True: - if self.read(8192) == b"": ++ if self.read(8192) == "": + break def __enter__(self): return self @@@ -149,8 -161,13 +154,13 @@@ class multipart(object) if self.eof: raise StopIteration() self.lastpart = formpart(self) - self.lastpart.parsehead(self.headcs) + 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 diff --cc wrw/proto.py index 3dcf0d7,ad1775a..a67fa19 --- a/wrw/proto.py +++ b/wrw/proto.py @@@ -102,15 -103,15 +102,15 @@@ def simpleerror(env, startreq, code, ti return [buf] def urlq(url): - if isinstance(url, str): + if isinstance(url, unicode): url = url.encode("utf-8") ret = "" - invalid = "&=#?/\"'" - invalid = b";&=#?/\"'" ++ invalid = ";&=#?/\"'" for c in url: - if c in invalid or (c <= 32) or (c >= 128): - ret += "%%%02X" % c + if c in invalid or (ord(c) <= 32) or (ord(c) >= 128): + ret += "%%%02X" % ord(c) else: - ret += chr(c) + ret += c return ret class urlerror(ValueError): @@@ -194,3 -195,37 +194,22 @@@ def parurl(url, pars={}, **augment) return url + "?" + qs else: return url + + # Wrap these, since binascii is a bit funky. :P + def enhex(bs): - return base64.b16encode(bs).decode("us-ascii") ++ return base64.b16encode(bs) + def unhex(es): - if not isinstance(es, collections.ByteString): - try: - es = es.encode("us-ascii") - except UnicodeError: - raise binascii.Error("non-ascii character in hex-string") + return base64.b16decode(es) + def enb32(bs): - return base64.b32encode(bs).decode("us-ascii") ++ return base64.b32encode(bs) + def unb32(es): - if not isinstance(es, collections.ByteString): - try: - es = es.encode("us-ascii") - except UnicodeError: - raise binascii.Error("non-ascii character in base32-string") + if (len(es) % 8) != 0: + es += b"=" * (8 - (len(es) % 8)) + es = es.upper() # The whole point of Base32 is that it's case-insensitive :P + return base64.b32decode(es) + def enb64(bs): - return base64.b64encode(bs).decode("us-ascii") ++ return base64.b64encode(bs) + def unb64(es): - if not isinstance(es, collections.ByteString): - try: - es = es.encode("us-ascii") - except UnicodeError: - raise binascii.Error("non-ascii character in base64-string") + if (len(es) % 4) != 0: + es += b"=" * (4 - (len(es) % 4)) + return base64.b64decode(es) diff --cc wrw/session.py index 0c585a3,8141827..476113a --- a/wrw/session.py +++ b/wrw/session.py @@@ -1,5 -1,5 +1,9 @@@ import threading, time, pickle, random, os ++<<<<<<< HEAD +import cookie, env ++======= + from . import cookie, env, proto ++>>>>>>> master __all__ = ["db", "get"]