2007-10-30 07:55:08 +08:00
|
|
|
import time
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2007-10-30 07:55:08 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2016-01-09 00:08:08 +08:00
|
|
|
from django.contrib.sessions.backends.base import UpdateError
|
|
|
|
from django.shortcuts import redirect
|
2005-10-09 08:55:08 +08:00
|
|
|
from django.utils.cache import patch_vary_headers
|
2015-11-07 23:12:37 +08:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2007-10-31 11:59:40 +08:00
|
|
|
from django.utils.http import cookie_date
|
2005-08-17 06:54:05 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class SessionMiddleware(MiddlewareMixin):
|
|
|
|
def __init__(self, get_response=None):
|
|
|
|
self.get_response = get_response
|
2009-03-19 00:55:59 +08:00
|
|
|
engine = import_module(settings.SESSION_ENGINE)
|
2013-06-30 14:26:10 +08:00
|
|
|
self.SessionStore = engine.SessionStore
|
|
|
|
|
|
|
|
def process_request(self, request):
|
2015-05-14 02:51:18 +08:00
|
|
|
session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME)
|
2013-06-30 14:26:10 +08:00
|
|
|
request.session = self.SessionStore(session_key)
|
2005-08-17 06:54:05 +08:00
|
|
|
|
|
|
|
def process_response(self, request, response):
|
2008-09-17 16:10:55 +08:00
|
|
|
"""
|
|
|
|
If request.session was modified, or if the configuration is to save the
|
2013-08-19 13:36:36 +08:00
|
|
|
session every time, save the changes and set a session cookie or delete
|
|
|
|
the session cookie if the session has been emptied.
|
2008-09-17 16:10:55 +08:00
|
|
|
"""
|
2005-08-17 06:54:05 +08:00
|
|
|
try:
|
2007-03-08 16:46:59 +08:00
|
|
|
accessed = request.session.accessed
|
2005-08-17 06:54:05 +08:00
|
|
|
modified = request.session.modified
|
2013-08-19 13:36:36 +08:00
|
|
|
empty = request.session.is_empty()
|
2005-08-17 06:54:05 +08:00
|
|
|
except AttributeError:
|
2006-01-15 14:18:03 +08:00
|
|
|
pass
|
|
|
|
else:
|
2013-08-19 13:36:36 +08:00
|
|
|
# First check if we need to delete this cookie.
|
|
|
|
# The session should be deleted only if the session is entirely empty
|
|
|
|
if settings.SESSION_COOKIE_NAME in request.COOKIES and empty:
|
2016-06-21 23:03:25 +08:00
|
|
|
response.delete_cookie(
|
|
|
|
settings.SESSION_COOKIE_NAME,
|
|
|
|
path=settings.SESSION_COOKIE_PATH,
|
|
|
|
domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
)
|
2013-08-19 13:36:36 +08:00
|
|
|
else:
|
|
|
|
if accessed:
|
|
|
|
patch_vary_headers(response, ('Cookie',))
|
2015-08-06 04:51:42 +08:00
|
|
|
if (modified or settings.SESSION_SAVE_EVERY_REQUEST) and not empty:
|
2013-08-19 13:36:36 +08:00
|
|
|
if request.session.get_expire_at_browser_close():
|
|
|
|
max_age = None
|
|
|
|
expires = None
|
|
|
|
else:
|
|
|
|
max_age = request.session.get_expiry_age()
|
|
|
|
expires_time = time.time() + max_age
|
|
|
|
expires = cookie_date(expires_time)
|
|
|
|
# Save the session data and refresh the client cookie.
|
|
|
|
# Skip session save for 500 responses, refs #3881.
|
|
|
|
if response.status_code != 500:
|
2016-01-09 00:08:08 +08:00
|
|
|
try:
|
|
|
|
request.session.save()
|
|
|
|
except UpdateError:
|
|
|
|
# The user is now logged out; redirecting to same
|
|
|
|
# page will result in a redirect to the login page
|
|
|
|
# if required.
|
|
|
|
return redirect(request.path)
|
2016-03-29 06:33:29 +08:00
|
|
|
response.set_cookie(
|
|
|
|
settings.SESSION_COOKIE_NAME,
|
|
|
|
request.session.session_key, max_age=max_age,
|
|
|
|
expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
path=settings.SESSION_COOKIE_PATH,
|
|
|
|
secure=settings.SESSION_COOKIE_SECURE or None,
|
|
|
|
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
|
|
|
|
)
|
2005-08-17 06:54:05 +08:00
|
|
|
return response
|