diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py
index 0f86908..7b71ede 100644
a
|
b
|
|
1 | 1 | import errno |
2 | 2 | import os |
3 | 3 | import tempfile |
| 4 | import time |
4 | 5 | |
5 | 6 | from django.conf import settings |
6 | 7 | from django.contrib.sessions.backends.base import SessionBase, CreateError |
… |
… |
class SessionStore(SessionBase):
|
49 | 50 | try: |
50 | 51 | with open(self._key_to_file(), "rb") as session_file: |
51 | 52 | file_data = session_file.read() |
52 | | # Don't fail if there is no data in the session file. |
53 | | # We may have opened the empty placeholder file. |
54 | | if file_data: |
55 | | try: |
56 | | session_data = self.decode(file_data) |
57 | | except (EOFError, SuspiciousOperation): |
| 53 | # Create new session in case of an expired one |
| 54 | if self._expired(): |
58 | 55 | self.create() |
| 56 | # Don't fail if there is no data in the session file. |
| 57 | # We may have opened the empty placeholder file. |
| 58 | elif file_data: |
| 59 | try: |
| 60 | session_data = self.decode(file_data) |
| 61 | except (EOFError, SuspiciousOperation): |
| 62 | self.create() |
59 | 63 | except IOError: |
60 | 64 | self.create() |
61 | 65 | return session_data |
… |
… |
class SessionStore(SessionBase):
|
127 | 131 | except (OSError, IOError, EOFError): |
128 | 132 | pass |
129 | 133 | |
| 134 | def _expired(self): |
| 135 | # The age of session file from last modify time |
| 136 | age = time.time() - os.path.getmtime(self._key_to_file()) |
| 137 | return age > settings.SESSION_COOKIE_AGE |
| 138 | |
130 | 139 | def exists(self, session_key): |
131 | 140 | return os.path.exists(self._key_to_file(session_key)) |
132 | 141 | |
diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py
index 92ea6bb..d6da388 100644
a
|
b
|
class FileSessionTests(SessionTestsMixin, unittest.TestCase):
|
345 | 345 | self.assertRaises(SuspiciousOperation, |
346 | 346 | self.backend("a/b/c").load) |
347 | 347 | |
| 348 | # This test fails with cookie and cache session, thats why added it to |
| 349 | # file tests only. |
| 350 | @override_settings(SESSION_COOKIE_AGE=0) |
| 351 | def test_onload_expiry_check(self): |
| 352 | """ |
| 353 | Test to ensure that expiry of session is checked on-load |
| 354 | """ |
| 355 | |
| 356 | self.session['test-data'] = True |
| 357 | old_key = self.session.session_key |
| 358 | self.session.save() |
| 359 | self.assertTrue(self.session.get('test-data')) |
| 360 | self.session.load() |
| 361 | new_key = self.session.session_key |
| 362 | |
| 363 | # Make sure a new key was generated after the invalidation of the old |
| 364 | # one. |
| 365 | self.assertNotEqual(old_key, new_key) |
| 366 | # The key test-data should not be present in the session. |
| 367 | self.assertFalse(self.session.get('test-data')) |
348 | 368 | |
349 | 369 | class CacheSessionTests(SessionTestsMixin, unittest.TestCase): |
350 | 370 | |