X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Fsession.py;h=814182759d584e30e5aff52df6c7781815e74458;hp=3b2b2b29cc98dfbf435544ffb842e3af9b05d32c;hb=HEAD;hpb=b9e22c33901b627684450411477f6c459fc302da diff --git a/wrw/session.py b/wrw/session.py index 3b2b2b2..8141827 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,23 +1,14 @@ import threading, time, pickle, random, os -import cookie, env +from . import cookie, env, proto __all__ = ["db", "get"] -def hexencode(str): - ret = "" - for byte in str: - ret += "%02X" % (ord(byte),) - return ret - def gennonce(length): - nonce = "" - for i in xrange(length): - nonce += chr(random.randint(0, 255)) - return nonce + return os.urandom(length) class session(object): - def __init__(self, lock, expire = 86400 * 7): - self.id = hexencode(gennonce(16)) + def __init__(self, lock, expire=86400 * 7): + self.id = proto.enhex(gennonce(16)) self.dict = {} self.lock = lock self.ctime = self.atime = self.mtime = int(time.time()) @@ -39,7 +30,7 @@ class session(object): def __getitem__(self, key): return self.dict[key] - def get(self, key, default = None): + def get(self, key, default=None): return self.dict.get(key, default) def __setitem__(self, key, value): @@ -74,7 +65,7 @@ class session(object): return "" % self.id class db(object): - def __init__(self, backdb = None, cookiename = "wrwsess", path = "/"): + def __init__(self, backdb=None, cookiename="wrwsess", path="/"): self.live = {} self.cookiename = cookiename self.path = path @@ -86,7 +77,7 @@ class db(object): def clean(self): now = int(time.time()) with self.lock: - clist = self.live.keys() + clist = list(self.live.keys()) for sessid in clist: with self.lock: try: @@ -157,33 +148,44 @@ class db(object): else: raise Exception("Illegal session entry: " + repr(entry[1])) - def fetch(self, req): - now = int(time.time()) - sessid = cookie.get(req, self.cookiename) - new = False + def checkclean(self): with self.lock: if self.cthread is None: self.cthread = threading.Thread(target = self.cleanloop) self.cthread.setDaemon(True) self.cthread.start() + + def mksession(self, req): + return session(threading.RLock()) + + def mkcookie(self, req, sess): + cookie.add(req, self.cookiename, sess.id, + path=self.path, + expires=cookie.cdate(time.time() + sess.expire)) + + def fetch(self, req): + now = int(time.time()) + sessid = cookie.get(req, self.cookiename) + new = False try: if sessid is None: raise KeyError() sess = self._fetch(sessid) except KeyError: - sess = session(threading.RLock()) + sess = self.mksession(req) new = True def ckfreeze(req): if sess.dirty(): if new: - cookie.add(req, self.cookiename, sess.id, self.path) + self.mkcookie(req, sess) with self.lock: self.live[sess.id] = [sess.lock, sess] try: self.freeze(sess) except: pass + self.checkclean() req.oncommit(ckfreeze) return sess @@ -193,7 +195,7 @@ class db(object): data = self.backdb[sessid] try: return pickle.loads(data) - except Exception, e: + except: raise KeyError() def freeze(self, sess): @@ -224,7 +226,7 @@ class dirback(object): with open(os.path.join(self.path, key), "w") as out: out.write(value) -default = env.var(db(backdb = dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid()))))) +default = env.var(db(backdb=dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid()))))) def get(req): return default.val.get(req)