Skip to content

Commit 93ffa67

Browse files
committed
Merge pull request #60 from artygus/master
Fixes WebSocket frame length too large error
2 parents a34e003 + 56b6a43 commit 93ffa67

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/websocket.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
#include "validate_utf8.h"
1818

19+
#ifndef htobe64
20+
#ifdef __APPLE__
21+
#include <libkern/OSByteOrder.h>
22+
#define htobe64(h) OSSwapHostToBigInt64(h)
23+
#elif _MSC_VER
24+
#define htobe64(h) _byteswap_uint64(h)
25+
#endif
26+
#endif
27+
1928
typedef int8_t ws_state;
2029
#define STATE_ERROR 1
2130
#define STATE_READ_HTTP_REQUEST 2
@@ -304,9 +313,18 @@ ws_status ws_send_frame(ws_t self,
304313
*out_tail++ = ((is_masking ? 0x80 : 0) | (!payload_n ? payload_length :
305314
payload_n == 2 ? 126: 127));
306315

307-
int8_t j;
308-
for (j = payload_n - 1; j >= 0; j--) {
309-
*out_tail++ = (unsigned char)((payload_length >> (j<<3)) & 0xFF);
316+
if (payload_n != 0) {
317+
if (payload_n == 2) {
318+
uint16_t sz16 = htons(payload_length);
319+
memcpy(out_tail, &sz16, payload_n);
320+
} else {
321+
uint64_t sz64 = htobe64(payload_length);
322+
memcpy(out_tail, &sz64, payload_n);
323+
}
324+
325+
int i;
326+
for (i = 0; i < payload_n; i++)
327+
*out_tail++;
310328
}
311329

312330
if (is_masking) {

0 commit comments

Comments
 (0)