Browse Source

Merge pull request #2346 from davidism/2345-vary

use existing response.vary property to set vary header
pull/2347/head
David Lord 8 years ago committed by GitHub
parent
commit
b7eca826aa
  1. 14
      flask/helpers.py
  2. 3
      flask/sessions.py
  3. 21
      tests/test_basic.py

14
flask/helpers.py

@ -1004,17 +1004,3 @@ def is_ip(value):
return True return True
return False 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

3
flask/sessions.py

@ -19,7 +19,6 @@ from itsdangerous import BadSignature, URLSafeTimedSerializer
from werkzeug.datastructures import CallbackDict from werkzeug.datastructures import CallbackDict
from werkzeug.http import http_date, parse_date from werkzeug.http import http_date, parse_date
from flask.helpers import patch_vary_header
from . import Markup, json from . import Markup, json
from ._compat import iteritems, text_type from ._compat import iteritems, text_type
from .helpers import is_ip, total_seconds from .helpers import is_ip, total_seconds
@ -407,7 +406,7 @@ class SecureCookieSessionInterface(SessionInterface):
# Add a "Vary: Cookie" header if the session was accessed at all. # Add a "Vary: Cookie" header if the session was accessed at all.
if session.accessed: if session.accessed:
patch_vary_header(response, 'Cookie') response.vary.add('Cookie')
if not self.should_set_cookie(app, session): if not self.should_set_cookie(app, session):
return return

21
tests/test_basic.py

@ -9,20 +9,21 @@
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import pytest import pickle
import re import re
import uuid
import time import time
import flask import uuid
import pickle
from datetime import datetime from datetime import datetime
from threading import Thread from threading import Thread
from flask._compat import text_type
from werkzeug.exceptions import BadRequest, NotFound, Forbidden import pytest
import werkzeug.serving
from werkzeug.exceptions import BadRequest, Forbidden, NotFound
from werkzeug.http import parse_date from werkzeug.http import parse_date
from werkzeug.routing import BuildError from werkzeug.routing import BuildError
import werkzeug.serving
import flask
from flask._compat import text_type
def test_options_work(app, client): def test_options_work(app, client):
@ -523,14 +524,14 @@ def test_session_vary_cookie(app, client):
@app.route('/vary-cookie-header-set') @app.route('/vary-cookie-header-set')
def vary_cookie_header_set(): def vary_cookie_header_set():
response = flask.Response() response = flask.Response()
response.headers['Vary'] = 'Cookie' response.vary.add('Cookie')
flask.session['test'] = 'test' flask.session['test'] = 'test'
return response return response
@app.route('/vary-header-set') @app.route('/vary-header-set')
def vary_header_set(): def vary_header_set():
response = flask.Response() response = flask.Response()
response.headers['Vary'] = 'Accept-Encoding, Accept-Language' response.vary.update(('Accept-Encoding', 'Accept-Language'))
flask.session['test'] = 'test' flask.session['test'] = 'test'
return response return response

Loading…
Cancel
Save