Browse Source

extract patch_vary_header to helpers

pull/2322/head
David Lord 8 years ago
parent
commit
bdf31cd423
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 14
      flask/helpers.py
  2. 29
      flask/sessions.py

14
flask/helpers.py

@ -1004,3 +1004,17 @@ def is_ip(value):
return True
return False
def patch_vary_header(response, value):
"""Add a value to the ``Vary`` header if it is not already present."""
header = response.headers.get('Vary', '')
headers = [h for h in (h.strip() for h in header.split(',')) if h]
lower_value = value.lower()
if not any(h.lower() == lower_value for h in headers):
headers.append(value)
updated_header = ', '.join(headers)
response.headers['Vary'] = updated_header

29
flask/sessions.py

@ -9,18 +9,20 @@
:license: BSD, see LICENSE for more details.
"""
import uuid
import hashlib
import uuid
import warnings
from base64 import b64encode, b64decode
from base64 import b64decode, b64encode
from datetime import datetime
from werkzeug.http import http_date, parse_date
from itsdangerous import BadSignature, URLSafeTimedSerializer
from werkzeug.datastructures import CallbackDict
from werkzeug.http import http_date, parse_date
from flask.helpers import patch_vary_header
from . import Markup, json
from ._compat import iteritems, text_type
from .helpers import total_seconds, is_ip
from itsdangerous import URLSafeTimedSerializer, BadSignature
from .helpers import is_ip, total_seconds
class SessionMixin(object):
@ -405,7 +407,7 @@ class SecureCookieSessionInterface(SessionInterface):
# Add a "Vary: Cookie" header if the session was accessed at all.
if session.accessed:
self._patch_vary_cookie_header(response)
patch_vary_header(response, 'Cookie')
if not self.should_set_cookie(app, session):
return
@ -423,16 +425,3 @@ class SecureCookieSessionInterface(SessionInterface):
path=path,
secure=secure
)
def _patch_vary_cookie_header(self, response):
"""
Add a 'Cookie' value to the 'Vary' header if one is not already present.
"""
header = response.headers.get('Vary', '')
headers = [h.strip() for h in header.split(',') if h]
if not any(h.lower() == 'cookie' for h in headers):
headers.append('Cookie')
updated_header = ', '.join(headers)
response.headers['Vary'] = updated_header

Loading…
Cancel
Save