import binascii, hashlib, threading, time
-import resp
+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
p = raw.find(":")
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)
+ 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(urlparse.parse_qsl(rbody))
return buf
return ret
def close(self):
- self.fillbuf(-1)
+ while True:
+ if self.read(8192) == "":
+ break
def __enter__(self):
return self
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
-import time, calendar
+import time, calendar, collections, binascii, base64
statusinfo = {
400: ("Bad Request", "Invalid HTTP request."),
if isinstance(url, unicode):
url = url.encode("utf-8")
ret = ""
- invalid = "&=#?/\"'"
+ invalid = ";&=#?/\"'"
for c in url:
if c in invalid or (ord(c) <= 32) or (ord(c) >= 128):
ret += "%%%02X" % ord(c)
return url + "?" + qs
else:
return url
+
+# Wrap these, since binascii is a bit funky. :P
+def enhex(bs):
+ return base64.b16encode(bs)
+def unhex(es):
+ return base64.b16decode(es)
+def enb32(bs):
+ return base64.b32encode(bs)
+def unb32(es):
+ 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)
+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"]
class session(object):
def __init__(self, lock, expire=86400 * 7):
- self.id = gennonce(16).encode("hex")
+ self.id = proto.enhex(gennonce(16))
self.dict = {}
self.lock = lock
self.ctime = self.atime = self.mtime = int(time.time())
def formparams(callable):
spec = inspect.getargspec(callable)
def wrapper(req):
- data = form.formdata(req)
+ try:
+ data = form.formdata(req)
+ except IOError:
+ raise resp.httperror(400, "Invalid request", "Form data was incomplete")
args = dict(data.items())
args["req"] = req
if not spec.keywords: