Skip to content

Commit

Permalink
some more doc
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Dec 16, 2010
1 parent 78d2d4b commit c37cedf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions sugar/middleware/cors.py
@@ -1,8 +1,10 @@
from django.conf import settings

class CORSMiddleware(object):
"""
Middleware that serves up JSON media types with a CORS header to
allow people to use your JSON data from JavaScript without requiring
them to proxy it.
Middleware that serves up representations with a CORS header to
allow third parties to use your web api from JavaScript without
requiring them to proxy it.
See: http://www.w3.org/TR/cors/
Implement
Expand All @@ -11,12 +13,26 @@ class CORSMiddleware(object):
'sugar.middleware.cors.CORSMiddleware',
This will inject all your 'application/json' responses with:
Access-Control-Allow-Origin: *
Optionally you can configure the mediatypes you'd like to have be served
up with CORS in your settings:
CORS = [

This comment has been minimized.

Copy link
@acdha

acdha Dec 16, 2010

For flexibility, what do you think about instead defining this as a list of (path prefix, mime type, header) values? That way you'd simply iterate over the list and if request.path.startswith(prefix) you'd use the second & third values - intriguingly, you could even use that for completely custom headers.

The alternative would simply be to keep this really simple and add a decorator variant which could be used in views or urls.py

"{
["application/json", "application/x-suggestions+json"],
"Access-Control-Allow-Origin": "*"
}
]
hmmm... not sure what to do here (yet)
"""

def __init__(self):
# should any others be added here?
self.json_mediatypes = ["application/json",
"application/x-suggestions+json",]
self.json_mediatypes = ["application/json"]

def process_response(self, request, response):
# just get the mediatype (strip charset)
Expand Down
2 changes: 1 addition & 1 deletion sugar/tests.py
Expand Up @@ -48,7 +48,7 @@ def test_middleware(self):
cors = CORSMiddleware()
request = HttpRequest()
response = HttpResponse('["foo"]',
mimetype='application/json')
mimetype='application/json')
cors.process_response(request, response)
self.assertEqual(response['access-control-allow-origin'], '*')

Expand Down

1 comment on commit c37cedf

@edsu
Copy link
Owner Author

@edsu edsu commented on c37cedf Dec 16, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea a lot ... you want to add it?

Please sign in to comment.