sanic_cors/core.py
def set_cors_headers(req, resp, context, options):
    """
    Performs the actual evaluation of Sanic-CORS options and actually
    modifies the response object.

    This function is used both in the decorator and the after_request
    callback
    :param sanic.request.Request req:

    """
    try:
        request_context = context.request[id(req)]
    except AttributeError:
        LOG.debug("Cannot find the request context. Is request already finished?")
        return resp
    # If CORS has already been evaluated via the decorator, skip
    evaluated = request_context.get(SANIC_CORS_EVALUATED, False)
    if evaluated:
        LOG.debug('CORS have been already evaluated, skipping')
        return resp

    # `resp` can be None in the case of using Websockets
    # however this case should have been handled in the `extension` and `decorator` methods
    # before getting here. This is a final failsafe check to prevent crashing
    if resp is None:
        return None

    if resp.headers is None:
        resp.headers = CIMultiDict()

    headers_to_set = get_cors_headers(options, req.headers, req.method)

    LOG.debug('Settings CORS headers: %s', str(headers_to_set))

    # dict .extend() does not work on CIDict so
    # iterate over them and add them individually.
    try:
        resp.headers.extend(headers_to_set)
    except Exception as e1:
        for k, v in headers_to_set.items():
            try:
                resp.headers.add(k, v)
            except Exception as e2:
                resp.headers[k] = v
        return resp
def set_cors_headers(req, resp, context, options):
    try:
        request_context = context.request[id(req)]
    except AttributeError:
        LOG.debug("Cannot find the request context. Is request already finished?")
        return resp
    # If CORS has already been evaluated via the decorator, skip
    evaluated = request_context.get(SANIC_CORS_EVALUATED, False)
    if evaluated:
        LOG.debug('CORS have been already evaluated, skipping')
        return resp

    # `resp` can be None in the case of using Websockets
    # however this case should have been handled in the `extension` and `decorator` methods
    # before getting here. This is a final failsafe check to prevent crashing
    if resp is None:
        return None

    if resp.headers is None:
        resp.headers = CIMultiDict()

    headers_to_set = get_cors_headers(options, req.headers, req.method)

    LOG.debug('Settings CORS headers: %s', str(headers_to_set))

    # dict .extend() does not work on CIDict so
    # iterate over them and add them individually.
    try:
        resp.headers.extend(headers_to_set)
    except Exception as e1:
        for k, v in headers_to_set.items():
            try:
                resp.headers.add(k, v)
            except Exception as e2:
                resp.headers[k] = v
        return resp
def set_cors_headers ( req , resp , context , options ) : try : request_context = context . request [ id ( req ) ] except AttributeError : LOG . debug ( "Cannot find the request context. Is request already finished?" ) return resp # If CORS has already been evaluated via the decorator, skip evaluated = request_context . get ( SANIC_CORS_EVALUATED , False ) if evaluated : LOG . debug ( 'CORS have been already evaluated, skipping' ) return resp # `resp` can be None in the case of using Websockets # however this case should have been handled in the `extension` and `decorator` methods # before getting here. This is a final failsafe check to prevent crashing if resp is None : return None if resp . headers is None : resp . headers = CIMultiDict ( ) headers_to_set = get_cors_headers ( options , req . headers , req . method ) LOG . debug ( 'Settings CORS headers: %s' , str ( headers_to_set ) ) # dict .extend() does not work on CIDict so # iterate over them and add them individually. try : resp . headers . extend ( headers_to_set ) except Exception as e1 : for k , v in headers_to_set . items ( ) : try : resp . headers . add ( k , v ) except Exception as e2 : resp . headers [ k ] = v return resp
Performs the actual evaluation of Sanic-CORS options and actually
    modifies the response object.

    This function is used both in the decorator and the after_request
    callback
    :param sanic.request.Request req:
Performs the actual evaluation of Sanic - CORS options and actually modifies the response object .