Пример #1
0
    def encode_hybi(buf, opcode, base64=False):
        """ Encode a HyBi style WebSocket frame.
        Optional opcode:
            0x0 - continuation
            0x1 - text frame (base64 encode buf)
            0x2 - binary frame (use raw buf)
            0x8 - connection close
            0x9 - ping
            0xA - pong
        """
        if base64:
            buf = b64encode(buf)

        b1 = 0x80 | (opcode & 0x0f)  # FIN + opcode
        payload_len = len(buf)
        if payload_len <= 125:
            header = pack('>BB', b1, payload_len)
        elif payload_len > 125 and payload_len < 65536:
            header = pack('>BBH', b1, 126, payload_len)
        elif payload_len >= 65536:
            header = pack('>BBQ', b1, 127, payload_len)

        #print("Encoded: %s" % repr(header + buf))

        return header + buf, len(header), 0
Пример #2
0
    def encode_hybi(buf, opcode, base64=False):
        """ Encode a HyBi style WebSocket frame.
        Optional opcode:
            0x0 - continuation
            0x1 - text frame (base64 encode buf)
            0x2 - binary frame (use raw buf)
            0x8 - connection close
            0x9 - ping
            0xA - pong
        """
        if base64:
            buf = b64encode(buf)

        b1 = 0x80 | (opcode & 0x0F)  # FIN + opcode
        payload_len = len(buf)
        if payload_len <= 125:
            header = pack(">BB", b1, payload_len)
        elif payload_len > 125 and payload_len < 65536:
            header = pack(">BBH", b1, 126, payload_len)
        elif payload_len >= 65536:
            header = pack(">BBQ", b1, 127, payload_len)

        # print("Encoded: %s" % repr(header + buf))

        return header + buf, len(header), 0
Пример #3
0
    def do_websocket_handshake(self, headers, path):
        h = self.headers = headers
        self.path = path

        prot = 'WebSocket-Protocol'
        protocols = h.get('Sec-' + prot, h.get(prot, '')).split(',')

        ver = h.get('Sec-WebSocket-Version')
        if ver:
            # HyBi/IETF version of the protocol

            # HyBi-07 report version 7
            # HyBi-08 - HyBi-12 report version 8
            # HyBi-13 reports version 13
            if ver in ['7', '8', '13']:
                self.version = "hybi-%02d" % int(ver)
            else:
                raise self.EClose('Unsupported protocol version %s' % ver)

            key = h['Sec-WebSocket-Key']

            # Choose binary if client supports it
            if 'binary' in protocols:
                self.base64 = False
            elif 'base64' in protocols:
                self.base64 = True
            else:
                raise self.EClose(
                    "Client must support 'binary' or 'base64' protocol")

            # Generate the hash value for the accept header
            accept = b64encode(sha1(s2b(key + self.GUID)).digest())

            response = self.server_handshake_hybi % b2s(accept)
            if self.base64:
                response += "Sec-WebSocket-Protocol: base64\r\n"
            else:
                response += "Sec-WebSocket-Protocol: binary\r\n"
            response += "\r\n"

        else:
            # Hixie version of the protocol (75 or 76)

            if h.get('key3'):
                trailer = self.gen_md5(h)
                pre = "Sec-"
                self.version = "hixie-76"
            else:
                trailer = ""
                pre = ""
                self.version = "hixie-75"

            # We only support base64 in Hixie era
            self.base64 = True

            response = self.server_handshake_hixie % (
                pre, h['Origin'], pre, self.scheme, h['Host'], path)

            if 'base64' in protocols:
                response += "%sWebSocket-Protocol: base64\r\n" % pre
            else:
                self.msg(
                    "Warning: client does not report 'base64' protocol support"
                )
            response += "\r\n" + trailer

        return response
Пример #4
0
 def encode_hixie(buf):
     return s2b("\x00" + b2s(b64encode(buf)) + "\xff"), 1, 1
Пример #5
0
    def do_websocket_handshake(self, headers, path):
        h = self.headers = headers
        self.path = path

        prot = "WebSocket-Protocol"
        protocols = h.get("Sec-" + prot, h.get(prot, "")).split(",")

        ver = h.get("Sec-WebSocket-Version")
        if ver:
            # HyBi/IETF version of the protocol

            # HyBi-07 report version 7
            # HyBi-08 - HyBi-12 report version 8
            # HyBi-13 reports version 13
            if ver in ["7", "8", "13"]:
                self.version = "hybi-%02d" % int(ver)
            else:
                raise self.EClose("Unsupported protocol version %s" % ver)

            key = h["Sec-WebSocket-Key"]

            # Choose binary if client supports it
            if "binary" in protocols:
                self.base64 = False
            elif "base64" in protocols:
                self.base64 = True
            else:
                raise self.EClose("Client must support 'binary' or 'base64' protocol")

            # Generate the hash value for the accept header
            accept = b64encode(sha1(s2b(key + self.GUID)).digest())

            response = self.server_handshake_hybi % b2s(accept)
            if self.base64:
                response += "Sec-WebSocket-Protocol: base64\r\n"
            else:
                response += "Sec-WebSocket-Protocol: binary\r\n"
            response += "\r\n"

        else:
            # Hixie version of the protocol (75 or 76)

            if h.get("key3"):
                trailer = self.gen_md5(h)
                pre = "Sec-"
                self.version = "hixie-76"
            else:
                trailer = ""
                pre = ""
                self.version = "hixie-75"

            # We only support base64 in Hixie era
            self.base64 = True

            response = self.server_handshake_hixie % (pre, h["Origin"], pre, self.scheme, h["Host"], path)

            if "base64" in protocols:
                response += "%sWebSocket-Protocol: base64\r\n" % pre
            else:
                self.msg("Warning: client does not report 'base64' protocol support")
            response += "\r\n" + trailer

        return response
Пример #6
0
 def encode_hixie(buf):
     return s2b("\x00" + b2s(b64encode(buf)) + "\xff"), 1, 1