Skip to content

Commit

Permalink
Merge pull request #85 from tooxie/cors-on-options
Browse files Browse the repository at this point in the history
CORS on OPTIONS method
  • Loading branch information
tooxie committed Jun 2, 2013
2 parents d556deb + f442164 commit fcb195d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
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

0 comments on commit fcb195d

Please sign in to comment.