include/punk/punk_wsconn.h

filter: Statementstmt 91.5% 355 / 391 (3 uncoverable) Branchbran 74.7% 271 / 416 (57 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.9% 626 / 807 (60 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_model.h ↑ Summary include/punk/punk_host.h »
1 /* punk_wsconn.h - the live WebSocket connection.
2 *
3 * Drives the frame codec (punk_ws.h) over one socket the application owns
4 * after Hyperman::detach, watching it on the worker loop through Hyperman's
5 * C ABI (hm_abi.h, via ExtUtils::Depends). Reads, unmasks, reassembles,
6 * answers pings and runs the closing handshake entirely in C; Perl is
7 * entered only to deliver a completed message or a lifecycle event.
8 *
9 * The object is a blessed IV-ref to a punk_wsconn (the Punk::Router
10 * precedent - a C-buffer-heavy object rather than a slot AV). It holds a
11 * strong reference to itself while the socket is live, so a handler that
12 * wires callbacks and returns does not take the connection down with its
13 * lexicals; teardown drops it.
14 */
15  
16 #ifndef PUNK_WSCONN_H
17 #define PUNK_WSCONN_H
18  
19 #define PW_ST_CONNECTING 0
20 #define PW_ST_OPEN 1
21 #define PW_ST_CLOSING 2
22 #define PW_ST_CLOSED 3
23  
24 #define PW_DEFAULT_MAX_MESSAGE (16 * 1024 * 1024)
25 #define PW_DEFAULT_WRITE_LIMIT (16 * 1024 * 1024)
26 #define PW_READ_CHUNK 16384
27 #define PW_CLOSE_TIMEOUT 10.0
28  
29 typedef struct punk_wsconn {
30 int fd;
31 /* Extended CONNECT (RFC 8441 / RFC 9220): the connection rides an h2 or
32 * h3 STREAM rather than a socket, so there is no descriptor at all.
33 * When this is set, fd is meaningless: writes go through the stream
34 * handle and reads arrive by callback instead of from a read watcher.
35 * Everything above this line - the framing, masking, ping/pong, close
36 * handshake and UTF-8 validation - is unchanged, which is the whole
37 * reason the handshake and the codec were kept apart. */
38 void *sh;
39 int state;
40 int blocking; /* opt-in fallback: no loop, no watchers */
41 unsigned char reading, writing, close_sent, close_rcvd, in_teardown;
42  
43 char *rbuf; size_t rlen, rcap;
44 char *wbuf; size_t wlen, woff, wcap;
45  
46 char *frag; size_t fraglen, fragcap; /* message reassembly */
47 unsigned char frag_op, frag_active;
48 pw_utf8 utf8; /* across text fragments */
49  
50 size_t max_message_size, write_buffer_limit;
51  
52 uint16_t peer_code;
53 SV *peer_reason;
54 SV *protocol;
55 HV *cbs; /* event name -> coderef */
56 SV *self_rv; /* strong self-ref while live */
57  
58 void *loop; /* hm_loop*, borrowed */
59 const hm_abi *abi;
60 hm_abi_timer *close_tw;
61 } punk_wsconn;
62  
63 /* ---- Hyperman's ABI, resolved once (the punk_dispatch.h idiom) ----------- */
64  
65 static const hm_abi *PUNK_HM = NULL;
66 static int PUNK_HM_TRIED = 0;
67  
68 /* NULL means "no detach seam": websocket routes then need blocking mode.
69 * PUNK_NO_HM_ABI forces that path for tests. */
70 /* Once the table has resolved there is nothing left to decide, and the answer
71 * cannot change: the resolution is attempted exactly once. Callers on a hot
72 * path take this instead of punk_hm, because punk_hm's env check is a linear
73 * scan of the environment - unmeasurable with a handful of variables, and 40%
74 * of a future-returning request with the few hundred a container carries.
75 * Unresolved, this falls through to the full path, so the switch still works
76 * for the block-mode callers that read it per call. */
77 static const hm_abi *punk_hm(pTHX);
78 833 static const hm_abi *punk_hm_resolved(pTHX) {
79 833 ▶ return PUNK_HM ? PUNK_HM : punk_hm(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
return PUNK_HM ? PUNK_HM : punk_hm(aTHX); 11 822 833
80 }
81  
82 1825 static const hm_abi *punk_hm(pTHX) {
83 1825 ▶ if (getenv("PUNK_NO_HM_ABI")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (getenv("PUNK_NO_HM_ABI")) return NULL; 11 1814 1825
84 1814 ▶ if (!PUNK_HM_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_HM_TRIED) { 262 1552 1814
85 262 dSP; int count, ok; IV p = 0;
86 262 PUNK_HM_TRIED = 1;
87 262 ok = pk_require_once(aTHX_ "Hyperman", FALSE);
88 262 SPAGAIN; /* the require may have reallocated the value stack */
89 /*
90 # uncoverable branch false
91 # uncoverable condition right count:1 class:ignore_covered_err */
92 262 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 262 0 262
93 262 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 262 262
94 262 count = call_pv("Hyperman::_abi_ptr", G_SCALAR | G_EVAL);
95 262 SPAGAIN;
96 /*
97 # uncoverable branch false
98 # uncoverable condition right count:1 class:ignore_covered_err
99 # uncoverable condition right count:2 class:ignore_covered_err */
100 262 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 262 0 262
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 262 0 262
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 262 0 262
101 /*
102 # uncoverable statement
103 # uncoverable branch true
104 # uncoverable branch false
105 # uncoverable condition left count:1
106 # uncoverable condition right count:1 */
107 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
108 262 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 262 0 262
109 /*
110 # uncoverable branch false
111 # uncoverable condition right count:1 class:ignore_covered_err */
112 262 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 262 0 262
113 262 const hm_abi *a = INT2PTR(const hm_abi *, p);
114 /* >= : the table only grows at its tail, so a later
115 * Hyperman still satisfies what we compiled against */
116 /*
117 # uncoverable branch false
118 # uncoverable condition right count:1 class:ignore_covered_err
119 # uncoverable condition right count:2 class:ignore_covered_err */
120 262 ▶ if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a; 262 0 262
if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a; 262 0 262
121 }
122 }
123 }
124 1814 return PUNK_HM;
125 }
126  
127 /* Open a Hyperman stream handle for this request, or NULL.
128 *
129 * The ticket is psgix.hyperman.stream: [fd, generation, stream id], which
130 * unlike psgix.hyperman.conn is present on HTTP/2 - there is no fd that means
131 * "this stream", but there is a stream id, and that is the whole reason the
132 * seam is a handle rather than a descriptor.
133 *
134 * NULL when the server is not Hyperman, when the ticket names a connection
135 * that has gone, or when the response has not been deferred yet (an HTTP/1.1
136 * handler that has not returned would have its return value serialised on top
137 * of the streamed body). Every caller falls through to the transport it would
138 * have used, so a NULL here is a choice of transport and never an error.
139 *
140 * No version check: punk_hm already refuses a table older than the header
141 * this was compiled against, so a resolved table has the v6 entries. */
142 42 static void *punk_hm_stream_open(pTHX_ HV *envh, const hm_abi **out_abi,
143 void **out_loop, int status, AV *hdrs) {
144 42 ▶ SV **t = envh ? hv_fetchs(envh, "psgix.hyperman.stream", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **t = envh ? hv_fetchs(envh, "psgix.hyperman.stream", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 42 0 42
145 const hm_abi *A;
146 AV *tick;
147 SV **fsv, **isv, **ssv;
148 void *loop, *h;
149 /*
150 # uncoverable condition left count:2 class:ignore_covered_err
151 # uncoverable condition left count:3 class:ignore_covered_err
152 # uncoverable condition left count:4 class:ignore_covered_err */
153 42 ▶ if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; 2 40 42
if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; 2 0 2
if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; 2 0 2
if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; 0 2 2
154 2 A = punk_hm(aTHX);
155 /*
156 # uncoverable branch true
157 # uncoverable condition left count:1 class:ignore_covered_err */
158 2 ▶ if (!A) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!A) return NULL; 0 2 2
159 2 tick = (AV *)SvRV(*t);
160 2 fsv = av_fetch(tick, 0, 0);
161 2 isv = av_fetch(tick, 1, 0);
162 2 ssv = av_fetch(tick, 2, 0);
163 /*
164 # uncoverable branch true
165 # uncoverable condition left count:1 class:ignore_covered_err
166 # uncoverable condition left count:2 class:ignore_covered_err
167 # uncoverable condition left count:3 class:ignore_covered_err
168 # uncoverable condition left count:4 class:ignore_covered_err */
169 2 ▶ if (!(fsv && *fsv && isv && *isv)) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(fsv && *fsv && isv && *isv)) return NULL; 2 0 2
if (!(fsv && *fsv && isv && *isv)) return NULL; 2 0 2
if (!(fsv && *fsv && isv && *isv)) return NULL; 2 0 2
if (!(fsv && *fsv && isv && *isv)) return NULL; 0 2 2
170 2 loop = A->cur_loop(aTHX);
171 /*
172 # uncoverable branch true
173 # uncoverable condition left count:1 class:ignore_covered_err */
174 2 ▶ if (!loop) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) return NULL; 0 2 2
175 4 ▶ h = A->stream_open(aTHX_ loop, (int)SvIV(*fsv), SvUV(*isv),
Branch: 1 branch
BranchTrueFalseTotal
h = A->stream_open(aTHX_ loop, (int)SvIV(*fsv), SvUV(*isv), 2 0 2
176 2 ▶ (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1,
Branch: 1 branch
BranchTrueFalseTotal
(ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1, 2 0 2
177 status, sv_2mortal(newRV_inc((SV *)hdrs)));
178 /*
179 # uncoverable branch true
180 # uncoverable condition left count:1 class:ignore_covered_err */
181 2 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 2 2
182 2 *out_abi = A;
183 2 *out_loop = loop;
184 2 return h;
185 }
186  
187 /* ---- small buffer helpers ------------------------------------------------ */
188  
189 /* a buffer bigger than this is returned once it is empty */
190 #define PW_BUF_KEEP (64 * 1024)
191  
192 33454 static void pw_buf_reserve(char **buf, size_t *cap, size_t need) {
193 33454 ▶ if (*cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (*cap >= need) return; 33263 191 33454
194 {
195 191 ▶ size_t n = *cap ? *cap : 1024;
Branch: 1 branch
BranchTrueFalseTotal
size_t n = *cap ? *cap : 1024; 47 144 191
196 517 ▶ while (n < need) n *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (n < need) n *= 2; 326 191 517
197 191 *buf = (char *)realloc(*buf, n);
198 /*
199 # uncoverable branch true
200 # uncoverable condition left count:1 class:ignore_covered_err */
201 191 ▶ if (!*buf) croak("Punk::WebSocket: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!*buf) croak("Punk::WebSocket: out of memory"); 0 191 191
202 191 *cap = n;
203 }
204 }
205  
206 6555 static void pw_append(char **buf, size_t *len, size_t *cap,
207 const char *src, size_t n) {
208 6555 pw_buf_reserve(buf, cap, *len + n);
209 6555 memcpy(*buf + *len, src, n);
210 6555 *len += n;
211 6555 }
212  
213 /* ---- event dispatch ------------------------------------------------------
214 * The ABI contract is that C callbacks must not croak, so every call into
215 * Perl is trapped. A handler that dies surfaces as the error event and
216 * closes the connection with 1011. */
217  
218 static void pw_queue_frame(pTHX_ punk_wsconn *ws, unsigned char op,
219 const char *payload, size_t len);
220 static void pw_fail(pTHX_ punk_wsconn *ws, uint16_t code, const char *why);
221 static void pw_teardown(pTHX_ punk_wsconn *ws, uint16_t code,
222 const char *reason, size_t rlen);
223 static void pw_flush(pTHX_ punk_wsconn *ws);
224  
225 2118462 static SV *pw_cb(pTHX_ punk_wsconn *ws, const char *name) {
226 SV **e;
227 /*
228 # uncoverable branch true
229 # uncoverable condition left count:1 class:ignore_covered_err */
230 2118462 ▶ if (!ws->cbs) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ws->cbs) return NULL; 0 2118462 2118462
231 2118462 e = hv_fetch(ws->cbs, name, (I32)strlen(name), 0);
232 /*
233 # uncoverable condition right count:2 class:ignore_covered_err
234 # uncoverable condition right count:3 class:ignore_covered_err */
235 2118462 ▶ return (e && *e && SvROK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e)) ? *e : NULL; 2118361 101 2118462
return (e && *e && SvROK(*e)) ? *e : NULL; 2118361 0 2118361
return (e && *e && SvROK(*e)) ? *e : NULL; 2118361 0 2118361
236 }
237  
238 /* Fire an event with (self, @args). Returns 0 if a handler died. */
239 2116354 static int pw_emit(pTHX_ punk_wsconn *ws, const char *name,
240 SV **args, int nargs) {
241 2116354 SV *cb = pw_cb(aTHX_ ws, name);
242 2116354 dSP;
243 int i, died;
244 2116354 ▶ if (!cb) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) return 1; 100 2116254 2116354
245 2116254 ENTER; SAVETMPS;
246 2116254 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2116254 2116254
247 2116254 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 2116254 0 2116254
EXTEND(SP, nargs + 1); 0 2116254 2116254
248 2116254 ▶ PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2116254 0 2116254
249 4232507 ▶ for (i = 0; i < nargs; i++) PUSHs(args[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(args[i]); 2116253 2116254 4232507
250 2116254 PUTBACK;
251 2116254 call_sv(cb, G_DISCARD | G_EVAL);
252 2116254 SPAGAIN;
253 2116254 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2116254 0 2116254
254 2116254 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 1 2116253 2116254
255 1 ▶ SV *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVsv(ERRSV); 1 0 1
256 1 SV *ecb = pw_cb(aTHX_ ws, "error");
257 /*
258 # uncoverable branch true
259 # uncoverable condition left count:1 class:ignore_covered_err
260 # uncoverable condition left count:2 class:ignore_covered_err
261 # uncoverable condition right count:2 class:ignore_covered_err */
262 1 ▶ if (ecb && strNE(name, "error")) {
Branch: 2 branches
BranchTrueFalseTotal
if (ecb && strNE(name, "error")) { 0 1 1
if (ecb && strNE(name, "error")) { 0 0 0
263 0 ▶ PUSHMARK(SP); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); /* # uncoverable statement */ 0 0 0
264 0 ▶ EXTEND(SP, 2); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); /* # uncoverable statement */ 0 0 0
265 /*
266 # uncoverable statement
267 # uncoverable condition left count:1
268 # uncoverable condition right count:1 */
269 0 ▶ PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); 0 0 0
270 0 PUSHs(sv_2mortal(err)); /* # uncoverable statement */
271 0 PUTBACK; /* # uncoverable statement */
272 0 call_sv(ecb, G_DISCARD | G_EVAL); /* # uncoverable statement */
273 0 SPAGAIN; /* # uncoverable statement */
274 0 ▶ (void)SvTRUE(ERRSV); /* an error handler that dies: give up */ /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
(void)SvTRUE(ERRSV); /* an error handler that dies: give up */ /* # uncoverable statement */ 0 0 0
275 }
276 else {
277 1 STRLEN el; const char *ep = SvPV_const(err, el);
278 /* escaped: a die text is request-shaped, and a newline in it
279 * would be a second log line */
280 1 PerlIO_printf(PerlIO_stderr(),
281 "Punk::WebSocket: %s handler died: %" SVf "\n",
282 1 name, SVfARG(pl_plain(aTHX_ ep, el, SvUTF8(err))));
283 1 SvREFCNT_dec(err);
284 }
285 }
286 2116254 PUTBACK;
287 2116254 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 2116253 2116254
288 2116254 return !died;
289 }
290  
291 /* ---- writing -------------------------------------------------------------
292 * Everything queues; pw_flush drains what the socket will take and arms a
293 * write watcher for the rest. A peer that will not read cannot stall the
294 * worker, and cannot make us drop bytes mid-frame either. */
295  
296 static void pw_on_writable(pTHX_ int fd, int mask, void *ud);
297  
298 2259 static void pw_flush(pTHX_ punk_wsconn *ws) {
299 2259 ▶ if (ws->sh) {
Branch: 1 branch
BranchTrueFalseTotal
if (ws->sh) { 4 2255 2259
300 /* The server owns the framing and the backpressure here, so this is
301 * a hand-off rather than a write loop: there is no partial write to
302 * resume and no write watcher to arm. */
303 /*
304 # uncoverable branch false
305 # uncoverable condition right count:1 class:ignore_covered_err
306 # uncoverable condition right count:2 class:ignore_covered_err */
307 4 ▶ if (ws->woff < ws->wlen && ws->abi) {
Branch: 2 branches
BranchTrueFalseTotal
if (ws->woff < ws->wlen && ws->abi) { 4 0 4
if (ws->woff < ws->wlen && ws->abi) { 4 0 4
308 4 int r = ws->abi->stream_write(aTHX_ ws->sh, ws->wbuf + ws->woff,
309 4 ws->wlen - ws->woff);
310 /*
311 # uncoverable branch true
312 # uncoverable condition left count:1 class:ignore_covered_err */
313 4 ▶ if (r < 0) { pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); return; } 0 4 4
314 4 ws->woff = ws->wlen;
315 }
316 4 ws->wlen = ws->woff = 0;
317 /*
318 # uncoverable condition right count:2 class:ignore_covered_err
319 # uncoverable condition right count:3 class:ignore_covered_err */
320 4 ▶ if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd)
Branch: 3 branches
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) 1 3 4
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) 1 0 1
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) 1 0 1
321 1 pw_teardown(aTHX_ ws, 0, NULL, 0);
322 4 return;
323 }
324 4508 ▶ while (ws->woff < ws->wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (ws->woff < ws->wlen) { 2256 2252 4508
325 2256 ssize_t n = write(ws->fd, ws->wbuf + ws->woff, ws->wlen - ws->woff);
326 2256 ▶ if (n > 0) { ws->woff += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { ws->woff += (size_t)n; continue; } 2253 3 2256
327 /*
328 # uncoverable branch false
329 # uncoverable condition right count:1 class:ignore_covered_err
330 # uncoverable condition right count:2 class:ignore_covered_err
331 # uncoverable condition left count:3 class:ignore_covered_err
332 # uncoverable condition right count:3 class:ignore_covered_err */
333 3 ▶ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 3 0 3
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 0 3 3
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 0 0 0
334 /*
335 # uncoverable condition right count:2 class:ignore_covered_err
336 # uncoverable condition right count:3 class:ignore_covered_err
337 # uncoverable condition right count:4 class:ignore_covered_err */
338 3 ▶ if (!ws->writing && !ws->blocking && ws->abi && ws->loop) {
Branch: 4 branches
BranchTrueFalseTotal
if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { 1 2 3
if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { 1 0 1
if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { 1 0 1
if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { 1 0 1
339 1 ws->abi->io_watch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE,
340 pw_on_writable, ws);
341 1 ws->writing = 1;
342 }
343 3 return;
344 }
345 /*
346 # uncoverable statement
347 # uncoverable branch true
348 # uncoverable branch false
349 # uncoverable condition left count:1
350 # uncoverable condition right count:1
351 # uncoverable condition left count:2
352 # uncoverable condition right count:2 */
353 0 ▶ if (n < 0 && errno == EINTR) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && errno == EINTR) continue; 0 0 0
if (n < 0 && errno == EINTR) continue; 0 0 0
354 0 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); /* write error */ /* # uncoverable statement */
355 0 return; /* # uncoverable statement */
356 }
357 2252 ws->wlen = ws->woff = 0;
358 /*
359 # uncoverable condition right count:2 class:ignore_covered_err
360 # uncoverable condition right count:3 class:ignore_covered_err */
361 2252 ▶ if (ws->writing && ws->abi && ws->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (ws->writing && ws->abi && ws->loop) { 1 2251 2252
if (ws->writing && ws->abi && ws->loop) { 1 0 1
if (ws->writing && ws->abi && ws->loop) { 1 0 1
362 1 ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE);
363 1 ws->writing = 0;
364 }
365 /* the closing handshake is done once our close frame is on the wire */
366 2252 ▶ if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) /* # uncoverable condition right count:2 class:ignore_covered_err */ 29 2223 2252
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) /* # uncoverable condition right count:2 class:ignore_covered_err */ 29 0 29
if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 21 29
367 8 pw_teardown(aTHX_ ws, 0, NULL, 0);
368 }
369  
370 1 static void pw_on_writable_in(pTHX_ int fd, int mask, void *ud) {
371 1 punk_wsconn *ws = (punk_wsconn *)ud;
372 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
373 1 pw_flush(aTHX_ ws);
374 1 }
375  
376 1 static void pw_on_writable(pTHX_ int fd, int mask, void *ud) {
377 1 punk_wsconn *ws_ = (punk_wsconn *)ud;
378 1 ▶ SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
379 1 PK_LOOP_ENTER;
380 1 pw_on_writable_in(aTHX_ fd, mask, ud);
381 1 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 1 1
382 /*
383 # uncoverable branch false
384 # uncoverable condition right count:1 class:ignore_covered_err */
385 1 ▶ if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ 1 0 1
386 1 }
387  
388 2206 static void pw_queue_frame(pTHX_ punk_wsconn *ws, unsigned char op,
389 const char *payload, size_t len) {
390 char hdr[10];
391 size_t hn;
392 /*
393 # uncoverable branch true class:ignore_covered_err
394 # uncoverable condition left count:1 class:ignore_covered_err */
395 2207 ▶ if (ws->state == PW_ST_CLOSED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSED) return; 0 2206 2206
396 /*
397 # uncoverable branch true class:ignore_covered_err
398 # uncoverable condition left count:1 class:ignore_covered_err
399 # uncoverable condition right count:1 class:ignore_covered_err
400 # uncoverable condition left count:2 class:ignore_covered_err */
401 2206 ▶ if (ws->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (ws->write_buffer_limit 2206 0 2206
402 2206 ▶ && ws->wlen - ws->woff + len > ws->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& ws->wlen - ws->woff + len > ws->write_buffer_limit) { 1 2205 2206
403 1 pw_fail(aTHX_ ws, PW_CLOSE_POLICY_VIOLATION, "send buffer full"); /* # uncoverable statement class:ignore_covered_err */
404 1 return; /* # uncoverable statement class:ignore_covered_err */
405 }
406 2205 hn = pw_encode_header(hdr, op, 1, len);
407 2205 pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, hdr, hn);
408 /*
409 # uncoverable branch false
410 # uncoverable condition right count:1 class:ignore_covered_err */
411 2205 ▶ if (len) pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, payload, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len) pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, payload, len); 2205 0 2205
412 2205 pw_flush(aTHX_ ws);
413 }
414  
415 30 static void pw_queue_close(pTHX_ punk_wsconn *ws, uint16_t code,
416 const char *reason, size_t rlen) {
417 char body[2 + PW_MAX_REASON];
418 size_t blen;
419 /*
420 # uncoverable branch true
421 # uncoverable condition left count:1 class:ignore_covered_err
422 # uncoverable condition left count:2 class:ignore_covered_err */
423 30 ▶ if (ws->close_sent || ws->state == PW_ST_CLOSED) return;
Branch: 2 branches
BranchTrueFalseTotal
if (ws->close_sent || ws->state == PW_ST_CLOSED) return; 30 0 30
if (ws->close_sent || ws->state == PW_ST_CLOSED) return; 0 30 30
424 30 blen = pw_close_payload(body, code, reason, rlen);
425 30 ws->close_sent = 1;
426 /*
427 # uncoverable branch false
428 # uncoverable condition right count:1 class:ignore_covered_err */
429 30 ▶ if (ws->state == PW_ST_OPEN) ws->state = PW_ST_CLOSING;
Branch: 1 branch
BranchTrueFalseTotal
if (ws->state == PW_ST_OPEN) ws->state = PW_ST_CLOSING; 30 0 30
430 30 pw_queue_frame(aTHX_ ws, PW_OP_CLOSE, body, blen);
431 }
432  
433 /* A protocol failure: tell the peer why, then close. */
434 14 static void pw_fail(pTHX_ punk_wsconn *ws, uint16_t code, const char *why) {
435 /*
436 # uncoverable branch true
437 # uncoverable condition left count:1 class:ignore_covered_err
438 # uncoverable condition left count:2 class:ignore_covered_err */
439 14 ▶ if (ws->state == PW_ST_CLOSED || ws->in_teardown) return;
Branch: 2 branches
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSED || ws->in_teardown) return; 14 0 14
if (ws->state == PW_ST_CLOSED || ws->in_teardown) return; 0 14 14
440 {
441 14 ▶ SV *msg = sv_2mortal(newSVpv(why ? why : "protocol error", 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *msg = sv_2mortal(newSVpv(why ? why : "protocol error", 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
442 14 SV *a[1]; a[0] = msg;
443 14 (void)pw_emit(aTHX_ ws, "error", a, 1);
444 }
445 14 ▶ pw_queue_close(aTHX_ ws, code, why, why ? strlen(why) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_queue_close(aTHX_ ws, code, why, why ? strlen(why) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
446 /* do not wait for the peer's echo after a protocol failure */
447 14 ▶ pw_teardown(aTHX_ ws, code, why, why ? strlen(why) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_teardown(aTHX_ ws, code, why, why ? strlen(why) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
448 }
449  
450 /* ---- teardown ------------------------------------------------------------ */
451  
452 59 static void pw_teardown(pTHX_ punk_wsconn *ws, uint16_t code,
453 const char *reason, size_t rlen) {
454 SV *args[2];
455 59 ▶ if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 59 0 59
if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 9 50 59
456 50 ws->in_teardown = 1;
457  
458 /*
459 # uncoverable condition right count:2 class:ignore_covered_err
460 # uncoverable condition right count:3 class:ignore_covered_err */
461 50 ▶ if (ws->abi && ws->loop && !ws->blocking) {
Branch: 3 branches
BranchTrueFalseTotal
if (ws->abi && ws->loop && !ws->blocking) { 40 10 50
if (ws->abi && ws->loop && !ws->blocking) { 40 0 40
if (ws->abi && ws->loop && !ws->blocking) { 40 0 40
462 40 ▶ if (ws->reading) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->reading) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_READ); 38 2 40
463 /*
464 # uncoverable branch true class:ignore_covered_err
465 # uncoverable condition left count:1 class:ignore_covered_err */
466 40 ▶ if (ws->writing) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->writing) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE); 0 40 40
467 40 ▶ if (ws->close_tw) {
Branch: 1 branch
BranchTrueFalseTotal
if (ws->close_tw) { 3 37 40
468 3 ws->abi->timer_cancel(aTHX_ ws->loop, ws->close_tw);
469 3 ws->close_tw = NULL;
470 }
471 }
472 50 ws->reading = ws->writing = 0;
473 50 ▶ if (ws->sh) {
Branch: 1 branch
BranchTrueFalseTotal
if (ws->sh) { 2 48 50
474 /* Stream-backed: there is no descriptor to close, and the server
475 * owns the stream. Releasing the handle is what ends it - and it
476 * has to happen exactly once, so the pointer is cleared first. */
477 2 void *h = ws->sh;
478 2 ws->sh = NULL;
479 /*
480 # uncoverable branch false
481 # uncoverable condition right count:1 class:ignore_covered_err */
482 2 ▶ if (ws->abi) (void)ws->abi->stream_close(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->abi) (void)ws->abi->stream_close(aTHX_ h); 2 0 2
483 }
484 50 ▶ if (ws->fd >= 0) { close(ws->fd); ws->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (ws->fd >= 0) { close(ws->fd); ws->fd = -1; } 48 2 50
485 50 ws->state = PW_ST_CLOSED;
486  
487 50 ▶ args[0] = sv_2mortal(newSViv(ws->peer_code ? ws->peer_code : code));
Branch: 1 branch
BranchTrueFalseTotal
args[0] = sv_2mortal(newSViv(ws->peer_code ? ws->peer_code : code)); 10 40 50
488 10 args[1] = ws->peer_reason ? sv_2mortal(newSVsv(ws->peer_reason))
489 50 ▶ : sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs(""));
Branch: 2 branches
BranchTrueFalseTotal
: sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs("")); 10 40 50
: sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs("")); 15 25 40
490 50 (void)pw_emit(aTHX_ ws, "close", args, 2);
491  
492 /*
493 # uncoverable branch false
494 # uncoverable condition right count:1 class:ignore_covered_err */
495 50 ▶ if (ws->cbs) { SvREFCNT_dec((SV *)ws->cbs); ws->cbs = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (ws->cbs) { SvREFCNT_dec((SV *)ws->cbs); ws->cbs = NULL; } 50 0 50
496 50 ws->in_teardown = 0;
497 /* drop the self-reference last: this may free the object */
498 /*
499 # uncoverable branch false
500 # uncoverable condition right count:1 class:ignore_covered_err */
501 50 ▶ if (ws->self_rv) { SV *s = ws->self_rv; ws->self_rv = NULL; SvREFCNT_dec(s); }
Branch: 1 branch
BranchTrueFalseTotal
if (ws->self_rv) { SV *s = ws->self_rv; ws->self_rv = NULL; SvREFCNT_dec(s); } 50 0 50
502 }
503  
504 1 static void pw_close_timeout_in(pTHX_ void *ud) {
505 1 punk_wsconn *ws = (punk_wsconn *)ud;
506 1 ws->close_tw = NULL;
507 1 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, "close timeout", 13);
508 1 }
509  
510 1 static void pw_close_timeout(pTHX_ void *ud) {
511 1 punk_wsconn *ws_ = (punk_wsconn *)ud;
512 1 ▶ SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
513 1 PK_LOOP_ENTER;
514 1 pw_close_timeout_in(aTHX_ ud);
515 1 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 1 0 1
516 /*
517 # uncoverable branch false
518 # uncoverable condition right count:1 class:ignore_covered_err */
519 1 ▶ if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ 1 0 1
520 1 }
521  
522 /* ---- reading -------------------------------------------------------------
523 * One pass over everything buffered: control frames are answered inline,
524 * data frames reassemble into ws->frag until FIN. */
525  
526 26832 static void pw_process(pTHX_ punk_wsconn *ws) {
527 26832 size_t off = 0;
528 /* Teardown drops the connection's self-reference, which can free the
529 * struct while we are still inside this loop (a close frame, or any
530 * protocol failure, does exactly that). Hold a reference for the
531 * duration so the tail of this function - and our caller - still have
532 * something to look at; the object goes away when this guard does. */
533 26832 ▶ SV *guard = ws->self_rv ? SvREFCNT_inc(ws->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *guard = ws->self_rv ? SvREFCNT_inc(ws->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 26832 0 26832
534 /*
535 # uncoverable branch true count:2 class:ignore_covered_err
536 # uncoverable condition right count:1 class:ignore_covered_err */
537 2143078 ▶ while (ws->state != PW_ST_CLOSED && off < ws->rlen) {
Branch: 2 branches
BranchTrueFalseTotal
while (ws->state != PW_ST_CLOSED && off < ws->rlen) { 1 2143077 2143078
while (ws->state != PW_ST_CLOSED && off < ws->rlen) { 2142891 186 2143077
538 pw_frame f;
539 2142891 int n = pw_decode_frame(ws->rbuf + off, ws->rlen - off, &f,
540 ws->max_message_size, 1);
541 2142891 ▶ if (n == PW_NEED_MORE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == PW_NEED_MORE) break; 26622 2116269 2142891
542 2116269 ▶ if (n == PW_E_PROTO) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == PW_E_PROTO) { 3 2116266 2116269
543 3 pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, "protocol error");
544 23 goto consumed;
545 }
546 2116266 ▶ if (n == PW_E_TOO_BIG) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == PW_E_TOO_BIG) { 1 2116265 2116266
547 1 pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big");
548 1 goto consumed;
549 }
550 2116265 pw_unmask(&f, ws->rbuf + off + f.header_size);
551 {
552 2116265 const char *pl = ws->rbuf + off + f.header_size;
553 2116265 size_t pn = (size_t)f.payload_len;
554  
555 2116265 ▶ if (pw_is_control(f.opcode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_is_control(f.opcode)) { 2120 2114145 2116265
556 2120 ▶ if (f.opcode == PW_OP_PING) {
Branch: 1 branch
BranchTrueFalseTotal
if (f.opcode == PW_OP_PING) { 2106 14 2120
557 2106 pw_queue_frame(aTHX_ ws, PW_OP_PONG, pl, pn);
558 /* the payload SV only when somebody listens */
559 /*
560 # uncoverable branch false
561 # uncoverable condition right count:1 class:ignore_covered_err */
562 2106 ▶ if (pw_cb(aTHX_ ws, "ping")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_cb(aTHX_ ws, "ping")) { 2106 0 2106
563 2106 SV *a[1]; a[0] = newSVpvn(pl, pn);
564 2106 (void)pw_emit(aTHX_ ws, "ping", a, 1);
565 2106 SvREFCNT_dec(a[0]);
566 }
567 }
568 14 ▶ else if (f.opcode == PW_OP_PONG) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f.opcode == PW_OP_PONG) { 1 13 14
569 /*
570 # uncoverable branch false
571 # uncoverable condition right count:1 class:ignore_covered_err */
572 1 ▶ if (pw_cb(aTHX_ ws, "pong")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_cb(aTHX_ ws, "pong")) { 1 0 1
573 1 SV *a[1]; a[0] = newSVpvn(pl, pn);
574 1 (void)pw_emit(aTHX_ ws, "pong", a, 1);
575 1 SvREFCNT_dec(a[0]);
576 }
577 }
578 else { /* close */
579 13 uint16_t code = PW_CLOSE_NO_STATUS;
580 13 ▶ if (pn == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (pn == 1) { 1 12 13
581 1 pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR,
582 "bad close payload");
583 1 goto consumed;
584 }
585 /*
586 # uncoverable branch false
587 # uncoverable condition right count:1 class:ignore_covered_err */
588 12 ▶ if (pn >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pn >= 2) { 12 0 12
589 12 code = (uint16_t)(((unsigned char)pl[0] << 8)
590 12 | (unsigned char)pl[1]);
591 12 ▶ if (!pw_close_code_ok(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pw_close_code_ok(code)) { 1 11 12
592 1 pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR,
593 "bad close code");
594 1 goto consumed;
595 }
596 11 ▶ if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) { 3 8 11
if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) { 1 2 3
597 1 pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD,
598 "close reason is not utf8");
599 1 goto consumed;
600 }
601 }
602 10 ws->close_rcvd = 1;
603 10 ws->peer_code = code;
604 /*
605 # uncoverable branch true
606 # uncoverable condition left count:1 class:ignore_covered_err */
607 10 ▶ if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); 0 10 10
608 2 ws->peer_reason = pn > 2 ? newSVpvn(pl + 2, pn - 2)
609 10 ▶ : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
: newSVpvs(""); 2 8 10
610 10 SvUTF8_on(ws->peer_reason);
611 10 ▶ if (!ws->close_sent)
Branch: 1 branch
BranchTrueFalseTotal
if (!ws->close_sent) 9 1 10
612 9 ▶ pw_queue_close(aTHX_ ws, code == PW_CLOSE_NO_STATUS
Branch: 1 branch
BranchTrueFalseTotal
pw_queue_close(aTHX_ ws, code == PW_CLOSE_NO_STATUS 9 0 9
613 ? PW_CLOSE_NORMAL : code, NULL, 0);
614 10 off += (size_t)n;
615 /* everything queued is flushed by pw_queue_close ->
616 * pw_flush, which tears down once it drains */
617 /*
618 # uncoverable branch false
619 # uncoverable condition right count:1 class:ignore_covered_err */
620 10 ▶ if (ws->wlen == ws->woff)
Branch: 1 branch
BranchTrueFalseTotal
if (ws->wlen == ws->woff) 10 0 10
621 10 pw_teardown(aTHX_ ws, code, NULL, 0);
622 10 goto consumed;
623 }
624 }
625 else { /* data */
626 2114145 unsigned char op = f.opcode;
627 2114145 ▶ if (op == PW_OP_CONT) {
Branch: 1 branch
BranchTrueFalseTotal
if (op == PW_OP_CONT) { 5 2114140 2114145
628 5 ▶ if (!ws->frag_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ws->frag_active) { 1 4 5
629 1 pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR,
630 "continuation without a start frame");
631 1 goto consumed;
632 }
633 }
634 else {
635 2114140 ▶ if (ws->frag_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (ws->frag_active) { 1 2114139 2114140
636 1 pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR,
637 "new data frame inside a fragmented message");
638 1 goto consumed;
639 }
640 2114139 ws->frag_active = 1;
641 2114139 ws->frag_op = op;
642 2114139 ws->fraglen = 0;
643 2114139 pw_utf8_reset(&ws->utf8);
644 }
645 2114143 ▶ if (ws->max_message_size /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (ws->max_message_size /* # uncoverable condition right count:1 class:ignore_covered_err */ 2114143 0 2114143
646 2114143 ▶ && ws->fraglen + pn > ws->max_message_size) {
Branch: 1 branch
BranchTrueFalseTotal
&& ws->fraglen + pn > ws->max_message_size) { 1 2114142 2114143
647 1 pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG,
648 "message too big");
649 1 goto consumed;
650 }
651 /* validate text as it arrives, so a split multibyte
652 * character is judged on the whole message, not a fragment */
653 2114142 ▶ if (ws->frag_op == PW_OP_TEXT
Branch: 1 branch
BranchTrueFalseTotal
if (ws->frag_op == PW_OP_TEXT 2102139 12003 2114142
654 2102139 ▶ && !pw_utf8_step(&ws->utf8, pl, pn)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !pw_utf8_step(&ws->utf8, pl, pn)) { 3 2102136 2102139
655 3 pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD,
656 "text is not utf8");
657 3 goto consumed;
658 }
659 /* One frame that is the whole message - nearly every one -
660 * is handed on straight from the read buffer; only a
661 * fragmented message is gathered in `frag` first. */
662 {
663 2114139 ▶ int whole = f.fin && ws->fraglen == 0;
Branch: 2 branches
BranchTrueFalseTotal
int whole = f.fin && ws->fraglen == 0; 2114134 5 2114139
int whole = f.fin && ws->fraglen == 0; 2114132 2 2114134
664 2114139 ▶ if (pn && !whole) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (pn && !whole) /* # uncoverable condition right count:1 class:ignore_covered_err */ 2114139 0 2114139
if (pn && !whole) /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 2114132 2114139
665 7 pw_append(&ws->frag, &ws->fraglen, &ws->fragcap, pl, pn);
666  
667 2114139 ▶ if (f.fin) {
Branch: 1 branch
BranchTrueFalseTotal
if (f.fin) { 2114134 5 2114139
668 2114134 int is_text = (ws->frag_op == PW_OP_TEXT);
669 /*
670 # uncoverable branch true
671 # uncoverable condition left count:1 class:ignore_covered_err
672 # uncoverable condition left count:2 class:ignore_covered_err */
673 2114134 ▶ if (is_text && !pw_utf8_ok(&ws->utf8)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_text && !pw_utf8_ok(&ws->utf8)) { 2102131 12003 2114134
if (is_text && !pw_utf8_ok(&ws->utf8)) { 0 2102131 2102131
674 0 pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD, /* # uncoverable statement */
675 "text is not utf8");
676 0 goto consumed; /* # uncoverable statement */
677 }
678 {
679 2114134 SV *msg = whole
680 2114132 ▶ ? newSVpvn(pn ? pl : "", pn) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
? newSVpvn(pn ? pl : "", pn) /* # uncoverable condition right count:1 class:ignore_covered_err */ 2114132 0 2114132
681 2114134 ▶ : newSVpvn(ws->fraglen ? ws->frag : "", /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
: newSVpvn(ws->fraglen ? ws->frag : "", /* # uncoverable condition right count:2 class:ignore_covered_err */ 2114132 2 2114134
: newSVpvn(ws->fraglen ? ws->frag : "", /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
682 ws->fraglen);
683 SV *a[1];
684 2114134 ▶ if (is_text) SvUTF8_on(msg);
Branch: 1 branch
BranchTrueFalseTotal
if (is_text) SvUTF8_on(msg); 2102131 12003 2114134
685 2114134 a[0] = msg;
686 2114134 ws->frag_active = 0;
687 2114134 ws->fraglen = 0;
688 /* a large fragmented message leaves a large
689 * buffer; give it back once it is handed on */
690 /*
691 # uncoverable branch true
692 # uncoverable condition left count:1 class:ignore_covered_err */
693 2114134 ▶ if (ws->fragcap > PW_BUF_KEEP) {
Branch: 1 branch
BranchTrueFalseTotal
if (ws->fragcap > PW_BUF_KEEP) { 0 2114134 2114134
694 0 free(ws->frag); /* # uncoverable statement */
695 0 ws->frag = NULL; ws->fragcap = 0; /* # uncoverable statement */
696 }
697 2114134 ▶ (void)pw_emit(aTHX_ ws,
Branch: 1 branch
BranchTrueFalseTotal
(void)pw_emit(aTHX_ ws, 2102131 12003 2114134
698 is_text ? "message" : "binary", a, 1);
699 /* Released here, not left to a temporaries
700 * frame: this loop hands on every message a fast
701 * sender has ready before it returns, and as
702 * mortals they would all be alive at once. */
703 2114134 SvREFCNT_dec(msg);
704 }
705 }
706 }
707 }
708 }
709 2116246 off += (size_t)n;
710 }
711 187 consumed:
712 26832 ▶ if (off && ws->rbuf) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (off && ws->rbuf) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 9058 17774 26832
if (off && ws->rbuf) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 9058 0 9058
713 9058 ▶ if (off < ws->rlen) memmove(ws->rbuf, ws->rbuf + off, ws->rlen - off);
Branch: 1 branch
BranchTrueFalseTotal
if (off < ws->rlen) memmove(ws->rbuf, ws->rbuf + off, ws->rlen - off); 8862 196 9058
714 9058 ws->rlen -= off;
715 /* an empty read buffer that grew for one large message shrinks
716 * back, so an idle connection does not keep it for its whole life */
717 9058 ▶ if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) {
Branch: 2 branches
BranchTrueFalseTotal
if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) { 196 8862 9058
if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) { 11 185 196
718 11 free(ws->rbuf);
719 11 ws->rbuf = NULL; ws->rcap = 0;
720 }
721 }
722 /*
723 # uncoverable branch false
724 # uncoverable condition right count:1 class:ignore_covered_err */
725 26832 ▶ if (guard) SvREFCNT_dec(guard);
Branch: 1 branch
BranchTrueFalseTotal
if (guard) SvREFCNT_dec(guard); 26832 0 26832
726 26832 }
727  
728 /* Bytes handed up by the server on an Extended CONNECT stream. The socket
729 * read loop's job, without the socket: append and let the codec run. */
730 5 static void pw_feed(pTHX_ punk_wsconn *ws, const char *buf, size_t len,
731 int fin) {
732 /*
733 # uncoverable branch true
734 # uncoverable condition left count:1 class:ignore_covered_err */
735 5 ▶ if (ws->state == PW_ST_CLOSED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSED) return; 0 5 5
736 5 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 4 1 5
737 8 size_t cap = ws->max_message_size
738 4 ▶ ? ws->max_message_size + PW_READ_CHUNK : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
? ws->max_message_size + PW_READ_CHUNK : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
739 /*
740 # uncoverable branch true
741 # uncoverable condition left count:1 class:ignore_covered_err
742 # uncoverable condition right count:1 class:ignore_covered_err
743 # uncoverable condition left count:2 class:ignore_covered_err */
744 4 ▶ if (cap && ws->rlen + len > cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (cap && ws->rlen + len > cap) { 4 0 4
if (cap && ws->rlen + len > cap) { 0 4 4
745 0 pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big"); /* # uncoverable statement */
746 0 return; /* # uncoverable statement */
747 }
748 4 pw_buf_reserve(&ws->rbuf, &ws->rcap, ws->rlen + len);
749 4 memcpy(ws->rbuf + ws->rlen, buf, len);
750 4 ws->rlen += len;
751 }
752 5 pw_process(aTHX_ ws);
753 /* The peer half-closed its side of the stream: the same thing a read of
754 * zero means on a socket. */
755 5 ▶ if (fin && ws->state != PW_ST_CLOSED) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (fin && ws->state != PW_ST_CLOSED) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 4 5
if (fin && ws->state != PW_ST_CLOSED) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
756 1 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0);
757 }
758  
759 /* The ABI's read half (v8) delivers here. */
760 5 static void pw_on_stream_data_in(pTHX_ void *h, const char *buf, STRLEN len,
761 int fin, void *ud) {
762 5 punk_wsconn *ws = (punk_wsconn *)ud;
763 PERL_UNUSED_ARG(h);
764 5 pw_feed(aTHX_ ws, buf, (size_t)len, fin);
765 5 }
766  
767 5 static void pw_on_stream_data(pTHX_ void *h, const char *buf, STRLEN len,
768 int fin, void *ud) {
769 5 punk_wsconn *ws_ = (punk_wsconn *)ud;
770 5 ▶ SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
771 5 PK_LOOP_ENTER;
772 5 pw_on_stream_data_in(aTHX_ h, buf, len, fin, ud);
773 5 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 2 3 5
774 /*
775 # uncoverable branch false
776 # uncoverable condition right count:1 class:ignore_covered_err */
777 5 ▶ if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ 5 0 5
778 5 }
779  
780 /* The stream died under us - an h2 RST_STREAM, or the connection going. */
781 0 static void pw_on_stream_abort_in(pTHX_ void *h, void *ud) {
782 0 punk_wsconn *ws = (punk_wsconn *)ud;
783 PERL_UNUSED_ARG(h);
784 /*
785 # uncoverable branch false
786 # uncoverable condition right count:1 class:ignore_covered_err */
787 0 ▶ if (ws->state != PW_ST_CLOSED)
Branch: 1 branch
BranchTrueFalseTotal
if (ws->state != PW_ST_CLOSED) 0 0 0
788 0 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0);
789 0 }
790  
791 0 static void pw_on_stream_abort(pTHX_ void *h, void *ud) {
792 0 punk_wsconn *ws_ = (punk_wsconn *)ud;
793 0 ▶ SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
794 0 PK_LOOP_ENTER;
795 0 pw_on_stream_abort_in(aTHX_ h, ud);
796 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
797 /*
798 # uncoverable branch false
799 # uncoverable condition right count:1 class:ignore_covered_err */
800 0 ▶ if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ 0 0 0
801 0 }
802  
803 /* How many reads one readiness callback may do before it hands the loop
804 * back. A peer that keeps its socket non-empty would otherwise be read and
805 * processed until it stopped, and every other connection and timer on the
806 * worker waited for it. The watcher is level-triggered, so what is left
807 * fires it again. */
808 #define PW_READ_BUDGET 16
809  
810 1745 static void pw_on_readable_in(pTHX_ int fd, int mask, void *ud) {
811 1745 punk_wsconn *ws = (punk_wsconn *)ud;
812 1745 int budget = PW_READ_BUDGET;
813 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
814 26797 for (;;) {
815 ssize_t n;
816 28542 ▶ if (budget-- <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (budget-- <= 0) return; 1664 26878 28542
817 53756 size_t cap = ws->max_message_size
818 26878 ▶ ? ws->max_message_size + PW_READ_CHUNK : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
? ws->max_message_size + PW_READ_CHUNK : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 26878 0 26878
819 /*
820 # uncoverable branch true
821 # uncoverable condition left count:1 class:ignore_covered_err
822 # uncoverable condition right count:1 class:ignore_covered_err
823 # uncoverable condition left count:2 class:ignore_covered_err */
824 26878 ▶ if (cap && ws->rlen >= cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (cap && ws->rlen >= cap) { 26878 0 26878
if (cap && ws->rlen >= cap) { 0 26878 26878
825 0 pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big"); /* # uncoverable statement */
826 0 return; /* # uncoverable statement */
827 }
828 26878 pw_buf_reserve(&ws->rbuf, &ws->rcap, ws->rlen + PW_READ_CHUNK);
829 26878 n = read(ws->fd, ws->rbuf + ws->rlen, PW_READ_CHUNK);
830 26878 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 26814 64 26878
831 /* Processed as it arrives, not once the socket runs dry. The cap
832 * above is on what is buffered, and buffering everything a fast
833 * sender had ready counted whole frames that only needed handing
834 * on: megabytes of small valid messages were closed as one
835 * message too big. */
836 26814 ws->rlen += (size_t)n;
837 26814 pw_process(aTHX_ ws);
838 26814 ▶ if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 26797 17 26814
if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 26797 26797
839 26797 continue;
840 }
841 64 ▶ if (n == 0) { /* peer went away */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { /* peer went away */ 20 44 64
842 20 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0);
843 20 return;
844 }
845 /*
846 # uncoverable branch true
847 # uncoverable condition left count:1 class:ignore_covered_err */
848 44 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 44 44
849 /*
850 # uncoverable branch false class:ignore_covered_err
851 # uncoverable branch true count:1 class:ignore_covered_err
852 # uncoverable branch true count:2 class:ignore_covered_err
853 # uncoverable branch false count:3 class:ignore_covered_err
854 # uncoverable condition right count:1 class:ignore_covered_err
855 # uncoverable condition left count:2 class:ignore_covered_err
856 # uncoverable condition right count:2 class:ignore_covered_err */
857 44 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) break;
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 44 44
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 0 0
858 0 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); /* # uncoverable statement class:ignore_covered_err */
859 0 return; /* # uncoverable statement class:ignore_covered_err */
860 }
861 }
862  
863 1745 static void pw_on_readable(pTHX_ int fd, int mask, void *ud) {
864 1745 punk_wsconn *ws_ = (punk_wsconn *)ud;
865 1745 ▶ SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1745 0 1745
866 1745 PK_LOOP_ENTER;
867 1745 pw_on_readable_in(aTHX_ fd, mask, ud);
868 1745 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 36 1709 1745
869 /*
870 # uncoverable branch false
871 # uncoverable condition right count:1 class:ignore_covered_err */
872 1745 ▶ if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ 1745 0 1745
873 1745 }
874  
875 /* ---- construction and destruction ---------------------------------------- */
876  
877 50 static punk_wsconn *pw_new(pTHX_ int fd, HV *opts) {
878 punk_wsconn *ws;
879 SV **e;
880 50 Newxz(ws, 1, punk_wsconn);
881 50 ws->fd = fd;
882 50 ws->state = PW_ST_CONNECTING;
883 50 ws->cbs = newHV();
884 50 ws->max_message_size = PW_DEFAULT_MAX_MESSAGE;
885 50 ws->write_buffer_limit = PW_DEFAULT_WRITE_LIMIT;
886 50 pw_utf8_reset(&ws->utf8);
887 /*
888 # uncoverable branch false
889 # uncoverable condition right count:1 class:ignore_covered_err */
890 50 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 50 0 50
891 /* zero keeps the default: it used to mean no bound at all, and a
892 * connection with no bound could grow the worker without limit */
893 /*
894 # uncoverable condition right count:2 class:ignore_covered_err
895 # uncoverable condition right count:3 class:ignore_covered_err
896 # uncoverable condition right count:4 class:ignore_covered_err */
897 50 ▶ if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e)
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) 3 47 50
if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) 3 0 3
if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) 3 0 3
898 3 ▶ && SvUV(*e))
Branch: 1 branch
BranchTrueFalseTotal
&& SvUV(*e)) 3 0 3
899 3 ws->max_message_size = (size_t)SvUV(*e);
900 /*
901 # uncoverable condition right count:2 class:ignore_covered_err
902 # uncoverable condition right count:3 class:ignore_covered_err */
903 50 ▶ if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) 1 49 50
if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) 1 0 1
if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) 1 0 1
904 1 ws->write_buffer_limit = (size_t)SvUV(*e);
905 /*
906 # uncoverable condition right count:2 class:ignore_covered_err
907 # uncoverable condition right count:3 class:ignore_covered_err */
908 50 ▶ if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e)) 2 48 50
if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e)) 2 0 2
if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e)) 2 0 2
909 2 ws->protocol = newSVsv(*e);
910 /*
911 # uncoverable condition right count:2 class:ignore_covered_err
912 # uncoverable condition right count:3 class:ignore_covered_err */
913 50 ▶ if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e))
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) 10 40 50
if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) 10 0 10
if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) 10 0 10
914 10 ws->blocking = 1;
915 }
916 50 return ws;
917 }
918  
919 /* The blessed IV-ref -> the struct, guarded like Punk::Router's handles. */
920 338 static punk_wsconn *punk_ws_of(pTHX_ SV *self) {
921 /*
922 # uncoverable branch true
923 # uncoverable condition left count:1 class:ignore_covered_err
924 # uncoverable condition left count:2 class:ignore_covered_err */
925 338 ▶ if (!SvROK(self) || !SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) 338 0 338
if (!SvROK(self) || !SvIOK(SvRV(self))) 0 338 338
926 0 croak("Punk::WebSocket: not a connection"); /* # uncoverable statement */
927 338 return (punk_wsconn *)INT2PTR(void *, SvIV(SvRV(self)));
928 }
929  
930 50 static void pw_free(pTHX_ punk_wsconn *ws) {
931 /*
932 # uncoverable branch true
933 # uncoverable condition left count:1 class:ignore_covered_err */
934 50 ▶ if (!ws) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ws) return; 0 50 50
935 /*
936 # uncoverable branch true
937 # uncoverable condition left count:1 class:ignore_covered_err */
938 50 ▶ if (ws->fd >= 0) close(ws->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->fd >= 0) close(ws->fd); 0 50 50
939 /*
940 # uncoverable branch true
941 # uncoverable condition left count:1 class:ignore_covered_err */
942 50 ▶ if (ws->cbs) SvREFCNT_dec((SV *)ws->cbs);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->cbs) SvREFCNT_dec((SV *)ws->cbs); 0 50 50
943 50 ▶ if (ws->protocol) SvREFCNT_dec(ws->protocol);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->protocol) SvREFCNT_dec(ws->protocol); 2 48 50
944 50 ▶ if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); 10 40 50
945 /*
946 # uncoverable branch false
947 # uncoverable condition right count:1 class:ignore_covered_err */
948 50 ▶ if (ws->rbuf) free(ws->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->rbuf) free(ws->rbuf); 50 0 50
949 /*
950 # uncoverable branch false
951 # uncoverable condition right count:1 class:ignore_covered_err */
952 50 ▶ if (ws->wbuf) free(ws->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->wbuf) free(ws->wbuf); 50 0 50
953 50 ▶ if (ws->frag) free(ws->frag);
Branch: 1 branch
BranchTrueFalseTotal
if (ws->frag) free(ws->frag); 5 45 50
954 50 Safefree(ws);
955 }
956  
957 #endif /* PUNK_WSCONN_H */
« include/punk/punk_model.h ↑ Summary include/punk/punk_host.h »