2020-01-30 09:07:26 +08:00
|
|
|
import base64
|
2021-11-30 12:07:14 +08:00
|
|
|
from engineio import json as _json
|
2020-01-30 09:07:26 +08:00
|
|
|
|
|
|
|
(OPEN, CLOSE, PING, PONG, MESSAGE, UPGRADE, NOOP) = (0, 1, 2, 3, 4, 5, 6)
|
|
|
|
packet_names = ['OPEN', 'CLOSE', 'PING', 'PONG', 'MESSAGE', 'UPGRADE', 'NOOP']
|
|
|
|
|
2021-05-08 22:25:29 +08:00
|
|
|
binary_types = (bytes, bytearray)
|
2020-01-30 09:07:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Packet(object):
|
|
|
|
"""Engine.IO packet."""
|
|
|
|
|
|
|
|
json = _json
|
|
|
|
|
2021-05-08 22:25:29 +08:00
|
|
|
def __init__(self, packet_type=NOOP, data=None, encoded_packet=None):
|
2020-01-30 09:07:26 +08:00
|
|
|
self.packet_type = packet_type
|
|
|
|
self.data = data
|
2024-03-04 01:15:23 +08:00
|
|
|
self.encode_cache = None
|
2021-05-08 22:25:29 +08:00
|
|
|
if isinstance(data, str):
|
2020-01-30 09:07:26 +08:00
|
|
|
self.binary = False
|
|
|
|
elif isinstance(data, binary_types):
|
|
|
|
self.binary = True
|
|
|
|
else:
|
|
|
|
self.binary = False
|
2021-05-08 22:25:29 +08:00
|
|
|
if self.binary and self.packet_type != MESSAGE:
|
|
|
|
raise ValueError('Binary packets can only be of type MESSAGE')
|
2021-11-30 12:07:14 +08:00
|
|
|
if encoded_packet is not None:
|
2020-01-30 09:07:26 +08:00
|
|
|
self.decode(encoded_packet)
|
|
|
|
|
2021-05-08 22:25:29 +08:00
|
|
|
def encode(self, b64=False):
|
2024-03-04 01:15:23 +08:00
|
|
|
"""Encode the packet for transmission.
|
|
|
|
|
|
|
|
Note: as a performance optimization, subsequent calls to this method
|
|
|
|
will return a cached encoded packet, even if the data has changed.
|
|
|
|
"""
|
|
|
|
if self.encode_cache:
|
|
|
|
return self.encode_cache
|
2020-01-30 09:07:26 +08:00
|
|
|
if self.binary:
|
|
|
|
if b64:
|
2021-05-08 22:25:29 +08:00
|
|
|
encoded_packet = 'b' + base64.b64encode(self.data).decode(
|
|
|
|
'utf-8')
|
2020-01-30 09:07:26 +08:00
|
|
|
else:
|
2021-05-08 22:25:29 +08:00
|
|
|
encoded_packet = self.data
|
|
|
|
else:
|
|
|
|
encoded_packet = str(self.packet_type)
|
|
|
|
if isinstance(self.data, str):
|
2020-01-30 09:07:26 +08:00
|
|
|
encoded_packet += self.data
|
2021-05-08 22:25:29 +08:00
|
|
|
elif isinstance(self.data, dict) or isinstance(self.data, list):
|
|
|
|
encoded_packet += self.json.dumps(self.data,
|
|
|
|
separators=(',', ':'))
|
|
|
|
elif self.data is not None:
|
|
|
|
encoded_packet += str(self.data)
|
2024-03-04 01:15:23 +08:00
|
|
|
self.encode_cache = encoded_packet
|
2020-01-30 09:07:26 +08:00
|
|
|
return encoded_packet
|
|
|
|
|
|
|
|
def decode(self, encoded_packet):
|
|
|
|
"""Decode a transmitted package."""
|
2021-05-08 22:25:29 +08:00
|
|
|
self.binary = isinstance(encoded_packet, binary_types)
|
2022-11-08 02:06:49 +08:00
|
|
|
if not self.binary and len(encoded_packet) == 0:
|
|
|
|
raise ValueError('Invalid empty packet received')
|
2021-05-08 22:25:29 +08:00
|
|
|
b64 = not self.binary and encoded_packet[0] == 'b'
|
|
|
|
if b64:
|
2020-01-30 09:07:26 +08:00
|
|
|
self.binary = True
|
2021-05-08 22:25:29 +08:00
|
|
|
self.packet_type = MESSAGE
|
|
|
|
self.data = base64.b64decode(encoded_packet[1:])
|
2020-01-30 09:07:26 +08:00
|
|
|
else:
|
2021-05-08 22:25:29 +08:00
|
|
|
if self.binary and not isinstance(encoded_packet, bytes):
|
|
|
|
encoded_packet = bytes(encoded_packet)
|
2020-01-30 09:07:26 +08:00
|
|
|
if self.binary:
|
2021-05-08 22:25:29 +08:00
|
|
|
self.packet_type = MESSAGE
|
|
|
|
self.data = encoded_packet
|
2020-01-30 09:07:26 +08:00
|
|
|
else:
|
2021-05-08 22:25:29 +08:00
|
|
|
self.packet_type = int(encoded_packet[0])
|
2020-01-30 09:07:26 +08:00
|
|
|
try:
|
2021-05-08 22:25:29 +08:00
|
|
|
self.data = self.json.loads(encoded_packet[1:])
|
2020-01-30 09:07:26 +08:00
|
|
|
if isinstance(self.data, int):
|
|
|
|
# do not allow integer payloads, see
|
|
|
|
# github.com/miguelgrinberg/python-engineio/issues/75
|
|
|
|
# for background on this decision
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
2021-05-08 22:25:29 +08:00
|
|
|
self.data = encoded_packet[1:]
|