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_after_request dies if the request has headers=None #217

Closed
dusktreader opened this issue Apr 25, 2018 · 1 comment
Closed

cors_after_request dies if the request has headers=None #217

dusktreader opened this issue Apr 25, 2018 · 1 comment

Comments

@dusktreader
Copy link
Contributor

None is the default value of headers. If none are set, the following exception is raised:

Traceback (most recent call last):
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/tbeck/.virtualenvs/praetorian/lib/python3.6/site-packages/flask_cors/extension.py", line 173, in cors_after_request
    if resp.headers.get(ACL_ORIGIN):

This can happen with unhandled exceptions which cause a 500 error, or any response that does not set headers.

@ccrvlh
Copy link

ccrvlh commented Jun 19, 2020

Been having this problem on 3.0.8.
I think it started after I added health check api path, but don't understand exactly why.
My currently code for before_request and after_request is:

    # Log before request.
    @app.before_request
    def before():
        g.start = time.time()
        g.status = 0
        if request.endpoint == 'main.health_check':
            g.status = 1
            pass
        else:
            logger.debug(f'Received request to endpoint [{request.endpoint}], in blueprint [{request.blueprint}] and security [{request.is_secure}].')

    # Useful debugging interceptor to log all endpoint responses
    @app.after_request
    def after(response):
        if g.status == 1:
            pass
        else:
            now = time.time() 
            duration = round(now - g.start, 2)
            duration = time.localtime(duration)
            duration = str(duration[4]) + 'm:' + str(duration[5]) + 's'
            logger.debug(f'Finished request: [{response.status}], with [{response.data.decode("utf-8")}] and took [{duration}] seconds.')
            return response

And my health check route is:

@main.route('/api/health')
def health_check():
    return ''

Any ideas on what I might me doing wront?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants