Skip to content

Instantly share code, notes, and snippets.

@strogonoff
Created November 16, 2011 08:56
Show Gist options
  • Select an option

  • Save strogonoff/1369619 to your computer and use it in GitHub Desktop.

Select an option

Save strogonoff/1369619 to your computer and use it in GitHub Desktop.

Revisions

  1. strogonoff revised this gist Nov 16, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion middleware.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,6 @@ class XsSharing(object):
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Based off https://gist.github.com/426829
    """
    def process_request(self, request):
    if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
  2. strogonoff revised this gist Nov 16, 2011. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -15,12 +15,13 @@

    class XsSharing(object):
    """
    This middleware allows cross-domain XHR using the html5 postMessage API.
    Access-Control-Allow-Origin: http://foo.example
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    This middleware allows cross-domain XHR using the html5 postMessage API.
    Access-Control-Allow-Origin: http://foo.example
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Based off https://gist.github.com/426829
    Based off https://gist.github.com/426829
    """
    def process_request(self, request):
    if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
  3. strogonoff revised this gist Nov 16, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion middleware.py
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,6 @@ class XsSharing(object):
    Based off https://gist.github.com/426829
    """
    def process_request(self, request):

    if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
    response = http.HttpResponse()
    response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
  4. strogonoff revised this gist Nov 16, 2011. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,6 @@ def process_request(self, request):
    return None

    def process_response(self, request, response):
    # Avoid unnecessary work

    response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
    response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS )
    response['Access-Control-Allow-Headers'] = ",".join( XS_SHARING_ALLOWED_HEADERS )
  5. strogonoff revised this gist Nov 16, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    from django import http

    try:
    from django.conf import settings
    XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS
  6. strogonoff created this gist Nov 16, 2011.
    43 changes: 43 additions & 0 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    try:
    from django.conf import settings
    XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS
    XS_SHARING_ALLOWED_METHODS = settings.XS_SHARING_ALLOWED_METHODS
    XS_SHARING_ALLOWED_HEADERS = settings.XS_SHARING_ALLOWED_HEADERS
    XS_SHARING_ALLOWED_CREDENTIALS = settings.XS_SHARING_ALLOWED_CREDENTIALS
    except AttributeError:
    XS_SHARING_ALLOWED_ORIGINS = '*'
    XS_SHARING_ALLOWED_METHODS = ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE']
    XS_SHARING_ALLOWED_HEADERS = ['Content-Type', '*']
    XS_SHARING_ALLOWED_CREDENTIALS = 'true'


    class XsSharing(object):
    """
    This middleware allows cross-domain XHR using the html5 postMessage API.
    Access-Control-Allow-Origin: http://foo.example
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Based off https://gist.github.com/426829
    """
    def process_request(self, request):

    if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
    response = http.HttpResponse()
    response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
    response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS )
    response['Access-Control-Allow-Headers'] = ",".join( XS_SHARING_ALLOWED_HEADERS )
    response['Access-Control-Allow-Credentials'] = XS_SHARING_ALLOWED_CREDENTIALS
    return response

    return None

    def process_response(self, request, response):
    # Avoid unnecessary work

    response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
    response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS )
    response['Access-Control-Allow-Headers'] = ",".join( XS_SHARING_ALLOWED_HEADERS )
    response['Access-Control-Allow-Credentials'] = XS_SHARING_ALLOWED_CREDENTIALS

    return response