# Path: fluxghost/utils/websocket.py
# MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'


from hashlib import sha1
from fluxghost.utils.websocket import MAGIC_STRING
import logging
import base64
        handler.close_connection = 1
        upgrade = handler.headers.get('Upgrade')
        conn = handler.headers.get('Connection')
        ws_version = handler.headers.get('Sec-WebSocket-Version')
        ws_key = handler.headers.get('Sec-WebSocket-Key')

        if not (upgrade and (upgrade.lower() == 'websocket')):
            logger.error("Bad Header 'Upgrade': %s" % upgrade)
            handler.response_403(body="Bad WebSocket request")
            return False

        if not (conn and ('upgrade' in conn.lower().split(', '))):
            logger.error("Bad Header 'Connection': %s" % conn)
            handler.response_403(body="Bad WebSocket request")
            return False

        if not (ws_version and (13 in map(int, ws_version.split(',')))):
            logger.error("Bad Header 'Sec-WebSocket-Version': %s" % ws_version)
            handler.response_403(body="Bad WebSocket request")
            return False

        self.handshake(handler, ws_key)
        return True

    def handshake(self, handler, ws_key, **kw):
        handshake_key = ('%s%s' % (ws_key, MAGIC_STRING)).encode()
        accept_key = base64.encodestring(sha1(handshake_key).digest())[:-1]

        handler.send_response(101, 'Switching Protocols')
        handler.send_header('Upgrade', 'websocket')

# Path: fluxghost/utils/websocket.py
# MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'


# Path: fluxghost/http_handlers/websocket_handler.py
from hashlib import sha1
from fluxghost.utils.websocket import MAGIC_STRING
import logging
import base64

        handler.close_connection = 1
        upgrade = handler.headers.get('Upgrade')
        conn = handler.headers.get('Connection')
        ws_version = handler.headers.get('Sec-WebSocket-Version')
        ws_key = handler.headers.get('Sec-WebSocket-Key')

        if not (upgrade and (upgrade.lower() == 'websocket')):
            logger.error("Bad Header 'Upgrade': %s" % upgrade)
            handler.response_403(body="Bad WebSocket request")
            return False

        if not (conn and ('upgrade' in conn.lower().split(', '))):
            logger.error("Bad Header 'Connection': %s" % conn)
            handler.response_403(body="Bad WebSocket request")
            return False

        if not (ws_version and (13 in map(int, ws_version.split(',')))):
            logger.error("Bad Header 'Sec-WebSocket-Version': %s" % ws_version)
            handler.response_403(body="Bad WebSocket request")
            return False

        self.handshake(handler, ws_key)
        return True

    def handshake(self, handler, ws_key, **kw):
        handshake_key = ('%s%s' % (ws_key, MAGIC_STRING)).encode()
        accept_key = base64.encodestring(sha1(handshake_key).digest())[:-1]

        handler.send_response(101, 'Switching Protocols')
        handler.send_header('Upgrade', 'websocket')

        handler.send_header('Connection', 'Upgrade')