xs/websocket.xs

filter: Statementstmt 84.1% 133 / 158 Branchbran 52.9% 110 / 204 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.5% 243 / 362 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/upload.xs ↑ Summary xs/headers.xs »
1 MODULE = Punk PACKAGE = Punk::WebSocket
2  
3 PROTOTYPES: DISABLE
4  
5 # ---- stateless codec hooks -------------------------------------------------
6 # The frame layer on its own, so it can be driven from tests against an
7 # independent reference implementation, and so Rooms can encode a broadcast
8 # frame once and queue the same bytes to every member.
9  
10 # Encode one server frame (never masked).
11 SV *
12 _encode_frame(opcode, fin, payload)
13 IV opcode
14 IV fin
15 SV *payload
16 CODE:
17 {
18 STRLEN plen;
19 13 const char *p = SvPV_const(payload, plen);
20 char hdr[10];
21 13 size_t hn = pw_encode_header(hdr, (unsigned char)opcode,
22 fin ? 1 : 0, plen);
23 13 RETVAL = newSVpvn(hdr, hn);
24 13 ▶ if (plen) sv_catpvn(RETVAL, p, plen);
Branch: 1 branch
BranchTrueFalseTotal
if (plen) sv_catpvn(RETVAL, p, plen); 11 2 13
25 }
26 OUTPUT:
27 RETVAL
28  
29 # One complete text (UTF-8) or binary message, ready to write. Rooms encode
30 # a broadcast once through these and queue the same bytes to every member.
31 SV *
32 _encode_text(payload)
33 SV *payload
34 ALIAS:
35 _encode_binary = 1
36 CODE:
37 {
38 STRLEN plen;
39 2 const char *p = ix ? SvPV_const(payload, plen)
40 200006 ▶ : SvPVutf8(payload, plen);
Branch: 1 branch
BranchTrueFalseTotal
: SvPVutf8(payload, plen); 2 100002 100004
41 char hdr[10];
42 100004 ▶ size_t hn = pw_encode_header(hdr, ix ? PW_OP_BINARY : PW_OP_TEXT,
Branch: 1 branch
BranchTrueFalseTotal
size_t hn = pw_encode_header(hdr, ix ? PW_OP_BINARY : PW_OP_TEXT, 2 100002 100004
43 1, plen);
44 100004 RETVAL = newSVpvn(hdr, hn);
45 100004 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 100004 0 100004
46 # uncoverable condition right count:1 class:ignore_covered_err
47 if (plen) sv_catpvn(RETVAL, p, plen);
48 }
49 OUTPUT:
50 RETVAL
51  
52 # Encode a close frame: 2-byte code plus an optional reason.
53 SV *
54 _encode_close(code, ...)
55 IV code
56 CODE:
57 {
58 char body[2 + PW_MAX_REASON], hdr[10];
59 3 const char *reason = NULL;
60 3 STRLEN rlen = 0;
61 size_t blen, hn;
62 5 ▶ if (items > 1 && SvOK(ST(1))) reason = SvPVutf8(ST(1), rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) reason = SvPVutf8(ST(1), rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 1 3
if (items > 1 && SvOK(ST(1))) reason = SvPVutf8(ST(1), rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
63 3 blen = pw_close_payload(body, (uint16_t)code, reason, rlen);
64 3 hn = pw_encode_header(hdr, PW_OP_CLOSE, 1, blen);
65 3 RETVAL = newSVpvn(hdr, hn);
66 3 sv_catpvn(RETVAL, body, blen);
67 }
68 OUTPUT:
69 RETVAL
70  
71 # Decode one frame from a buffer of client (masked) bytes.
72 # (consumed, fin, opcode, payload) a whole frame
73 # (0) need more data
74 # (negative) protocol failure: -1 proto (1002),
75 # -2 too big (1009), -3 utf8 (1007)
76 # max_msg defaults to 0 (unbounded); server defaults to 1 (masking required).
77 void
78 _decode_frame(bytes, ...)
79 SV *bytes
80 PPCODE:
81 {
82 STRLEN len;
83 105035 const char *buf = SvPV_const(bytes, len);
84 105035 ▶ size_t max_msg = items > 1 && SvOK(ST(1)) ? (size_t)SvUV(ST(1)) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
size_t max_msg = items > 1 && SvOK(ST(1)) ? (size_t)SvUV(ST(1)) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 105031 105035
size_t max_msg = items > 1 && SvOK(ST(1)) ? (size_t)SvUV(ST(1)) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
85 105035 ▶ int server = items > 2 && SvOK(ST(2)) ? (int)SvIV(ST(2)) : 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int server = items > 2 && SvOK(ST(2)) ? (int)SvIV(ST(2)) : 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 105034 105035
int server = items > 2 && SvOK(ST(2)) ? (int)SvIV(ST(2)) : 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
86 pw_frame f;
87 105035 int n = pw_decode_frame(buf, len, &f, max_msg, server);
88 105035 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 21 105014 105035
89 21 ▶ mXPUSHi(n);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(n); 0 21 21
90 }
91 else {
92 105014 SV *pl = newSVpvn(f.payload, (STRLEN)f.payload_len);
93 105014 ▶ if (f.masked) pw_unmask(&f, SvPVX(pl));
Branch: 1 branch
BranchTrueFalseTotal
if (f.masked) pw_unmask(&f, SvPVX(pl)); 105013 1 105014
94 105014 ▶ mXPUSHi(n);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(n); 0 105014 105014
95 105014 ▶ mXPUSHi(f.fin);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(f.fin); 0 105014 105014
96 105014 ▶ mXPUSHi(f.opcode);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(f.opcode); 0 105014 105014
97 105014 ▶ mXPUSHs(pl);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(pl); 0 105014 105014
98 }
99 }
100  
101 # Is this a well-formed UTF-8 string? (the 1007 check, exposed for tests)
102 int
103 _utf8_valid(bytes)
104 SV *bytes
105 CODE:
106 {
107 STRLEN len;
108 10 const char *p = SvPV_const(bytes, len);
109 10 RETVAL = pw_utf8_valid(p, len);
110 }
111 OUTPUT:
112 RETVAL
113  
114 # May a peer send this close code? (RFC 6455 7.4)
115 int
116 _close_code_ok(code)
117 IV code
118 CODE:
119 18 RETVAL = pw_close_code_ok((uint16_t)code);
120 OUTPUT:
121 RETVAL
122  
123 # ---- the live connection ---------------------------------------------------
124  
125 # Is Hyperman's detach ABI available? (the boot check behind websocket routes)
126 int
127 _hm_available()
128 CODE:
129 104 ▶ RETVAL = punk_hm(aTHX) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_hm(aTHX) ? 1 : 0; 4 100 104
130 OUTPUT:
131 RETVAL
132  
133 # Take this request's socket over: $env -> the client fd, now ours.
134 IV
135 _hm_detach(env)
136 SV *env
137 CODE:
138 {
139 38 const hm_abi *A = punk_hm(aTHX);
140 HV *ehv;
141 SV **e;
142 AV *cid;
143 SV **fsv, **isv;
144 void *loop;
145 int fd, rc;
146 38 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 38 38
147 0 # uncoverable condition left count:1 class:ignore_covered_err
148 if (!A)
149 38 ▶ croak("Punk::WebSocket: Hyperman's detach ABI is unavailable " /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk::WebSocket: Hyperman's detach ABI is unavailable " /* # uncoverable statement */ 38 0 38
croak("Punk::WebSocket: Hyperman's detach ABI is unavailable " /* # uncoverable statement */ 0 38 38
150 0 "(needs Hyperman 0.11+)");
151 38 # uncoverable branch true
152 38 # uncoverable condition left count:1 class:ignore_covered_err
153 38 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 38 0 38
# uncoverable condition left count:2 class:ignore_covered_err 38 0 38
# uncoverable condition left count:2 class:ignore_covered_err 38 0 38
# uncoverable condition left count:2 class:ignore_covered_err 0 38 38
154 0 if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV)
155 croak("Punk::WebSocket::_hm_detach: env must be a hashref"); /* # uncoverable statement */
156 38 ehv = (HV *)SvRV(env);
157 38 e = hv_fetchs(ehv, "psgix.hyperman.conn", 0);
158 38 # uncoverable branch true
159 38 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 38 0 38
# uncoverable condition left count:1 class:ignore_covered_err 38 0 38
# uncoverable condition left count:1 class:ignore_covered_err 38 0 38
# uncoverable condition left count:1 class:ignore_covered_err 0 38 38
160 0 # uncoverable condition left count:2 class:ignore_covered_err
161 38 # uncoverable condition left count:3 class:ignore_covered_err
162 38 ▶ # uncoverable condition left count:4 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:4 class:ignore_covered_err 0 38 38
163 38 if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV))
164 38 croak("Punk::WebSocket: this request cannot be detached " /* # uncoverable statement */
165 38 ▶ "(no psgix.hyperman.conn)");
Branch: 1 branch
BranchTrueFalseTotal
"(no psgix.hyperman.conn)"); 0 38 38
166 0 ▶ cid = (AV *)SvRV(*e);
Branch: 5 branches
BranchTrueFalseTotal
cid = (AV *)SvRV(*e); 0 0 0
cid = (AV *)SvRV(*e); 0 0 0
cid = (AV *)SvRV(*e); 0 0 0
cid = (AV *)SvRV(*e); 0 0 0
cid = (AV *)SvRV(*e); 0 0 0
167 fsv = av_fetch(cid, 0, 0);
168 isv = av_fetch(cid, 1, 0);
169 # uncoverable branch true
170 # uncoverable condition left count:1 class:ignore_covered_err
171 # uncoverable condition left count:2 class:ignore_covered_err
172 # uncoverable condition left count:3 class:ignore_covered_err
173 # uncoverable condition left count:4 class:ignore_covered_err
174 if (!(fsv && *fsv && isv && *isv))
175 croak("Punk::WebSocket: malformed psgix.hyperman.conn"); /* # uncoverable statement */
176 loop = A->cur_loop(aTHX);
177 # uncoverable branch true
178 # uncoverable condition left count:1 class:ignore_covered_err
179 if (!loop) croak("Punk::WebSocket: no running Hyperman loop");
180 fd = (int)SvIV(*fsv);
181 rc = A->conn_detach(aTHX_ loop, fd, SvUV(*isv));
182 # uncoverable branch true
183 38 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 38 38
184 if (rc != 0)
185 croak("Punk::WebSocket: detach failed (%d: %s)", rc, /* # uncoverable statement */
186 rc == -1 ? "connection gone or stale" /* # uncoverable statement */
187 /* These two are handled before the ladder reaches here, so
188 * a live server never shows them: an h2/h3 upgrade is an
189 * Extended CONNECT that opens a stream handle, and a TLS
190 * HTTP/1.1 upgrade opens a 101 stream handle (the tunnel).
191 * Both are above; only an older Hyperman with no such
192 * handle falls through to a detach that then refuses. */
193 : rc == -2 ? "HTTP/2 streams share one connection, so there " /* # uncoverable statement */
194 "is no descriptor to hand over; WebSocket over "
195 "h2 uses Extended CONNECT, which needs a Hyperman "
196 "new enough to open a stream handle"
197 # uncoverable statement
198 # uncoverable condition left count:1
199 # uncoverable condition right count:1
200 : rc == -3 ? "a TLS session cannot be detached; a websocket "
201 "over HTTPS needs Hyperman 0.48+, whose 101 "
202 "stream handle carries it instead"
203 # uncoverable statement
204 # uncoverable condition left count:1
205 # uncoverable condition right count:1
206 : rc == -4 ? "a response is still draining"
207 # uncoverable statement
208 # uncoverable condition left count:1
209 # uncoverable condition right count:1
210 # uncoverable condition left count:2
211 # uncoverable condition right count:2
212 : rc == -5 ? "already detached" : "unknown");
213 RETVAL = (IV)fd;
214 }
215 OUTPUT:
216 RETVAL
217  
218 # Attach to a detached fd: queue the handshake bytes, arm the read watcher.
219 SV *
220 _attach(class, fd, handshake, opts)
221 SV *class
222 IV fd
223 SV *handshake
224 SV *opts
225 ALIAS:
226 _attach_blocking = 1
227 CODE:
228 {
229 punk_wsconn *ws;
230 48 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 48 0 48
231 96 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 48 0 48
232 48 ▶ const hm_abi *A = ix ? NULL : punk_hm(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
const hm_abi *A = ix ? NULL : punk_hm(aTHX); 38 10 48
233 STRLEN hlen;
234 const char *hs;
235 48 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 38 10 48
# uncoverable branch true 0 38 38
236 0 # uncoverable condition left count:1 class:ignore_covered_err
237 48 # uncoverable condition left count:2 class:ignore_covered_err
238 48 if (!ix && !A)
239 48 ▶ croak("Punk::WebSocket: Hyperman's ABI is unavailable"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::WebSocket: Hyperman's ABI is unavailable"); /* # uncoverable statement */ 38 10 48
240 38 ws = pw_new(aTHX_ (int)fd, o);
241 38 ws->blocking = ix ? 1 : 0;
242 38 ▶ if (!ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ix) { 0 38 38
243 0 ws->abi = A;
244 ws->loop = A->cur_loop(aTHX);
245 # uncoverable branch true
246 # uncoverable condition left count:1 class:ignore_covered_err
247 48 if (!ws->loop) { pw_free(aTHX_ ws);
248 48 ▶ croak("Punk::WebSocket: no running loop"); } /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk::WebSocket: no running loop"); } /* # uncoverable statement */ 48 0 48
croak("Punk::WebSocket: no running loop"); } /* # uncoverable statement */ 10 38 48
249 }
250 /* non-blocking on the loop; the blocking fallback wants it blocking */
251 96 {
252 int fl = fcntl((int)fd, F_GETFL, 0);
253 # uncoverable branch true count:3
254 48 # uncoverable branch false class:ignore_covered_err
255 48 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 48 0 48
256 48 if (fl >= 0) (void)fcntl((int)fd, F_SETFL,
257 48 ▶ ix ? (fl & ~O_NONBLOCK) : (fl | O_NONBLOCK));
Branch: 1 branch
BranchTrueFalseTotal
ix ? (fl & ~O_NONBLOCK) : (fl | O_NONBLOCK)); 48 0 48
258 48 }
259 48 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class), PTR2IV(ws));
260 /* a strong self-reference keeps the connection alive after the
261 48 ▶ * handler that wired it returns; teardown drops it */
Branch: 1 branch
BranchTrueFalseTotal
* handler that wired it returns; teardown drops it */ 38 10 48
262 38 ws->self_rv = newSVsv(RETVAL);
263 ws->state = PW_ST_OPEN;
264 38 hs = SvPV_const(handshake, hlen);
265 # uncoverable branch false
266 # uncoverable condition right count:1 class:ignore_covered_err
267 if (hlen) {
268 pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, hs, hlen);
269 pw_flush(aTHX_ ws);
270 }
271 if (!ix) {
272 ws->abi->io_watch(aTHX_ ws->loop, ws->fd, HM_ABI_READ,
273 pw_on_readable, ws);
274 ws->reading = 1;
275 }
276 }
277 OUTPUT:
278 RETVAL
279  
280 # Attach to an Extended CONNECT stream (RFC 8441 on HTTP/2, RFC 9220 on
281 # HTTP/3) rather than to a descriptor.
282 #
283 # There is no socket here and no handshake bytes to flush: the 200 and its
284 # headers went out when the stream handle was opened, because on a
285 # multiplexed transport the acceptance IS the response. Everything after
286 # that point - framing, masking, ping/pong, the close handshake - is the
287 # same codec the detached path uses, which is why this is a second door and
288 # not a second implementation.
289 SV *
290 _attach_stream(class, handle, opts)
291 SV *class
292 IV handle
293 SV *opts
294 CODE:
295 {
296 punk_wsconn *ws;
297 2 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 2 0 2
298 4 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 2 0 2
299 2 const hm_abi *A = punk_hm(aTHX);
300 2 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 2 2
301 2 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 2 0 2
# uncoverable condition left count:1 class:ignore_covered_err 0 2 2
302 0 if (!A) croak("Punk::WebSocket: Hyperman's ABI is unavailable");
303 # uncoverable branch true
304 2 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 2 2
305 2 # uncoverable condition left count:2 class:ignore_covered_err
306 2 if (A->abi_version < 8 || !A->stream_on_data)
307 2 croak("Punk::WebSocket: websocket over HTTP/2 or HTTP/3 needs " /* # uncoverable statement */
308 2 "Hyperman's ABI v8 (the read half of a stream handle)");
309 2 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 2 2
310 0 # uncoverable condition left count:1 class:ignore_covered_err
311 4 if (!handle) croak("Punk::WebSocket: no stream handle");
312 2 ws = pw_new(aTHX_ -1, o); /* -1: there is no descriptor */
313 2 ws->abi = A;
314 2 ws->loop = A->cur_loop(aTHX);
315 2 ws->sh = INT2PTR(void *, handle);
316 # uncoverable branch true
317 # uncoverable condition left count:1 class:ignore_covered_err
318 if (!ws->loop) { pw_free(aTHX_ ws);
319 croak("Punk::WebSocket: no running loop"); } /* # uncoverable statement */
320 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class), PTR2IV(ws));
321 ws->self_rv = newSVsv(RETVAL);
322 ws->state = PW_ST_OPEN;
323 (void)A->stream_on_data(aTHX_ ws->sh, pw_on_stream_data, ws);
324 (void)A->stream_on_abort(aTHX_ ws->sh, pw_on_stream_abort, ws);
325 }
326 OUTPUT:
327 RETVAL
328  
329 # Register an event handler. Unknown names croak - a typo in a callback
330 # name is a bug you want at wiring time, not silence at runtime.
331 SV *
332 on(self, event, cb)
333 SV *self
334 SV *event
335 SV *cb
336 CODE:
337 {
338 101 punk_wsconn *ws = punk_ws_of(aTHX_ self);
339 STRLEN elen;
340 101 const char *ev = SvPV_const(event, elen);
341 static const char *const known[] = {
342 "open", "message", "binary", "ping", "pong", "close", "error", NULL
343 };
344 101 int i, ok = 0;
345 303 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 303 0 303
346 303 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 101 202 303
347 101 ▶ for (i = 0; known[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) 0 101 101
348 0 if (strEQ(ev, known[i])) { ok = 1; break; }
349 # uncoverable branch true
350 101 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 101 0 101
# uncoverable condition left count:1 class:ignore_covered_err 0 101 101
351 0 if (!ok)
352 croak("Punk::WebSocket: unknown event '%s' (open, message, " /* # uncoverable statement */
353 101 ▶ "binary, ping, pong, close, error)", ev); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
"binary, ping, pong, close, error)", ev); /* # uncoverable statement */ 101 0 101
354 101 # uncoverable branch true
355 # uncoverable condition left count:1 class:ignore_covered_err
356 # uncoverable condition left count:2 class:ignore_covered_err
357 if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV)
358 croak("Punk::WebSocket: the handler for '%s' must be a coderef", /* # uncoverable statement */
359 ev); /* # uncoverable statement */
360 # uncoverable branch false
361 # uncoverable condition right count:1 class:ignore_covered_err
362 if (ws->cbs) (void)hv_store(ws->cbs, ev, (I32)elen, newSVsv(cb), 0);
363 RETVAL = newSVsv(self);
364 }
365 OUTPUT:
366 RETVAL
367  
368 # Fire the open event (the dispatcher does this once the handler has wired).
369 void
370 _open(self)
371 SV *self
372 CODE:
373 {
374 49 punk_wsconn *ws = punk_ws_of(aTHX_ self);
375 49 (void)pw_emit(aTHX_ ws, "open", NULL, 0);
376 }
377  
378 void
379 send(self, payload)
380 SV *self
381 SV *payload
382 ALIAS:
383 send_binary = 1
384 ping = 2
385 pong = 3
386 CODE:
387 {
388 149 punk_wsconn *ws = punk_ws_of(aTHX_ self);
389 STRLEN len;
390 const char *p;
391 149 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 3 146 149
# uncoverable condition left count:2 class:ignore_covered_err 0 3 3
# uncoverable condition left count:2 class:ignore_covered_err 0 0 0
392 # uncoverable condition left count:3 class:ignore_covered_err
393 # uncoverable condition right count:3 class:ignore_covered_err
394 149 ▶ unsigned char op = ix == 0 ? PW_OP_TEXT
Branch: 1 branch
BranchTrueFalseTotal
unsigned char op = ix == 0 ? PW_OP_TEXT 79 70 149
395 140 ▶ : ix == 1 ? PW_OP_BINARY
Branch: 1 branch
BranchTrueFalseTotal
: ix == 1 ? PW_OP_BINARY 67 3 70
396 70 ▶ : ix == 2 ? PW_OP_PING : PW_OP_PONG; /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
: ix == 2 ? PW_OP_PING : PW_OP_PONG; /* # uncoverable statement */ 70 0 70
: ix == 2 ? PW_OP_PING : PW_OP_PONG; /* # uncoverable statement */ 0 70 70
: ix == 2 ? PW_OP_PING : PW_OP_PONG; /* # uncoverable statement */ 0 0 0
397 0 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 0 0 0
398 # uncoverable condition left count:1 class:ignore_covered_err
399 70 if (ws->state != PW_ST_OPEN) return;
400 p = ix == 0 ? SvPVutf8(payload, len) : SvPV_const(payload, len);
401 # uncoverable branch true
402 # uncoverable condition left count:1 class:ignore_covered_err
403 # uncoverable condition left count:2 class:ignore_covered_err
404 # uncoverable condition left count:3 class:ignore_covered_err
405 # uncoverable condition right count:3 class:ignore_covered_err
406 if ((op == PW_OP_PING || op == PW_OP_PONG) && len > PW_MAX_CONTROL)
407 croak("Punk::WebSocket: a %s payload is limited to %d bytes", /* # uncoverable statement */
408 op == PW_OP_PING ? "ping" : "pong", PW_MAX_CONTROL); /* # uncoverable statement */
409 pw_queue_frame(aTHX_ ws, op, p, len);
410 }
411  
412 # Queue pre-encoded frame bytes (Punk::WebSocket::Room broadcasts).
413 void
414 # uncoverable statement
415 # uncoverable subroutine
416 _send_raw(self, frame)
417 SV *self
418 SV *frame
419 CODE:
420 {
421 0 punk_wsconn *ws = punk_ws_of(aTHX_ self); /* # uncoverable statement */
422 STRLEN len;
423 0 const char *p = SvPV_const(frame, len); /* # uncoverable statement */
424 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
425 0 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 0 0
426 0 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 0 0 0
427 0 # uncoverable condition left count:1
428 0 # uncoverable condition right count:1
429 if (ws->state != PW_ST_OPEN) return;
430 0 # uncoverable statement
431 0 # uncoverable branch true
432 # uncoverable branch false
433 # uncoverable condition left count:1
434 # uncoverable condition right count:1
435 # uncoverable condition left count:2
436 # uncoverable condition right count:2
437 if (ws->write_buffer_limit
438 && ws->wlen - ws->woff + len > ws->write_buffer_limit) { /* # uncoverable statement */
439 pw_fail(aTHX_ ws, PW_CLOSE_POLICY_VIOLATION, "send buffer full"); /* # uncoverable statement */
440 return; /* # uncoverable statement */
441 }
442 pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, p, len); /* # uncoverable statement */
443 pw_flush(aTHX_ ws); /* # uncoverable statement */
444 }
445  
446 void
447 close(self, ...)
448 SV *self
449 CODE:
450 {
451 7 punk_wsconn *ws = punk_ws_of(aTHX_ self);
452 7 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 7 0 7
# uncoverable condition right count:1 class:ignore_covered_err 7 0 7
453 7 # uncoverable condition right count:2 class:ignore_covered_err
454 7 ▶ IV code = items > 1 && SvOK(ST(1)) ? SvIV(ST(1)) : PW_CLOSE_NORMAL;
Branch: 1 branch
BranchTrueFalseTotal
IV code = items > 1 && SvOK(ST(1)) ? SvIV(ST(1)) : PW_CLOSE_NORMAL; 7 0 7
455 21 ▶ STRLEN rlen = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN rlen = 0; 7 0 7
456 const char *reason = items > 2 && SvOK(ST(2))
457 ? SvPVutf8(ST(2), rlen) : NULL;
458 /* the frame carries sixteen bits, and a peer may fail the
459 7 ▶ * connection on a code it may not receive: refuse before the cast
Branch: 3 branches
BranchTrueFalseTotal
* connection on a code it may not receive: refuse before the cast 7 0 7
* connection on a code it may not receive: refuse before the cast 7 0 7
* connection on a code it may not receive: refuse before the cast 0 7 7
460 0 * turns 70000 into 4464 or 0 into a reserved code */
461 # uncoverable branch true
462 7 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 7 7
463 7 # uncoverable condition left count:2 class:ignore_covered_err
464 # uncoverable condition left count:3 class:ignore_covered_err
465 7 ▶ if (code < 0 || code > 0xFFFF || !pw_close_code_ok((uint16_t)code))
Branch: 4 branches
BranchTrueFalseTotal
if (code < 0 || code > 0xFFFF || !pw_close_code_ok((uint16_t)code)) 4 3 7
if (code < 0 || code > 0xFFFF || !pw_close_code_ok((uint16_t)code)) 4 0 4
if (code < 0 || code > 0xFFFF || !pw_close_code_ok((uint16_t)code)) 4 0 4
if (code < 0 || code > 0xFFFF || !pw_close_code_ok((uint16_t)code)) 4 0 4
466 4 ▶ croak("Punk::WebSocket: close code %" IVdf " is not one a peer " /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::WebSocket: close code %" IVdf " is not one a peer " /* # uncoverable statement */ 4 0 4
467 4 "may be sent (1000-1003, 1007-1011 or 3000-4999)", code); /* # uncoverable statement */
468 # uncoverable branch true
469 # uncoverable condition left count:1 class:ignore_covered_err
470 if (ws->state == PW_ST_CLOSED) return;
471 pw_queue_close(aTHX_ ws, (uint16_t)code, reason, rlen);
472 /* give the peer a bounded window to echo the close */
473 # uncoverable condition right count:2 class:ignore_covered_err
474 # uncoverable condition right count:3 class:ignore_covered_err
475 # uncoverable condition right count:4 class:ignore_covered_err
476 # uncoverable condition right count:5 class:ignore_covered_err
477 if (!ws->blocking && ws->abi && ws->loop && !ws->close_tw
478 && ws->state == PW_ST_CLOSING)
479 ws->close_tw = ws->abi->timer(aTHX_ ws->loop, PW_CLOSE_TIMEOUT,
480 pw_close_timeout, ws);
481 }
482  
483 IV
484 state(self)
485 SV *self
486 ALIAS:
487 is_open = 1
488 is_closing = 2
489 is_closed = 3
490 fd = 4
491 CODE:
492 {
493 1 punk_wsconn *ws = punk_ws_of(aTHX_ self);
494 1 ▶ RETVAL = ix == 0 ? ws->state /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ix == 0 ? ws->state /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 1 1
495 2 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 1 1
# uncoverable condition left count:1 class:ignore_covered_err 0 1 1
# uncoverable condition left count:1 class:ignore_covered_err 0 1 1
# uncoverable condition left count:1 class:ignore_covered_err 0 1 1
496 0 # uncoverable condition left count:2 class:ignore_covered_err
497 0 # uncoverable condition left count:3 class:ignore_covered_err
498 1 # uncoverable condition left count:4 class:ignore_covered_err
499 : ix == 1 ? (ws->state == PW_ST_OPEN)
500 : ix == 2 ? (ws->state == PW_ST_CLOSING) /* # uncoverable statement class:ignore_covered_err */
501 : ix == 3 ? (ws->state == PW_ST_CLOSED) /* # uncoverable statement class:ignore_covered_err */
502 : ws->fd;
503 }
504 OUTPUT:
505 RETVAL
506  
507 SV *
508 protocol(self)
509 SV *self
510 CODE:
511 {
512 2 punk_wsconn *ws = punk_ws_of(aTHX_ self);
513 2 ▶ RETVAL = ws->protocol ? newSVsv(ws->protocol) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ws->protocol ? newSVsv(ws->protocol) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
514 }
515 OUTPUT:
516 RETVAL
517  
518 # The blocking fallback: own the socket inside the handler until it closes.
519 void
520 _run_blocking(self)
521 SV *self
522 CODE:
523 {
524 10 punk_wsconn *ws = punk_ws_of(aTHX_ self);
525 23 ▶ while (ws->state != PW_ST_CLOSED) {
Branch: 1 branch
BranchTrueFalseTotal
while (ws->state != PW_ST_CLOSED) { 17 6 23
526 ssize_t n;
527 17 pw_buf_reserve(&ws->rbuf, &ws->rcap, ws->rlen + PW_READ_CHUNK);
528 17 n = read(ws->fd, ws->rbuf + ws->rlen, PW_READ_CHUNK);
529 17 ▶ if (n > 0) { ws->rlen += (size_t)n; pw_process(aTHX_ ws); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { ws->rlen += (size_t)n; pw_process(aTHX_ ws); continue; } 13 4 17
530 4 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 4 4
# uncoverable branch true 0 0 0
531 4 # uncoverable condition left count:1 class:ignore_covered_err
532 4 # uncoverable condition left count:2 class:ignore_covered_err
533 # uncoverable condition right count:2 class:ignore_covered_err
534 if (n < 0 && errno == EINTR) continue;
535 pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0);
536 break;
537 }
538 }
539  
540 void
541 DESTROY(self)
542 SV *self
543 CODE:
544 {
545 50 ▶ punk_wsconn *ws = (SvROK(self) && SvIOK(SvRV(self)))
Branch: 1 branch
BranchTrueFalseTotal
punk_wsconn *ws = (SvROK(self) && SvIOK(SvRV(self))) 50 0 50
546 100 ▶ ? (punk_wsconn *)INT2PTR(void *, SvIV(SvRV(self))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (punk_wsconn *)INT2PTR(void *, SvIV(SvRV(self))) : NULL; 50 0 50
547 50 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 50 50
548 # uncoverable condition left count:1 class:ignore_covered_err
549 if (!ws) return;
550 /* a live connection dropped without closing (worker exit): shut it
551 50 ▶ * down, but the self-ref means we normally arrive here after
Branch: 2 branches
BranchTrueFalseTotal
* down, but the self-ref means we normally arrive here after 0 50 50
* down, but the self-ref means we normally arrive here after 0 0 0
552 0 * teardown has already run */
553 50 # uncoverable branch true
554 # uncoverable condition left count:1 class:ignore_covered_err
555 # uncoverable condition left count:2 class:ignore_covered_err
556 # uncoverable condition right count:2 class:ignore_covered_err
557 if (ws->state != PW_ST_CLOSED && !ws->in_teardown)
558 pw_teardown(aTHX_ ws, PW_CLOSE_GOING_AWAY, NULL, 0); /* # uncoverable statement */
559 pw_free(aTHX_ ws);
560 }
« xs/upload.xs ↑ Summary xs/headers.xs »