1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-27 10:07:22 +00:00
bazarr/libs/engineio/payload.py

47 lines
1.5 KiB
Python
Raw Normal View History

import urllib
2020-01-30 01:07:26 +00:00
from . import packet
class Payload(object):
"""Engine.IO payload."""
max_decode_packets = 16
def __init__(self, packets=None, encoded_payload=None):
self.packets = packets or []
if encoded_payload is not None:
self.decode(encoded_payload)
def encode(self, jsonp_index=None):
2020-01-30 01:07:26 +00:00
"""Encode the payload for transmission."""
encoded_payload = ''
2020-01-30 01:07:26 +00:00
for pkt in self.packets:
if encoded_payload:
encoded_payload += '\x1e'
encoded_payload += pkt.encode(b64=True)
2020-01-30 01:07:26 +00:00
if jsonp_index is not None:
encoded_payload = '___eio[' + \
str(jsonp_index) + \
']("' + \
encoded_payload.replace('"', '\\"') + \
'");'
2020-01-30 01:07:26 +00:00
return encoded_payload
def decode(self, encoded_payload):
"""Decode a transmitted payload."""
self.packets = []
if len(encoded_payload) == 0:
return
# JSONP POST payload starts with 'd='
if encoded_payload.startswith('d='):
2020-01-30 01:07:26 +00:00
encoded_payload = urllib.parse.parse_qs(
encoded_payload)['d'][0]
2020-01-30 01:07:26 +00:00
encoded_packets = encoded_payload.split('\x1e')
if len(encoded_packets) > self.max_decode_packets:
raise ValueError('Too many packets in payload')
self.packets = [packet.Packet(encoded_packet=encoded_packet)
for encoded_packet in encoded_packets]