我正在尝试解码websocket框架,但是在解码扩展的有效负载时我没有成功。这是我到目前为止所取得的成就:
char *in = data;
char *buffer;
unsigned int i;
unsigned char mask[4];
unsigned int packet_length = 0;
int rc;
/* Expect a finished text frame. */
assert(in[0] == '\x81');
packet_length = ((unsigned char) in[1]) & 0x7f;
mask[0] = in[2];
mask[1] = in[3];
mask[2] = in[4];
mask[3] = in[5];
if (packet_length <= 125) { **// This decoding works**
/* Unmask the payload. */
for (i = 0; i < packet_length; i++)
in[6 + i] ^= mask[i % 4];
rc = asprintf(&buffer, "%.*s", packet_length, in + 6);
} else
if (packet_length == 126) { **//This decosing does NOT work**
/* Unmask the payload. */
for (i = 0; i < packet_length; i++)
in[8 + i] ^= mask[i % 4];
rc = asprintf(&buffer, "%.*s", packet_length, in + 8);
}
我做错了什么?如何编码扩展有效负载?
答案 0 :(得分:5)
如果packet_length为126,则后面的2个字节给出要读取的数据长度。
如果packet_length是127,则后面的8个字节给出要读取的数据长度。
掩码包含在以下4个字节中(在长度之后)。
要解码的消息如下。
规范的data framing section对此有一个有用的说明。
如果您将代码重新订购为
答案 1 :(得分:4)
粘着点在&gt; 125字节有效载荷。
格式非常简单,假设您在JavaScript中发送了10个:
ws.send("a".repeat(10))
然后服务器将收到:
bytes[16]=818a8258a610e339c771e339c771e339
但现在让我们说你在JavaScript中发送了126个:
ws.send("a".repeat(126))
然后服务器将收到:
bytes[134]=81fe007ee415f1e5857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574908485749084857490848574
如果有效载荷的长度> 125,字节1的值为0xfe,格式改为:
C#中的示例代码:
List<byte[]> decodeWebsocketFrame(Byte[] bytes)
{
List<Byte[]> ret = new List<Byte[]>();
int offset = 0;
while (offset + 6 < bytes.Length)
{
// format: 0==ascii/binary 1=length-0x80, byte 2,3,4,5=key, 6+len=message, repeat with offset for next...
int len = bytes[offset + 1] - 0x80;
if (len <= 125)
{
//String data = Encoding.UTF8.GetString(bytes);
//Debug.Log("len=" + len + "bytes[" + bytes.Length + "]=" + ByteArrayToString(bytes) + " data[" + data.Length + "]=" + data);
Debug.Log("len=" + len + " offset=" + offset);
Byte[] key = new Byte[] { bytes[offset + 2], bytes[offset + 3], bytes[offset + 4], bytes[offset + 5] };
Byte[] decoded = new Byte[len];
for (int i = 0; i < len; i++)
{
int realPos = offset + 6 + i;
decoded[i] = (Byte)(bytes[realPos] ^ key[i % 4]);
}
offset += 6 + len;
ret.Add(decoded);
} else
{
int a = bytes[offset + 2];
int b = bytes[offset + 3];
len = (a << 8) + b;
//Debug.Log("Length of ws: " + len);
Byte[] key = new Byte[] { bytes[offset + 4], bytes[offset + 5], bytes[offset + 6], bytes[offset + 7] };
Byte[] decoded = new Byte[len];
for (int i = 0; i < len; i++)
{
int realPos = offset + 8 + i;
decoded[i] = (Byte)(bytes[realPos] ^ key[i % 4]);
}
offset += 8 + len;
ret.Add(decoded);
}
}
return ret;
}