Skip to content

Commit

Permalink
Fixes another issue introduced with Sanic 19.12, where automatic_opti…
Browse files Browse the repository at this point in the history
…ons cannot work when the router is run before

  the Sanic-CORS middleware
Automatic_options is now default to True when running Sanic-CORS in extension-mode (it was already default True when running in @decorator mode)
  • Loading branch information
ashleysommer committed Feb 22, 2020
1 parent a63cfcb commit cff1fb8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.10.0.post3
- Fixes another issue introduced with Sanic 19.12, where automatic_options cannot work when the router is run before
the Sanic-CORS middleware

## 0.10.0.post2
- Fixes the issue where the sanic asyncio server write_error routine cannot use an async Exception handler.
- Fixes #38 (again)
Expand Down
2 changes: 1 addition & 1 deletion sanic_cors/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
supports_credentials=False,
max_age=None,
send_wildcard=False,
automatic_options=False,
automatic_options=True,
vary_header=True,
resources=r'/*',
intercept_exceptions=True,
Expand Down
28 changes: 18 additions & 10 deletions sanic_cors/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async def route_wrapper(self, route, req, context, request_args, request_kw,
_ = decorator_kw.pop('with_context') # ignore this.
_options = decorator_kw
options = get_cors_options(context.app, _options)
if options.get('automatic_options') and req.method == 'OPTIONS':
if options.get('automatic_options', True) and req.method == 'OPTIONS':
resp = response.HTTPResponse()
else:
resp = route(req, *request_args, **request_kw)
Expand Down Expand Up @@ -237,10 +237,11 @@ def unapplied_cors_request_middleware(req, context):
log = context.log
debug = partial(log, logging.DEBUG)
for res_regex, res_options in resources:
if res_options.get('automatic_options') and try_match(path, res_regex):
if res_options.get('automatic_options', True) and \
try_match(path, res_regex):
debug("Request to '{:s}' matches CORS resource '{}'. "
"Using options: {}".format(
path, get_regexp_pattern(res_regex), res_options))
path, get_regexp_pattern(res_regex), res_options))
resp = response.HTTPResponse()

try:
Expand Down Expand Up @@ -377,13 +378,20 @@ def wrapper(cls, f, ctx, req, e):
opts = ctx.options
log = ctx.log
# get response from the original handler
do_await = iscoroutinefunction(f)
resp = f(req, e)
if do_await:
log(logging.DEBUG,
"Found an async Exception handler response. "
"Cannot apply CORS to it. Passing it on.")
return resp
if (req is not None and SANIC_19_12_0 <= SANIC_VERSION and
isinstance(e, MethodNotSupported) and req.method == "OPTIONS" and
opts.get('automatic_options', True)):
# A very specific set of requirments to trigger this kind of
# automatic-options resp
resp = response.HTTPResponse()
else:
do_await = iscoroutinefunction(f)
resp = f(req, e)
if do_await:
log(logging.DEBUG,
"Found an async Exception handler response. "
"Cannot apply CORS to it. Passing it on.")
return resp
# SanicExceptions are equiv to Flask Aborts,
# always apply CORS to them.
if (req is not None and resp is not None) and \
Expand Down
2 changes: 1 addition & 1 deletion sanic_cors/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.10.0.post2'
__version__ = '0.10.0.post3'

0 comments on commit cff1fb8

Please sign in to comment.