2008-08-21 05:12:45 +08:00
|
|
|
import errno
|
2007-09-16 05:29:14 +08:00
|
|
|
import os
|
2007-12-05 04:24:22 +08:00
|
|
|
import tempfile
|
2008-06-23 13:08:07 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
from django.conf import settings
|
2008-08-14 11:57:18 +08:00
|
|
|
from django.contrib.sessions.backends.base import SessionBase, CreateError
|
2007-12-05 04:24:22 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
|
2007-09-16 05:29:14 +08:00
|
|
|
|
2008-06-23 13:08:07 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
class SessionStore(SessionBase):
|
|
|
|
"""
|
|
|
|
Implements a file based session store.
|
|
|
|
"""
|
|
|
|
def __init__(self, session_key=None):
|
2008-03-20 14:43:58 +08:00
|
|
|
self.storage_path = getattr(settings, "SESSION_FILE_PATH", None)
|
|
|
|
if not self.storage_path:
|
|
|
|
self.storage_path = tempfile.gettempdir()
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-12-05 04:24:22 +08:00
|
|
|
# Make sure the storage path is valid.
|
|
|
|
if not os.path.isdir(self.storage_path):
|
2008-06-23 13:08:07 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The session storage path %r doesn't exist. Please set your"
|
|
|
|
" SESSION_FILE_PATH setting to an existing directory in which"
|
|
|
|
" Django can store session data." % self.storage_path)
|
2008-01-06 20:53:09 +08:00
|
|
|
|
|
|
|
self.file_prefix = settings.SESSION_COOKIE_NAME
|
2007-09-16 05:29:14 +08:00
|
|
|
super(SessionStore, self).__init__(session_key)
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def _key_to_file(self, session_key=None):
|
|
|
|
"""
|
|
|
|
Get the file associated with this session key.
|
|
|
|
"""
|
|
|
|
if session_key is None:
|
|
|
|
session_key = self.session_key
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
# Make sure we're not vulnerable to directory traversal. Session keys
|
2008-06-23 13:08:07 +08:00
|
|
|
# should always be md5s, so they should never contain directory
|
|
|
|
# components.
|
2007-09-16 05:29:14 +08:00
|
|
|
if os.path.sep in session_key:
|
2008-06-23 13:08:07 +08:00
|
|
|
raise SuspiciousOperation(
|
|
|
|
"Invalid characters (directory components) in session key")
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
return os.path.join(self.storage_path, self.file_prefix + session_key)
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def load(self):
|
|
|
|
session_data = {}
|
|
|
|
try:
|
|
|
|
session_file = open(self._key_to_file(), "rb")
|
|
|
|
try:
|
2007-09-16 10:03:46 +08:00
|
|
|
try:
|
|
|
|
session_data = self.decode(session_file.read())
|
2008-08-14 11:57:18 +08:00
|
|
|
except (EOFError, SuspiciousOperation):
|
|
|
|
self.create()
|
2007-09-16 05:29:14 +08:00
|
|
|
finally:
|
|
|
|
session_file.close()
|
2008-08-14 11:57:18 +08:00
|
|
|
except IOError:
|
2007-09-16 05:29:14 +08:00
|
|
|
pass
|
|
|
|
return session_data
|
|
|
|
|
2008-08-14 11:57:18 +08:00
|
|
|
def create(self):
|
|
|
|
while True:
|
|
|
|
self._session_key = self._get_new_session_key()
|
|
|
|
try:
|
|
|
|
self.save(must_create=True)
|
|
|
|
except CreateError:
|
|
|
|
continue
|
|
|
|
self.modified = True
|
|
|
|
self._session_cache = {}
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, must_create=False):
|
|
|
|
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | getattr(os, 'O_BINARY', 0)
|
|
|
|
if must_create:
|
|
|
|
flags |= os.O_EXCL
|
2008-08-14 11:58:09 +08:00
|
|
|
# Because this may trigger a load from storage, we must do it before
|
|
|
|
# truncating the file to save.
|
2008-08-15 03:43:08 +08:00
|
|
|
session_data = self._get_session(no_load=must_create)
|
2007-09-16 05:29:14 +08:00
|
|
|
try:
|
2008-08-14 11:57:18 +08:00
|
|
|
fd = os.open(self._key_to_file(self.session_key), flags)
|
2007-09-16 05:29:14 +08:00
|
|
|
try:
|
2008-08-14 11:58:09 +08:00
|
|
|
os.write(fd, self.encode(session_data))
|
2007-09-16 05:29:14 +08:00
|
|
|
finally:
|
2008-08-14 11:57:18 +08:00
|
|
|
os.close(fd)
|
|
|
|
except OSError, e:
|
|
|
|
if must_create and e.errno == errno.EEXIST:
|
|
|
|
raise CreateError
|
|
|
|
raise
|
|
|
|
except (IOError, EOFError):
|
2007-09-16 05:29:14 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def exists(self, session_key):
|
|
|
|
if os.path.exists(self._key_to_file(session_key)):
|
|
|
|
return True
|
|
|
|
return False
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2008-08-14 11:57:46 +08:00
|
|
|
def delete(self, session_key=None):
|
|
|
|
if session_key is None:
|
2008-08-15 22:59:11 +08:00
|
|
|
if self._session_key is None:
|
|
|
|
return
|
2008-08-14 11:57:46 +08:00
|
|
|
session_key = self._session_key
|
2007-09-16 05:29:14 +08:00
|
|
|
try:
|
|
|
|
os.unlink(self._key_to_file(session_key))
|
|
|
|
except OSError:
|
|
|
|
pass
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def clean(self):
|
2007-09-16 10:03:46 +08:00
|
|
|
pass
|