Skip to content
Myr

Merge pull request #2322 from nnja/dont_overwrite_vary_header

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Don't overwrite Vary header when setting for cookie access

authored by David Lord · committed by GitHub · parents a4ae7f4, bdf31cd · Browse files · 3 files+45 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+45 added−11 removed

  • tests:+22 added−4 removed
  • other:+23 added−7 removed

3 files+45 added−11 removed

modified:flask/helpers.py+14 added
1002 unmodified lines
1003 else:1004 return True10051006 return False100710081009def patch_vary_header(response, value):1010 """Add a value to the ``Vary`` header if it is not already present."""10111012 header = response.headers.get('Vary', '')1013 headers = [h for h in (h.strip() for h in header.split(',')) if h]1014 lower_value = value.lower()10151016 if not any(h.lower() == lower_value for h in headers):1017 headers.append(value)10181019 updated_header = ', '.join(headers)1020 response.headers['Vary'] = updated_header
modified:flask/sessions.py+9 added-7 removed
7 unmodified lines
8 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import uuid12import hashlib13import uuid14import warnings15from base64 import b64encode, b64decode15from base64 import b64decode, b64encode16from datetime import datetime17from werkzeug.http import http_date, parse_date1718from itsdangerous import BadSignature, URLSafeTimedSerializer19from werkzeug.datastructures import CallbackDict20from werkzeug.http import http_date, parse_date2122from flask.helpers import patch_vary_header23from . import Markup, json24from ._compat import iteritems, text_type21from .helpers import total_seconds, is_ip25from .helpers import is_ip, total_seconds2623from itsdangerous import URLSafeTimedSerializer, BadSignature272528class SessionMixin(object):29 """Expands a basic dictionary with an accessors that are expected30 by Flask extensions and users for the session.31 """
374 unmodified lines
406 return407408 # Add a "Vary: Cookie" header if the session was accessed at all.409 if session.accessed:408 response.headers.add('Vary', 'Cookie')410 patch_vary_header(response, 'Cookie')411412 if not self.should_set_cookie(app, session):413 return414
13 unmodified lines
modified:tests/test_basic.py+22 added-4 removed
518 unmodified lines
519 @app.route('/setdefault')520 def setdefault():521 return flask.session.setdefault('test', 'default')522523 @app.route('/vary-cookie-header-set')524 def vary_cookie_header_set():525 response = flask.Response()526 response.headers['Vary'] = 'Cookie'527 flask.session['test'] = 'test'528 return response529530 @app.route('/vary-header-set')531 def vary_header_set():532 response = flask.Response()533 response.headers['Vary'] = 'Accept-Encoding, Accept-Language'534 flask.session['test'] = 'test'535 return response536537 @app.route('/no-vary-header')538 def no_vary_header():539 return ''540527 def expect(path, header=True):541 def expect(path, header_value='Cookie'):542 rv = client.get(path)543530 if header:531 assert rv.headers['Vary'] == 'Cookie'544 if header_value:545 # The 'Vary' key should exist in the headers only once.546 assert len(rv.headers.get_all('Vary')) == 1547 assert rv.headers['Vary'] == header_value548 else:549 assert 'Vary' not in rv.headers550551 expect('/set')552 expect('/get')553 expect('/getitem')554 expect('/setdefault')555 expect('/vary-cookie-header-set')556 expect('/vary-header-set', 'Accept-Encoding, Accept-Language, Cookie')539 expect('/no-vary-header', False)557 expect('/no-vary-header', None)558559560def test_flashes(app, req_ctx):561 app.secret_key = 'testkey'
1255 unmodified lines