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