import binascii, hashlib, threading, time
-from . import resp
+from . import resp, proto
class unauthorized(resp.httperror):
def __init__(self, challenge, message=None, detail=None):
if mech != "basic":
return None, None
try:
- data = data.encode("us-ascii")
- except UnicodeError:
- return None, None
- try:
- raw = binascii.a2b_base64(data)
+ raw = proto.unb64(data)
except binascii.Error:
return None, None
try:
-import time, calendar
+import time, calendar, collections, binascii, base64
statusinfo = {
400: ("Bad Request", "Invalid HTTP request."),
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")
+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")
+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")
+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)
-import threading, time, pickle, random, os, binascii
-from . import cookie, env
+import threading, time, pickle, random, os
+from . import cookie, env, proto
__all__ = ["db", "get"]
class session(object):
def __init__(self, lock, expire=86400 * 7):
- self.id = binascii.b2a_hex(gennonce(16)).decode("us-ascii")
+ self.id = proto.enhex(gennonce(16))
self.dict = {}
self.lock = lock
self.ctime = self.atime = self.mtime = int(time.time())