extract patch_vary_header to helpers

This commit is contained in:
David Lord 2017-05-25 10:26:02 -07:00
parent e7cd68ba58
commit bdf31cd423
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 23 additions and 20 deletions

View file

@ -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

View file

@ -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