WebSocket.xs

filter: Statementstmt 73.3% 209 / 285 Branchbran 56.9% 115 / 202 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.5% 324 / 487 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« vendor/bq_websocket/bq_websocket.c ↑ Summary blib/lib/Unblock/WebSocket.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include <errno.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9  
10 #include "include/unblock_websocket_native.h"
11  
12 #if defined(_WIN32)
13 # define WIN32_LEAN_AND_MEAN
14 # define NOMINMAX
15 # include <windows.h>
16 # include <bcrypt.h>
17 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
18 || defined(__NetBSD__) || defined(__DragonFly__)
19 # include <stdlib.h>
20 #elif defined(__linux__)
21 # include <sys/random.h>
22 #else
23 # include <fcntl.h>
24 # include <unistd.h>
25 #endif
26  
27 static int
28 17 unblock_ws_secure_random(void *buffer, size_t length)
29 {
30 #if defined(_WIN32)
31 unsigned char *out = (unsigned char *)buffer;
32 while (length > 0) {
33 ULONG chunk = length > 0xffffffffUL ? 0xffffffffUL : (ULONG)length;
34 NTSTATUS status = BCryptGenRandom(
35 NULL, (PUCHAR)out, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG
36 );
37 if (status != 0)
38 return 0;
39 out += chunk;
40 length -= chunk;
41 }
42 return 1;
43 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
44 || defined(__NetBSD__) || defined(__DragonFly__)
45 arc4random_buf(buffer, length);
46 return 1;
47 #elif defined(__linux__)
48 17 unsigned char *out = (unsigned char *)buffer;
49 34 ▶ while (length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (length > 0) { 17 17 34
50 17 ssize_t n = getrandom(out, length, 0);
51 17 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 17 17
52 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
53 0 continue;
54 0 return 0;
55 }
56 17 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 17 17
57 0 return 0;
58 17 out += (size_t)n;
59 17 length -= (size_t)n;
60 }
61 17 return 1;
62 #else
63 int fd;
64 unsigned char *out = (unsigned char *)buffer;
65  
66 do {
67 fd = open("/dev/urandom", O_RDONLY);
68 } while (fd < 0 && errno == EINTR);
69 if (fd < 0)
70 return 0;
71  
72 while (length > 0) {
73 ssize_t n = read(fd, out, length);
74 if (n < 0) {
75 if (errno == EINTR)
76 continue;
77 close(fd);
78 return 0;
79 }
80 if (n == 0) {
81 close(fd);
82 return 0;
83 }
84 out += (size_t)n;
85 length -= (size_t)n;
86 }
87 close(fd);
88 return 1;
89 #endif
90 }
91  
92 #define BQWS_SINGLE_THREAD 1
93 #define BQWS_DEBUG 0
94 #define bqws_mutex int
95 #define bqws_mutex_init(m) ((void)(m))
96 #define bqws_mutex_free(m) ((void)(m))
97 #define bqws_mutex_lock(m) ((void)(m))
98 #define bqws_mutex_unlock(m) ((void)(m))
99 #define bqws_assert_locked(m) ((void)(m))
100 #include "vendor/bq_websocket/bq_websocket.c"
101  
102 typedef struct {
103 bqws_socket *ws;
104 } unblock_ws_native;
105  
106 static unblock_ws_native *
107 51 unblock_ws_native_create(uint32_t role, size_t max_message_size)
108 {
109 unblock_ws_native *state;
110 bqws_opts opts;
111  
112 51 ▶ if (role != UNBLOCK_WEBSOCKET_ROLE_CLIENT
Branch: 1 branch
BranchTrueFalseTotal
if (role != UNBLOCK_WEBSOCKET_ROLE_CLIENT 41 10 51
113 41 ▶ && role != UNBLOCK_WEBSOCKET_ROLE_SERVER)
Branch: 1 branch
BranchTrueFalseTotal
&& role != UNBLOCK_WEBSOCKET_ROLE_SERVER) 0 41 41
114 0 return NULL;
115 51 ▶ if (max_message_size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (max_message_size == 0) 0 51 51
116 0 return NULL;
117  
118 51 state = (unblock_ws_native *)calloc(1, sizeof(*state));
119 51 ▶ if (state == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) 0 51 51
120 0 return NULL;
121  
122 51 memset(&opts, 0, sizeof(opts));
123 51 opts.skip_handshake = true;
124 51 opts.recv_control_messages = true;
125 51 opts.ping_interval = SIZE_MAX;
126 51 opts.connect_timeout = SIZE_MAX;
127 51 opts.close_timeout = SIZE_MAX;
128 51 opts.ping_response_timeout = SIZE_MAX;
129 51 opts.limits.max_memory_used = SIZE_MAX;
130 51 opts.limits.max_recv_msg_size = max_message_size;
131 51 opts.limits.max_recv_queue_messages = SIZE_MAX;
132 51 opts.limits.max_recv_queue_size = SIZE_MAX;
133 51 opts.limits.max_partial_message_parts = SIZE_MAX;
134  
135 51 ▶ if (role == UNBLOCK_WEBSOCKET_ROLE_CLIENT)
Branch: 1 branch
BranchTrueFalseTotal
if (role == UNBLOCK_WEBSOCKET_ROLE_CLIENT) 10 41 51
136 10 state->ws = bqws_new_client(&opts, NULL);
137 else
138 41 state->ws = bqws_new_server(&opts, NULL);
139  
140 51 ▶ if (state->ws == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->ws == NULL) { 0 51 51
141 0 free(state);
142 0 return NULL;
143 }
144  
145 51 return state;
146 }
147  
148 static void
149 51 unblock_ws_native_destroy(void *opaque)
150 {
151 51 unblock_ws_native *state = (unblock_ws_native *)opaque;
152 51 ▶ if (state == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) 0 51 51
153 0 return;
154 51 ▶ if (state->ws != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->ws != NULL) { 51 0 51
155 51 bqws_free_socket(state->ws);
156 51 state->ws = NULL;
157 }
158 51 free(state);
159 }
160  
161 static uint32_t
162 48 unblock_ws_event_code(bqws_msg_type type)
163 {
164 48 switch (type) {
165 20 case BQWS_MSG_TEXT: return UNBLOCK_WEBSOCKET_EVENT_TEXT;
166 7 case BQWS_MSG_BINARY: return UNBLOCK_WEBSOCKET_EVENT_BINARY;
167 11 case BQWS_MSG_CONTROL_CLOSE: return UNBLOCK_WEBSOCKET_EVENT_CLOSE;
168 8 case BQWS_MSG_CONTROL_PING: return UNBLOCK_WEBSOCKET_EVENT_PING;
169 2 case BQWS_MSG_CONTROL_PONG: return UNBLOCK_WEBSOCKET_EVENT_PONG;
170 0 default: return 0;
171 }
172 }
173  
174 typedef struct {
175 unblock_websocket_event_fn fn;
176 void *user;
177 } unblock_ws_legacy_event;
178  
179 static int
180 0 unblock_ws_legacy_event_adapter(
181 void *user,
182 uint32_t event,
183 uint32_t flags,
184 const char *data,
185 size_t length
186 )
187 {
188 0 unblock_ws_legacy_event *legacy = (unblock_ws_legacy_event *)user;
189  
190 0 ▶ if (flags != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (flags != 0) 0 0 0
191 0 return UNBLOCK_WEBSOCKET_ERROR;
192  
193 0 ▶ if (legacy->fn == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (legacy->fn == NULL) 0 0 0
194 0 return UNBLOCK_WEBSOCKET_OK;
195  
196 0 return legacy->fn(legacy->user, event, data, length);
197 }
198  
199 static int
200 60 unblock_ws_native_input_ex(
201 void *opaque,
202 const char *data,
203 size_t length,
204 size_t *consumed,
205 unblock_websocket_event_ex_fn event_fn,
206 void *event_user
207 )
208 {
209 60 unblock_ws_native *state = (unblock_ws_native *)opaque;
210 60 size_t used = 0;
211 bqws_msg *msg;
212  
213 60 ▶ if (consumed == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (consumed == NULL) 0 60 60
214 0 return UNBLOCK_WEBSOCKET_ERROR;
215 60 *consumed = 0;
216  
217 60 ▶ if (state == NULL || state->ws == NULL || (length > 0 && data == NULL))
Branch: 4 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 60 0 60
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 60 0 60
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 60 0 60
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 0 60 60
218 0 return UNBLOCK_WEBSOCKET_ERROR;
219  
220 90 ▶ while (used < length) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < length) { 60 30 90
221 60 size_t n = bqws_read_from(state->ws, data + used, length - used);
222 60 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 60 60
223 0 break;
224 60 used += n;
225 60 ▶ if (bqws_get_error(state->ws) != BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(state->ws) != BQWS_OK 41 19 60
226 41 ▶ || bqws_get_state(state->ws) >= BQWS_STATE_CLOSING)
Branch: 1 branch
BranchTrueFalseTotal
|| bqws_get_state(state->ws) >= BQWS_STATE_CLOSING) 30 11 41
227 break;
228 }
229 60 *consumed = used;
230  
231 108 ▶ while ((msg = bqws_recv(state->ws)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((msg = bqws_recv(state->ws)) != NULL) { 48 60 108
232 48 uint32_t event = unblock_ws_event_code(msg->type);
233 96 uint32_t flags = (msg_imp(msg))->compressed
234 48 ▶ ? UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED : 0U;
Branch: 1 branch
BranchTrueFalseTotal
? UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED : 0U; 11 37 48
235 48 int callback_result = UNBLOCK_WEBSOCKET_OK;
236  
237 48 ▶ if (event == UNBLOCK_WEBSOCKET_EVENT_TEXT
Branch: 1 branch
BranchTrueFalseTotal
if (event == UNBLOCK_WEBSOCKET_EVENT_TEXT 20 28 48
238 20 ▶ && !(flags & UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED)
Branch: 1 branch
BranchTrueFalseTotal
&& !(flags & UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED) 13 7 20
239 13 ▶ && !unblock_bqws_valid_utf8(
Branch: 1 branch
BranchTrueFalseTotal
&& !unblock_bqws_valid_utf8( 0 13 13
240 13 (const uint8_t *)msg->data, msg->size)) {
241 0 bqws_free_msg(msg);
242 0 bqws_direct_fail(state->ws, BQWS_ERR_BAD_UTF8);
243 0 break;
244 }
245  
246 48 ▶ if (event == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == 0) { 0 48 48
247 0 bqws_free_msg(msg);
248 0 bqws_direct_fail(state->ws, BQWS_ERR_BAD_OPCODE);
249 0 break;
250 }
251  
252 48 ▶ if (event_fn != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (event_fn != NULL) 48 0 48
253 48 callback_result = event_fn(
254 48 event_user, event, flags, msg->data, msg->size);
255  
256 48 bqws_free_msg(msg);
257  
258 48 ▶ if (callback_result != UNBLOCK_WEBSOCKET_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (callback_result != UNBLOCK_WEBSOCKET_OK) 0 48 48
259 0 return callback_result;
260 }
261  
262 60 ▶ if (bqws_get_error(state->ws) != BQWS_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(state->ws) != BQWS_OK) 19 41 60
263 19 return UNBLOCK_WEBSOCKET_ERROR;
264  
265 41 ▶ if (used != length && bqws_get_state(state->ws) < BQWS_STATE_CLOSING)
Branch: 2 branches
BranchTrueFalseTotal
if (used != length && bqws_get_state(state->ws) < BQWS_STATE_CLOSING) 0 41 41
if (used != length && bqws_get_state(state->ws) < BQWS_STATE_CLOSING) 0 0 0
266 0 return UNBLOCK_WEBSOCKET_ERROR;
267  
268 41 return UNBLOCK_WEBSOCKET_OK;
269 }
270  
271 static int
272 0 unblock_ws_native_input(
273 void *opaque,
274 const char *data,
275 size_t length,
276 size_t *consumed,
277 unblock_websocket_event_fn event_fn,
278 void *event_user
279 )
280 {
281 unblock_ws_legacy_event legacy;
282 0 legacy.fn = event_fn;
283 0 legacy.user = event_user;
284  
285 0 return unblock_ws_native_input_ex(
286 opaque,
287 data,
288 length,
289 consumed,
290 unblock_ws_legacy_event_adapter,
291 &legacy
292 );
293 }
294  
295 static int
296 17 unblock_ws_native_send_ex(
297 void *opaque,
298 uint32_t opcode,
299 uint32_t flags,
300 const char *data,
301 size_t length
302 )
303 {
304 17 unblock_ws_native *state = (unblock_ws_native *)opaque;
305 17 bool compressed =
306 17 (flags & UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED) != 0;
307  
308 17 ▶ if (state == NULL || state->ws == NULL || (length > 0 && data == NULL))
Branch: 4 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 17 0 17
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 17 0 17
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 17 0 17
if (state == NULL || state->ws == NULL || (length > 0 && data == NULL)) 0 17 17
309 0 return UNBLOCK_WEBSOCKET_ERROR;
310 17 ▶ if (flags & ~UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & ~UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED) 0 17 17
311 0 return UNBLOCK_WEBSOCKET_ERROR;
312  
313 17 ▶ if (opcode == UNBLOCK_WEBSOCKET_EVENT_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (opcode == UNBLOCK_WEBSOCKET_EVENT_TEXT) { 10 7 17
314 10 ▶ if (!compressed
Branch: 1 branch
BranchTrueFalseTotal
if (!compressed 8 2 10
315 8 ▶ && !unblock_bqws_valid_utf8((const uint8_t *)data, length))
Branch: 1 branch
BranchTrueFalseTotal
&& !unblock_bqws_valid_utf8((const uint8_t *)data, length)) 0 8 8
316 0 return UNBLOCK_WEBSOCKET_ERROR;
317 10 unblock_bqws_send_data(
318 state->ws, BQWS_MSG_TEXT, data, length, compressed);
319 }
320 7 ▶ else if (opcode == UNBLOCK_WEBSOCKET_EVENT_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (opcode == UNBLOCK_WEBSOCKET_EVENT_BINARY) { 5 2 7
321 5 unblock_bqws_send_data(
322 state->ws, BQWS_MSG_BINARY, data, length, compressed);
323 }
324 2 ▶ else if (opcode == UNBLOCK_WEBSOCKET_EVENT_PING) {
Branch: 1 branch
BranchTrueFalseTotal
else if (opcode == UNBLOCK_WEBSOCKET_EVENT_PING) { 2 0 2
325 2 ▶ if (compressed || length > 125)
Branch: 2 branches
BranchTrueFalseTotal
if (compressed || length > 125) 2 0 2
if (compressed || length > 125) 0 2 2
326 0 return UNBLOCK_WEBSOCKET_ERROR;
327 2 bqws_send_ping(state->ws, data, length);
328 }
329 0 ▶ else if (opcode == UNBLOCK_WEBSOCKET_EVENT_PONG) {
Branch: 1 branch
BranchTrueFalseTotal
else if (opcode == UNBLOCK_WEBSOCKET_EVENT_PONG) { 0 0 0
330 0 ▶ if (compressed || length > 125)
Branch: 2 branches
BranchTrueFalseTotal
if (compressed || length > 125) 0 0 0
if (compressed || length > 125) 0 0 0
331 0 return UNBLOCK_WEBSOCKET_ERROR;
332 0 bqws_send_pong(state->ws, data, length);
333 }
334 else {
335 0 return UNBLOCK_WEBSOCKET_ERROR;
336 }
337  
338 17 return bqws_get_error(state->ws) == BQWS_OK
339 17 ▶ ? UNBLOCK_WEBSOCKET_OK : UNBLOCK_WEBSOCKET_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
? UNBLOCK_WEBSOCKET_OK : UNBLOCK_WEBSOCKET_ERROR; 17 0 17
340 }
341  
342 static int
343 0 unblock_ws_native_send(
344 void *opaque,
345 uint32_t opcode,
346 const char *data,
347 size_t length
348 )
349 {
350 0 return unblock_ws_native_send_ex(
351 opaque, opcode, 0U, data, length);
352 }
353  
354 static int
355 15 unblock_ws_native_configure(
356 void *opaque,
357 uint32_t features,
358 size_t max_wire_message_size
359 )
360 {
361 15 unblock_ws_native *state = (unblock_ws_native *)opaque;
362 bqws_limits limits;
363  
364 15 ▶ if (state == NULL || state->ws == NULL || max_wire_message_size == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL || max_wire_message_size == 0) 15 0 15
if (state == NULL || state->ws == NULL || max_wire_message_size == 0) 15 0 15
if (state == NULL || state->ws == NULL || max_wire_message_size == 0) 0 15 15
365 0 return UNBLOCK_WEBSOCKET_ERROR;
366 15 ▶ if (features & ~UNBLOCK_WEBSOCKET_FEATURE_PERMESSAGE_DEFLATE)
Branch: 1 branch
BranchTrueFalseTotal
if (features & ~UNBLOCK_WEBSOCKET_FEATURE_PERMESSAGE_DEFLATE) 0 15 15
367 0 return UNBLOCK_WEBSOCKET_ERROR;
368  
369 15 limits = bqws_get_limits(state->ws);
370 15 limits.max_recv_msg_size = max_wire_message_size;
371 15 bqws_set_limits(state->ws, &limits);
372  
373 15 unblock_bqws_set_allow_rsv1(
374 state->ws,
375 15 (features & UNBLOCK_WEBSOCKET_FEATURE_PERMESSAGE_DEFLATE) != 0
376 );
377  
378 15 return UNBLOCK_WEBSOCKET_OK;
379 }
380  
381 static int
382 10 unblock_ws_native_close(
383 void *opaque,
384 uint32_t status_code,
385 const char *reason,
386 size_t reason_length
387 )
388 {
389 10 unblock_ws_native *state = (unblock_ws_native *)opaque;
390  
391 10 ▶ if (state == NULL || state->ws == NULL
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL 10 0 10
if (state == NULL || state->ws == NULL 10 0 10
392 10 ▶ || (reason_length > 0 && reason == NULL))
Branch: 2 branches
BranchTrueFalseTotal
|| (reason_length > 0 && reason == NULL)) 5 5 10
|| (reason_length > 0 && reason == NULL)) 0 5 5
393 0 return UNBLOCK_WEBSOCKET_ERROR;
394 10 ▶ if (!unblock_bqws_valid_close_code((uint16_t)status_code)
Branch: 1 branch
BranchTrueFalseTotal
if (!unblock_bqws_valid_close_code((uint16_t)status_code) 10 0 10
395 10 ▶ || status_code > 0xffffU)
Branch: 1 branch
BranchTrueFalseTotal
|| status_code > 0xffffU) 0 10 10
396 0 return UNBLOCK_WEBSOCKET_ERROR;
397 10 ▶ if (reason_length > 123)
Branch: 1 branch
BranchTrueFalseTotal
if (reason_length > 123) 0 10 10
398 0 return UNBLOCK_WEBSOCKET_ERROR;
399 10 ▶ if (!unblock_bqws_valid_utf8((const uint8_t *)reason, reason_length))
Branch: 1 branch
BranchTrueFalseTotal
if (!unblock_bqws_valid_utf8((const uint8_t *)reason, reason_length)) 0 10 10
400 0 return UNBLOCK_WEBSOCKET_ERROR;
401  
402 10 bqws_close(
403 state->ws,
404 (bqws_close_reason)status_code,
405 reason,
406 reason_length
407 );
408  
409 10 return bqws_get_error(state->ws) == BQWS_OK
410 10 ▶ ? UNBLOCK_WEBSOCKET_OK : UNBLOCK_WEBSOCKET_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
? UNBLOCK_WEBSOCKET_OK : UNBLOCK_WEBSOCKET_ERROR; 10 0 10
411 }
412  
413 static size_t
414 77 unblock_ws_native_output(void *opaque, char *buffer, size_t capacity)
415 {
416 77 unblock_ws_native *state = (unblock_ws_native *)opaque;
417 77 ▶ if (state == NULL || state->ws == NULL || (capacity > 0 && buffer == NULL))
Branch: 4 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL || (capacity > 0 && buffer == NULL)) 77 0 77
if (state == NULL || state->ws == NULL || (capacity > 0 && buffer == NULL)) 77 0 77
if (state == NULL || state->ws == NULL || (capacity > 0 && buffer == NULL)) 77 0 77
if (state == NULL || state->ws == NULL || (capacity > 0 && buffer == NULL)) 0 77 77
418 0 return 0;
419 77 return bqws_write_to(state->ws, buffer, capacity);
420 }
421  
422 static uint32_t
423 84 unblock_ws_native_error_code(void *opaque)
424 {
425 84 unblock_ws_native *state = (unblock_ws_native *)opaque;
426 84 ▶ if (state == NULL || state->ws == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL) 84 0 84
if (state == NULL || state->ws == NULL) 0 84 84
427 0 return (uint32_t)BQWS_ERR_UNKNOWN;
428 84 return (uint32_t)bqws_get_error(state->ws);
429 }
430  
431 static const char *
432 15 unblock_ws_native_error_string(void *opaque)
433 {
434 15 return bqws_error_str((bqws_error)unblock_ws_native_error_code(opaque));
435 }
436  
437 static size_t
438 7 unblock_ws_native_memory_used(void *opaque)
439 {
440 7 unblock_ws_native *state = (unblock_ws_native *)opaque;
441 7 ▶ if (state == NULL || state->ws == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL) 7 0 7
if (state == NULL || state->ws == NULL) 0 7 7
442 0 return 0;
443 7 return bqws_get_memory_used(state->ws);
444 }
445  
446 static void *
447 0 unblock_ws_native_abi_create(uint32_t role, size_t max_message_size)
448 {
449 0 return (void *)unblock_ws_native_create(role, max_message_size);
450 }
451  
452 static const unblock_websocket_native_ops_v1_t unblock_ws_native_ops = {
453 UNBLOCK_WEBSOCKET_NATIVE_ABI_VERSION,
454 sizeof(unblock_websocket_native_ops_v1_t),
455 "Unblock::WebSocket native protocol engine",
456 unblock_ws_native_abi_create,
457 unblock_ws_native_destroy,
458 unblock_ws_native_input,
459 unblock_ws_native_send,
460 unblock_ws_native_close,
461 unblock_ws_native_output,
462 unblock_ws_native_error_code,
463 unblock_ws_native_error_string,
464 unblock_ws_native_memory_used,
465 unblock_ws_native_configure,
466 unblock_ws_native_input_ex,
467 unblock_ws_native_send_ex
468 };
469  
470 static unblock_ws_native *
471 251 unblock_ws_from_sv(SV *self)
472 {
473 unblock_ws_native *state;
474  
475 251 ▶ if (!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) 0 251 251
476 0 croak("invalid Unblock::WebSocket::_Native object");
477  
478 251 state = INT2PTR(unblock_ws_native *, SvIV((SV *)SvRV(self)));
479 251 ▶ if (state == NULL || state->ws == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL) 251 0 251
if (state == NULL || state->ws == NULL) 0 251 251
480 0 croak("invalid Unblock::WebSocket::_Native state");
481  
482 251 return state;
483 }
484  
485 static SV *
486 51 unblock_ws_new_object(const char *class, const char *role, UV max_message_size)
487 {
488 uint32_t native_role;
489 unblock_ws_native *state;
490 SV *object;
491  
492 51 ▶ if (strEQ(role, "client"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(role, "client")) 10 41 51
493 10 native_role = UNBLOCK_WEBSOCKET_ROLE_CLIENT;
494 41 ▶ else if (strEQ(role, "server"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(role, "server")) 41 0 41
495 41 native_role = UNBLOCK_WEBSOCKET_ROLE_SERVER;
496 else
497 0 return NULL;
498  
499 51 state = unblock_ws_native_create(native_role, (size_t)max_message_size);
500 51 ▶ if (state == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) 0 51 51
501 0 return NULL;
502  
503 51 object = newSV(0);
504 51 sv_setref_pv(object, class, (void *)state);
505 51 return object;
506 }
507  
508 static SV *
509 77 unblock_ws_flush_sv(unblock_ws_native *state)
510 {
511 77 SV *out = newSVpvn("", 0);
512 char buffer[65536];
513  
514 0 for (;;) {
515 77 size_t n = unblock_ws_native_output(state, buffer, sizeof(buffer));
516 77 ▶ if (n > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) 55 22 77
517 55 sv_catpvn(out, buffer, n);
518 77 ▶ if (n < sizeof(buffer))
Branch: 1 branch
BranchTrueFalseTotal
if (n < sizeof(buffer)) 77 0 77
519 77 break;
520 }
521 77 return out;
522 }
523  
524 typedef struct {
525 AV *events;
526 } unblock_ws_perl_events;
527  
528 static int
529 48 unblock_ws_collect_event(
530 void *user,
531 uint32_t event,
532 uint32_t flags,
533 const char *data,
534 size_t length
535 )
536 {
537 48 unblock_ws_perl_events *collector = (unblock_ws_perl_events *)user;
538 48 AV *pair = newAV();
539 48 ▶ SV *payload = newSVpvn(data == NULL ? "" : data, (STRLEN)length);
Branch: 1 branch
BranchTrueFalseTotal
SV *payload = newSVpvn(data == NULL ? "" : data, (STRLEN)length); 48 0 48
540  
541 48 av_push(pair, newSVuv((UV)event));
542 48 av_push(pair, payload);
543 48 av_push(pair, newSVuv((UV)flags));
544 48 av_push(collector->events, newRV_noinc((SV *)pair));
545 48 return UNBLOCK_WEBSOCKET_OK;
546 }
547  
548 MODULE = Unblock::WebSocket PACKAGE = Unblock::WebSocket::_Native
549  
550 PROTOTYPES: DISABLE
551  
552 SV *
553 new(class, role, max_message_size, allow_permessage_deflate = 0)
554 const char *class
555 const char *role
556 UV max_message_size
557 IV allow_permessage_deflate
558 PREINIT:
559 unblock_ws_native *state;
560 CODE:
561 51 RETVAL = unblock_ws_new_object(class, role, max_message_size);
562 51 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 0 51 51
563 0 croak("native WebSocket context initialization failed");
564  
565 51 ▶ if (allow_permessage_deflate) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_permessage_deflate) { 15 36 51
566 15 state = unblock_ws_from_sv(RETVAL);
567 15 ▶ if (unblock_ws_native_configure(
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_ws_native_configure( 0 15 15
568 state,
569 UNBLOCK_WEBSOCKET_FEATURE_PERMESSAGE_DEFLATE,
570 (size_t)max_message_size)
571 != UNBLOCK_WEBSOCKET_OK) {
572 0 SvREFCNT_dec(RETVAL);
573 0 croak("native WebSocket compression configuration failed");
574 }
575 }
576 OUTPUT:
577 RETVAL
578  
579 SV *
580 feed(self, bytes)
581 SV *self
582 SV *bytes
583 PREINIT:
584 unblock_ws_native *state;
585 STRLEN length;
586 const char *data;
587 size_t consumed;
588 int result;
589 AV *events;
590 unblock_ws_perl_events collector;
591 CODE:
592 60 state = unblock_ws_from_sv(self);
593 60 data = SvPVbyte(bytes, length);
594 60 events = newAV();
595 60 collector.events = events;
596 60 result = unblock_ws_native_input_ex(
597 state,
598 data,
599 (size_t)length,
600 &consumed,
601 unblock_ws_collect_event,
602 &collector
603 );
604 60 ▶ if (result != UNBLOCK_WEBSOCKET_OK
Branch: 1 branch
BranchTrueFalseTotal
if (result != UNBLOCK_WEBSOCKET_OK 19 41 60
605 19 ▶ && unblock_ws_native_error_code(state) == (uint32_t)BQWS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
&& unblock_ws_native_error_code(state) == (uint32_t)BQWS_OK) { 0 19 19
606 0 SvREFCNT_dec((SV *)events);
607 0 croak("native WebSocket input failed without protocol error");
608 }
609 60 ▶ if (consumed != (size_t)length
Branch: 1 branch
BranchTrueFalseTotal
if (consumed != (size_t)length 0 60 60
610 0 ▶ && unblock_ws_native_error_code(state) == (uint32_t)BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
&& unblock_ws_native_error_code(state) == (uint32_t)BQWS_OK 0 0 0
611 0 ▶ && bqws_get_state(state->ws) < BQWS_STATE_CLOSING) {
Branch: 1 branch
BranchTrueFalseTotal
&& bqws_get_state(state->ws) < BQWS_STATE_CLOSING) { 0 0 0
612 0 SvREFCNT_dec((SV *)events);
613 0 croak("native WebSocket consumed only %lu of %lu input bytes",
614 (unsigned long)consumed, (unsigned long)length);
615 }
616 60 RETVAL = newRV_noinc((SV *)events);
617 OUTPUT:
618 RETVAL
619  
620 void
621 queue_message(self, opcode, bytes, compressed = 0)
622 SV *self
623 UV opcode
624 SV *bytes
625 IV compressed
626 PREINIT:
627 unblock_ws_native *state;
628 STRLEN length;
629 const char *data;
630 uint32_t flags;
631 CODE:
632 17 state = unblock_ws_from_sv(self);
633 17 ▶ if (opcode == UNBLOCK_WEBSOCKET_EVENT_TEXT
Branch: 1 branch
BranchTrueFalseTotal
if (opcode == UNBLOCK_WEBSOCKET_EVENT_TEXT 10 7 17
634 10 ▶ && !compressed
Branch: 1 branch
BranchTrueFalseTotal
&& !compressed 8 2 10
635 8 ▶ && SvUTF8(bytes))
Branch: 1 branch
BranchTrueFalseTotal
&& SvUTF8(bytes)) 0 8 8
636 0 data = SvPVutf8(bytes, length);
637 else
638 17 data = SvPVbyte(bytes, length);
639  
640 17 ▶ flags = compressed ? UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED : 0U;
Branch: 1 branch
BranchTrueFalseTotal
flags = compressed ? UNBLOCK_WEBSOCKET_MESSAGE_COMPRESSED : 0U; 4 13 17
641 17 ▶ if (unblock_ws_native_send_ex(
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_ws_native_send_ex( 0 17 17
642 state,
643 (uint32_t)opcode,
644 flags,
645 data,
646 (size_t)length)
647 != UNBLOCK_WEBSOCKET_OK)
648 0 croak("native WebSocket send failed");
649  
650 void
651 queue_close(self, status_code, reason)
652 SV *self
653 UV status_code
654 SV *reason
655 PREINIT:
656 unblock_ws_native *state;
657 STRLEN length;
658 const char *data;
659 CODE:
660 10 state = unblock_ws_from_sv(self);
661 10 ▶ if (SvUTF8(reason))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(reason)) 0 10 10
662 0 data = SvPVutf8(reason, length);
663 else
664 10 data = SvPVbyte(reason, length);
665 10 ▶ if (unblock_ws_native_close(
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_ws_native_close( 0 10 10
666 state, (uint32_t)status_code, data, (size_t)length)
667 != UNBLOCK_WEBSOCKET_OK)
668 0 croak("native WebSocket close failed");
669  
670 SV *
671 flush(self)
672 SV *self
673 PREINIT:
674 unblock_ws_native *state;
675 CODE:
676 77 state = unblock_ws_from_sv(self);
677 77 RETVAL = unblock_ws_flush_sv(state);
678 OUTPUT:
679 RETVAL
680  
681 UV
682 _memory_used(self)
683 SV *self
684 PREINIT:
685 unblock_ws_native *state;
686 CODE:
687 7 state = unblock_ws_from_sv(self);
688 7 RETVAL = (UV)unblock_ws_native_memory_used(state);
689 OUTPUT:
690 RETVAL
691  
692 UV
693 _error_code(self)
694 SV *self
695 PREINIT:
696 unblock_ws_native *state;
697 CODE:
698 50 state = unblock_ws_from_sv(self);
699 50 ▶ RETVAL = (UV)unblock_ws_native_error_code(state);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)unblock_ws_native_error_code(state); 33 17 50
700 OUTPUT:
701 RETVAL
702  
703 SV *
704 _error_string(self)
705 SV *self
706 PREINIT:
707 unblock_ws_native *state;
708 CODE:
709 15 state = unblock_ws_from_sv(self);
710 15 RETVAL = newSVpv(unblock_ws_native_error_string(state), 0);
711 OUTPUT:
712 RETVAL
713  
714 UV
715 _operations_address(class)
716 const char *class
717 CODE:
718 PERL_UNUSED_ARG(class);
719 1 ▶ RETVAL = PTR2UV(&unblock_ws_native_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&unblock_ws_native_ops); 0 1 1
720 OUTPUT:
721 RETVAL
722  
723 SV *
724 _random_bytes(class, length)
725 const char *class
726 UV length
727 PREINIT:
728 SV *out;
729 char *buffer;
730 CODE:
731 PERL_UNUSED_ARG(class);
732 4 ▶ if (length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length == 0) 0 4 4
733 0 croak("random byte length must be positive");
734 4 out = newSV((STRLEN)length + 1);
735 4 SvPOK_on(out);
736 4 SvCUR_set(out, (STRLEN)length);
737 4 buffer = SvPVX(out);
738 4 ▶ if (!unblock_ws_secure_random(buffer, (size_t)length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unblock_ws_secure_random(buffer, (size_t)length)) { 0 4 4
739 0 SvREFCNT_dec(out);
740 0 croak("secure random byte generation failed");
741 }
742 4 buffer[length] = '\0';
743 4 RETVAL = out;
744 OUTPUT:
745 RETVAL
746  
747 void
748 DESTROY(self)
749 SV *self
750 PREINIT:
751 unblock_ws_native *state;
752 CODE:
753 51 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 51 0 51
754 51 state = INT2PTR(unblock_ws_native *, SvIV((SV *)SvRV(self)));
755 51 ▶ if (state != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state != NULL) { 51 0 51
756 51 unblock_ws_native_destroy(state);
757 51 sv_setiv((SV *)SvRV(self), 0);
758 }
759 }
« vendor/bq_websocket/bq_websocket.c ↑ Summary blib/lib/Unblock/WebSocket.pm »