Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS on OPTIONS method #85

Merged
merged 3 commits into from
Jun 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shiva/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def before_request():
def after_request(response):
if getattr(g, 'cors', False):
response.headers['Access-Control-Allow-Origin'] = g.cors
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
response.headers['Access-Control-Allow-Headers'] = \
'Accept, Content-Type, Origin, X-Requested-With'

return response

Expand Down
15 changes: 2 additions & 13 deletions shiva/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,14 @@ def decorated(*args, **kwargs):
origin = request.headers.get('Origin')

# `app.config.get('CORS_ALLOWED_ORIGINS', [])` should really be the
# default option in `def allow_origins` for # `custom_origins` but
# default option in `def allow_origins` for `custom_origins` but
# that would use `app` outside of the application context
allowed_origins = custom_origins or \
app.config.get('CORS_ALLOWED_ORIGINS', [])

# Actual headers are added in `after_request`, unless it's an
# OPTIONS request.
# Actual headers are added in `after_request`
g.cors = _get_origin(allowed_origins, origin)

if request.method == 'OPTIONS':
headers = {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Headers': 'Content-Type',
}
response = make_response('ok')
response.headers.update(headers)

return response

return func(*args, **kwargs)

return decorated
Expand Down
5 changes: 5 additions & 0 deletions shiva/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def __new__(cls, *args, **kwargs):

return super(Resource, cls).__new__(cls, *args, **kwargs)

# Without this the shiva.decorator.allow_origins method won't get called
# when issuing an OPTIONS request.
def options(self):
return JSONResponse()


class JSONResponse(Response):
"""
Expand Down