2011-05-31 06:27:47 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_deny(view_func):
|
|
|
|
"""
|
|
|
|
Modifies a view function so its response has the X-Frame-Options HTTP
|
|
|
|
header set to 'DENY' as long as the response doesn't already have that
|
|
|
|
header set.
|
|
|
|
|
|
|
|
e.g.
|
|
|
|
|
|
|
|
@xframe_options_deny
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
def wrapped_view(*args, **kwargs):
|
|
|
|
resp = view_func(*args, **kwargs)
|
2015-05-14 02:51:18 +08:00
|
|
|
if resp.get('X-Frame-Options') is None:
|
2011-05-31 06:27:47 +08:00
|
|
|
resp['X-Frame-Options'] = 'DENY'
|
|
|
|
return resp
|
2017-01-22 02:20:17 +08:00
|
|
|
return wraps(view_func)(wrapped_view)
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_sameorigin(view_func):
|
|
|
|
"""
|
|
|
|
Modifies a view function so its response has the X-Frame-Options HTTP
|
|
|
|
header set to 'SAMEORIGIN' as long as the response doesn't already have
|
|
|
|
that header set.
|
|
|
|
|
|
|
|
e.g.
|
|
|
|
|
|
|
|
@xframe_options_sameorigin
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
def wrapped_view(*args, **kwargs):
|
|
|
|
resp = view_func(*args, **kwargs)
|
2015-05-14 02:51:18 +08:00
|
|
|
if resp.get('X-Frame-Options') is None:
|
2011-05-31 06:27:47 +08:00
|
|
|
resp['X-Frame-Options'] = 'SAMEORIGIN'
|
|
|
|
return resp
|
2017-01-22 02:20:17 +08:00
|
|
|
return wraps(view_func)(wrapped_view)
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_exempt(view_func):
|
|
|
|
"""
|
|
|
|
Modifies a view function by setting a response variable that instructs
|
|
|
|
XFrameOptionsMiddleware to NOT set the X-Frame-Options HTTP header.
|
|
|
|
|
|
|
|
e.g.
|
|
|
|
|
|
|
|
@xframe_options_exempt
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
def wrapped_view(*args, **kwargs):
|
|
|
|
resp = view_func(*args, **kwargs)
|
|
|
|
resp.xframe_options_exempt = True
|
|
|
|
return resp
|
2017-01-22 02:20:17 +08:00
|
|
|
return wraps(view_func)(wrapped_view)
|