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):
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
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):
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
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
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):
return url + "?" + qs
else:
return url
- return base64.b16encode(bs).decode("us-ascii")
+
+ # Wrap these, since binascii is a bit funky. :P
+ def enhex(bs):
- 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.b16encode(bs)
+ def unhex(es):
- return base64.b32encode(bs).decode("us-ascii")
+ return base64.b16decode(es)
+ def enb32(bs):
- if not isinstance(es, collections.ByteString):
- try:
- es = es.encode("us-ascii")
- except UnicodeError:
- raise binascii.Error("non-ascii character in base32-string")
++ return base64.b32encode(bs)
+ def unb32(es):
- return base64.b64encode(bs).decode("us-ascii")
+ 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):
- if not isinstance(es, collections.ByteString):
- try:
- es = es.encode("us-ascii")
- except UnicodeError:
- raise binascii.Error("non-ascii character in base64-string")
++ return base64.b64encode(bs)
+ def unb64(es):
+ if (len(es) % 4) != 0:
+ es += b"=" * (4 - (len(es) % 4))
+ return base64.b64decode(es)
import threading, time, pickle, random, os
++<<<<<<< HEAD
+import cookie, env
++=======
+ from . import cookie, env, proto
++>>>>>>> master
__all__ = ["db", "get"]