Websockets.xs

filter: Statementstmt 78.1% 882 / 1129 Branchbran 51.9% 585 / 1126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.0% 1467 / 2255 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/Websockets.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7  
8 #include <libwebsockets.h>
9 #include <string.h>
10 #include <stddef.h>
11  
12 /* Magic numbers for use-after-free detection */
13 #define EV_WS_CTX_MAGIC 0xDEADBEEF
14 #define EV_WS_CTX_FREED 0xFEEDFACE
15 #define EV_WS_CONN_MAGIC 0xCAFEBABE
16 #define EV_WS_CONN_FREED 0xBADC0FFE
17  
18 /* Forward declarations */
19 typedef struct ev_ws_ctx_s ev_ws_ctx_t;
20 typedef struct ev_ws_conn_s ev_ws_conn_t;
21 typedef struct ev_ws_fd_s ev_ws_fd_t;
22  
23 #define EV_WS_SRV_MAGIC 0xFEEDCAFE
24 #define EV_WS_SRV_FREED 0xFEEDDEAD
25  
26 typedef struct ev_ws_server_s {
27 unsigned int magic;
28 SV* on_connect;
29 SV* on_message;
30 SV* on_close;
31 SV* on_error;
32 SV* on_pong;
33 SV* on_drain;
34 SV* on_handshake;
35 HV* response_headers; /* headers to inject into upgrade response */
36 size_t max_message_size;
37 char* protocol_name;
38 struct lws_protocols vhost_protocols[2];
39 } ev_ws_server_t;
40  
41 typedef ev_ws_ctx_t* EV__Websockets__Context;
42 typedef ev_ws_conn_t* EV__Websockets__Connection;
43 typedef struct ev_loop* EV__Loop;
44  
45 /* File descriptor watcher tracking */
46 struct ev_ws_fd_s {
47 ev_io io;
48 ev_ws_ctx_t* ctx;
49 int fd;
50 int poll_events; /* registered POLLIN/POLLOUT interest mask */
51 };
52  
53 /* Send buffer node for pending writes (FAM: data follows the struct) */
54 typedef struct ev_ws_send_s {
55 struct ev_ws_send_s* next;
56 size_t len;
57 enum lws_write_protocol write_mode;
58 char data[1]; /* C89-compatible flexible array; LWS_PRE + payload */
59 } ev_ws_send_t;
60  
61 /* Context structure - manages lws_context and connections */
62 struct ev_ws_ctx_s {
63 unsigned int magic;
64 int refcnt; /* lifecycle refcount: Perl + each in-flight lws_service */
65 int* alive_flag; /* points to caller's stack variable during lws_service */
66 struct ev_loop* loop;
67 struct lws_context* lws_ctx;
68 ev_ws_conn_t* connections;
69 ev_ws_conn_t* flush_head; /* conns with a buffered, fully-received message
70 awaiting delivery once the service call drains */
71 ev_ws_conn_t* flush_tail; /* append point, so delivery across connections
72 follows queue order; reset with flush_head */
73 ev_ws_fd_t** fd_table;
74 int fd_table_size;
75 ev_timer timer;
76 };
77  
78 /* Connection structure */
79 struct ev_ws_conn_s {
80 unsigned int magic;
81 ev_ws_ctx_t* ctx;
82 struct lws* wsi;
83 ev_ws_conn_t* next; /* linked list */
84 ev_ws_conn_t* prev;
85  
86 int refcnt;
87 SV* perl_self; /* non-owning ptr to the blessed RV's target; its existence
88 holds one refcnt (added in get_conn_sv, dropped in DESTROY) */
89  
90 /* Callbacks */
91 SV* on_connect;
92 SV* on_message;
93 SV* on_close;
94 SV* on_error;
95 SV* on_pong;
96 SV* on_drain;
97  
98 /* Custom Headers */
99 HV* custom_headers;
100  
101 /* Response Headers (client: response headers; server: request headers) */
102 HV* response_headers;
103  
104 /* Receive buffer for reassembling a message across callbacks */
105 char* recv_buf;
106 size_t recv_len;
107 size_t recv_alloc;
108 int recv_is_binary;
109 int recv_complete; /* buffered message is fully received (final frame) */
110 ev_ws_conn_t* flush_next; /* link in ctx->flush_head while pending delivery */
111 int on_flush; /* already queued on ctx->flush_head (dedup) */
112 size_t max_message_size;
113  
114 /* Send queue */
115 ev_ws_send_t* send_head;
116 ev_ws_send_t* send_tail;
117 size_t send_queue_bytes;
118  
119 /* Adopted file handle (prevents Perl from closing the fd) */
120 SV* adopted_fh;
121  
122 /* Connect timeout */
123 ev_timer connect_timer;
124 int connect_timer_active;
125 struct ev_loop* loop;
126  
127 /* Fragmented send state */
128 int in_fragmented_send;
129  
130 /* Per-connection metadata */
131 HV* stash;
132  
133 /* State */
134 int connected;
135 int closing;
136 };
137  
138 #define EV_WS_PROTOCOL_NAME "ev-websockets"
139  
140 /* Extensions support (compression) */
141 #ifdef LWS_HAS_EXTENSIONS
142 static const struct lws_extension extensions[] = {
143 {
144 "permessage-deflate",
145 lws_extension_callback_pm_deflate,
146 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
147 },
148 { NULL, NULL, NULL }
149 };
150 #endif
151  
152 static int ev_ws_debug = 0;
153  
154 /* Strict decimal port parse: 1..65535 with no trailing junk, else -1. atoi()
155 silently yielded 0 for input like "notaport", which surfaced later as an
156 obscure asynchronous connect failure instead of a clear error. */
157 23 static int parse_port_strict(const char* s) {
158 23 long v = 0;
159 23 ▶ if (!s || !*s) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !*s) return -1; 23 0 23
if (!s || !*s) return -1; 0 23 23
160 134 ▶ for (; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; *s; s++) { 111 23 134
161 111 ▶ if (*s < '0' || *s > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '0' || *s > '9') return -1; 111 0 111
if (*s < '0' || *s > '9') return -1; 0 111 111
162 111 v = v * 10 + (*s - '0');
163 111 ▶ if (v > 65535) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 65535) return -1; 0 111 111
164 }
165 23 ▶ return v >= 1 ? (int)v : -1;
Branch: 1 branch
BranchTrueFalseTotal
return v >= 1 ? (int)v : -1; 23 0 23
166 }
167  
168 /* Bridges userdata into ws_callback() before lws_adopt returns. */
169 static ev_ws_conn_t* pending_adoption = NULL;
170 static HV* handshake_headers_map = NULL; /* wsi-ptr → per-conn response headers HV */
171 static struct lws_context* ssl_keepalive_ctx = NULL; /* see ensure_ssl_keepalive() */
172  
173 /* Copy an lws header token into a fresh SV, or return NULL if absent/empty */
174 226 static SV* hdr_to_sv(struct lws *wsi, enum lws_token_indexes tok) {
175 226 int total = lws_hdr_total_length(wsi, tok);
176 226 ▶ if (total > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (total > 0) { 54 172 226
177 char *buf;
178 int n;
179 54 Newx(buf, total + 1, char);
180 54 n = lws_hdr_copy(wsi, buf, total + 1, tok);
181 54 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 54 0 54
182 54 SV *val = newSVpvn(buf, n);
183 54 Safefree(buf);
184 54 return val;
185 }
186 0 Safefree(buf);
187 }
188 172 return NULL;
189 }
190  
191 /* Capture a header value into an HV under the given key */
192 224 static void capture_header(struct lws *wsi, HV *hv, enum lws_token_indexes tok,
193 const char *name, STRLEN nlen) {
194 224 SV *val = hdr_to_sv(wsi, tok);
195 224 ▶ if (val && !hv_store(hv, name, nlen, val, 0))
Branch: 2 branches
BranchTrueFalseTotal
if (val && !hv_store(hv, name, nlen, val, 0)) 54 170 224
if (val && !hv_store(hv, name, nlen, val, 0)) 0 54 54
196 0 SvREFCNT_dec(val);
197 224 }
198  
199 typedef struct { enum lws_token_indexes tok; const char *name; STRLEN nlen; } header_def_t;
200  
201 static const header_def_t request_hdrs[] = {
202 { WSI_TOKEN_GET_URI, "Path", 4 },
203 { WSI_TOKEN_HOST, "Host", 4 },
204 { WSI_TOKEN_ORIGIN, "Origin", 6 },
205 { WSI_TOKEN_HTTP_COOKIE, "Cookie", 6 },
206 { WSI_TOKEN_HTTP_AUTHORIZATION, "Authorization", 13 },
207 { WSI_TOKEN_PROTOCOL, "Sec-WebSocket-Protocol", 22 },
208 { WSI_TOKEN_HTTP_USER_AGENT, "User-Agent", 10 },
209 { WSI_TOKEN_X_FORWARDED_FOR, "X-Forwarded-For", 15 },
210 };
211 #define N_REQUEST_HDRS (int)(sizeof(request_hdrs)/sizeof(request_hdrs[0]))
212  
213 18 static void capture_request_headers(struct lws *wsi, HV *hv) {
214 int i;
215 162 ▶ for (i = 0; i < N_REQUEST_HDRS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < N_REQUEST_HDRS; i++) 144 18 162
216 144 capture_header(wsi, hv, request_hdrs[i].tok,
217 144 request_hdrs[i].name, request_hdrs[i].nlen);
218 18 }
219  
220 /* Inject all key/value pairs from an HV as HTTP headers via lws.
221 Returns -1 if lws rejected a header (client path aborts the handshake);
222 server callers ignore the result and simply stop adding. */
223 1 static int inject_headers(struct lws *wsi, HV *hv,
224 unsigned char **p, unsigned char *end) {
225 HE *entry;
226 char kbuf[256];
227 1 hv_iterinit(hv);
228 2 ▶ while ((entry = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(hv))) { 1 1 2
229 I32 klen;
230 1 const char *key = hv_iterkey(entry, &klen);
231 SV *val_sv;
232 STRLEN vlen;
233 const char *val;
234 1 ▶ if (klen >= 254) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (klen >= 254) continue; 0 1 1
235 1 val_sv = hv_iterval(hv, entry);
236 1 val = SvPV(val_sv, vlen);
237 1 memcpy(kbuf, key, klen);
238 1 kbuf[klen] = ':';
239 1 kbuf[klen + 1] = '\0';
240 1 ▶ if (lws_add_http_header_by_name(wsi, (unsigned char *)kbuf,
Branch: 1 branch
BranchTrueFalseTotal
if (lws_add_http_header_by_name(wsi, (unsigned char *)kbuf, 0 1 1
241 (unsigned char *)val, vlen, p, end))
242 0 return -1;
243 }
244 1 return 0;
245 }
246  
247 /* Format a wsi pointer as the lookup key for handshake_headers_map.
248 Callers must pass a buffer of at least 32 bytes; returns the key length. */
249 0 static int wsi_key(char *buf, struct lws *wsi) {
250 0 return snprintf(buf, 32, "%p", (void*)wsi);
251 }
252  
253 #define DEBUG_LOG(fmt, ...) do { if (ev_ws_debug) fprintf(stderr, "[EV::WS] " fmt "\n", ##__VA_ARGS__); } while(0)
254  
255 255 static void ctx_ref(ev_ws_ctx_t* ctx) {
256 255 ctx->refcnt++;
257 255 }
258  
259 279 static void ctx_unref(ev_ws_ctx_t* ctx) {
260 279 ▶ if (--ctx->refcnt == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctx->refcnt == 0) { 24 255 279
261 24 Safefree(ctx);
262 }
263 279 }
264  
265 /* Schedule the next lws housekeeping wake-up.
266  
267 lws_service_adjust_timeout returns the ms until lws next needs servicing; 0
268 means "service as soon as possible". This timer only paces lws's time-based
269 work (connection/handshake timeouts, TLS cert aging, draining buffered rx) --
270 socket readability/writability is driven by the per-fd io watcher, not here.
271 We deliberately floor the delay at 1ms rather than arming an ev_idle watcher
272 on 0: do_lws_service is now non-blocking, so an always-ready idle watcher
273 would busy-spin at 100% CPU whenever lws keeps asking for immediate service.
274 A 1ms floor lets the loop block briefly so every other EV watcher still
275 fires, at negligible latency for this coarse, time-based work. */
276 279 static void schedule_timeout(ev_ws_ctx_t* ctx) {
277 279 int delay_ms = lws_service_adjust_timeout(ctx->lws_ctx, 1000, 0);
278 double delay_s;
279  
280 279 ▶ if (delay_ms < 1) delay_ms = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (delay_ms < 1) delay_ms = 1; 3 276 279
281 279 delay_s = (double)delay_ms / 1000.0;
282  
283 279 ev_timer_stop(ctx->loop, &ctx->timer);
284 279 ev_timer_set(&ctx->timer, delay_s, 0.);
285 279 ev_timer_start(ctx->loop, &ctx->timer);
286 279 }
287  
288 static void flush_recv_messages(ev_ws_ctx_t* ctx);
289  
290 6 static void do_lws_service(ev_ws_ctx_t* ctx) {
291 6 ▶ if (ctx && ctx->magic == EV_WS_CTX_MAGIC && ctx->lws_ctx) {
Branch: 3 branches
BranchTrueFalseTotal
if (ctx && ctx->magic == EV_WS_CTX_MAGIC && ctx->lws_ctx) { 6 0 6
if (ctx && ctx->magic == EV_WS_CTX_MAGIC && ctx->lws_ctx) { 6 0 6
if (ctx && ctx->magic == EV_WS_CTX_MAGIC && ctx->lws_ctx) { 6 0 6
292 6 int alive = 1;
293 6 int* prev_flag = ctx->alive_flag;
294 6 ctx->alive_flag = &alive;
295 6 ctx_ref(ctx);
296 /* "Forced service": drive connections that need servicing with no
297 pending socket event -- rx already read into lws's buflist, plus any
298 due lws_sul timeouts. Counterintuitively a timeout_ms of 0 is NOT
299 non-blocking here: lws maps it to its maximum internal poll wait, so
300 the old lws_service(ctx, 0) blocked for seconds on an idle connection
301 and starved every other EV watcher. A negative timeout_ms clamps the
302 wait to 0, servicing only ready + forced-service work and returning at
303 once (see the lws_service_adjust_timeout docs). lws_service_fd(ctx,
304 NULL) is invalid since lws 3.2 and never drains buflists. Per-fd I/O
305 is driven by io_cb via lws_service_fd(&pollfd). */
306 6 lws_service_tsi(ctx->lws_ctx, -1, 0);
307 6 ▶ if (alive)
Branch: 1 branch
BranchTrueFalseTotal
if (alive) 6 0 6
308 6 flush_recv_messages(ctx); /* deliver messages reassembled above */
309 6 ▶ if (alive) {
Branch: 1 branch
BranchTrueFalseTotal
if (alive) { 6 0 6
310 6 ctx->alive_flag = prev_flag;
311 6 schedule_timeout(ctx);
312 0 ▶ } else if (prev_flag) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (prev_flag) { 0 0 0
313 0 *prev_flag = 0; /* propagate destruction up the alive_flag chain */
314 }
315 6 ctx_unref(ctx);
316 }
317 6 }
318  
319 6 static void timer_cb(EV_P_ ev_timer* w, int revents) {
320 (void)loop; (void)revents;
321 6 do_lws_service((ev_ws_ctx_t*)w->data);
322 6 }
323  
324 /* Forward declarations */
325 static void io_cb(EV_P_ ev_io* w, int revents);
326 static void add_fd_watcher(ev_ws_ctx_t* ctx, int fd, int events);
327 static void change_fd_watcher(ev_ws_ctx_t* ctx, int fd, int events);
328 static void conn_ref(ev_ws_conn_t* conn);
329 static void conn_unref(ev_ws_conn_t* conn);
330  
331 131 static SV* get_conn_sv(ev_ws_conn_t* conn) {
332 131 ▶ if (conn->perl_self) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->perl_self) { 89 42 131
333 89 SV* rv = newRV_inc(conn->perl_self);
334 89 sv_bless(rv, gv_stashpv("EV::Websockets::Connection", 1));
335 89 return rv;
336 }
337 42 SV* rv = newSV(0);
338 42 sv_setref_pv(rv, "EV::Websockets::Connection", (void*)conn);
339 42 conn->perl_self = SvRV(rv);
340
341 /* We are creating a new Perl owner for this connection.
342 Increment refcnt so DESTROY doesn't kill it prematurely if LWS still needs it. */
343 42 conn_ref(conn);
344
345 42 return rv;
346 }
347  
348 /* All emit_* callbacks share this skeleton: guard on a registered handler,
349 ref the conn across the call (a callback may close/destroy it), push $conn
350 plus handler-specific args, invoke under G_EVAL, and warn — without
351 recursing — on a die. EMIT_BEGIN opens a block that EMIT_END closes;
352 handler-specific args are XPUSHs'd in between. */
353 #define EMIT_BEGIN(conn, cb_field) \
354 if ((conn) == NULL || (conn)->cb_field == NULL) return; \
355 { \
356 SV* _emit_cb = (conn)->cb_field; \
357 dSP; \
358 ENTER; \
359 SAVETMPS; \
360 PUSHMARK(SP); \
361 conn_ref(conn); \
362 XPUSHs(sv_2mortal(get_conn_sv(conn)));
363  
364 #define EMIT_END(conn, label) \
365 PUTBACK; \
366 sv_setsv(ERRSV, &PL_sv_undef); \
367 call_sv(_emit_cb, G_DISCARD | G_EVAL); \
368 if (SvTRUE(ERRSV)) \
369 warn("EV::Websockets: exception in " label ": %s", SvPV_nolen(ERRSV)); \
370 FREETMPS; \
371 LEAVE; \
372 conn_unref(conn); \
373 }
374  
375 /* Guard shared by the send_* methods: croak if the connection has been
376 destroyed or is not currently open for writing. */
377 #define CHECK_CONN_OPEN(self) STMT_START { \
378 if ((self)->magic != EV_WS_CONN_MAGIC) \
379 croak("Connection has been destroyed"); \
380 if (!(self)->wsi || !(self)->connected || (self)->closing) \
381 croak("Connection is not open"); \
382 } STMT_END
383  
384 /* Guard for send/send_binary: a whole-message write must not interleave with
385 an in-progress fragmented message. */
386 #define CHECK_NOT_FRAGMENTING(self) STMT_START { \
387 if ((self)->in_fragmented_send) \
388 croak("Cannot send while a fragmented message is in progress; " \
389 "finish the fragment with send_fragment(..., is_final => 1) first"); \
390 } STMT_END
391  
392 2 static void emit_error(ev_ws_conn_t* conn, const char* error) {
393 2 ▶ EMIT_BEGIN(conn, on_error);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_error); 2 0 2
EMIT_BEGIN(conn, on_error); 0 2 2
EMIT_BEGIN(conn, on_error); 0 2 2
EMIT_BEGIN(conn, on_error); 0 2 2
394 2 ▶ XPUSHs(sv_2mortal(newSVpv(error, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(error, 0))); 0 2 2
395 2 ▶ EMIT_END(conn, "error handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "error handler"); 2 0 2
EMIT_END(conn, "error handler"); 2 0 2
EMIT_END(conn, "error handler"); 0 2 2
EMIT_END(conn, "error handler"); 0 0 0
EMIT_END(conn, "error handler"); 2 0 2
396 }
397  
398 38 static void emit_connect(ev_ws_conn_t* conn) {
399 38 ▶ EMIT_BEGIN(conn, on_connect);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_connect); 38 0 38
EMIT_BEGIN(conn, on_connect); 2 36 38
EMIT_BEGIN(conn, on_connect); 0 36 36
EMIT_BEGIN(conn, on_connect); 0 36 36
400 36 ▶ if (conn->response_headers)
Branch: 1 branch
BranchTrueFalseTotal
if (conn->response_headers) 36 0 36
401 36 ▶ XPUSHs(sv_2mortal(newRV_inc((SV*)conn->response_headers)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV*)conn->response_headers))); 0 36 36
402 else
403 0 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 0 0
404 37 ▶ EMIT_END(conn, "connect handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "connect handler"); 36 0 36
EMIT_END(conn, "connect handler"); 36 0 36
EMIT_END(conn, "connect handler"); 1 35 36
EMIT_END(conn, "connect handler"); 1 0 1
EMIT_END(conn, "connect handler"); 36 0 36
405 }
406  
407 30 static void emit_message(ev_ws_conn_t* conn, const char* data, size_t len, int is_binary, int is_final) {
408 30 ▶ EMIT_BEGIN(conn, on_message);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_message); 30 0 30
EMIT_BEGIN(conn, on_message); 0 30 30
EMIT_BEGIN(conn, on_message); 0 30 30
EMIT_BEGIN(conn, on_message); 0 30 30
409 30 ▶ XPUSHs(sv_2mortal(newSVpvn(data, len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(data, len))); 0 30 30
410 30 ▶ XPUSHs(sv_2mortal(newSViv(is_binary)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(is_binary))); 0 30 30
411 30 ▶ XPUSHs(sv_2mortal(newSViv(is_final)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(is_final))); 0 30 30
412 30 ▶ EMIT_END(conn, "message handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "message handler"); 30 0 30
EMIT_END(conn, "message handler"); 30 0 30
EMIT_END(conn, "message handler"); 0 30 30
EMIT_END(conn, "message handler"); 0 0 0
EMIT_END(conn, "message handler"); 30 0 30
413 }
414  
415 /* Deliver buffered, fully-received messages once an lws service call has
416 drained all currently-available input. The receive path reassembles a
417 message across callbacks (necessary because permessage-deflate inflates one
418 frame into several callbacks, each reporting lws_is_final_fragment() == 1)
419 and queues the connection here; we emit only complete messages. The ref taken
420 when queuing keeps each conn alive across its callback, so it is safe to
421 touch conn after emit_message returns. */
422 255 static void flush_recv_messages(ev_ws_ctx_t* ctx) {
423 255 ev_ws_conn_t* conn = ctx->flush_head;
424 255 ctx->flush_head = NULL;
425 255 ctx->flush_tail = NULL; /* must clear with the head: a stale tail would be
426 appended to (use-after-free) on the next queue */
427 286 ▶ while (conn) {
Branch: 1 branch
BranchTrueFalseTotal
while (conn) { 31 255 286
428 31 ev_ws_conn_t* next = conn->flush_next;
429 31 conn->flush_next = NULL;
430 31 conn->on_flush = 0;
431 31 ▶ if (conn->magic == EV_WS_CONN_MAGIC && conn->recv_complete) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC && conn->recv_complete) { 31 0 31
if (conn->magic == EV_WS_CONN_MAGIC && conn->recv_complete) { 30 1 31
432 /* recv_complete (not recv_len>0): a zero-length message is a valid
433 complete message and must still be delivered. */
434 30 ▶ emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1);
Branch: 1 branch
BranchTrueFalseTotal
emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1); 30 0 30
435 30 ▶ if (conn->magic == EV_WS_CONN_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC) { 30 0 30
436 30 conn->recv_len = 0;
437 30 conn->recv_complete = 0;
438 }
439 }
440 31 conn_unref(conn); /* release the flush-list ref */
441 31 conn = next;
442 }
443 255 }
444  
445 31 static void emit_close(ev_ws_conn_t* conn, int code, const char* reason) {
446 31 ▶ EMIT_BEGIN(conn, on_close);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_close); 31 0 31
EMIT_BEGIN(conn, on_close); 0 31 31
EMIT_BEGIN(conn, on_close); 0 31 31
EMIT_BEGIN(conn, on_close); 0 31 31
447 31 ▶ XPUSHs(sv_2mortal(newSViv(code)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(code))); 0 31 31
448 31 ▶ XPUSHs(reason ? sv_2mortal(newSVpv(reason, 0)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(reason ? sv_2mortal(newSVpv(reason, 0)) : &PL_sv_undef); 0 31 31
XPUSHs(reason ? sv_2mortal(newSVpv(reason, 0)) : &PL_sv_undef); 0 31 31
449 32 ▶ EMIT_END(conn, "close handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "close handler"); 31 0 31
EMIT_END(conn, "close handler"); 31 0 31
EMIT_END(conn, "close handler"); 1 30 31
EMIT_END(conn, "close handler"); 1 0 1
EMIT_END(conn, "close handler"); 31 0 31
450 }
451  
452 3 static void emit_pong(ev_ws_conn_t* conn, const char* data, size_t len) {
453 3 ▶ EMIT_BEGIN(conn, on_pong);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_pong); 3 0 3
EMIT_BEGIN(conn, on_pong); 0 3 3
EMIT_BEGIN(conn, on_pong); 0 3 3
EMIT_BEGIN(conn, on_pong); 0 3 3
454 3 ▶ XPUSHs(sv_2mortal(newSVpvn(data ? data : "", len)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(data ? data : "", len))); 0 3 3
XPUSHs(sv_2mortal(newSVpvn(data ? data : "", len))); 3 0 3
455 4 ▶ EMIT_END(conn, "pong handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "pong handler"); 3 0 3
EMIT_END(conn, "pong handler"); 3 0 3
EMIT_END(conn, "pong handler"); 1 2 3
EMIT_END(conn, "pong handler"); 1 0 1
EMIT_END(conn, "pong handler"); 3 0 3
456 }
457  
458 33 static void emit_drain(ev_ws_conn_t* conn) {
459 33 ▶ EMIT_BEGIN(conn, on_drain);
Branch: 4 branches
BranchTrueFalseTotal
EMIT_BEGIN(conn, on_drain); 33 0 33
EMIT_BEGIN(conn, on_drain); 32 1 33
EMIT_BEGIN(conn, on_drain); 0 1 1
EMIT_BEGIN(conn, on_drain); 0 1 1
460 2 ▶ EMIT_END(conn, "drain handler");
Branch: 5 branches
BranchTrueFalseTotal
EMIT_END(conn, "drain handler"); 1 0 1
EMIT_END(conn, "drain handler"); 1 0 1
EMIT_END(conn, "drain handler"); 1 0 1
EMIT_END(conn, "drain handler"); 1 0 1
EMIT_END(conn, "drain handler"); 1 0 1
461 }
462  
463 /* Stop the connect-timeout watcher if it is running */
464 112 static void stop_connect_timer(ev_ws_conn_t* conn) {
465 112 ▶ if (conn->connect_timer_active && conn->loop) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn->connect_timer_active && conn->loop) { 0 112 112
if (conn->connect_timer_active && conn->loop) { 0 0 0
466 0 ev_timer_stop(conn->loop, &conn->connect_timer);
467 0 conn->connect_timer_active = 0;
468 }
469 112 }
470  
471 /* Drop references to the (up to seven) callback SVs collected while parsing
472 options, on an error path that croaks before they are owned elsewhere.
473 Pass NULL for absent slots; SvREFCNT_dec of NULL is guarded out. */
474 5 static void free_cb_svs(SV* on_connect, SV* on_message, SV* on_close,
475 SV* on_error, SV* on_pong, SV* on_drain,
476 SV* on_handshake) {
477 5 ▶ if (on_connect) SvREFCNT_dec(on_connect);
Branch: 1 branch
BranchTrueFalseTotal
if (on_connect) SvREFCNT_dec(on_connect); 0 5 5
478 5 ▶ if (on_message) SvREFCNT_dec(on_message);
Branch: 1 branch
BranchTrueFalseTotal
if (on_message) SvREFCNT_dec(on_message); 3 2 5
479 5 ▶ if (on_close) SvREFCNT_dec(on_close);
Branch: 1 branch
BranchTrueFalseTotal
if (on_close) SvREFCNT_dec(on_close); 0 5 5
480 5 ▶ if (on_error) SvREFCNT_dec(on_error);
Branch: 1 branch
BranchTrueFalseTotal
if (on_error) SvREFCNT_dec(on_error); 0 5 5
481 5 ▶ if (on_pong) SvREFCNT_dec(on_pong);
Branch: 1 branch
BranchTrueFalseTotal
if (on_pong) SvREFCNT_dec(on_pong); 0 5 5
482 5 ▶ if (on_drain) SvREFCNT_dec(on_drain);
Branch: 1 branch
BranchTrueFalseTotal
if (on_drain) SvREFCNT_dec(on_drain); 0 5 5
483 5 ▶ if (on_handshake) SvREFCNT_dec(on_handshake);
Branch: 1 branch
BranchTrueFalseTotal
if (on_handshake) SvREFCNT_dec(on_handshake); 0 5 5
484 5 }
485  
486 /* Drop the server's callback/header SV references (not protocol_name, which the
487 live vhost still points at, nor the struct itself). Idempotent via NULL-out. */
488 18 static void free_server_svs(ev_ws_server_t* srv) {
489 18 ▶ if (srv->on_connect) { SvREFCNT_dec(srv->on_connect); srv->on_connect = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_connect) { SvREFCNT_dec(srv->on_connect); srv->on_connect = NULL; } 18 0 18
490 18 ▶ if (srv->on_message) { SvREFCNT_dec(srv->on_message); srv->on_message = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_message) { SvREFCNT_dec(srv->on_message); srv->on_message = NULL; } 17 1 18
491 18 ▶ if (srv->on_close) { SvREFCNT_dec(srv->on_close); srv->on_close = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_close) { SvREFCNT_dec(srv->on_close); srv->on_close = NULL; } 17 1 18
492 18 ▶ if (srv->on_error) { SvREFCNT_dec(srv->on_error); srv->on_error = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_error) { SvREFCNT_dec(srv->on_error); srv->on_error = NULL; } 2 16 18
493 18 ▶ if (srv->on_pong) { SvREFCNT_dec(srv->on_pong); srv->on_pong = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_pong) { SvREFCNT_dec(srv->on_pong); srv->on_pong = NULL; } 1 17 18
494 18 ▶ if (srv->on_drain) { SvREFCNT_dec(srv->on_drain); srv->on_drain = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_drain) { SvREFCNT_dec(srv->on_drain); srv->on_drain = NULL; } 0 18 18
495 18 ▶ if (srv->on_handshake) { SvREFCNT_dec(srv->on_handshake); srv->on_handshake = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (srv->on_handshake) { SvREFCNT_dec(srv->on_handshake); srv->on_handshake = NULL; } 0 18 18
496 18 ▶ if (srv->response_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (srv->response_headers) { 0 18 18
497 0 SvREFCNT_dec((SV*)srv->response_headers);
498 0 srv->response_headers = NULL;
499 }
500 18 }
501  
502 /* Free a connection's resources (not the struct itself) */
503 42 static void free_conn_resources(ev_ws_conn_t* conn) {
504 ev_ws_send_t* send;
505 ev_ws_send_t* next_send;
506  
507 42 ▶ DEBUG_LOG("Freeing connection resources: conn=%p", conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Freeing connection resources: conn=%p", conn); 0 42 42
508  
509 42 stop_connect_timer(conn);
510  
511 42 ▶ if (conn->on_connect) { SvREFCNT_dec(conn->on_connect); conn->on_connect = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_connect) { SvREFCNT_dec(conn->on_connect); conn->on_connect = NULL; } 38 4 42
512 42 ▶ if (conn->on_message) { SvREFCNT_dec(conn->on_message); conn->on_message = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_message) { SvREFCNT_dec(conn->on_message); conn->on_message = NULL; } 32 10 42
513 42 ▶ if (conn->on_close) { SvREFCNT_dec(conn->on_close); conn->on_close = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_close) { SvREFCNT_dec(conn->on_close); conn->on_close = NULL; } 37 5 42
514 42 ▶ if (conn->on_error) { SvREFCNT_dec(conn->on_error); conn->on_error = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_error) { SvREFCNT_dec(conn->on_error); conn->on_error = NULL; } 26 16 42
515 42 ▶ if (conn->on_pong) { SvREFCNT_dec(conn->on_pong); conn->on_pong = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_pong) { SvREFCNT_dec(conn->on_pong); conn->on_pong = NULL; } 3 39 42
516 42 ▶ if (conn->on_drain) { SvREFCNT_dec(conn->on_drain); conn->on_drain = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->on_drain) { SvREFCNT_dec(conn->on_drain); conn->on_drain = NULL; } 1 41 42
517  
518 42 ▶ if (conn->custom_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->custom_headers) { 1 41 42
519 1 SvREFCNT_dec((SV*)conn->custom_headers);
520 1 conn->custom_headers = NULL;
521 }
522  
523 42 ▶ if (conn->response_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->response_headers) { 38 4 42
524 38 SvREFCNT_dec((SV*)conn->response_headers);
525 38 conn->response_headers = NULL;
526 }
527  
528 42 ▶ if (conn->stash) { SvREFCNT_dec((SV*)conn->stash); conn->stash = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->stash) { SvREFCNT_dec((SV*)conn->stash); conn->stash = NULL; } 1 41 42
529  
530 42 ▶ if (conn->adopted_fh) { SvREFCNT_dec(conn->adopted_fh); conn->adopted_fh = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->adopted_fh) { SvREFCNT_dec(conn->adopted_fh); conn->adopted_fh = NULL; } 1 41 42
531  
532 42 ▶ if (conn->recv_buf) { Safefree(conn->recv_buf); conn->recv_buf = NULL; conn->recv_alloc = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->recv_buf) { Safefree(conn->recv_buf); conn->recv_buf = NULL; conn->recv_alloc = 0; } 27 15 42
533  
534 42 ▶ for (send = conn->send_head; send != NULL; send = next_send) {
Branch: 1 branch
BranchTrueFalseTotal
for (send = conn->send_head; send != NULL; send = next_send) { 0 42 42
535 0 next_send = send->next;
536 0 Safefree(send);
537 }
538 42 conn->send_head = NULL;
539 42 conn->send_tail = NULL;
540 42 conn->send_queue_bytes = 0;
541 42 }
542  
543 210 static void conn_ref(ev_ws_conn_t* conn) {
544 210 conn->refcnt++;
545 210 ▶ DEBUG_LOG("conn_ref: %p refcnt=%d", conn, conn->refcnt);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("conn_ref: %p refcnt=%d", conn, conn->refcnt); 0 210 210
546 210 }
547  
548 276 static void conn_unref(ev_ws_conn_t* conn) {
549 276 ▶ DEBUG_LOG("conn_unref: %p refcnt=%d", conn, conn->refcnt);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("conn_unref: %p refcnt=%d", conn, conn->refcnt); 0 276 276
550 276 ▶ if (--conn->refcnt == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--conn->refcnt == 0) { 42 234 276
551 42 ▶ DEBUG_LOG("Actually freeing conn: %p", conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Actually freeing conn: %p", conn); 0 42 42
552 42 free_conn_resources(conn);
553 42 conn->magic = EV_WS_CONN_FREED;
554 42 Safefree(conn);
555 }
556 276 }
557  
558 42 static void link_conn(ev_ws_ctx_t* ctx, ev_ws_conn_t* conn) {
559 42 conn->ctx = ctx;
560 42 conn->next = ctx->connections;
561 42 ▶ if (ctx->connections) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->connections) { 19 23 42
562 19 ctx->connections->prev = conn;
563 }
564 42 ctx->connections = conn;
565 42 }
566  
567 32 static void unlink_conn(ev_ws_conn_t* conn) {
568 32 ▶ if (conn->ctx == NULL) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn->ctx == NULL) return; 0 32 32
569  
570 32 ▶ if (conn->prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->prev) { 12 20 32
571 12 conn->prev->next = conn->next;
572 } else {
573 20 conn->ctx->connections = conn->next;
574 }
575 32 ▶ if (conn->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->next) { 7 25 32
576 7 conn->next->prev = conn->prev;
577 }
578 32 conn->prev = NULL;
579 32 conn->next = NULL;
580 32 conn->ctx = NULL;
581 }
582  
583 36 static void queue_send(ev_ws_conn_t* conn, const char* data, size_t len, enum lws_write_protocol write_mode) {
584 36 int was_empty = (conn->send_head == NULL);
585 36 size_t alloc = offsetof(ev_ws_send_t, data) + LWS_PRE + len;
586 36 ev_ws_send_t* send = (ev_ws_send_t*)safemalloc(alloc);
587  
588 36 ▶ if (data && len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (data && len > 0) { 36 0 36
if (data && len > 0) { 36 0 36
589 36 memcpy(send->data + LWS_PRE, data, len);
590 }
591 36 send->len = len;
592 36 send->write_mode = write_mode;
593 36 send->next = NULL;
594  
595 36 ▶ if (conn->send_tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->send_tail) { 2 34 36
596 2 conn->send_tail->next = send;
597 2 conn->send_tail = send;
598 } else {
599 34 conn->send_head = send;
600 34 conn->send_tail = send;
601 }
602 36 conn->send_queue_bytes += len;
603  
604 36 ▶ if (was_empty && conn->wsi) {
Branch: 2 branches
BranchTrueFalseTotal
if (was_empty && conn->wsi) { 34 2 36
if (was_empty && conn->wsi) { 34 0 34
605 34 lws_callback_on_writable(conn->wsi);
606 }
607 36 }
608  
609 248 static void io_cb(EV_P_ ev_io* w, int revents) {
610 248 ev_ws_fd_t* fdw = (ev_ws_fd_t*)w;
611 248 ev_ws_ctx_t* ctx = fdw->ctx;
612 struct lws_pollfd pollfd;
613  
614 (void)loop;
615  
616 248 ▶ if (ctx == NULL || ctx->magic != EV_WS_CTX_MAGIC || ctx->lws_ctx == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (ctx == NULL || ctx->magic != EV_WS_CTX_MAGIC || ctx->lws_ctx == NULL) { 248 0 248
if (ctx == NULL || ctx->magic != EV_WS_CTX_MAGIC || ctx->lws_ctx == NULL) { 248 0 248
if (ctx == NULL || ctx->magic != EV_WS_CTX_MAGIC || ctx->lws_ctx == NULL) { 0 248 248
617 0 return;
618 }
619  
620 248 pollfd.fd = fdw->fd;
621 248 pollfd.events = fdw->poll_events;
622 248 pollfd.revents = 0;
623  
624 248 ▶ if (revents & EV_READ) pollfd.revents |= POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) pollfd.revents |= POLLIN; 115 133 248
625 248 ▶ if (revents & EV_WRITE) pollfd.revents |= POLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) pollfd.revents |= POLLOUT; 142 106 248
626 248 ▶ if (revents & EV_ERROR) pollfd.revents |= POLLERR | POLLHUP;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_ERROR) pollfd.revents |= POLLERR | POLLHUP; 0 248 248
627  
628 {
629 248 int alive = 1;
630 248 int* prev_flag = ctx->alive_flag;
631 248 ctx->alive_flag = &alive;
632 248 ctx_ref(ctx);
633 248 lws_service_fd(ctx->lws_ctx, &pollfd);
634 248 ▶ if (alive)
Branch: 1 branch
BranchTrueFalseTotal
if (alive) 248 0 248
635 248 flush_recv_messages(ctx); /* deliver messages reassembled above */
636 248 ▶ if (alive) {
Branch: 1 branch
BranchTrueFalseTotal
if (alive) { 248 0 248
637 248 ctx->alive_flag = prev_flag;
638 248 schedule_timeout(ctx);
639 0 ▶ } else if (prev_flag) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (prev_flag) { 0 0 0
640 0 *prev_flag = 0; /* propagate destruction up the alive_flag chain */
641 }
642 248 ctx_unref(ctx);
643 }
644 }
645  
646 #define FD_TABLE_INIT_SIZE 64
647  
648 23 static void fd_table_grow(ev_ws_ctx_t* ctx, int needed) {
649 23 ▶ int new_size = ctx->fd_table_size ? ctx->fd_table_size : FD_TABLE_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
int new_size = ctx->fd_table_size ? ctx->fd_table_size : FD_TABLE_INIT_SIZE; 0 23 23
650 23 ▶ while (new_size <= needed) new_size *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_size <= needed) new_size *= 2; 0 23 23
651 23 Renew(ctx->fd_table, new_size, ev_ws_fd_t*);
652 23 Zero(ctx->fd_table + ctx->fd_table_size, new_size - ctx->fd_table_size, ev_ws_fd_t*);
653 23 ctx->fd_table_size = new_size;
654 23 }
655  
656 60 static void add_fd_watcher(ev_ws_ctx_t* ctx, int fd, int events) {
657 ev_ws_fd_t* fdw;
658 60 int ev_events = 0;
659  
660 60 ▶ if (fd < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return; 0 60 60
661 60 ▶ if (fd >= ctx->fd_table_size) fd_table_grow(ctx, fd);
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= ctx->fd_table_size) fd_table_grow(ctx, fd); 23 37 60
662  
663 60 fdw = ctx->fd_table[fd];
664 60 ▶ if (fdw != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fdw != NULL) { 0 60 60
665 0 change_fd_watcher(ctx, fd, events);
666 0 return;
667 }
668  
669 60 Newxz(fdw, 1, ev_ws_fd_t);
670 60 fdw->ctx = ctx;
671 60 fdw->fd = fd;
672 60 fdw->poll_events = events;
673  
674 60 ▶ if (events & POLLIN) ev_events |= EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLIN) ev_events |= EV_READ; 60 0 60
675 60 ▶ if (events & POLLOUT) ev_events |= EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLOUT) ev_events |= EV_WRITE; 0 60 60
676  
677 60 ▶ DEBUG_LOG("add_fd_watcher: fd=%d poll_events=%d ev_events=%d", fd, events, ev_events);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("add_fd_watcher: fd=%d poll_events=%d ev_events=%d", fd, events, ev_events); 0 60 60
678  
679 60 ▶ ev_io_init(&fdw->io, io_cb, fd, ev_events ? ev_events : EV_READ);
Branch: 1 branch
BranchTrueFalseTotal
ev_io_init(&fdw->io, io_cb, fd, ev_events ? ev_events : EV_READ); 60 0 60
680 60 ▶ if (ev_events)
Branch: 1 branch
BranchTrueFalseTotal
if (ev_events) 60 0 60
681 60 ev_io_start(ctx->loop, &fdw->io);
682  
683 60 ctx->fd_table[fd] = fdw;
684 }
685  
686 32 static void del_fd_watcher(ev_ws_ctx_t* ctx, int fd) {
687 ev_ws_fd_t* fdw;
688 32 ▶ if (fd < 0 || fd >= ctx->fd_table_size) return;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= ctx->fd_table_size) return; 32 0 32
if (fd < 0 || fd >= ctx->fd_table_size) return; 0 32 32
689 32 fdw = ctx->fd_table[fd];
690 32 ▶ if (fdw == NULL) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fdw == NULL) return; 0 32 32
691  
692 32 ev_io_stop(ctx->loop, &fdw->io);
693 32 ctx->fd_table[fd] = NULL;
694 32 Safefree(fdw);
695 }
696  
697 393 static void change_fd_watcher(ev_ws_ctx_t* ctx, int fd, int events) {
698 ev_ws_fd_t* fdw;
699 393 int ev_events = 0;
700  
701 393 ▶ if (fd < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return; 0 393 393
702 393 ▶ if (fd >= ctx->fd_table_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= ctx->fd_table_size) { 0 393 393
703 0 add_fd_watcher(ctx, fd, events);
704 0 return;
705 }
706 393 fdw = ctx->fd_table[fd];
707 393 ▶ if (fdw == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fdw == NULL) { 0 393 393
708 0 add_fd_watcher(ctx, fd, events);
709 0 return;
710 }
711  
712 393 fdw->poll_events = events;
713  
714 393 ▶ if (events & POLLIN) ev_events |= EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLIN) ev_events |= EV_READ; 382 11 393
715 393 ▶ if (events & POLLOUT) ev_events |= EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLOUT) ev_events |= EV_WRITE; 133 260 393
716  
717 393 ▶ DEBUG_LOG("change_fd_watcher: fd=%d poll_events=%d ev_events=%d", fd, events, ev_events);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("change_fd_watcher: fd=%d poll_events=%d ev_events=%d", fd, events, ev_events); 0 393 393
718  
719 393 ev_io_stop(ctx->loop, &fdw->io);
720 393 ▶ if (ev_events == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ev_events == 0) return; 6 387 393
721 387 ev_io_set(&fdw->io, fd, ev_events);
722 387 ev_io_start(ctx->loop, &fdw->io);
723 }
724  
725 24 static void free_all_fd_watchers(ev_ws_ctx_t* ctx) {
726 int i;
727 1496 ▶ for (i = 0; i < ctx->fd_table_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->fd_table_size; i++) { 1472 24 1496
728 1472 ev_ws_fd_t* fdw = ctx->fd_table[i];
729 1472 ▶ if (fdw) {
Branch: 1 branch
BranchTrueFalseTotal
if (fdw) { 28 1444 1472
730 28 ▶ if (ctx->loop) ev_io_stop(ctx->loop, &fdw->io);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->loop) ev_io_stop(ctx->loop, &fdw->io); 28 0 28
731 28 Safefree(fdw);
732 28 ctx->fd_table[i] = NULL;
733 }
734 }
735 24 Safefree(ctx->fd_table);
736 24 ctx->fd_table = NULL;
737 24 ctx->fd_table_size = 0;
738 24 }
739  
740 0 static void connect_timeout_cb(EV_P_ ev_timer* w, int revents) {
741 0 ev_ws_conn_t* conn = (ev_ws_conn_t*)w->data;
742 (void)loop; (void)revents;
743 0 conn->connect_timer_active = 0;
744 0 conn->closing = 1;
745 0 conn_ref(conn);
746 0 emit_error(conn, "connect timeout");
747 0 ▶ if (conn->magic == EV_WS_CONN_MAGIC && conn->wsi)
Branch: 2 branches
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC && conn->wsi) 0 0 0
if (conn->magic == EV_WS_CONN_MAGIC && conn->wsi) 0 0 0
748 0 lws_callback_on_writable(conn->wsi);
749 0 conn_unref(conn);
750 0 }
751  
752 /* libwebsockets callback */
753 2191 static int ws_callback(struct lws* wsi, enum lws_callback_reasons reason,
754 void* user, void* in, size_t len) {
755 2191 ▶ struct lws_context* lws_ctx = wsi ? lws_get_context(wsi) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
struct lws_context* lws_ctx = wsi ? lws_get_context(wsi) : NULL; 2191 0 2191
756 2191 ▶ ev_ws_ctx_t* ctx = lws_ctx ? (ev_ws_ctx_t*)lws_context_user(lws_ctx) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ev_ws_ctx_t* ctx = lws_ctx ? (ev_ws_ctx_t*)lws_context_user(lws_ctx) : NULL; 2191 0 2191
757 2191 ev_ws_conn_t* conn = (ev_ws_conn_t*)user;
758  
759 2191 ▶ if (!conn && wsi && pending_adoption) {
Branch: 3 branches
BranchTrueFalseTotal
if (!conn && wsi && pending_adoption) { 680 1511 2191
if (!conn && wsi && pending_adoption) { 680 0 680
if (!conn && wsi && pending_adoption) { 1 679 680
760 1 ▶ DEBUG_LOG("Associating pending adoption: conn=%p", pending_adoption);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Associating pending adoption: conn=%p", pending_adoption); 0 1 1
761 1 lws_set_wsi_user(wsi, pending_adoption);
762 1 conn = pending_adoption;
763 1 ▶ if (!conn->wsi) conn->wsi = wsi;
Branch: 1 branch
BranchTrueFalseTotal
if (!conn->wsi) conn->wsi = wsi; 1 0 1
764 }
765  
766 2191 ▶ if (ctx && ctx->magic != EV_WS_CTX_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && ctx->magic != EV_WS_CTX_MAGIC) { 2174 17 2191
if (ctx && ctx->magic != EV_WS_CTX_MAGIC) { 221 1953 2174
767 /* Context is being destroyed. Only handle cleanup callbacks. */
768 221 ▶ if (reason != LWS_CALLBACK_WSI_DESTROY &&
Branch: 2 branches
BranchTrueFalseTotal
if (reason != LWS_CALLBACK_WSI_DESTROY && 193 28 221
if (reason != LWS_CALLBACK_WSI_DESTROY && 150 43 193
769 reason != LWS_CALLBACK_PROTOCOL_DESTROY) {
770 150 return 0;
771 }
772 }
773  
774 2041 ▶ DEBUG_LOG("callback reason=%d user=%p ctx=%p wsi=%p conn=%p", (int)reason, user, ctx, wsi, conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("callback reason=%d user=%p ctx=%p wsi=%p conn=%p", (int)reason, user, ctx, wsi, conn); 0 2041 2041
775  
776 2041 switch (reason) {
777 /* Poll fd management callbacks */
778 60 case LWS_CALLBACK_ADD_POLL_FD: {
779 60 struct lws_pollargs* pa = (struct lws_pollargs*)in;
780 60 ▶ DEBUG_LOG("ADD_POLL_FD: ctx=%p fd=%d events=%d", ctx, pa->fd, pa->events);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("ADD_POLL_FD: ctx=%p fd=%d events=%d", ctx, pa->fd, pa->events); 0 60 60
781 60 ▶ if (ctx && ctx->magic == EV_WS_CTX_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 60 0 60
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 60 0 60
782 60 add_fd_watcher(ctx, pa->fd, pa->events);
783 }
784 60 break;
785 }
786  
787 32 case LWS_CALLBACK_DEL_POLL_FD: {
788 32 struct lws_pollargs* pa = (struct lws_pollargs*)in;
789 32 ▶ if (ctx && ctx->magic == EV_WS_CTX_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 32 0 32
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 32 0 32
790 32 del_fd_watcher(ctx, pa->fd);
791 }
792 32 break;
793 }
794  
795 393 case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
796 393 struct lws_pollargs* pa = (struct lws_pollargs*)in;
797 393 ▶ if (ctx && ctx->magic == EV_WS_CTX_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 393 0 393
if (ctx && ctx->magic == EV_WS_CTX_MAGIC) { 393 0 393
798 393 change_fd_watcher(ctx, pa->fd, pa->events);
799 }
800 393 break;
801 }
802  
803 21 case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: {
804 21 ▶ if (conn && conn->custom_headers) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->custom_headers) { 21 0 21
if (conn && conn->custom_headers) { 1 20 21
805 1 unsigned char **p = (unsigned char **)in;
806 1 unsigned char *end = (*p) + len;
807 1 ▶ if (inject_headers(wsi, conn->custom_headers, p, end))
Branch: 1 branch
BranchTrueFalseTotal
if (inject_headers(wsi, conn->custom_headers, p, end)) 0 1 1
808 0 return -1;
809 }
810 21 break;
811 }
812
813 20 case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: {
814 20 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 20 0 20
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 20 0 20
815 static const header_def_t resp_hdrs[] = {
816 { WSI_TOKEN_HTTP_SET_COOKIE, "Set-Cookie", 10 },
817 { WSI_TOKEN_HTTP_CONTENT_TYPE, "Content-Type", 12 },
818 { WSI_TOKEN_HTTP_SERVER, "Server", 6 },
819 { WSI_TOKEN_PROTOCOL, "Sec-WebSocket-Protocol", 22 },
820 #ifdef WSI_TOKEN_HTTP_LOCATION
821 { WSI_TOKEN_HTTP_LOCATION, "Location", 8 },
822 #endif
823 #ifdef WSI_TOKEN_HTTP_WWW_AUTHENTICATE
824 { WSI_TOKEN_HTTP_WWW_AUTHENTICATE, "WWW-Authenticate", 16 },
825 #endif
826 };
827 int hi;
828 20 ▶ if (!conn->response_headers) conn->response_headers = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!conn->response_headers) conn->response_headers = newHV(); 20 0 20
829 100 ▶ for (hi = 0; hi < (int)(sizeof(resp_hdrs)/sizeof(resp_hdrs[0])); hi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (hi = 0; hi < (int)(sizeof(resp_hdrs)/sizeof(resp_hdrs[0])); hi++) { 80 20 100
830 80 capture_header(wsi, conn->response_headers, resp_hdrs[hi].tok,
831 80 resp_hdrs[hi].name, resp_hdrs[hi].nlen);
832 }
833 }
834 20 break;
835 }
836  
837 18 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: {
838 18 struct lws_vhost *vh = lws_get_vhost(wsi);
839 18 ▶ ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL; 18 0 18
840 18 ▶ if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->on_handshake) {
Branch: 3 branches
BranchTrueFalseTotal
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->on_handshake) { 18 0 18
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->on_handshake) { 18 0 18
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->on_handshake) { 0 18 18
841 0 HV *hdrs = newHV();
842 int count;
843 SV *result;
844 0 capture_request_headers(wsi, hdrs);
845  
846 0 dSP;
847 0 ENTER;
848 0 SAVETMPS;
849 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
850 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV*)hdrs)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV*)hdrs))); 0 0 0
851 0 PUTBACK;
852 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
853 0 count = call_sv(srv->on_handshake, G_SCALAR | G_EVAL);
854 0 SPAGAIN;
855 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
856 0 ▶ warn("EV::Websockets: exception in on_handshake: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Websockets: exception in on_handshake: %s", SvPV_nolen(ERRSV)); 0 0 0
857 0 ▶ if (count) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count) POPs; 0 0 0
858 0 PUTBACK;
859 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
860 0 LEAVE;
861 0 return -1;
862 }
863 0 ▶ result = count ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
result = count ? POPs : &PL_sv_undef; 0 0 0
864 0 ▶ if (!SvTRUE(result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(result)) { 0 0 0
865 0 PUTBACK;
866 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
867 0 LEAVE;
868 0 return -1;
869 }
870 0 ▶ if (SvROK(result) && SvTYPE(SvRV(result)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(result) && SvTYPE(SvRV(result)) == SVt_PVHV) { 0 0 0
if (SvROK(result) && SvTYPE(SvRV(result)) == SVt_PVHV) { 0 0 0
871 char key[32];
872 0 int klen = wsi_key(key, wsi);
873 0 SV *val = newRV_inc(SvRV(result));
874 0 ▶ if (!handshake_headers_map)
Branch: 1 branch
BranchTrueFalseTotal
if (!handshake_headers_map) 0 0 0
875 0 handshake_headers_map = newHV();
876 0 ▶ if (!hv_store(handshake_headers_map, key, klen, val, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(handshake_headers_map, key, klen, val, 0)) 0 0 0
877 0 SvREFCNT_dec(val);
878 }
879 0 PUTBACK;
880 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
881 0 LEAVE;
882 }
883 18 break;
884 }
885  
886 /* Connection callbacks */
887 18 case LWS_CALLBACK_ESTABLISHED:
888 18 ▶ if (!conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!conn) { 18 0 18
889 18 struct lws_vhost *vh = lws_get_vhost(wsi);
890 18 ▶ ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL; 18 0 18
891 18 ▶ if (ctx && srv && srv->magic == EV_WS_SRV_MAGIC) {
Branch: 3 branches
BranchTrueFalseTotal
if (ctx && srv && srv->magic == EV_WS_SRV_MAGIC) { 18 0 18
if (ctx && srv && srv->magic == EV_WS_SRV_MAGIC) { 18 0 18
if (ctx && srv && srv->magic == EV_WS_SRV_MAGIC) { 18 0 18
892 ev_ws_conn_t *c;
893 18 Newxz(c, 1, ev_ws_conn_t);
894 18 c->magic = EV_WS_CONN_MAGIC;
895 18 c->wsi = wsi;
896 18 c->refcnt = 1; /* For LWS */
897 18 c->max_message_size = srv->max_message_size;
898 18 c->loop = ctx->loop;
899 18 link_conn(ctx, c);
900 18 ▶ c->on_connect = srv->on_connect ? SvREFCNT_inc(srv->on_connect) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_connect = srv->on_connect ? SvREFCNT_inc(srv->on_connect) : NULL; 18 0 18
901 18 ▶ c->on_message = srv->on_message ? SvREFCNT_inc(srv->on_message) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_message = srv->on_message ? SvREFCNT_inc(srv->on_message) : NULL; 16 2 18
902 18 ▶ c->on_close = srv->on_close ? SvREFCNT_inc(srv->on_close) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_close = srv->on_close ? SvREFCNT_inc(srv->on_close) : NULL; 18 0 18
903 18 ▶ c->on_error = srv->on_error ? SvREFCNT_inc(srv->on_error) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_error = srv->on_error ? SvREFCNT_inc(srv->on_error) : NULL; 2 16 18
904 18 ▶ c->on_pong = srv->on_pong ? SvREFCNT_inc(srv->on_pong) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_pong = srv->on_pong ? SvREFCNT_inc(srv->on_pong) : NULL; 1 17 18
905 18 ▶ c->on_drain = srv->on_drain ? SvREFCNT_inc(srv->on_drain) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->on_drain = srv->on_drain ? SvREFCNT_inc(srv->on_drain) : NULL; 0 18 18
906  
907 18 c->response_headers = newHV();
908 18 capture_request_headers(wsi, c->response_headers);
909  
910 18 lws_set_wsi_user(wsi, c);
911 18 conn = c;
912 }
913 }
914 /* fallthrough */
915 case LWS_CALLBACK_CLIENT_ESTABLISHED:
916 38 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 38 0 38
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 38 0 38
917 38 ▶ DEBUG_LOG("Connected (reason %d): conn=%p", (int)reason, conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Connected (reason %d): conn=%p", (int)reason, conn); 0 38 38
918 38 stop_connect_timer(conn);
919 38 conn->connected = 1;
920 38 emit_connect(conn);
921 }
922 38 break;
923  
924 18 case LWS_CALLBACK_ADD_HEADERS: {
925 18 struct lws_process_html_args *args = (struct lws_process_html_args *)in;
926 18 unsigned char *p_end = (unsigned char *)args->p + args->max_len;
927 18 struct lws_vhost *vh = lws_get_vhost(wsi);
928 18 ▶ ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ev_ws_server_t *srv = vh ? (ev_ws_server_t *)lws_get_vhost_user(vh) : NULL; 18 0 18
929 18 ▶ if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->response_headers)
Branch: 3 branches
BranchTrueFalseTotal
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->response_headers) 18 0 18
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->response_headers) 18 0 18
if (srv && srv->magic == EV_WS_SRV_MAGIC && srv->response_headers) 0 18 18
930 0 inject_headers(wsi, srv->response_headers,
931 0 (unsigned char **)&args->p, p_end);
932 18 ▶ if (handshake_headers_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (handshake_headers_map) { 0 18 18
933 char key[32];
934 0 int klen = wsi_key(key, wsi);
935 0 SV *val = hv_delete(handshake_headers_map, key, klen, 0);
936 0 ▶ if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 0 0 0
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 0 0 0
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 0 0 0
937 0 inject_headers(wsi, (HV*)SvRV(val), (unsigned char **)&args->p, p_end);
938 }
939 18 break;
940 }
941  
942 3 case LWS_CALLBACK_RECEIVE_PONG:
943 case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
944 3 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 3 0 3
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 3 0 3
945 3 emit_pong(conn, (const char *)in, len);
946 }
947 3 break;
948  
949 33 case LWS_CALLBACK_RECEIVE:
950 case LWS_CALLBACK_CLIENT_RECEIVE:
951 33 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 33 0 33
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 33 0 33
952 33 int is_final = lws_is_final_fragment(wsi);
953 33 int is_binary = lws_frame_is_binary(wsi);
954 33 ▶ DEBUG_LOG("Received data (reason %d): len=%zu final=%d binary=%d", (int)reason, len, is_final, is_binary);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Received data (reason %d): len=%zu final=%d binary=%d", (int)reason, len, is_final, is_binary); 0 33 33
955  
956 33 ▶ if (lws_is_first_fragment(wsi)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lws_is_first_fragment(wsi)) { 31 2 33
957 /* A new message begins. If a completed message is still
958 buffered, deliver it first: with permessage-deflate lws
959 inflates one frame into several callbacks each flagged
960 "final", so we must reassemble across callbacks rather
961 than treat every final callback as a whole message. */
962 31 ▶ if (conn->recv_complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->recv_complete) { 0 31 31
963 0 conn_ref(conn);
964 0 ▶ emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1);
Branch: 1 branch
BranchTrueFalseTotal
emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1); 0 0 0
965 0 ▶ if (conn->magic != EV_WS_CONN_MAGIC) { conn_unref(conn); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->magic != EV_WS_CONN_MAGIC) { conn_unref(conn); break; } 0 0 0
966 0 conn_unref(conn);
967 }
968 31 conn->recv_len = 0;
969 31 conn->recv_complete = 0;
970 31 conn->recv_is_binary = is_binary;
971 }
972  
973 /* Enforce max message size */
974 33 ▶ if (conn->max_message_size > 0 && conn->recv_len + len > conn->max_message_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn->max_message_size > 0 && conn->recv_len + len > conn->max_message_size) { 3 30 33
if (conn->max_message_size > 0 && conn->recv_len + len > conn->max_message_size) { 1 2 3
975 1 conn_ref(conn);
976 1 emit_error(conn, "message exceeds max_message_size");
977 1 ▶ if (conn->magic == EV_WS_CONN_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC) { 1 0 1
978 1 Safefree(conn->recv_buf);
979 1 conn->recv_buf = NULL;
980 1 conn->recv_len = 0;
981 1 conn->recv_alloc = 0;
982 1 conn->recv_complete = 0;
983 }
984 1 conn_unref(conn);
985 1 return -1;
986 }
987  
988 /* Accumulate data */
989 32 ▶ if (conn->recv_len + len > conn->recv_alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->recv_len + len > conn->recv_alloc) { 28 4 32
990 28 ▶ size_t new_alloc = conn->recv_alloc ? conn->recv_alloc * 2 : 4096;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_alloc = conn->recv_alloc ? conn->recv_alloc * 2 : 4096; 0 28 28
991 28 ▶ while (new_alloc < conn->recv_len + len) new_alloc *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_alloc < conn->recv_len + len) new_alloc *= 2; 0 28 28
992 28 ▶ if (conn->max_message_size > 0 && new_alloc > conn->max_message_size)
Branch: 2 branches
BranchTrueFalseTotal
if (conn->max_message_size > 0 && new_alloc > conn->max_message_size) 2 26 28
if (conn->max_message_size > 0 && new_alloc > conn->max_message_size) 2 0 2
993 2 new_alloc = conn->max_message_size;
994 28 Renew(conn->recv_buf, new_alloc, char);
995 28 conn->recv_alloc = new_alloc;
996 }
997 32 ▶ if (len) /* guard: recv_buf may still be NULL for an empty frame */
Branch: 1 branch
BranchTrueFalseTotal
if (len) /* guard: recv_buf may still be NULL for an empty frame */ 32 0 32
998 32 memcpy(conn->recv_buf + conn->recv_len, in, len);
999 32 conn->recv_len += len;
1000 32 conn->recv_complete = is_final;
1001  
1002 /* Queue the connection so its completed message is delivered
1003 once the current lws service call drains all available input
1004 (see flush_recv_messages). is_final alone is unreliable under
1005 permessage-deflate, so we defer delivery: a fully-received
1006 message (recv_complete) is emitted either in the first-fragment
1007 branch above when the next message starts, or at flush time.
1008 The flush-list ref keeps conn alive until then. */
1009 32 ▶ if (!conn->on_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if (!conn->on_flush) { 31 1 32
1010 31 ev_ws_ctx_t* fctx = conn->ctx;
1011 31 conn_ref(conn);
1012 31 conn->flush_next = NULL;
1013 31 ▶ if (fctx->flush_tail)
Branch: 1 branch
BranchTrueFalseTotal
if (fctx->flush_tail) 1 30 31
1014 1 fctx->flush_tail->flush_next = conn;
1015 else
1016 30 fctx->flush_head = conn;
1017 31 fctx->flush_tail = conn;
1018 31 conn->on_flush = 1;
1019 }
1020 }
1021 32 break;
1022  
1023 69 case LWS_CALLBACK_SERVER_WRITEABLE:
1024 case LWS_CALLBACK_CLIENT_WRITEABLE:
1025 69 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 69 0 69
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 69 0 69
1026 ev_ws_send_t* send;
1027 int n;
1028 105 ▶ while (conn->send_head) {
Branch: 1 branch
BranchTrueFalseTotal
while (conn->send_head) { 36 69 105
1029 36 send = conn->send_head;
1030 36 ▶ DEBUG_LOG("Writing data: len=%zu mode=%d", send->len, (int)send->write_mode);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Writing data: len=%zu mode=%d", send->len, (int)send->write_mode); 0 36 36
1031 36 n = lws_write(wsi, (unsigned char*)send->data + LWS_PRE, send->len, send->write_mode);
1032  
1033 36 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 36 36
1034 0 lws_set_wsi_user(wsi, NULL);
1035 0 conn->wsi = NULL;
1036 0 conn->connected = 0;
1037 0 conn->in_fragmented_send = 0;
1038 0 unlink_conn(conn);
1039 0 conn_ref(conn);
1040 0 emit_error(conn, "write failed");
1041 0 conn_unref(conn);
1042 0 conn_unref(conn); /* drop wsi ref */
1043 0 return -1;
1044 }
1045  
1046 36 conn->send_head = send->next;
1047 36 ▶ if (conn->send_head == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->send_head == NULL) { 34 2 36
1048 34 conn->send_tail = NULL;
1049 }
1050 36 conn->send_queue_bytes -= send->len;
1051 36 Safefree(send);
1052  
1053 36 ▶ if (lws_send_pipe_choked(wsi)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lws_send_pipe_choked(wsi)) { 0 36 36
1054 0 lws_callback_on_writable(wsi);
1055 0 break;
1056 }
1057 }
1058 69 ▶ if (conn->closing && conn->send_head == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn->closing && conn->send_head == NULL) { 36 33 69
if (conn->closing && conn->send_head == NULL) { 36 0 36
1059 36 ▶ DEBUG_LOG("Closing connection via writeable callback");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Closing connection via writeable callback"); 0 36 36
1060 36 return -1;
1061 }
1062 33 ▶ if (conn->send_head == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->send_head == NULL) { 33 0 33
1063 33 emit_drain(conn); /* self-guards on a registered on_drain */
1064 }
1065 }
1066 33 break;
1067  
1068 1 case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
1069 1 ▶ DEBUG_LOG("CLIENT_CONNECTION_ERROR: conn=%p", conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("CLIENT_CONNECTION_ERROR: conn=%p", conn); 0 1 1
1070 1 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 1 0 1
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 1 0 1
1071 1 ▶ const char* err = in ? (const char*)in : "connection error";
Branch: 1 branch
BranchTrueFalseTotal
const char* err = in ? (const char*)in : "connection error"; 1 0 1
1072 1 stop_connect_timer(conn);
1073 /* Clear wsi user pointer so WSI_DESTROY (which follows) sees NULL
1074 and skips — we handle cleanup here to avoid double conn_unref
1075 if Context::DESTROY fires from within the callback. */
1076 1 lws_set_wsi_user(wsi, NULL);
1077 1 conn->wsi = NULL;
1078 1 conn->connected = 0;
1079 1 unlink_conn(conn);
1080 1 conn_ref(conn);
1081 1 emit_error(conn, err);
1082 1 conn_unref(conn);
1083 1 conn_unref(conn); /* drop wsi ref */
1084 }
1085 1 break;
1086  
1087 31 case LWS_CALLBACK_CLIENT_CLOSED:
1088 case LWS_CALLBACK_CLOSED:
1089 31 ▶ DEBUG_LOG("CLOSED: conn=%p", conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("CLOSED: conn=%p", conn); 0 31 31
1090 31 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 31 0 31
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 31 0 31
1091 31 int close_code = 1000;
1092 31 stop_connect_timer(conn);
1093 31 const char* close_reason = NULL;
1094 char reason_buf[126];
1095  
1096 31 ▶ if (in && len >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (in && len >= 2) { 0 31 31
if (in && len >= 2) { 0 0 0
1097 0 close_code = ((unsigned char *)in)[0] << 8 | ((unsigned char *)in)[1];
1098 0 ▶ if (len > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 2) { 0 0 0
1099 0 size_t rlen = len - 2;
1100 0 ▶ if (rlen > sizeof(reason_buf) - 1) rlen = sizeof(reason_buf) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (rlen > sizeof(reason_buf) - 1) rlen = sizeof(reason_buf) - 1; 0 0 0
1101 0 memcpy(reason_buf, (char *)in + 2, rlen);
1102 0 reason_buf[rlen] = '\0';
1103 0 close_reason = reason_buf;
1104 }
1105 }
1106  
1107 31 ▶ DEBUG_LOG("Emitting close: code=%d", close_code);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Emitting close: code=%d", close_code); 0 31 31
1108 31 lws_set_wsi_user(wsi, NULL);
1109 31 conn->connected = 0;
1110 31 conn->wsi = NULL;
1111 31 unlink_conn(conn);
1112 31 conn_ref(conn);
1113 /* Deliver a fully-received-but-not-yet-flushed message before
1114 on_close, so on_message precedes on_close even when the data
1115 and the close arrive in the same lws service pass (delivery is
1116 otherwise deferred to flush_recv_messages, which runs after the
1117 service returns -- i.e. after this CLOSED callback). */
1118 31 ▶ if (conn->recv_complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->recv_complete) { 0 31 31
1119 0 ▶ emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1);
Branch: 1 branch
BranchTrueFalseTotal
emit_message(conn, conn->recv_buf ? conn->recv_buf : "", conn->recv_len, conn->recv_is_binary, 1); 0 0 0
1120 0 ▶ if (conn->magic == EV_WS_CONN_MAGIC) { conn->recv_len = 0; conn->recv_complete = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC) { conn->recv_len = 0; conn->recv_complete = 0; } 0 0 0
1121 }
1122 31 ▶ if (conn->magic == EV_WS_CONN_MAGIC)
Branch: 1 branch
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC) 31 0 31
1123 31 emit_close(conn, close_code, close_reason);
1124 31 conn_unref(conn);
1125 31 conn_unref(conn); /* drop wsi ref */
1126 }
1127 31 break;
1128  
1129 43 case LWS_CALLBACK_PROTOCOL_DESTROY: {
1130 43 ▶ struct lws_vhost *vh = wsi ? lws_get_vhost(wsi) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
struct lws_vhost *vh = wsi ? lws_get_vhost(wsi) : NULL; 43 0 43
1131 43 ▶ if (vh) {
Branch: 1 branch
BranchTrueFalseTotal
if (vh) { 43 0 43
1132 43 ev_ws_server_t *srv = (ev_ws_server_t *)lws_get_vhost_user(vh);
1133 43 ▶ if (srv && (srv->magic == EV_WS_SRV_MAGIC || srv->magic == EV_WS_SRV_FREED)) {
Branch: 3 branches
BranchTrueFalseTotal
if (srv && (srv->magic == EV_WS_SRV_MAGIC || srv->magic == EV_WS_SRV_FREED)) { 42 1 43
if (srv && (srv->magic == EV_WS_SRV_MAGIC || srv->magic == EV_WS_SRV_FREED)) { 24 18 42
if (srv && (srv->magic == EV_WS_SRV_MAGIC || srv->magic == EV_WS_SRV_FREED)) { 0 24 24
1134 /* SRV_FREED means a failed listen() already dropped the SV
1135 refs but deliberately left protocol_name alive (the vhost
1136 still pointed at it); free it here, once, at teardown. */
1137 18 ▶ if (srv->magic == EV_WS_SRV_MAGIC)
Branch: 1 branch
BranchTrueFalseTotal
if (srv->magic == EV_WS_SRV_MAGIC) 18 0 18
1138 18 free_server_svs(srv);
1139 18 ▶ if (srv->protocol_name) Safefree(srv->protocol_name);
Branch: 1 branch
BranchTrueFalseTotal
if (srv->protocol_name) Safefree(srv->protocol_name); 0 18 18
1140 18 Safefree(srv);
1141 }
1142 }
1143 43 break;
1144 }
1145  
1146 60 case LWS_CALLBACK_WSI_DESTROY:
1147 60 ▶ if (handshake_headers_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (handshake_headers_map) { 0 60 60
1148 char key[32];
1149 0 int klen = wsi_key(key, wsi);
1150 0 hv_delete(handshake_headers_map, key, klen, G_DISCARD);
1151 }
1152 60 ▶ if (conn && conn->magic == EV_WS_CONN_MAGIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 0 60 60
if (conn && conn->magic == EV_WS_CONN_MAGIC) { 0 0 0
1153 0 ▶ DEBUG_LOG("WSI destroyed: conn=%p", conn);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("WSI destroyed: conn=%p", conn); 0 0 0
1154 0 conn->wsi = NULL;
1155 0 conn->connected = 0;
1156 0 unlink_conn(conn);
1157 0 conn_unref(conn); /* drop wsi ref; frees resources when refcnt hits 0 */
1158 }
1159 60 break;
1160  
1161 0 case LWS_CALLBACK_HTTP:
1162 /* A plain (non-upgrade) HTTP request arrived on a WebSocket
1163 listener. Without this case it fell through to "default: return
1164 0", which tells lws the transaction was handled -- but we never
1165 send a response, so the connection sat open until the peer gave
1166 up. Port scanners, health checks and browsers all hit this and
1167 accumulated half-open connections. Answer 426 and close. */
1168 0 lws_return_http_status(wsi, 426, "Upgrade Required");
1169 0 return -1;
1170  
1171 1201 default:
1172 1201 break;
1173 }
1174  
1175 2004 return 0;
1176 }
1177  
1178 static const struct lws_protocols protocols[] = {
1179 {
1180 EV_WS_PROTOCOL_NAME,
1181 ws_callback,
1182 0,
1183 65536, /* rx buffer size */
1184 0,
1185 NULL,
1186 0
1187 },
1188 { NULL, NULL, 0, 0, 0, NULL, 0 }
1189 };
1190  
1191 /* Pin libwebsockets' global TLS init for the whole process.
1192  
1193 lws refcounts the global OpenSSL init across contexts created with
1194 LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT. When that refcount falls to zero (the
1195 last such context is destroyed) lws runs OPENSSL_cleanup(), which OpenSSL
1196 1.1+/3.x cannot undo: creating another TLS context then fails, and lws's own
1197 error reporting dereferences torn-down state and crashes. So a program that
1198 destroys its TLS context and makes a new one (reconnect-with-fresh-ctx,
1199 worker recycling, test suites) would break.
1200  
1201 A TLS-using context still needs its own GLOBAL_INIT flag for its TLS to work
1202 (the flag is per-context: "initialize the SSL library at all"); we keep that.
1203 This keepalive is a single extra flagged context, created on first TLS use
1204 and never destroyed or serviced, that holds the refcount floor at >= 1 so no
1205 user context's teardown can trigger the cleanup. Returns 1 if held.
1206  
1207 Idempotent; single-threaded use only (like the rest of this module). */
1208 24 static int ensure_ssl_keepalive(void) {
1209 struct lws_context_creation_info info;
1210 24 ▶ if (ssl_keepalive_ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (ssl_keepalive_ctx) 7 17 24
1211 7 return 1;
1212 17 memset(&info, 0, sizeof(info));
1213 17 info.port = CONTEXT_PORT_NO_LISTEN;
1214 17 info.protocols = protocols;
1215 17 info.gid = -1;
1216 17 info.uid = -1;
1217 17 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
1218 17 ssl_keepalive_ctx = lws_create_context(&info);
1219 17 return ssl_keepalive_ctx != NULL;
1220 }
1221  
1222 MODULE = EV::Websockets PACKAGE = EV::Websockets
1223  
1224 BOOT:
1225 {
1226 20 ▶ I_EV_API("EV::Websockets");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Websockets"); 0 20 20
I_EV_API("EV::Websockets"); 20 0 20
I_EV_API("EV::Websockets"); 0 20 20
1227 20 lws_set_log_level(LLL_ERR | LLL_WARN, NULL);
1228 }
1229  
1230 void
1231 _set_debug(int enable);
1232 CODE:
1233 {
1234 0 ev_ws_debug = enable;
1235 0 ▶ if (enable)
Branch: 1 branch
BranchTrueFalseTotal
if (enable) 0 0 0
1236 0 lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG, NULL);
1237 else
1238 0 lws_set_log_level(LLL_ERR | LLL_WARN, NULL);
1239 }
1240  
1241 MODULE = EV::Websockets PACKAGE = EV::Websockets::Context
1242  
1243 EV::Websockets::Context
1244 _new(char* class, EV::Loop loop, const char* proxy = NULL, int proxy_port = 0, const char* ssl_cert = NULL, const char* ssl_key = NULL, const char* ssl_ca = NULL, int ssl_init = -1);
1245 CODE:
1246 {
1247 struct lws_context_creation_info info;
1248 void* foreign_loops[1];
1249  
1250 PERL_UNUSED_VAR(class);
1251  
1252 24 Newxz(RETVAL, 1, ev_ws_ctx_t);
1253 24 RETVAL->magic = EV_WS_CTX_MAGIC;
1254 24 RETVAL->refcnt = 1; /* Perl owns the context */
1255 24 RETVAL->loop = loop;
1256  
1257 24 foreign_loops[0] = loop;
1258  
1259 24 memset(&info, 0, sizeof(info));
1260 24 info.port = CONTEXT_PORT_NO_LISTEN;
1261 24 info.protocols = protocols;
1262 #ifdef LWS_HAS_EXTENSIONS
1263 24 info.extensions = extensions;
1264 #endif
1265 24 info.gid = -1;
1266 24 info.uid = -1;
1267 /* ssl_init: -1 = manage OpenSSL init (default); 1 = force; 0 = coexist
1268 (leave it to another TLS library). When we manage it, flag this context
1269 so its own TLS works, and also pin the global init in a process-lifetime
1270 keepalive so destroying this context can't drop lws's TLS refcount to
1271 zero (which would run OPENSSL_cleanup() and break later TLS use). */
1272 24 info.options = 0;
1273 24 ▶ if (ssl_init != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssl_init != 0) { 24 0 24
1274 24 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
1275 24 ensure_ssl_keepalive();
1276 }
1277 24 info.user = RETVAL;
1278 24 info.foreign_loops = foreign_loops;
1279 24 info.vhost_name = "default";
1280
1281 24 ▶ if (proxy && strlen(proxy) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (proxy && strlen(proxy) > 0) { 24 0 24
if (proxy && strlen(proxy) > 0) { 0 24 24
1282 0 ▶ DEBUG_LOG("Context using proxy: %s:%d", proxy, proxy_port);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Context using proxy: %s:%d", proxy, proxy_port); 0 0 0
1283 0 info.http_proxy_address = proxy;
1284 0 info.http_proxy_port = proxy_port;
1285 }
1286  
1287 24 ▶ if (ssl_cert && strlen(ssl_cert) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ssl_cert && strlen(ssl_cert) > 0) { 24 0 24
if (ssl_cert && strlen(ssl_cert) > 0) { 0 24 24
1288 0 info.ssl_cert_filepath = ssl_cert;
1289 0 info.ssl_private_key_filepath = ssl_key;
1290 0 ▶ if (ssl_ca && strlen(ssl_ca) > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ssl_ca && strlen(ssl_ca) > 0) 0 0 0
if (ssl_ca && strlen(ssl_ca) > 0) 0 0 0
1291 0 info.ssl_ca_filepath = ssl_ca;
1292 }
1293  
1294 24 ▶ DEBUG_LOG("Creating context (manual integration)");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Creating context (manual integration)"); 0 24 24
1295  
1296 24 RETVAL->lws_ctx = lws_create_context(&info);
1297 24 ▶ if (RETVAL->lws_ctx == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->lws_ctx == NULL) { 0 24 24
1298 0 Safefree(RETVAL);
1299 0 croak("Failed to create libwebsockets context");
1300 }
1301 24 ev_timer_init(&RETVAL->timer, timer_cb, 0.00001, 0.);
1302 24 RETVAL->timer.data = (void*)RETVAL;
1303  
1304 24 schedule_timeout(RETVAL);
1305  
1306 24 ▶ DEBUG_LOG("Context created successfully");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Context created successfully"); 0 24 24
1307 }
1308 OUTPUT:
1309 RETVAL
1310  
1311 void
1312 DESTROY(EV::Websockets::Context self);
1313 CODE:
1314 {
1315 ev_ws_conn_t* conn;
1316 ev_ws_conn_t* next;
1317  
1318 24 ▶ if (self->magic != EV_WS_CTX_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CTX_MAGIC) return; 0 24 24
1319  
1320 24 self->magic = EV_WS_CTX_FREED;
1321  
1322 24 ev_timer_stop(self->loop, &self->timer);
1323  
1324 24 free_all_fd_watchers(self);
1325  
1326 /* Release connections still queued for message delivery (no delivery during
1327 teardown); each holds a flush-list ref. They remain in self->connections
1328 and are torn down by the loop below. */
1329 24 ▶ while (self->flush_head) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->flush_head) { 0 24 24
1330 0 conn = self->flush_head;
1331 0 self->flush_head = conn->flush_next;
1332 0 conn->on_flush = 0;
1333 0 conn->flush_next = NULL;
1334 0 conn_unref(conn); /* release the flush-list ref */
1335 }
1336 24 self->flush_tail = NULL;
1337  
1338 /* Close all connections */
1339 34 ▶ for (conn = self->connections; conn != NULL; conn = next) {
Branch: 1 branch
BranchTrueFalseTotal
for (conn = self->connections; conn != NULL; conn = next) { 10 24 34
1340 10 next = conn->next;
1341 10 conn->ctx = NULL;
1342 10 conn->prev = NULL;
1343 10 conn->next = NULL;
1344 10 ▶ if (conn->wsi) {
Branch: 1 branch
BranchTrueFalseTotal
if (conn->wsi) { 10 0 10
1345 10 lws_set_wsi_user(conn->wsi, NULL);
1346 10 conn->wsi = NULL;
1347 }
1348 10 conn_unref(conn); /* drop wsi ref — may free conn */
1349 }
1350 24 self->connections = NULL;
1351  
1352 24 ▶ if (self->lws_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->lws_ctx) { 24 0 24
1353 24 lws_context_destroy(self->lws_ctx);
1354 24 self->lws_ctx = NULL;
1355 }
1356  
1357 24 self->loop = NULL;
1358 24 ▶ if (self->alive_flag) *self->alive_flag = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->alive_flag) *self->alive_flag = 0; 0 24 24
1359 24 ctx_unref(self); /* drops Perl ref; Safefree happens when refcnt==0 */
1360 }
1361  
1362 EV::Websockets::Connection
1363 connect(EV::Websockets::Context self, ...);
1364 PREINIT:
1365 struct lws_client_connect_info ccinfo;
1366 25 const char* url = NULL;
1367 25 const char* protocol = NULL;
1368 25 char* host = NULL;
1369 25 char* host_header = NULL; /* for IPv6: "[::1]" form */
1370 25 char* path = NULL;
1371 25 int port = 80;
1372 25 int use_ssl = 0;
1373 25 int ssl_verify = 1;
1374 25 char* url_copy = NULL;
1375 char* p;
1376 char* path_start;
1377 25 SV* on_connect = NULL;
1378 25 SV* on_message = NULL;
1379 25 SV* on_close = NULL;
1380 25 SV* on_error = NULL;
1381 25 SV* on_pong = NULL;
1382 25 SV* on_drain = NULL;
1383 25 SV* headers_hv = NULL;
1384 25 size_t max_message_size = 0;
1385 25 ▶ double connect_timeout = 0;
Branch: 1 branch
BranchTrueFalseTotal
double connect_timeout = 0; 25 0 25
1386 int i;
1387 CODE:
1388 {
1389 25 ▶ if (self->magic != EV_WS_CTX_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CTX_MAGIC) { 0 25 25
1390 0 croak("Context has been destroyed");
1391 }
1392  
1393 /* Options are key => value pairs. A dangling key used to be silently
1394 ignored, hiding typos and truncated argument lists. Checked before any
1395 callback SV is retained, so croaking here leaks nothing. */
1396 25 ▶ if (((items - 1) % 2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((items - 1) % 2) != 0) { 0 25 25
1397 0 croak("odd number of options: key '%s' has no value",
1398 SvPV_nolen(ST(items - 1)));
1399 }
1400  
1401 130 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 105 25 130
1402 105 ▶ if (i + 1 >= items) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= items) break; 0 105 105
1403 105 const char* key = SvPV_nolen(ST(i));
1404 105 SV* val = ST(i + 1);
1405  
1406 105 ▶ if (strcmp(key, "url") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "url") == 0) { 24 81 105
1407 24 url = SvPV_nolen(val);
1408 81 ▶ } else if (strcmp(key, "protocol") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "protocol") == 0) { 0 81 81
1409 0 protocol = SvPV_nolen(val);
1410 81 ▶ } else if (strcmp(key, "ssl_verify") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_verify") == 0) { 0 81 81
1411 0 ssl_verify = SvTRUE(val);
1412 81 ▶ } else if (strcmp(key, "max_message_size") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "max_message_size") == 0) { 0 81 81
1413 0 max_message_size = (size_t)SvUV(val);
1414 81 ▶ } else if (strcmp(key, "connect_timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "connect_timeout") == 0) { 0 81 81
1415 0 connect_timeout = SvNV(val);
1416 81 ▶ } else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 1 80 81
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 1 0 1
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 1 0 1
1417 1 headers_hv = val;
1418 80 ▶ } else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 61 80
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 0 19
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 0 19
1419 19 on_connect = SvREFCNT_inc(val);
1420 61 ▶ } else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 16 45 61
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 16 0 16
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 16 0 16
1421 16 on_message = SvREFCNT_inc(val);
1422 45 ▶ } else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 26 45
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 0 19
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 19 0 19
1423 19 on_close = SvREFCNT_inc(val);
1424 26 ▶ } else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 23 3 26
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 23 0 23
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 23 0 23
1425 23 on_error = SvREFCNT_inc(val);
1426 3 ▶ } else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 1 3
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 0 2
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 0 2
1427 2 on_pong = SvREFCNT_inc(val);
1428 1 ▶ } else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
1429 1 on_drain = SvREFCNT_inc(val);
1430 }
1431 }
1432  
1433 25 ▶ if (url == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (url == NULL) { 1 24 25
1434 1 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1435 1 croak("url parameter is required");
1436 }
1437  
1438 24 Newx(url_copy, strlen(url) + 1, char);
1439 24 strcpy(url_copy, url);
1440  
1441 24 ▶ if (strncasecmp(url_copy, "wss://", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncasecmp(url_copy, "wss://", 6) == 0) { 0 24 24
1442 0 use_ssl = LCCSCF_USE_SSL;
1443 0 ▶ if (!ssl_verify) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ssl_verify) { 0 0 0
1444 0 use_ssl |= LCCSCF_ALLOW_SELFSIGNED
1445 | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK
1446 | LCCSCF_ALLOW_INSECURE;
1447 }
1448 0 port = 443;
1449 0 host = url_copy + 6;
1450 24 ▶ } else if (strncasecmp(url_copy, "ws://", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncasecmp(url_copy, "ws://", 5) == 0) { 23 1 24
1451 23 host = url_copy + 5;
1452 } else {
1453 1 Safefree(url_copy);
1454 1 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1455 1 croak("URL must start with ws:// or wss://");
1456 }
1457  
1458 23 path_start = strchr(host, '/');
1459 23 ▶ if (path_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (path_start) { 0 23 23
1460 /* Allocate separate path string */
1461 0 Newx(path, strlen(path_start) + 1, char);
1462 0 strcpy(path, path_start);
1463 0 *path_start = '\0'; /* Terminate host */
1464 } else {
1465 23 Newx(path, 2, char);
1466 23 strcpy(path, "/");
1467 }
1468  
1469 /* Find port in host (handle IPv6 bracket notation) */
1470 23 ▶ if (host[0] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (host[0] == '[') { 0 23 23
1471 0 p = strchr(host, ']');
1472 0 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 0 0 0
1473 0 size_t iplen = p - host - 1;
1474 0 int hport = 0;
1475 0 host++; /* skip '[' */
1476 0 ▶ if (*(p + 1) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (*(p + 1) == ':') { 0 0 0
1477 0 hport = parse_port_strict(p + 2);
1478 0 ▶ if (hport < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hport < 0) { 0 0 0
1479 0 Safefree(url_copy); Safefree(path);
1480 0 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1481 0 croak("connect: invalid port in url (expected 1-65535)");
1482 }
1483 0 port = hport;
1484 }
1485 0 *p = '\0'; /* terminate IPv6 address */
1486 0 Newx(host_header, iplen + 10, char); /* [addr]:port\0 */
1487 0 ▶ if (hport && hport != (use_ssl ? 443 : 80))
Branch: 3 branches
BranchTrueFalseTotal
if (hport && hport != (use_ssl ? 443 : 80)) 0 0 0
if (hport && hport != (use_ssl ? 443 : 80)) 0 0 0
if (hport && hport != (use_ssl ? 443 : 80)) 0 0 0
1488 0 snprintf(host_header, iplen + 10, "[%s]:%d", host, hport);
1489 else
1490 0 snprintf(host_header, iplen + 10, "[%s]", host);
1491 }
1492 } else {
1493 23 p = strchr(host, ':');
1494 23 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 23 0 23
1495 23 *p = '\0';
1496 23 port = parse_port_strict(p + 1);
1497 23 ▶ if (port < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (port < 0) { 0 23 23
1498 0 Safefree(url_copy); Safefree(path);
1499 0 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1500 0 croak("connect: invalid port in url (expected 1-65535)");
1501 }
1502 }
1503 }
1504  
1505 23 Newxz(RETVAL, 1, ev_ws_conn_t);
1506 23 RETVAL->magic = EV_WS_CONN_MAGIC;
1507 23 RETVAL->refcnt = 2; /* wsi ref + sentinel (protects against sync WSI_DESTROY) */
1508 23 RETVAL->on_connect = on_connect;
1509 23 RETVAL->on_message = on_message;
1510 23 RETVAL->on_close = on_close;
1511 23 RETVAL->on_error = on_error;
1512 23 RETVAL->on_pong = on_pong;
1513 23 RETVAL->on_drain = on_drain;
1514 23 RETVAL->max_message_size = max_message_size;
1515 23 RETVAL->loop = self->loop;
1516  
1517 23 link_conn(self, RETVAL);
1518  
1519 23 memset(&ccinfo, 0, sizeof(ccinfo));
1520 23 ccinfo.context = self->lws_ctx;
1521 23 ccinfo.address = host;
1522 23 ccinfo.port = port;
1523 23 ccinfo.path = path;
1524 23 ▶ ccinfo.host = host_header ? host_header : host;
Branch: 1 branch
BranchTrueFalseTotal
ccinfo.host = host_header ? host_header : host; 0 23 23
1525 23 ▶ ccinfo.origin = host_header ? host_header : host;
Branch: 1 branch
BranchTrueFalseTotal
ccinfo.origin = host_header ? host_header : host; 0 23 23
1526 23 ccinfo.protocol = protocol;
1527 #ifdef LWS_HAS_EXTENSIONS
1528 23 ccinfo.client_exts = extensions;
1529 #endif
1530 23 ccinfo.ssl_connection = use_ssl;
1531 23 ccinfo.userdata = RETVAL;
1532  
1533 23 ▶ if (headers_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (headers_hv) { 1 22 23
1534 1 RETVAL->custom_headers = (HV*)SvREFCNT_inc(SvRV(headers_hv));
1535 }
1536  
1537 23 RETVAL->wsi = lws_client_connect_via_info(&ccinfo);
1538  
1539 23 Safefree(path);
1540 23 Safefree(host_header);
1541 23 Safefree(url_copy);
1542  
1543 23 ▶ if (RETVAL->wsi == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->wsi == NULL) { 0 23 23
1544 0 unlink_conn(RETVAL);
1545 0 ▶ if (RETVAL->perl_self == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->perl_self == NULL) { 0 0 0
1546 0 free_conn_resources(RETVAL);
1547 0 RETVAL->magic = EV_WS_CONN_FREED;
1548 0 Safefree(RETVAL);
1549 } else {
1550 0 conn_unref(RETVAL); /* drop sentinel; Perl DESTROY will handle final cleanup */
1551 }
1552 0 croak("Failed to initiate WebSocket connection");
1553 }
1554 23 conn_unref(RETVAL); /* drop sentinel */
1555  
1556 23 ▶ if (connect_timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect_timeout > 0) { 0 23 23
1557 0 ev_timer_init(&RETVAL->connect_timer, connect_timeout_cb, connect_timeout, 0.);
1558 0 RETVAL->connect_timer.data = (void*)RETVAL;
1559 0 ev_timer_start(self->loop, &RETVAL->connect_timer);
1560 0 RETVAL->connect_timer_active = 1;
1561 }
1562 }
1563 OUTPUT:
1564 RETVAL
1565  
1566 int
1567 listen(EV::Websockets::Context self, ...);
1568 PREINIT:
1569 struct lws_context_creation_info info;
1570 19 int port = 0;
1571 19 const char *name = "server";
1572 19 const char *ssl_cert = NULL;
1573 19 const char *ssl_key = NULL;
1574 19 const char *ssl_ca = NULL;
1575 19 SV* on_connect = NULL;
1576 19 SV* on_message = NULL;
1577 19 SV* on_close = NULL;
1578 19 SV* on_error = NULL;
1579 19 SV* on_pong = NULL;
1580 19 SV* on_drain = NULL;
1581 19 SV* on_handshake = NULL;
1582 19 SV* headers_hv = NULL;
1583 19 size_t max_message_size = 0;
1584 19 const char *protocol_name = NULL;
1585 19 ▶ const char *undef_opt = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *undef_opt = NULL; 19 0 19
1586 ev_ws_server_t *srv;
1587 struct lws_vhost *vh;
1588 int i;
1589 CODE:
1590 {
1591 19 ▶ if (self->magic != EV_WS_CTX_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CTX_MAGIC) { 0 19 19
1592 0 croak("Context has been destroyed");
1593 }
1594  
1595 /* Options are key => value pairs. A dangling key used to be silently
1596 ignored, hiding typos and truncated argument lists. Checked before any
1597 callback SV is retained, so croaking here leaks nothing. */
1598 19 ▶ if (((items - 1) % 2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((items - 1) % 2) != 0) { 0 19 19
1599 0 croak("odd number of options: key '%s' has no value",
1600 SvPV_nolen(ST(items - 1)));
1601 }
1602  
1603 96 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 77 19 96
1604 77 ▶ if (i + 1 >= items) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= items) break; 0 77 77
1605 77 const char* key = SvPV_nolen(ST(i));
1606 77 SV* val = ST(i + 1);
1607  
1608 77 ▶ if (strcmp(key, "port") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "port") == 0) { 19 58 77
1609 19 port = SvIV(val);
1610 58 ▶ } else if (strcmp(key, "name") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "name") == 0) { 1 57 58
1611 1 ▶ if (!SvOK(val)) { if (!undef_opt) undef_opt = "name"; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val)) { if (!undef_opt) undef_opt = "name"; } 0 1 1
if (!SvOK(val)) { if (!undef_opt) undef_opt = "name"; } 0 0 0
1612 1 else name = SvPV_nolen(val);
1613 57 ▶ } else if (strcmp(key, "protocol") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "protocol") == 0) { 0 57 57
1614 0 ▶ if (!SvOK(val)) { if (!undef_opt) undef_opt = "protocol"; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val)) { if (!undef_opt) undef_opt = "protocol"; } 0 0 0
if (!SvOK(val)) { if (!undef_opt) undef_opt = "protocol"; } 0 0 0
1615 0 else protocol_name = SvPV_nolen(val);
1616 57 ▶ } else if (strcmp(key, "ssl_cert") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_cert") == 0) { 0 57 57
1617 0 ▶ if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_cert"; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_cert"; } 0 0 0
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_cert"; } 0 0 0
1618 0 else ssl_cert = SvPV_nolen(val);
1619 57 ▶ } else if (strcmp(key, "ssl_key") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_key") == 0) { 0 57 57
1620 0 ▶ if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_key"; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_key"; } 0 0 0
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_key"; } 0 0 0
1621 0 else ssl_key = SvPV_nolen(val);
1622 57 ▶ } else if (strcmp(key, "ssl_ca") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_ca") == 0) { 0 57 57
1623 0 ▶ if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_ca"; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_ca"; } 0 0 0
if (!SvOK(val)) { if (!undef_opt) undef_opt = "ssl_ca"; } 0 0 0
1624 0 else ssl_ca = SvPV_nolen(val);
1625 57 ▶ } else if (strcmp(key, "max_message_size") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "max_message_size") == 0) { 2 55 57
1626 2 max_message_size = (size_t)SvUV(val);
1627 55 ▶ } else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 0 55 55
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 0 0 0
} else if (strcmp(key, "headers") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 0 0 0
1628 0 headers_hv = val;
1629 55 ▶ } else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 18 37 55
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 18 0 18
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 18 0 18
1630 18 on_connect = SvREFCNT_inc(val);
1631 37 ▶ } else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 20 37
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 0 17
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 0 17
1632 17 on_message = SvREFCNT_inc(val);
1633 20 ▶ } else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 3 20
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 0 17
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 17 0 17
1634 17 on_close = SvREFCNT_inc(val);
1635 3 ▶ } else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 1 3
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 0 2
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 0 2
1636 2 on_error = SvREFCNT_inc(val);
1637 1 ▶ } else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
1638 1 on_pong = SvREFCNT_inc(val);
1639 0 ▶ } else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
1640 0 on_drain = SvREFCNT_inc(val);
1641 0 ▶ } else if (strcmp(key, "on_handshake") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_handshake") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_handshake") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_handshake") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
1642 0 on_handshake = SvREFCNT_inc(val);
1643 }
1644 }
1645  
1646 /* An undef string option used to stringify to "" (with an "uninitialized
1647 value" warning), e.g. name => undef silently created a vhost named "". */
1648 19 ▶ if (undef_opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (undef_opt) { 0 19 19
1649 0 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, on_handshake);
1650 0 croak("listen: option '%s' must be a defined string", undef_opt);
1651 }
1652  
1653 19 ▶ if (strcmp(name, "default") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "default") == 0) { 1 18 19
1654 1 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, on_handshake);
1655 1 croak("listen: vhost name 'default' is reserved");
1656 }
1657  
1658 /* TLS needs both halves. Accepting just one used to silently create a
1659 PLAINTEXT listener, serving cleartext on a port the caller believes is
1660 encrypted -- fail loudly instead. */
1661 18 ▶ if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) {
Branch: 5 branches
BranchTrueFalseTotal
if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) { 0 18 18
if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) { 0 0 0
if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) { 0 18 18
if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) { 0 0 0
if ((ssl_cert && *ssl_cert) != (ssl_key && *ssl_key)) { 0 18 18
1662 0 ▶ int have_cert = ssl_cert && *ssl_cert;
Branch: 2 branches
BranchTrueFalseTotal
int have_cert = ssl_cert && *ssl_cert; 0 0 0
int have_cert = ssl_cert && *ssl_cert; 0 0 0
1663 0 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, on_handshake);
1664 0 ▶ croak("listen: ssl_cert and ssl_key must both be set for TLS (only %s was given)",
Branch: 1 branch
BranchTrueFalseTotal
croak("listen: ssl_cert and ssl_key must both be set for TLS (only %s was given)", 0 0 0
1665 have_cert ? "ssl_cert" : "ssl_key");
1666 }
1667  
1668 18 Newxz(srv, 1, ev_ws_server_t);
1669 18 srv->magic = EV_WS_SRV_MAGIC;
1670 18 srv->on_connect = on_connect;
1671 18 srv->on_message = on_message;
1672 18 srv->on_close = on_close;
1673 18 srv->on_error = on_error;
1674 18 srv->on_pong = on_pong;
1675 18 srv->on_drain = on_drain;
1676 18 srv->on_handshake = on_handshake;
1677 18 srv->max_message_size = max_message_size;
1678 18 ▶ if (headers_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (headers_hv) 0 18 18
1679 0 srv->response_headers = (HV*)SvREFCNT_inc(SvRV(headers_hv));
1680  
1681 18 ▶ if (protocol_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol_name) { 0 18 18
1682 0 STRLEN pnlen = strlen(protocol_name);
1683 0 Newx(srv->protocol_name, pnlen + 1, char);
1684 0 memcpy(srv->protocol_name, protocol_name, pnlen + 1);
1685 0 srv->vhost_protocols[0] = protocols[0];
1686 0 srv->vhost_protocols[0].name = srv->protocol_name;
1687 0 srv->vhost_protocols[1] = protocols[1];
1688 }
1689  
1690 18 memset(&info, 0, sizeof(info));
1691 18 info.port = port;
1692 18 ▶ info.protocols = srv->protocol_name ? srv->vhost_protocols : protocols;
Branch: 1 branch
BranchTrueFalseTotal
info.protocols = srv->protocol_name ? srv->vhost_protocols : protocols; 0 18 18
1693 18 info.vhost_name = name;
1694 18 info.user = srv;
1695 18 info.options = 0;
1696  
1697 18 ▶ if (ssl_cert && *ssl_cert && ssl_key && *ssl_key) {
Branch: 4 branches
BranchTrueFalseTotal
if (ssl_cert && *ssl_cert && ssl_key && *ssl_key) { 0 18 18
if (ssl_cert && *ssl_cert && ssl_key && *ssl_key) { 0 0 0
if (ssl_cert && *ssl_cert && ssl_key && *ssl_key) { 0 0 0
if (ssl_cert && *ssl_cert && ssl_key && *ssl_key) { 0 0 0
1698 0 info.ssl_cert_filepath = ssl_cert;
1699 0 info.ssl_private_key_filepath = ssl_key;
1700 0 ▶ if (ssl_ca && *ssl_ca)
Branch: 2 branches
BranchTrueFalseTotal
if (ssl_ca && *ssl_ca) 0 0 0
if (ssl_ca && *ssl_ca) 0 0 0
1701 0 info.ssl_ca_filepath = ssl_ca;
1702 0 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
1703 0 ensure_ssl_keepalive(); /* pin global init so vhost/context teardown won't OPENSSL_cleanup */
1704 }
1705  
1706 18 vh = lws_create_vhost(self->lws_ctx, &info);
1707 18 ▶ if (vh == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (vh == NULL) { 0 18 18
1708 0 free_server_svs(srv);
1709 0 ▶ if (srv->protocol_name) Safefree(srv->protocol_name);
Branch: 1 branch
BranchTrueFalseTotal
if (srv->protocol_name) Safefree(srv->protocol_name); 0 0 0
1710 0 Safefree(srv);
1711 0 croak("Failed to create vhost for listening");
1712 }
1713
1714 18 RETVAL = lws_get_vhost_listen_port(vh);
1715 18 ▶ if (RETVAL <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL <= 0) { 0 18 18
1716 /* Vhost created but port bind failed. Release the SV refs now, but
1717 leave protocol_name alive — the live vhost still points at it via
1718 vhost_protocols[0].name, so freeing it here would dangle. Do NOT
1719 Safefree(srv): the vhost retains the pointer. PROTOCOL_DESTROY frees
1720 protocol_name and srv at context teardown; the SRV_FREED sentinel
1721 tells it to skip the (already-released) SV refs. */
1722 0 free_server_svs(srv);
1723 0 srv->magic = EV_WS_SRV_FREED;
1724 0 croak("listen: failed to bind port");
1725 }
1726 18 ▶ DEBUG_LOG("Server listening on port %d", RETVAL);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Server listening on port %d", RETVAL); 0 18 18
1727 }
1728 OUTPUT:
1729 RETVAL
1730  
1731 EV::Websockets::Connection
1732 adopt(EV::Websockets::Context self, ...);
1733 PREINIT:
1734 3 int fd = -1;
1735 3 SV* fh_sv = NULL;
1736 3 SV* initial_data_sv = NULL;
1737 3 SV* on_connect = NULL;
1738 3 SV* on_message = NULL;
1739 3 SV* on_close = NULL;
1740 3 SV* on_error = NULL;
1741 3 SV* on_pong = NULL;
1742 3 SV* on_drain = NULL;
1743 3 ▶ size_t max_message_size = 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t max_message_size = 0; 3 0 3
1744 int i;
1745 CODE:
1746 {
1747 3 ▶ if (self->magic != EV_WS_CTX_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CTX_MAGIC) { 0 3 3
1748 0 croak("Context has been destroyed");
1749 }
1750  
1751 /* Options are key => value pairs. A dangling key used to be silently
1752 ignored, hiding typos and truncated argument lists. Checked before any
1753 callback SV is retained, so croaking here leaks nothing. */
1754 3 ▶ if (((items - 1) % 2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((items - 1) % 2) != 0) { 0 3 3
1755 0 croak("odd number of options: key '%s' has no value",
1756 SvPV_nolen(ST(items - 1)));
1757 }
1758  
1759 10 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 7 3 10
1760 7 ▶ if (i + 1 >= items) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= items) break; 0 7 7
1761 7 const char* key = SvPV_nolen(ST(i));
1762 7 SV* val = ST(i + 1);
1763  
1764 7 ▶ if (strcmp(key, "fh") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "fh") == 0) { 2 5 7
1765 2 fh_sv = val;
1766 5 ▶ } else if (strcmp(key, "initial_data") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "initial_data") == 0) { 0 5 5
1767 0 initial_data_sv = val;
1768 5 ▶ } else if (strcmp(key, "max_message_size") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "max_message_size") == 0) { 0 5 5
1769 0 max_message_size = (size_t)SvUV(val);
1770 5 ▶ } else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 4 5
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_connect") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
1771 1 on_connect = SvREFCNT_inc(val);
1772 4 ▶ } else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 3 1 4
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 3 0 3
} else if (strcmp(key, "on_message") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 3 0 3
1773 3 on_message = SvREFCNT_inc(val);
1774 1 ▶ } else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 1 1
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_close") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
1775 0 on_close = SvREFCNT_inc(val);
1776 1 ▶ } else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
} else if (strcmp(key, "on_error") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 1 0 1
1777 1 on_error = SvREFCNT_inc(val);
1778 0 ▶ } else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_pong") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
1779 0 on_pong = SvREFCNT_inc(val);
1780 0 ▶ } else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
} else if (strcmp(key, "on_drain") == 0 && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 0 0 0
1781 0 on_drain = SvREFCNT_inc(val);
1782 }
1783 }
1784  
1785 3 ▶ if (fh_sv == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh_sv == NULL) { 1 2 3
1786 1 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1787 1 croak("fh parameter is required");
1788 }
1789  
1790 2 IO* io = sv_2io(fh_sv);
1791 2 ▶ PerlIO *ifp = io ? IoIFP(io) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PerlIO *ifp = io ? IoIFP(io) : NULL; 2 0 2
1792 2 ▶ if (!ifp || (fd = PerlIO_fileno(ifp)) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ifp || (fd = PerlIO_fileno(ifp)) < 0) { 1 1 2
if (!ifp || (fd = PerlIO_fileno(ifp)) < 0) { 0 1 1
1793 1 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1794 1 croak("Invalid filehandle");
1795 }
1796  
1797 { /* lws happily adopts a pipe (or any fd it can poll) and then takes it
1798 over -- adopting STDOUT, for instance, breaks the process's stdout.
1799 getsockname() fails with ENOTSOCK for non-sockets, so use it to fail
1800 fast on misuse. */
1801 struct sockaddr_storage ss;
1802 1 socklen_t sslen = (socklen_t)sizeof(ss);
1803 1 ▶ if (getsockname(fd, (struct sockaddr*)&ss, &sslen) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (getsockname(fd, (struct sockaddr*)&ss, &sslen) != 0) { 0 1 1
1804 0 free_cb_svs(on_connect, on_message, on_close, on_error, on_pong, on_drain, NULL);
1805 0 croak("adopt: file handle is not a socket");
1806 }
1807 }
1808  
1809 1 Newxz(RETVAL, 1, ev_ws_conn_t);
1810 1 RETVAL->magic = EV_WS_CONN_MAGIC;
1811 1 RETVAL->refcnt = 2; /* wsi ref + sentinel (protects against sync WSI_DESTROY) */
1812 1 RETVAL->on_connect = on_connect;
1813 1 RETVAL->on_message = on_message;
1814 1 RETVAL->on_close = on_close;
1815 1 RETVAL->on_error = on_error;
1816 1 RETVAL->on_pong = on_pong;
1817 1 RETVAL->on_drain = on_drain;
1818 1 RETVAL->max_message_size = max_message_size;
1819 1 RETVAL->loop = self->loop;
1820 /* Hold a reference to the underlying glob/IO to prevent Perl
1821 * from closing the fd while lws owns it. For blessed glob refs
1822 * (IO::Socket etc.) we ref the glob itself so framework DESTROY
1823 * methods see it as still alive. */
1824 1 RETVAL->adopted_fh = SvROK(fh_sv) ? newRV_inc(SvRV(fh_sv))
1825 1 ▶ : SvREFCNT_inc(fh_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvREFCNT_inc(fh_sv); 1 0 1
1826  
1827 1 link_conn(self, RETVAL);
1828  
1829 {
1830 1 struct lws_vhost *vh = lws_get_vhost_by_name(self->lws_ctx, "server");
1831 1 ▶ if (!vh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!vh) { 1 0 1
1832 /* Auto-create a server vhost for adoption (no listener needed) */
1833 struct lws_context_creation_info vinfo;
1834 1 memset(&vinfo, 0, sizeof(vinfo));
1835 1 vinfo.port = CONTEXT_PORT_NO_LISTEN_SERVER;
1836 1 vinfo.protocols = protocols;
1837 1 vinfo.vhost_name = "server";
1838 1 vh = lws_create_vhost(self->lws_ctx, &vinfo);
1839 }
1840 1 ▶ if (!vh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!vh) { 0 1 1
1841 0 unlink_conn(RETVAL);
1842 0 free_conn_resources(RETVAL);
1843 0 RETVAL->magic = EV_WS_CONN_FREED;
1844 0 Safefree(RETVAL);
1845 0 croak("Failed to create vhost for adoption");
1846 }
1847 1 pending_adoption = RETVAL;
1848 1 ▶ if (initial_data_sv && SvOK(initial_data_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (initial_data_sv && SvOK(initial_data_sv)) { 0 1 1
if (initial_data_sv && SvOK(initial_data_sv)) { 0 0 0
1849 STRLEN rdlen;
1850 0 const char *rdbuf = SvPV(initial_data_sv, rdlen);
1851 0 RETVAL->wsi = lws_adopt_socket_vhost_readbuf(vh,
1852 (lws_sockfd_type)fd, rdbuf, rdlen);
1853 } else {
1854 1 RETVAL->wsi = lws_adopt_socket_vhost(vh, (lws_sockfd_type)fd);
1855 }
1856 1 pending_adoption = NULL;
1857 }
1858  
1859 1 ▶ if (RETVAL->wsi == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->wsi == NULL) { 0 1 1
1860 0 unlink_conn(RETVAL);
1861 0 ▶ if (RETVAL->perl_self == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->perl_self == NULL) { 0 0 0
1862 0 free_conn_resources(RETVAL);
1863 0 RETVAL->magic = EV_WS_CONN_FREED;
1864 0 Safefree(RETVAL);
1865 } else {
1866 0 conn_unref(RETVAL);
1867 }
1868 0 croak("Failed to adopt socket");
1869 }
1870 1 conn_unref(RETVAL); /* drop sentinel */
1871  
1872 /* Kick lws to process the adopted socket's readbuf (needed for lws 4.5+).
1873 * Use the same non-blocking forced-service call as do_lws_service: the
1874 * readbuf is pending work, so lws_service_tsi(ctx, -1, 0) drains it without
1875 * blocking. A plain lws_service(ctx, 0) would block the EV loop here when a
1876 * socket is adopted with no immediately-pending data.
1877 * Guard with extra refs: the service call may synchronously fire
1878 * error/destroy callbacks that would free RETVAL or ctx. */
1879 {
1880 1 int rejected, alive = 1;
1881 1 int* prev_flag = self->alive_flag;
1882 1 conn_ref(RETVAL);
1883 1 ctx_ref(self);
1884 1 self->alive_flag = &alive;
1885 1 lws_service_tsi(self->lws_ctx, -1, 0);
1886 1 ▶ if (alive)
Branch: 1 branch
BranchTrueFalseTotal
if (alive) 1 0 1
1887 1 flush_recv_messages(self); /* deliver any reassembled message */
1888 1 ▶ if (alive) {
Branch: 1 branch
BranchTrueFalseTotal
if (alive) { 1 0 1
1889 1 self->alive_flag = prev_flag;
1890 1 schedule_timeout(self);
1891 0 ▶ } else if (prev_flag) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (prev_flag) { 0 0 0
1892 /* Context destroyed during inner lws_service.
1893 Propagate destruction up the alive_flag chain. */
1894 0 *prev_flag = 0;
1895 }
1896 1 rejected = (RETVAL->wsi == NULL);
1897 1 conn_unref(RETVAL);
1898 1 ctx_unref(self);
1899 1 ▶ if (rejected)
Branch: 1 branch
BranchTrueFalseTotal
if (rejected) 0 1 1
1900 0 croak("Failed to adopt socket");
1901 }
1902 }
1903 OUTPUT:
1904 RETVAL
1905  
1906 void
1907 connections(EV::Websockets::Context self);
1908 PPCODE:
1909 {
1910 ev_ws_conn_t* conn;
1911 2 ▶ if (self->magic != EV_WS_CTX_MAGIC) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CTX_MAGIC) XSRETURN_EMPTY; 0 2 2
1912 8 ▶ for (conn = self->connections; conn != NULL; conn = conn->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (conn = self->connections; conn != NULL; conn = conn->next) { 6 2 8
1913 /* "connected" stays set during the "closing" drain (close() never
1914 clears it), and is cleared together with wsi on teardown — so this
1915 single predicate covers both the "connected" and "closing" states. */
1916 6 ▶ if (conn->magic == EV_WS_CONN_MAGIC && conn->connected) {
Branch: 2 branches
BranchTrueFalseTotal
if (conn->magic == EV_WS_CONN_MAGIC && conn->connected) { 6 0 6
if (conn->magic == EV_WS_CONN_MAGIC && conn->connected) { 4 2 6
1917 4 ▶ XPUSHs(sv_2mortal(get_conn_sv(conn)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_conn_sv(conn))); 0 4 4
1918 }
1919 }
1920 }
1921  
1922 MODULE = EV::Websockets PACKAGE = EV::Websockets::Connection
1923  
1924 void
1925 DESTROY(EV::Websockets::Connection self);
1926 CODE:
1927 {
1928 42 ▶ if (self->magic != EV_WS_CONN_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CONN_MAGIC) return; 0 42 42
1929  
1930 42 ▶ DEBUG_LOG("Perl object DESTROY: self=%p wsi=%p", self, self->wsi);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_LOG("Perl object DESTROY: self=%p wsi=%p", self, self->wsi); 0 42 42
1931  
1932 /* Clear the cached Perl object pointer in the C struct */
1933 42 self->perl_self = NULL;
1934  
1935 42 conn_unref(self); /* drop Perl ref */
1936 }
1937  
1938 void
1939 send(EV::Websockets::Connection self, SV* data);
1940 CODE:
1941 {
1942 STRLEN len;
1943 const char* buf;
1944  
1945 28 ▶ CHECK_CONN_OPEN(self);
Branch: 4 branches
BranchTrueFalseTotal
CHECK_CONN_OPEN(self); 0 28 28
CHECK_CONN_OPEN(self); 28 0 28
CHECK_CONN_OPEN(self); 28 0 28
CHECK_CONN_OPEN(self); 0 28 28
1946 28 ▶ CHECK_NOT_FRAGMENTING(self);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NOT_FRAGMENTING(self); 1 27 28
1947  
1948 27 buf = SvPV(data, len);
1949 27 queue_send(self, buf, len, LWS_WRITE_TEXT);
1950 }
1951  
1952 void
1953 send_binary(EV::Websockets::Connection self, SV* data);
1954 CODE:
1955 {
1956 STRLEN len;
1957 const char* buf;
1958  
1959 4 ▶ CHECK_CONN_OPEN(self);
Branch: 4 branches
BranchTrueFalseTotal
CHECK_CONN_OPEN(self); 0 4 4
CHECK_CONN_OPEN(self); 4 0 4
CHECK_CONN_OPEN(self); 4 0 4
CHECK_CONN_OPEN(self); 0 4 4
1960 4 ▶ CHECK_NOT_FRAGMENTING(self);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NOT_FRAGMENTING(self); 1 3 4
1961  
1962 3 buf = SvPV(data, len);
1963 3 queue_send(self, buf, len, LWS_WRITE_BINARY);
1964 }
1965  
1966 void
1967 send_ping(EV::Websockets::Connection self, SV* data = NULL);
1968 CODE:
1969 {
1970 3 STRLEN len = 0;
1971 3 const char* buf = NULL;
1972
1973 3 ▶ CHECK_CONN_OPEN(self);
Branch: 4 branches
BranchTrueFalseTotal
CHECK_CONN_OPEN(self); 0 3 3
CHECK_CONN_OPEN(self); 3 0 3
CHECK_CONN_OPEN(self); 3 0 3
CHECK_CONN_OPEN(self); 0 3 3
1974
1975 3 ▶ if (data && SvOK(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (data && SvOK(data)) { 3 0 3
if (data && SvOK(data)) { 3 0 3
1976 3 buf = SvPV(data, len);
1977 3 ▶ if (len > 125) len = 125; /* PING payload limit */
Branch: 1 branch
BranchTrueFalseTotal
if (len > 125) len = 125; /* PING payload limit */ 0 3 3
1978 }
1979
1980 3 queue_send(self, buf, len, LWS_WRITE_PING);
1981 }
1982  
1983 SV*
1984 get_protocol(EV::Websockets::Connection self);
1985 CODE:
1986 {
1987 2 SV* val = NULL;
1988 2 ▶ if (self->magic == EV_WS_CONN_MAGIC && self->wsi)
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == EV_WS_CONN_MAGIC && self->wsi) 2 0 2
if (self->magic == EV_WS_CONN_MAGIC && self->wsi) 2 0 2
1989 2 val = hdr_to_sv(self->wsi, WSI_TOKEN_PROTOCOL);
1990 2 ▶ RETVAL = val ? val : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = val ? val : newSV(0); 2 0 2
1991 }
1992 OUTPUT:
1993 RETVAL
1994  
1995 SV*
1996 peer_address(EV::Websockets::Connection self);
1997 CODE:
1998 {
1999 char buf[128];
2000 2 buf[0] = '\0';
2001 2 RETVAL = newSV(0);
2002 2 ▶ if (self->magic == EV_WS_CONN_MAGIC && self->wsi) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == EV_WS_CONN_MAGIC && self->wsi) { 2 0 2
if (self->magic == EV_WS_CONN_MAGIC && self->wsi) { 2 0 2
2003 2 lws_get_peer_simple(self->wsi, buf, sizeof(buf));
2004 2 ▶ if (buf[0])
Branch: 1 branch
BranchTrueFalseTotal
if (buf[0]) 2 0 2
2005 2 sv_setpv(RETVAL, buf);
2006 }
2007 }
2008 OUTPUT:
2009 RETVAL
2010  
2011 void
2012 send_pong(EV::Websockets::Connection self, SV* data = NULL);
2013 CODE:
2014 {
2015 1 STRLEN len = 0;
2016 1 const char* buf = NULL;
2017  
2018 1 ▶ CHECK_CONN_OPEN(self);
Branch: 4 branches
BranchTrueFalseTotal
CHECK_CONN_OPEN(self); 0 1 1
CHECK_CONN_OPEN(self); 1 0 1
CHECK_CONN_OPEN(self); 1 0 1
CHECK_CONN_OPEN(self); 0 1 1
2019  
2020 1 ▶ if (data && SvOK(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (data && SvOK(data)) { 1 0 1
if (data && SvOK(data)) { 1 0 1
2021 1 buf = SvPV(data, len);
2022 1 ▶ if (len > 125) len = 125;
Branch: 1 branch
BranchTrueFalseTotal
if (len > 125) len = 125; 0 1 1
2023 }
2024  
2025 1 queue_send(self, buf, len, LWS_WRITE_PONG);
2026 }
2027  
2028 void
2029 pause_recv(EV::Websockets::Connection self);
2030 CODE:
2031 {
2032 1 ▶ if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected)
Branch: 3 branches
BranchTrueFalseTotal
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
2033 1 lws_rx_flow_control(self->wsi, 0);
2034 }
2035  
2036 void
2037 resume_recv(EV::Websockets::Connection self);
2038 CODE:
2039 {
2040 1 ▶ if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected)
Branch: 3 branches
BranchTrueFalseTotal
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
if (self->magic == EV_WS_CONN_MAGIC && self->wsi && self->connected) 1 0 1
2041 1 lws_rx_flow_control(self->wsi, 1);
2042 }
2043  
2044 void
2045 close(EV::Websockets::Connection self, int code = 1000, const char* reason = NULL);
2046 CODE:
2047 {
2048 /* RFC 6455 7.4: only these may be sent on the wire. 1004/1005/1006/1015 are
2049 reserved and MUST NOT be sent, and anything outside the ranges is invalid
2050 -- previously any int was passed straight through, so close(1005) went out
2051 verbatim and close(70000) silently wrapped to 4464. */
2052 22 ▶ if (!((code >= 1000 && code <= 1003) ||
Branch: 4 branches
BranchTrueFalseTotal
if (!((code >= 1000 && code <= 1003) || 21 0 21
if (!((code >= 1000 && code <= 1003) || 1 20 21
if (!((code >= 1000 && code <= 1003) || 1 0 1
if (!((code >= 1000 && code <= 1003) || 1 0 1
2053 1 ▶ (code >= 1007 && code <= 1014) ||
Branch: 1 branch
BranchTrueFalseTotal
(code >= 1007 && code <= 1014) || 1 0 1
2054 1 ▶ (code >= 3000 && code <= 4999))) {
Branch: 1 branch
BranchTrueFalseTotal
(code >= 3000 && code <= 4999))) { 0 1 1
2055 0 croak("close: invalid close code %d "
2056 "(RFC 6455 allows 1000-1003, 1007-1014, 3000-4999)", code);
2057 }
2058  
2059 21 ▶ if (self->magic != EV_WS_CONN_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CONN_MAGIC) { 0 21 21
2060 0 return;
2061 }
2062 21 ▶ if (!self->wsi || !self->connected || self->closing) {
Branch: 3 branches
BranchTrueFalseTotal
if (!self->wsi || !self->connected || self->closing) { 20 1 21
if (!self->wsi || !self->connected || self->closing) { 20 0 20
if (!self->wsi || !self->connected || self->closing) { 1 19 20
2063 2 return;
2064 }
2065  
2066 19 ▶ DEBUG_LOG("Closing connection: code=%d reason=%s", code, reason ? reason : "none");
Branch: 2 branches
BranchTrueFalseTotal
DEBUG_LOG("Closing connection: code=%d reason=%s", code, reason ? reason : "none"); 0 19 19
DEBUG_LOG("Closing connection: code=%d reason=%s", code, reason ? reason : "none"); 0 0 0
2067 19 ▶ if (self->in_fragmented_send) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_fragmented_send) { 0 19 19
2068 0 queue_send(self, NULL, 0, LWS_WRITE_CONTINUATION);
2069 0 self->in_fragmented_send = 0;
2070 }
2071 19 self->closing = 1;
2072 25 ▶ lws_close_reason(self->wsi, (enum lws_close_status)code,
Branch: 1 branch
BranchTrueFalseTotal
lws_close_reason(self->wsi, (enum lws_close_status)code, 6 13 19
2073 reason ? (unsigned char*)reason : NULL,
2074 6 reason ? strlen(reason) : 0);
2075 19 lws_callback_on_writable(self->wsi);
2076 }
2077  
2078 int
2079 is_connected(EV::Websockets::Connection self);
2080 CODE:
2081 {
2082 6 ▶ RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && self->connected) ? 1 : 0;
Branch: 4 branches
BranchTrueFalseTotal
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && self->connected) ? 1 : 0; 6 0 6
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && self->connected) ? 1 : 0; 3 3 6
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && self->connected) ? 1 : 0; 2 1 3
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && self->connected) ? 1 : 0; 2 4 6
2083 }
2084 OUTPUT:
2085 RETVAL
2086  
2087 int
2088 is_connecting(EV::Websockets::Connection self);
2089 CODE:
2090 {
2091 3 ▶ RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0;
Branch: 5 branches
BranchTrueFalseTotal
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0; 3 0 3
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0; 3 0 3
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0; 2 1 3
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0; 2 0 2
RETVAL = (self->magic == EV_WS_CONN_MAGIC && self->wsi != NULL && !self->connected && !self->closing) ? 1 : 0; 0 3 3
2092 }
2093 OUTPUT:
2094 RETVAL
2095  
2096 const char*
2097 state(EV::Websockets::Connection self);
2098 CODE:
2099 {
2100 8 ▶ if (self->magic != EV_WS_CONN_MAGIC) RETVAL = "destroyed";
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CONN_MAGIC) RETVAL = "destroyed"; 0 8 8
2101 8 ▶ else if (!self->wsi) RETVAL = "closed";
Branch: 1 branch
BranchTrueFalseTotal
else if (!self->wsi) RETVAL = "closed"; 3 5 8
2102 5 ▶ else if (self->closing) RETVAL = "closing";
Branch: 1 branch
BranchTrueFalseTotal
else if (self->closing) RETVAL = "closing"; 0 5 5
2103 5 ▶ else if (self->connected) RETVAL = "connected";
Branch: 1 branch
BranchTrueFalseTotal
else if (self->connected) RETVAL = "connected"; 2 3 5
2104 3 else RETVAL = "connecting";
2105 }
2106 OUTPUT:
2107 RETVAL
2108  
2109 UV
2110 send_queue_size(EV::Websockets::Connection self);
2111 CODE:
2112 {
2113 0 ▶ RETVAL = (self->magic == EV_WS_CONN_MAGIC) ? (UV)self->send_queue_bytes : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (self->magic == EV_WS_CONN_MAGIC) ? (UV)self->send_queue_bytes : 0; 0 0 0
RETVAL = (self->magic == EV_WS_CONN_MAGIC) ? (UV)self->send_queue_bytes : 0; 0 0 0
2114 }
2115 OUTPUT:
2116 RETVAL
2117  
2118 void
2119 send_fragment(EV::Websockets::Connection self, SV* data, int is_binary = 0, int is_final = 1);
2120 CODE:
2121 {
2122 STRLEN len;
2123 const char* buf;
2124 enum lws_write_protocol mode;
2125  
2126 2 ▶ CHECK_CONN_OPEN(self);
Branch: 4 branches
BranchTrueFalseTotal
CHECK_CONN_OPEN(self); 0 2 2
CHECK_CONN_OPEN(self); 2 0 2
CHECK_CONN_OPEN(self); 2 0 2
CHECK_CONN_OPEN(self); 0 2 2
2127  
2128 2 buf = SvPV(data, len);
2129  
2130 2 ▶ if (!self->in_fragmented_send) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->in_fragmented_send) { 1 1 2
2131 1 mode = is_binary ? LWS_WRITE_BINARY : LWS_WRITE_TEXT;
2132 1 ▶ if (!is_final) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_final) { 1 0 1
2133 1 mode |= LWS_WRITE_NO_FIN;
2134 1 self->in_fragmented_send = 1;
2135 }
2136 } else {
2137 1 mode = LWS_WRITE_CONTINUATION;
2138 1 ▶ if (!is_final) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_final) { 0 1 1
2139 0 mode |= LWS_WRITE_NO_FIN;
2140 } else {
2141 1 self->in_fragmented_send = 0;
2142 }
2143 }
2144  
2145 2 queue_send(self, buf, len, mode);
2146 }
2147  
2148 SV*
2149 stash(EV::Websockets::Connection self);
2150 CODE:
2151 {
2152 2 ▶ if (self->magic != EV_WS_CONN_MAGIC)
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_WS_CONN_MAGIC) 0 2 2
2153 0 croak("Connection has been destroyed");
2154 2 ▶ if (!self->stash)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->stash) 1 1 2
2155 1 self->stash = newHV();
2156 2 RETVAL = newRV_inc((SV*)self->stash);
2157 }
2158 OUTPUT:
2159 RETVAL
« blib/lib/EV/Websockets.pm ↑ Summary