summaryrefslogtreecommitdiff
path: root/session.py
blob: 0787be88e159abe0f4df5691130040b83690a273 (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)