Question

I am using this function to decode a package sent via html5 websockets But when the payload goes over 125 bytes it crashes. Is it possible to make it larger?


def decode_data(data):
  databyte = bytearray(data)
  datalen = (0x7F & databyte[1])
  str_data = ''
  if(datalen > 0):
    mask_key = databyte[2:6]
    masked_data = databyte[6:(6+datalen)]
    unmasked_data = [masked_data[i] ^ mask_key[i%4] for i in range(len(masked_data))]
    str_data = str(bytearray(unmasked_data))
  return str_data
Was it helpful?

Solution

It's not a matter of making it longer. Per the RFC 6455:

  • If the length is 0-125, that is the payload length.
  • If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length.
  • If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length.

The length has to be sliced into separate bytes, which means you'll need to bit-shift to the right (with an amount of eight bits), and then only retain the last eight bits by doing and 1111 1111 (which is 255).

This answer goes over this principal in great detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top