From c651b08f3981cb9c1e4631563675730a3577e0a9 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 8 Mar 2007 08:46:59 +0000 Subject: [PATCH] Fixed #3586 -- Only output "Vary: Cookie" HTTP header when the session object is accessed. Leads to better caching performance. Thanks, Owen Griffiths. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4680 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/contrib/sessions/middleware.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 5b35e46ca3..57738cab36 100644 --- a/AUTHORS +++ b/AUTHORS @@ -89,6 +89,7 @@ answer newbie questions, and generally made Django that much better: Baishampayan Ghose martin.glueck@gmail.com Simon Greenhill + Owen Griffiths Espen Grindhaug Brian Harring Brant Harris diff --git a/django/contrib/sessions/middleware.py b/django/contrib/sessions/middleware.py index 728caa7e19..c134a9c9cf 100644 --- a/django/contrib/sessions/middleware.py +++ b/django/contrib/sessions/middleware.py @@ -10,6 +10,7 @@ TEST_COOKIE_VALUE = 'worked' class SessionWrapper(object): def __init__(self, session_key): self.session_key = session_key + self.accessed = False self.modified = False def __contains__(self, key): @@ -46,6 +47,7 @@ class SessionWrapper(object): def _get_session(self): # Lazily loads session from storage. + self.accessed = True try: return self._session_cache except AttributeError: @@ -72,12 +74,14 @@ class SessionMiddleware(object): def process_response(self, request, response): # If request.session was modified, or if response.session was set, save # those changes and set a session cookie. - patch_vary_headers(response, ('Cookie',)) try: + accessed = request.session.accessed modified = request.session.modified except AttributeError: pass else: + if accessed: + patch_vary_headers(response, ('Cookie',)) if modified or settings.SESSION_SAVE_EVERY_REQUEST: session_key = request.session.session_key or Session.objects.get_new_session_key() if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE: