def _cookie_callback(session, request, response, session_cookie_was_valid,
    cookie_on_exception, set_cookie, delete_cookie, cookieless_headers):
    """
    Response callback to set the appropriate Set-Cookie header.
    `session` is via functools.partial
    `request` and `response` are appended by add_response_callback
    """
    for header in cookieless_headers:
        if header in response.headers:
            return
    if session._invalidated:
        if session_cookie_was_valid:
            delete_cookie(response=response)
        return
    if session.new:
        if cookie_on_exception is True or request.exception is None:
            set_cookie(request=request, response=response)
            varies = 'Cookie',
            vary = set(response.vary if response.vary is not None else [])
            vary |= set(varies)
            response.vary = vary
        elif session_cookie_was_valid:
            delete_cookie(response=response)
