summaryrefslogtreecommitdiff
path: root/session.py
blob: 992899231a73c5aa2d1fb21f780881eab6561e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Cookie, hashlib, time

class Session(object):
	def __init__(self, func):
		self.func = func

	def __call__(self, app, environ, start_response, path):
		cookie = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None)
		sessionid = cookie.get('sessionid')
		if not sessionid:
			sessionid = hashlib.sha1(str(time.time) + environ['REMOTE_ADDR']).hexdigest()
			cookie['sessionid'] = sessionid
			start_response('302 Found', [
				('Set-Cookie', cookie['sessionid'].OutputString()),
				('Location', environ['REQUEST_URI'])])
			return []
		environ['sessionid'] = sessionid.value

		return self.func(app, environ, start_response, path)