Added pillar.flask_extra.vary_xhr() decorator

This produces a 'Vary: X-Requested-With' header on the response of
decorated view functions, which indicates to the browser (or intermediate
proxy servers) that the response may/will will be different for XHR and
non-XHR requests.
This commit is contained in:
2017-05-11 13:07:33 +02:00
parent 736686390f
commit 203c6418fd
2 changed files with 64 additions and 0 deletions

29
pillar/flask_extra.py Normal file
View File

@@ -0,0 +1,29 @@
import functools
import flask
def add_response_headers(headers: dict):
"""This decorator adds the headers passed in to the response"""
def decorator(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
resp = flask.make_response(f(*args, **kwargs))
h = resp.headers
for header, value in headers.items():
h[header] = value
return resp
return decorated_function
return decorator
def vary_xhr():
"""View function decorator; adds HTTP header "Vary: X-Requested-With" to the response"""
def decorator(f):
header_adder = add_response_headers({'Vary': 'X-Requested-With'})
return header_adder(f)
return decorator