如何在 C 中使用 zlib 库对 websocket permessage-deflate 进行编码和解码?
How to Encode and Decode websocket permessage-deflate using zlib library in C?
全部
我正在尝试使用 per-message-deflate 对 WebSocket 有效负载进行编码。我正在尝试使用一些测试代码,但我的编码不正确,你能帮帮我吗?
这是我的代码:
char a[180] = "{\"type\":\"message\",\"channel\":\"C04U0F9CW\",\"text\":\"TestingMessage\",\"id\":757}";
char b[180];
char c[180];
int i;
// deflate
// zlib struct
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array
//deflateInit(&defstream, Z_SYNC_FLUSH);
deflateInit2(&defstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
deflate(&defstream, Z_SYNC_FLUSH);
deflateEnd(&defstream);
for(i=0;i<=(char*)defstream.next_out - b;i++)
{
printf("%X",(Bytef)b[i]);
}
printf("\n%s\n",(Bytef *)b);
// This is one way of getting the size of the output
printf("Deflated size is: %lu\n", (char*)defstream.next_out - b);
我的输出是
kml@kml-VirtualBox:~/zlib-1.2.11/test$ gcc test.c -lz && ./a.out
789CAA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD18A391B98841AB8593A873454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B5C0000FFFF2E
x��V*�,HU�R�M-.NLOU�QJ�H��K��9���Y:�EKR+J�B!��%�y��pՙ)JV����
Deflated size is: 72
Inflate:
73
{"type":"message","channel":"C04U0F9CW","text":"TestingMessage","id":757}
所以我希望有效载荷会像这样
AA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD018A391B98841AB8593A8703454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B50000
This is the fiddler WebSocket traffic capture for that particular message
请帮助我获得正确的每条消息压缩编码。
Here is some more upgrade information
GET https://127.0.0.1/websoc.html HTTP/1.1
Host: mpmulti-yklu.slack-msgs.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: FriWg0AWhiLhLXXnfJK8kA==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
回应
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: S2Q+O/tRM0/mIG//Er2hDdqD0eY=
Sec-Websocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover
X-Via: haproxy-ext90
代码是正确的,但 defstream.avail_in 需要准确的长度,而不是额外的空字符。如果您提供准确的输入长度,它将正常工作。像这样尝试
int l = (uInt)strlen(a);
defstream.avail_in = l; // size of input, string + terminator
请尝试理解 z_stream 是如何赋值的检查代码然后你会更好地理解这一点。
我想很多人都在尝试解码,但他们没有得到 per-message-deflate 的正确值。
检查你的输出
"789CAA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD18A391B98841AB8593A873454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B5C0000FFFF2E"
这里 789C 是 header 和 0000FFFF 或者根据你的代码 (00FFFF) 是 deflate 消息的尾部。这里 0 代表 00,因为您的输出是打印一个“0”而不是“00”。因为多了一个尺码,你多了 5C。当您尝试以下代码时,您的“2E”也将被删除
int d = memcmp(msgend, &b[defstream.total_out - 4], 4) ? 0 : 4;
for(i=0;i<defstream.total_out - d;i++)
{
printf("%2X",(Bytef)b[i]);
}
所以你的预期答案是
AA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD018A391B98841AB8593A8703454B522B4A804221A9C5259979E9BE70D599294A56B=E16A6E6[][=]
根据您的输出,2 个字节为 0,因此根据您的代码,它将为“00”,并且您的输出遵循打印每个半字节(半字节)。
请试试这个对我有用,我现在可以正确解码了。
全部 我正在尝试使用 per-message-deflate 对 WebSocket 有效负载进行编码。我正在尝试使用一些测试代码,但我的编码不正确,你能帮帮我吗?
这是我的代码:
char a[180] = "{\"type\":\"message\",\"channel\":\"C04U0F9CW\",\"text\":\"TestingMessage\",\"id\":757}";
char b[180];
char c[180];
int i;
// deflate
// zlib struct
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array
//deflateInit(&defstream, Z_SYNC_FLUSH);
deflateInit2(&defstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
deflate(&defstream, Z_SYNC_FLUSH);
deflateEnd(&defstream);
for(i=0;i<=(char*)defstream.next_out - b;i++)
{
printf("%X",(Bytef)b[i]);
}
printf("\n%s\n",(Bytef *)b);
// This is one way of getting the size of the output
printf("Deflated size is: %lu\n", (char*)defstream.next_out - b);
我的输出是
kml@kml-VirtualBox:~/zlib-1.2.11/test$ gcc test.c -lz && ./a.out
789CAA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD18A391B98841AB8593A873454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B5C0000FFFF2E
x��V*�,HU�R�M-.NLOU�QJ�H��K��9���Y:�EKR+J�B!��%�y��pՙ)JV����
Deflated size is: 72
Inflate:
73
{"type":"message","channel":"C04U0F9CW","text":"TestingMessage","id":757}
所以我希望有效载荷会像这样
AA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD018A391B98841AB8593A8703454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B50000
This is the fiddler WebSocket traffic capture for that particular message
请帮助我获得正确的每条消息压缩编码。
Here is some more upgrade information
GET https://127.0.0.1/websoc.html HTTP/1.1
Host: mpmulti-yklu.slack-msgs.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: FriWg0AWhiLhLXXnfJK8kA==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
回应
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: S2Q+O/tRM0/mIG//Er2hDdqD0eY=
Sec-Websocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover
X-Via: haproxy-ext90
代码是正确的,但 defstream.avail_in 需要准确的长度,而不是额外的空字符。如果您提供准确的输入长度,它将正常工作。像这样尝试
int l = (uInt)strlen(a);
defstream.avail_in = l; // size of input, string + terminator
请尝试理解 z_stream 是如何赋值的检查代码然后你会更好地理解这一点。
我想很多人都在尝试解码,但他们没有得到 per-message-deflate 的正确值。
检查你的输出
"789CAA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD18A391B98841AB8593A873454B522B4A804221A9C5259979E9BE70D599294A56E6A6E6B5C0000FFFF2E"
这里 789C 是 header 和 0000FFFF 或者根据你的代码 (00FFFF) 是 deflate 消息的尾部。这里 0 代表 00,因为您的输出是打印一个“0”而不是“00”。因为多了一个尺码,你多了 5C。当您尝试以下代码时,您的“2E”也将被删除
int d = memcmp(msgend, &b[defstream.total_out - 4], 4) ? 0 : 4;
for(i=0;i<defstream.total_out - d;i++)
{
printf("%2X",(Bytef)b[i]);
}
所以你的预期答案是
AA562AA92C4855B252CA4D2D2E4E4C4F55D2514ACE48CCCB4BCD018A391B98841AB8593A8703454B522B4A804221A9C5259979E9BE70D599294A56B=E16A6E6[][=]
根据您的输出,2 个字节为 0,因此根据您的代码,它将为“00”,并且您的输出遵循打印每个半字节(半字节)。 请试试这个对我有用,我现在可以正确解码了。