Created
October 14, 2013 01:49
-
-
Save zedshaw/6969554 to your computer and use it in GitHub Desktop.
Wrapper for a django handler that adds the CORS stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from urlparse import urlparse | |
def cors(func): | |
def add_basic_headers(resp, url): | |
resp['Access-Control-Allow-Origin'] = url.scheme + "://" + url.netloc | |
resp['Access-Control-Allow-Credentials'] = 'true' | |
resp['Access-Control-Allow-Methods'] = 'GET' | |
resp['Access-Control-Allow-Headers'] = 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' | |
@wraps(func) | |
def inner(request, *args, **kw): | |
referer = request.META.get('HTTP_REFERER', None) | |
if request.method == "OPTIONS": | |
resp = HttpResponse("") | |
# 20 days | |
resp['Access-Control-Max-Age'] = 1728000 | |
resp['Content-Type'] = 'text/plain charset=UTF-8' | |
else: | |
resp = func(request, *args, **kw) | |
if referer: | |
add_basic_headers(resp, urlparse(referer)) | |
return resp | |
return inner | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment