| 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 |
460 |
| static const hm_abi *punk_hm_resolved(pTHX) { |
| 79 |
460 |
▶ | return PUNK_HM ? PUNK_HM : punk_hm(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| return PUNK_HM ? PUNK_HM : punk_hm(aTHX); |
10 |
450 |
460 |
|
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
1401 |
| static const hm_abi *punk_hm(pTHX) { |
| 83 |
1401 |
▶ | if (getenv("PUNK_NO_HM_ABI")) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (getenv("PUNK_NO_HM_ABI")) return NULL; |
11 |
1390 |
1401 |
|
| 84 |
1390 |
▶ | if (!PUNK_HM_TRIED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PUNK_HM_TRIED) { |
188 |
1202 |
1390 |
|
| 85 |
188 |
| dSP; int count, ok; IV p = 0; |
| 86 |
188 |
| PUNK_HM_TRIED = 1; |
| 87 |
188 |
| ok = pk_require_once(aTHX_ "Hyperman", FALSE); |
| 88 |
188 |
| SPAGAIN; /* the require may have reallocated the value stack */ |
| 89 |
188 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
188 |
0 |
188 |
|
| 90 |
188 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
188 |
188 |
|
| 91 |
188 |
| count = call_pv("Hyperman::_abi_ptr", G_SCALAR | G_EVAL); |
| 92 |
188 |
| SPAGAIN; |
| 93 |
188 |
▶ | if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
188 |
0 |
188 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
188 |
0 |
188 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
188 |
0 |
188 |
|
| 94 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 95 |
188 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
188 |
0 |
188 |
|
| 96 |
188 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
188 |
0 |
188 |
|
| 97 |
188 |
| const hm_abi *a = INT2PTR(const hm_abi *, p); |
| 98 |
|
| /* >= : the table only grows at its tail, so a later |
| 99 |
|
| * Hyperman still satisfies what we compiled against */ |
| 100 |
188 |
▶ | if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a; |
188 |
0 |
188 |
| if (a && a->abi_version >= HM_ABI_VERSION) PUNK_HM = a; |
188 |
0 |
188 |
|
| 101 |
|
| } |
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
1390 |
| return PUNK_HM; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /* Open a Hyperman stream handle for this request, or NULL. |
| 108 |
|
| * |
| 109 |
|
| * The ticket is psgix.hyperman.stream: [fd, generation, stream id], which |
| 110 |
|
| * unlike psgix.hyperman.conn is present on HTTP/2 - there is no fd that means |
| 111 |
|
| * "this stream", but there is a stream id, and that is the whole reason the |
| 112 |
|
| * seam is a handle rather than a descriptor. |
| 113 |
|
| * |
| 114 |
|
| * NULL when the server is not Hyperman, when the ticket names a connection |
| 115 |
|
| * that has gone, or when the response has not been deferred yet (an HTTP/1.1 |
| 116 |
|
| * handler that has not returned would have its return value serialised on top |
| 117 |
|
| * of the streamed body). Every caller falls through to the transport it would |
| 118 |
|
| * have used, so a NULL here is a choice of transport and never an error. |
| 119 |
|
| * |
| 120 |
|
| * No version check: punk_hm already refuses a table older than the header |
| 121 |
|
| * this was compiled against, so a resolved table has the v6 entries. */ |
| 122 |
9 |
| static void *punk_hm_stream_open(pTHX_ HV *envh, const hm_abi **out_abi, |
| 123 |
|
| void **out_loop, int status, AV *hdrs) { |
| 124 |
9 |
▶ | SV **t = envh ? hv_fetchs(envh, "psgix.hyperman.stream", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **t = envh ? hv_fetchs(envh, "psgix.hyperman.stream", 0) : NULL; |
9 |
0 |
9 |
|
| 125 |
|
| const hm_abi *A; |
| 126 |
|
| AV *tick; |
| 127 |
|
| SV **fsv, **isv, **ssv; |
| 128 |
|
| void *loop, *h; |
| 129 |
9 |
▶ | if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(t && *t && SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV)) return NULL; |
2 |
7 |
9 |
| 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 |
|
| 130 |
2 |
| A = punk_hm(aTHX); |
| 131 |
2 |
▶ | if (!A) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!A) return NULL; |
0 |
2 |
2 |
|
| 132 |
2 |
| tick = (AV *)SvRV(*t); |
| 133 |
2 |
| fsv = av_fetch(tick, 0, 0); |
| 134 |
2 |
| isv = av_fetch(tick, 1, 0); |
| 135 |
2 |
| ssv = av_fetch(tick, 2, 0); |
| 136 |
2 |
▶ | if (!(fsv && *fsv && isv && *isv)) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 137 |
2 |
| loop = A->cur_loop(aTHX); |
| 138 |
2 |
▶ | if (!loop) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!loop) return NULL; |
0 |
2 |
2 |
|
| 139 |
4 |
▶ | h = A->stream_open(aTHX_ loop, (int)SvIV(*fsv), SvUV(*isv), |
Branch: 1 branch
| Branch | True | False | Total |
| h = A->stream_open(aTHX_ loop, (int)SvIV(*fsv), SvUV(*isv), |
2 |
0 |
2 |
|
| 140 |
2 |
▶ | (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1, |
Branch: 1 branch
| Branch | True | False | Total |
| (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1, |
2 |
0 |
2 |
|
| 141 |
|
| status, sv_2mortal(newRV_inc((SV *)hdrs))); |
| 142 |
2 |
▶ | if (!h) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return NULL; |
0 |
2 |
2 |
|
| 143 |
2 |
| *out_abi = A; |
| 144 |
2 |
| *out_loop = loop; |
| 145 |
2 |
| return h; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /* ---- small buffer helpers ------------------------------------------------ */ |
| 149 |
|
| |
| 150 |
|
| /* a buffer bigger than this is returned once it is empty */ |
| 151 |
|
| #define PW_BUF_KEEP (64 * 1024) |
| 152 |
|
| |
| 153 |
31428 |
| static void pw_buf_reserve(char **buf, size_t *cap, size_t need) { |
| 154 |
31428 |
▶ | if (*cap >= need) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*cap >= need) return; |
31331 |
97 |
31428 |
|
| 155 |
|
| { |
| 156 |
97 |
▶ | size_t n = *cap ? *cap : 1024; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t n = *cap ? *cap : 1024; |
36 |
61 |
97 |
|
| 157 |
259 |
▶ | while (n < need) n *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (n < need) n *= 2; |
162 |
97 |
259 |
|
| 158 |
97 |
| *buf = (char *)realloc(*buf, n); |
| 159 |
97 |
▶ | if (!*buf) croak("Punk::WebSocket: out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*buf) croak("Punk::WebSocket: out of memory"); |
0 |
97 |
97 |
|
| 160 |
97 |
| *cap = n; |
| 161 |
|
| } |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
4514 |
| static void pw_append(char **buf, size_t *len, size_t *cap, |
| 165 |
|
| const char *src, size_t n) { |
| 166 |
4514 |
| pw_buf_reserve(buf, cap, *len + n); |
| 167 |
4514 |
| memcpy(*buf + *len, src, n); |
| 168 |
4514 |
| *len += n; |
| 169 |
4514 |
| } |
| 170 |
|
| |
| 171 |
|
| /* ---- event dispatch ------------------------------------------------------ |
| 172 |
|
| * The ABI contract is that C callbacks must not croak, so every call into |
| 173 |
|
| * Perl is trapped. A handler that dies surfaces as the error event and |
| 174 |
|
| * closes the connection with 1011. */ |
| 175 |
|
| |
| 176 |
|
| static void pw_queue_frame(pTHX_ punk_wsconn *ws, unsigned char op, |
| 177 |
|
| const char *payload, size_t len); |
| 178 |
|
| static void pw_fail(pTHX_ punk_wsconn *ws, uint16_t code, const char *why); |
| 179 |
|
| static void pw_teardown(pTHX_ punk_wsconn *ws, uint16_t code, |
| 180 |
|
| const char *reason, size_t rlen); |
| 181 |
|
| static void pw_flush(pTHX_ punk_wsconn *ws); |
| 182 |
|
| |
| 183 |
2118372 |
| static SV *pw_cb(pTHX_ punk_wsconn *ws, const char *name) { |
| 184 |
|
| SV **e; |
| 185 |
2118372 |
▶ | if (!ws->cbs) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ws->cbs) return NULL; |
0 |
2118372 |
2118372 |
|
| 186 |
2118372 |
| e = hv_fetch(ws->cbs, name, (I32)strlen(name), 0); |
| 187 |
2118372 |
▶ | return (e && *e && SvROK(*e)) ? *e : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvROK(*e)) ? *e : NULL; |
2118340 |
32 |
2118372 |
| return (e && *e && SvROK(*e)) ? *e : NULL; |
2118340 |
0 |
2118340 |
| return (e && *e && SvROK(*e)) ? *e : NULL; |
2118340 |
0 |
2118340 |
|
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* Fire an event with (self, @args). Returns 0 if a handler died. */ |
| 191 |
2116267 |
| static int pw_emit(pTHX_ punk_wsconn *ws, const char *name, |
| 192 |
|
| SV **args, int nargs) { |
| 193 |
2116267 |
| SV *cb = pw_cb(aTHX_ ws, name); |
| 194 |
2116267 |
| dSP; |
| 195 |
|
| int i, died; |
| 196 |
2116267 |
▶ | if (!cb) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cb) return 1; |
32 |
2116235 |
2116267 |
|
| 197 |
2116235 |
| ENTER; SAVETMPS; |
| 198 |
2116235 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2116235 |
2116235 |
|
| 199 |
2116235 |
▶ | EXTEND(SP, nargs + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, nargs + 1); |
2116235 |
0 |
2116235 |
| EXTEND(SP, nargs + 1); |
0 |
2116235 |
2116235 |
|
| 200 |
2116235 |
▶ | PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); |
2116235 |
0 |
2116235 |
|
| 201 |
4232470 |
▶ | for (i = 0; i < nargs; i++) PUSHs(args[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nargs; i++) PUSHs(args[i]); |
2116235 |
2116235 |
4232470 |
|
| 202 |
2116235 |
| PUTBACK; |
| 203 |
2116235 |
| call_sv(cb, G_DISCARD | G_EVAL); |
| 204 |
2116235 |
| SPAGAIN; |
| 205 |
2116235 |
▶ | died = SvTRUE(ERRSV) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV) ? 1 : 0; |
2116235 |
0 |
2116235 |
|
| 206 |
2116235 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
0 |
2116235 |
2116235 |
|
| 207 |
0 |
▶ | SV *err = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *err = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 208 |
0 |
| SV *ecb = pw_cb(aTHX_ ws, "error"); |
| 209 |
0 |
▶ | if (ecb && strNE(name, "error")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ecb && strNE(name, "error")) { |
0 |
0 |
0 |
| if (ecb && strNE(name, "error")) { |
0 |
0 |
0 |
|
| 210 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 211 |
0 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 212 |
0 |
▶ | PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(ws->self_rv ? ws->self_rv : &PL_sv_undef); |
0 |
0 |
0 |
|
| 213 |
0 |
| PUSHs(sv_2mortal(err)); |
| 214 |
0 |
| PUTBACK; |
| 215 |
0 |
| call_sv(ecb, G_DISCARD | G_EVAL); |
| 216 |
0 |
| SPAGAIN; |
| 217 |
0 |
▶ | (void)SvTRUE(ERRSV); /* an error handler that dies: give up */ |
Branch: 1 branch
| Branch | True | False | Total |
| (void)SvTRUE(ERRSV); /* an error handler that dies: give up */ |
0 |
0 |
0 |
|
| 218 |
|
| } |
| 219 |
|
| else { |
| 220 |
0 |
| STRLEN el; const char *ep = SvPV_const(err, el); |
| 221 |
|
| /* escaped: a die text is request-shaped, and a newline in it |
| 222 |
|
| * would be a second log line */ |
| 223 |
0 |
| PerlIO_printf(PerlIO_stderr(), |
| 224 |
|
| "Punk::WebSocket: %s handler died: %" SVf "\n", |
| 225 |
0 |
| name, SVfARG(pl_plain(aTHX_ ep, el, SvUTF8(err)))); |
| 226 |
0 |
| SvREFCNT_dec(err); |
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
2116235 |
| PUTBACK; |
| 230 |
2116235 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
2116235 |
2116235 |
|
| 231 |
2116235 |
| return !died; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| /* ---- writing ------------------------------------------------------------- |
| 235 |
|
| * Everything queues; pw_flush drains what the socket will take and arms a |
| 236 |
|
| * write watcher for the rest. A peer that will not read cannot stall the |
| 237 |
|
| * worker, and cannot make us drop bytes mid-frame either. */ |
| 238 |
|
| |
| 239 |
|
| static void pw_on_writable(pTHX_ int fd, int mask, void *ud); |
| 240 |
|
| |
| 241 |
2149 |
| static void pw_flush(pTHX_ punk_wsconn *ws) { |
| 242 |
2149 |
▶ | if (ws->sh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->sh) { |
4 |
2145 |
2149 |
|
| 243 |
|
| /* The server owns the framing and the backpressure here, so this is |
| 244 |
|
| * a hand-off rather than a write loop: there is no partial write to |
| 245 |
|
| * resume and no write watcher to arm. */ |
| 246 |
4 |
▶ | if (ws->woff < ws->wlen && ws->abi) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->woff < ws->wlen && ws->abi) { |
4 |
0 |
4 |
| if (ws->woff < ws->wlen && ws->abi) { |
4 |
0 |
4 |
|
| 247 |
4 |
| int r = ws->abi->stream_write(aTHX_ ws->sh, ws->wbuf + ws->woff, |
| 248 |
4 |
| ws->wlen - ws->woff); |
| 249 |
4 |
▶ | if (r < 0) { pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r < 0) { pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); return; } |
0 |
4 |
4 |
|
| 250 |
4 |
| ws->woff = ws->wlen; |
| 251 |
|
| } |
| 252 |
4 |
| ws->wlen = ws->woff = 0; |
| 253 |
4 |
▶ | if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 254 |
1 |
| pw_teardown(aTHX_ ws, 0, NULL, 0); |
| 255 |
4 |
| return; |
| 256 |
|
| } |
| 257 |
4290 |
▶ | while (ws->woff < ws->wlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ws->woff < ws->wlen) { |
2145 |
2145 |
4290 |
|
| 258 |
2145 |
| ssize_t n = write(ws->fd, ws->wbuf + ws->woff, ws->wlen - ws->woff); |
| 259 |
2145 |
▶ | if (n > 0) { ws->woff += (size_t)n; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { ws->woff += (size_t)n; continue; } |
2145 |
0 |
2145 |
|
| 260 |
0 |
▶ | if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
0 |
0 |
0 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
0 |
0 |
0 |
|
| 261 |
0 |
▶ | if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { |
0 |
0 |
0 |
| if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { |
0 |
0 |
0 |
| if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { |
0 |
0 |
0 |
| if (!ws->writing && !ws->blocking && ws->abi && ws->loop) { |
0 |
0 |
0 |
|
| 262 |
0 |
| ws->abi->io_watch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE, |
| 263 |
|
| pw_on_writable, ws); |
| 264 |
0 |
| ws->writing = 1; |
| 265 |
|
| } |
| 266 |
0 |
| return; |
| 267 |
|
| } |
| 268 |
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 |
|
| 269 |
0 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); /* write error */ |
| 270 |
0 |
| return; |
| 271 |
|
| } |
| 272 |
2145 |
| ws->wlen = ws->woff = 0; |
| 273 |
2145 |
▶ | if (ws->writing && ws->abi && ws->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (ws->writing && ws->abi && ws->loop) { |
0 |
2145 |
2145 |
| if (ws->writing && ws->abi && ws->loop) { |
0 |
0 |
0 |
| if (ws->writing && ws->abi && ws->loop) { |
0 |
0 |
0 |
|
| 274 |
0 |
| ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE); |
| 275 |
0 |
| ws->writing = 0; |
| 276 |
|
| } |
| 277 |
|
| /* the closing handshake is done once our close frame is on the wire */ |
| 278 |
2145 |
▶ | if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) |
Branch: 3 branches
| Branch | True | False | Total |
| if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) |
5 |
2140 |
2145 |
| if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) |
5 |
0 |
5 |
| if (ws->state == PW_ST_CLOSING && ws->close_sent && ws->close_rcvd) |
1 |
4 |
5 |
|
| 279 |
1 |
| pw_teardown(aTHX_ ws, 0, NULL, 0); |
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
0 |
| static void pw_on_writable_in(pTHX_ int fd, int mask, void *ud) { |
| 283 |
0 |
| punk_wsconn *ws = (punk_wsconn *)ud; |
| 284 |
|
| PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask); |
| 285 |
0 |
| pw_flush(aTHX_ ws); |
| 286 |
0 |
| } |
| 287 |
|
| |
| 288 |
0 |
| static void pw_on_writable(pTHX_ int fd, int mask, void *ud) { |
| 289 |
0 |
| punk_wsconn *ws_ = (punk_wsconn *)ud; |
| 290 |
0 |
▶ | SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
0 |
0 |
0 |
|
| 291 |
0 |
| PK_LOOP_ENTER; |
| 292 |
0 |
| pw_on_writable_in(aTHX_ fd, mask, ud); |
| 293 |
0 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
0 |
0 |
|
| 294 |
0 |
▶ | if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
0 |
0 |
0 |
|
| 295 |
0 |
| } |
| 296 |
|
| |
| 297 |
2126 |
| static void pw_queue_frame(pTHX_ punk_wsconn *ws, unsigned char op, |
| 298 |
|
| const char *payload, size_t len) { |
| 299 |
|
| char hdr[10]; |
| 300 |
|
| size_t hn; |
| 301 |
2126 |
▶ | if (ws->state == PW_ST_CLOSED) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->state == PW_ST_CLOSED) return; |
0 |
2126 |
2126 |
|
| 302 |
2126 |
▶ | if (ws->write_buffer_limit |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->write_buffer_limit |
2126 |
0 |
2126 |
|
| 303 |
2126 |
▶ | && 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 |
2126 |
2126 |
|
| 304 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_POLICY_VIOLATION, "send buffer full"); |
| 305 |
0 |
| return; |
| 306 |
|
| } |
| 307 |
2126 |
| hn = pw_encode_header(hdr, op, 1, len); |
| 308 |
2126 |
| pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, hdr, hn); |
| 309 |
2126 |
▶ | if (len) pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, payload, len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) pw_append(&ws->wbuf, &ws->wlen, &ws->wcap, payload, len); |
2126 |
0 |
2126 |
|
| 310 |
2126 |
| pw_flush(aTHX_ ws); |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
6 |
| static void pw_queue_close(pTHX_ punk_wsconn *ws, uint16_t code, |
| 314 |
|
| const char *reason, size_t rlen) { |
| 315 |
|
| char body[2 + PW_MAX_REASON]; |
| 316 |
|
| size_t blen; |
| 317 |
6 |
▶ | if (ws->close_sent || ws->state == PW_ST_CLOSED) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->close_sent || ws->state == PW_ST_CLOSED) return; |
6 |
0 |
6 |
| if (ws->close_sent || ws->state == PW_ST_CLOSED) return; |
0 |
6 |
6 |
|
| 318 |
6 |
| blen = pw_close_payload(body, code, reason, rlen); |
| 319 |
6 |
| ws->close_sent = 1; |
| 320 |
6 |
▶ | if (ws->state == PW_ST_OPEN) ws->state = PW_ST_CLOSING; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->state == PW_ST_OPEN) ws->state = PW_ST_CLOSING; |
6 |
0 |
6 |
|
| 321 |
6 |
| pw_queue_frame(aTHX_ ws, PW_OP_CLOSE, body, blen); |
| 322 |
|
| } |
| 323 |
|
| |
| 324 |
|
| /* A protocol failure: tell the peer why, then close. */ |
| 325 |
2 |
| static void pw_fail(pTHX_ punk_wsconn *ws, uint16_t code, const char *why) { |
| 326 |
2 |
▶ | if (ws->state == PW_ST_CLOSED || ws->in_teardown) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->state == PW_ST_CLOSED || ws->in_teardown) return; |
2 |
0 |
2 |
| if (ws->state == PW_ST_CLOSED || ws->in_teardown) return; |
0 |
2 |
2 |
|
| 327 |
|
| { |
| 328 |
2 |
▶ | SV *msg = sv_2mortal(newSVpv(why ? why : "protocol error", 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *msg = sv_2mortal(newSVpv(why ? why : "protocol error", 0)); |
2 |
0 |
2 |
|
| 329 |
2 |
| SV *a[1]; a[0] = msg; |
| 330 |
2 |
| (void)pw_emit(aTHX_ ws, "error", a, 1); |
| 331 |
|
| } |
| 332 |
2 |
▶ | pw_queue_close(aTHX_ ws, code, why, why ? strlen(why) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_queue_close(aTHX_ ws, code, why, why ? strlen(why) : 0); |
2 |
0 |
2 |
|
| 333 |
|
| /* do not wait for the peer's echo after a protocol failure */ |
| 334 |
2 |
▶ | pw_teardown(aTHX_ ws, code, why, why ? strlen(why) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_teardown(aTHX_ ws, code, why, why ? strlen(why) : 0); |
2 |
0 |
2 |
|
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* ---- teardown ------------------------------------------------------------ */ |
| 338 |
|
| |
| 339 |
22 |
| static void pw_teardown(pTHX_ punk_wsconn *ws, uint16_t code, |
| 340 |
|
| const char *reason, size_t rlen) { |
| 341 |
|
| SV *args[2]; |
| 342 |
22 |
▶ | if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; |
22 |
0 |
22 |
| if (ws->in_teardown || ws->state == PW_ST_CLOSED) return; |
2 |
20 |
22 |
|
| 343 |
20 |
| ws->in_teardown = 1; |
| 344 |
|
| |
| 345 |
20 |
▶ | if (ws->abi && ws->loop && !ws->blocking) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (ws->abi && ws->loop && !ws->blocking) { |
20 |
0 |
20 |
| if (ws->abi && ws->loop && !ws->blocking) { |
20 |
0 |
20 |
| if (ws->abi && ws->loop && !ws->blocking) { |
20 |
0 |
20 |
|
| 346 |
20 |
▶ | if (ws->reading) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_READ); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->reading) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_READ); |
18 |
2 |
20 |
|
| 347 |
20 |
▶ | if (ws->writing) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->writing) ws->abi->io_unwatch(aTHX_ ws->loop, ws->fd, HM_ABI_WRITE); |
0 |
20 |
20 |
|
| 348 |
20 |
▶ | if (ws->close_tw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->close_tw) { |
2 |
18 |
20 |
|
| 349 |
2 |
| ws->abi->timer_cancel(aTHX_ ws->loop, ws->close_tw); |
| 350 |
2 |
| ws->close_tw = NULL; |
| 351 |
|
| } |
| 352 |
|
| } |
| 353 |
20 |
| ws->reading = ws->writing = 0; |
| 354 |
20 |
▶ | if (ws->sh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->sh) { |
2 |
18 |
20 |
|
| 355 |
|
| /* Stream-backed: there is no descriptor to close, and the server |
| 356 |
|
| * owns the stream. Releasing the handle is what ends it - and it |
| 357 |
|
| * has to happen exactly once, so the pointer is cleared first. */ |
| 358 |
2 |
| void *h = ws->sh; |
| 359 |
2 |
| ws->sh = NULL; |
| 360 |
2 |
▶ | if (ws->abi) (void)ws->abi->stream_close(aTHX_ h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->abi) (void)ws->abi->stream_close(aTHX_ h); |
2 |
0 |
2 |
|
| 361 |
|
| } |
| 362 |
20 |
▶ | if (ws->fd >= 0) { close(ws->fd); ws->fd = -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->fd >= 0) { close(ws->fd); ws->fd = -1; } |
18 |
2 |
20 |
|
| 363 |
20 |
| ws->state = PW_ST_CLOSED; |
| 364 |
|
| |
| 365 |
20 |
▶ | args[0] = sv_2mortal(newSViv(ws->peer_code ? ws->peer_code : code)); |
Branch: 1 branch
| Branch | True | False | Total |
| args[0] = sv_2mortal(newSViv(ws->peer_code ? ws->peer_code : code)); |
2 |
18 |
20 |
|
| 366 |
2 |
| args[1] = ws->peer_reason ? sv_2mortal(newSVsv(ws->peer_reason)) |
| 367 |
20 |
▶ | : sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs("")); |
Branch: 2 branches
| Branch | True | False | Total |
| : sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs("")); |
2 |
18 |
20 |
| : sv_2mortal(rlen ? newSVpvn(reason, rlen) : newSVpvs("")); |
2 |
16 |
18 |
|
| 368 |
20 |
| (void)pw_emit(aTHX_ ws, "close", args, 2); |
| 369 |
|
| |
| 370 |
20 |
▶ | if (ws->cbs) { SvREFCNT_dec((SV *)ws->cbs); ws->cbs = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->cbs) { SvREFCNT_dec((SV *)ws->cbs); ws->cbs = NULL; } |
20 |
0 |
20 |
|
| 371 |
20 |
| ws->in_teardown = 0; |
| 372 |
|
| /* drop the self-reference last: this may free the object */ |
| 373 |
20 |
▶ | if (ws->self_rv) { SV *s = ws->self_rv; ws->self_rv = NULL; SvREFCNT_dec(s); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->self_rv) { SV *s = ws->self_rv; ws->self_rv = NULL; SvREFCNT_dec(s); } |
20 |
0 |
20 |
|
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
0 |
| static void pw_close_timeout_in(pTHX_ void *ud) { |
| 377 |
0 |
| punk_wsconn *ws = (punk_wsconn *)ud; |
| 378 |
0 |
| ws->close_tw = NULL; |
| 379 |
0 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, "close timeout", 13); |
| 380 |
0 |
| } |
| 381 |
|
| |
| 382 |
0 |
| static void pw_close_timeout(pTHX_ void *ud) { |
| 383 |
0 |
| punk_wsconn *ws_ = (punk_wsconn *)ud; |
| 384 |
0 |
▶ | SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
0 |
0 |
0 |
|
| 385 |
0 |
| PK_LOOP_ENTER; |
| 386 |
0 |
| pw_close_timeout_in(aTHX_ ud); |
| 387 |
0 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
0 |
0 |
|
| 388 |
0 |
▶ | if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
0 |
0 |
0 |
|
| 389 |
0 |
| } |
| 390 |
|
| |
| 391 |
|
| /* ---- reading ------------------------------------------------------------- |
| 392 |
|
| * One pass over everything buffered: control frames are answered inline, |
| 393 |
|
| * data frames reassemble into ws->frag until FIN. */ |
| 394 |
|
| |
| 395 |
26871 |
| static void pw_process(pTHX_ punk_wsconn *ws) { |
| 396 |
26871 |
| size_t off = 0; |
| 397 |
|
| /* Teardown drops the connection's self-reference, which can free the |
| 398 |
|
| * struct while we are still inside this loop (a close frame, or any |
| 399 |
|
| * protocol failure, does exactly that). Hold a reference for the |
| 400 |
|
| * duration so the tail of this function - and our caller - still have |
| 401 |
|
| * something to look at; the object goes away when this guard does. */ |
| 402 |
26871 |
▶ | SV *guard = ws->self_rv ? SvREFCNT_inc(ws->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *guard = ws->self_rv ? SvREFCNT_inc(ws->self_rv) : NULL; |
26871 |
0 |
26871 |
|
| 403 |
2143097 |
▶ | while (ws->state != PW_ST_CLOSED && off < ws->rlen) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (ws->state != PW_ST_CLOSED && off < ws->rlen) { |
0 |
2143097 |
2143097 |
| while (ws->state != PW_ST_CLOSED && off < ws->rlen) { |
2142927 |
170 |
2143097 |
|
| 404 |
|
| pw_frame f; |
| 405 |
2142927 |
| int n = pw_decode_frame(ws->rbuf + off, ws->rlen - off, &f, |
| 406 |
|
| ws->max_message_size, 1); |
| 407 |
2142927 |
▶ | if (n == PW_NEED_MORE) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == PW_NEED_MORE) break; |
26697 |
2116230 |
2142927 |
|
| 408 |
2116230 |
▶ | if (n == PW_E_PROTO) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == PW_E_PROTO) { |
1 |
2116229 |
2116230 |
|
| 409 |
1 |
| pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, "protocol error"); |
| 410 |
4 |
| goto consumed; |
| 411 |
|
| } |
| 412 |
2116229 |
▶ | if (n == PW_E_TOO_BIG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == PW_E_TOO_BIG) { |
0 |
2116229 |
2116229 |
|
| 413 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big"); |
| 414 |
0 |
| goto consumed; |
| 415 |
|
| } |
| 416 |
2116229 |
| pw_unmask(&f, ws->rbuf + off + f.header_size); |
| 417 |
|
| { |
| 418 |
2116229 |
| const char *pl = ws->rbuf + off + f.header_size; |
| 419 |
2116229 |
| size_t pn = (size_t)f.payload_len; |
| 420 |
|
| |
| 421 |
2116229 |
▶ | if (pw_is_control(f.opcode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pw_is_control(f.opcode)) { |
2107 |
2114122 |
2116229 |
|
| 422 |
2107 |
▶ | if (f.opcode == PW_OP_PING) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f.opcode == PW_OP_PING) { |
2105 |
2 |
2107 |
|
| 423 |
2105 |
| pw_queue_frame(aTHX_ ws, PW_OP_PONG, pl, pn); |
| 424 |
|
| /* the payload SV only when somebody listens */ |
| 425 |
2105 |
▶ | if (pw_cb(aTHX_ ws, "ping")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pw_cb(aTHX_ ws, "ping")) { |
2105 |
0 |
2105 |
|
| 426 |
2105 |
| SV *a[1]; a[0] = newSVpvn(pl, pn); |
| 427 |
2105 |
| (void)pw_emit(aTHX_ ws, "ping", a, 1); |
| 428 |
2105 |
| SvREFCNT_dec(a[0]); |
| 429 |
|
| } |
| 430 |
|
| } |
| 431 |
2 |
▶ | else if (f.opcode == PW_OP_PONG) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (f.opcode == PW_OP_PONG) { |
0 |
2 |
2 |
|
| 432 |
0 |
▶ | if (pw_cb(aTHX_ ws, "pong")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pw_cb(aTHX_ ws, "pong")) { |
0 |
0 |
0 |
|
| 433 |
0 |
| SV *a[1]; a[0] = newSVpvn(pl, pn); |
| 434 |
0 |
| (void)pw_emit(aTHX_ ws, "pong", a, 1); |
| 435 |
0 |
| SvREFCNT_dec(a[0]); |
| 436 |
|
| } |
| 437 |
|
| } |
| 438 |
|
| else { /* close */ |
| 439 |
2 |
| uint16_t code = PW_CLOSE_NO_STATUS; |
| 440 |
2 |
▶ | if (pn == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pn == 1) { |
0 |
2 |
2 |
|
| 441 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, |
| 442 |
|
| "bad close payload"); |
| 443 |
0 |
| goto consumed; |
| 444 |
|
| } |
| 445 |
2 |
▶ | if (pn >= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pn >= 2) { |
2 |
0 |
2 |
|
| 446 |
2 |
| code = (uint16_t)(((unsigned char)pl[0] << 8) |
| 447 |
2 |
| | (unsigned char)pl[1]); |
| 448 |
2 |
▶ | if (!pw_close_code_ok(code)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pw_close_code_ok(code)) { |
0 |
2 |
2 |
|
| 449 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, |
| 450 |
|
| "bad close code"); |
| 451 |
0 |
| goto consumed; |
| 452 |
|
| } |
| 453 |
2 |
▶ | if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) { |
1 |
1 |
2 |
| if (pn > 2 && !pw_utf8_valid(pl + 2, pn - 2)) { |
0 |
1 |
1 |
|
| 454 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD, |
| 455 |
|
| "close reason is not utf8"); |
| 456 |
0 |
| goto consumed; |
| 457 |
|
| } |
| 458 |
|
| } |
| 459 |
2 |
| ws->close_rcvd = 1; |
| 460 |
2 |
| ws->peer_code = code; |
| 461 |
2 |
▶ | if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); |
0 |
2 |
2 |
|
| 462 |
1 |
| ws->peer_reason = pn > 2 ? newSVpvn(pl + 2, pn - 2) |
| 463 |
2 |
▶ | : newSVpvs(""); |
Branch: 1 branch
| Branch | True | False | Total |
| : newSVpvs(""); |
1 |
1 |
2 |
|
| 464 |
2 |
| SvUTF8_on(ws->peer_reason); |
| 465 |
2 |
▶ | if (!ws->close_sent) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ws->close_sent) |
2 |
0 |
2 |
|
| 466 |
2 |
▶ | pw_queue_close(aTHX_ ws, code == PW_CLOSE_NO_STATUS |
Branch: 1 branch
| Branch | True | False | Total |
| pw_queue_close(aTHX_ ws, code == PW_CLOSE_NO_STATUS |
2 |
0 |
2 |
|
| 467 |
|
| ? PW_CLOSE_NORMAL : code, NULL, 0); |
| 468 |
2 |
| off += (size_t)n; |
| 469 |
|
| /* everything queued is flushed by pw_queue_close -> |
| 470 |
|
| * pw_flush, which tears down once it drains */ |
| 471 |
2 |
▶ | if (ws->wlen == ws->woff) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->wlen == ws->woff) |
2 |
0 |
2 |
|
| 472 |
2 |
| pw_teardown(aTHX_ ws, code, NULL, 0); |
| 473 |
2 |
| goto consumed; |
| 474 |
|
| } |
| 475 |
|
| } |
| 476 |
|
| else { /* data */ |
| 477 |
2114122 |
| unsigned char op = f.opcode; |
| 478 |
2114122 |
▶ | if (op == PW_OP_CONT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op == PW_OP_CONT) { |
1 |
2114121 |
2114122 |
|
| 479 |
1 |
▶ | if (!ws->frag_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ws->frag_active) { |
0 |
1 |
1 |
|
| 480 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, |
| 481 |
|
| "continuation without a start frame"); |
| 482 |
0 |
| goto consumed; |
| 483 |
|
| } |
| 484 |
|
| } |
| 485 |
|
| else { |
| 486 |
2114121 |
▶ | if (ws->frag_active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->frag_active) { |
0 |
2114121 |
2114121 |
|
| 487 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_PROTOCOL_ERROR, |
| 488 |
|
| "new data frame inside a fragmented message"); |
| 489 |
0 |
| goto consumed; |
| 490 |
|
| } |
| 491 |
2114121 |
| ws->frag_active = 1; |
| 492 |
2114121 |
| ws->frag_op = op; |
| 493 |
2114121 |
| ws->fraglen = 0; |
| 494 |
2114121 |
| pw_utf8_reset(&ws->utf8); |
| 495 |
|
| } |
| 496 |
2114122 |
▶ | if (ws->max_message_size |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->max_message_size |
2114122 |
0 |
2114122 |
|
| 497 |
2114122 |
▶ | && ws->fraglen + pn > ws->max_message_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| && ws->fraglen + pn > ws->max_message_size) { |
0 |
2114122 |
2114122 |
|
| 498 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, |
| 499 |
|
| "message too big"); |
| 500 |
0 |
| goto consumed; |
| 501 |
|
| } |
| 502 |
|
| /* validate text as it arrives, so a split multibyte |
| 503 |
|
| * character is judged on the whole message, not a fragment */ |
| 504 |
2114122 |
▶ | if (ws->frag_op == PW_OP_TEXT |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->frag_op == PW_OP_TEXT |
2102120 |
12002 |
2114122 |
|
| 505 |
2102120 |
▶ | && !pw_utf8_step(&ws->utf8, pl, pn)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !pw_utf8_step(&ws->utf8, pl, pn)) { |
1 |
2102119 |
2102120 |
|
| 506 |
1 |
| pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD, |
| 507 |
|
| "text is not utf8"); |
| 508 |
1 |
| goto consumed; |
| 509 |
|
| } |
| 510 |
|
| /* One frame that is the whole message - nearly every one - |
| 511 |
|
| * is handed on straight from the read buffer; only a |
| 512 |
|
| * fragmented message is gathered in `frag` first. */ |
| 513 |
|
| { |
| 514 |
2114121 |
▶ | int whole = f.fin && ws->fraglen == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| int whole = f.fin && ws->fraglen == 0; |
2114120 |
1 |
2114121 |
| int whole = f.fin && ws->fraglen == 0; |
2114119 |
1 |
2114120 |
|
| 515 |
2114121 |
▶ | if (pn && !whole) |
Branch: 2 branches
| Branch | True | False | Total |
| if (pn && !whole) |
2114121 |
0 |
2114121 |
| if (pn && !whole) |
2 |
2114119 |
2114121 |
|
| 516 |
2 |
| pw_append(&ws->frag, &ws->fraglen, &ws->fragcap, pl, pn); |
| 517 |
|
| |
| 518 |
2114121 |
▶ | if (f.fin) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f.fin) { |
2114120 |
1 |
2114121 |
|
| 519 |
2114120 |
| int is_text = (ws->frag_op == PW_OP_TEXT); |
| 520 |
2114120 |
▶ | if (is_text && !pw_utf8_ok(&ws->utf8)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_text && !pw_utf8_ok(&ws->utf8)) { |
2102118 |
12002 |
2114120 |
| if (is_text && !pw_utf8_ok(&ws->utf8)) { |
0 |
2102118 |
2102118 |
|
| 521 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_INVALID_PAYLOAD, |
| 522 |
|
| "text is not utf8"); |
| 523 |
0 |
| goto consumed; |
| 524 |
|
| } |
| 525 |
|
| { |
| 526 |
2114120 |
| SV *msg = whole |
| 527 |
2114119 |
▶ | ? newSVpvn(pn ? pl : "", pn) |
Branch: 1 branch
| Branch | True | False | Total |
| ? newSVpvn(pn ? pl : "", pn) |
2114119 |
0 |
2114119 |
|
| 528 |
2114120 |
▶ | : newSVpvn(ws->fraglen ? ws->frag : "", |
Branch: 2 branches
| Branch | True | False | Total |
| : newSVpvn(ws->fraglen ? ws->frag : "", |
2114119 |
1 |
2114120 |
| : newSVpvn(ws->fraglen ? ws->frag : "", |
1 |
0 |
1 |
|
| 529 |
|
| ws->fraglen); |
| 530 |
|
| SV *a[1]; |
| 531 |
2114120 |
▶ | if (is_text) SvUTF8_on(msg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_text) SvUTF8_on(msg); |
2102118 |
12002 |
2114120 |
|
| 532 |
2114120 |
| a[0] = msg; |
| 533 |
2114120 |
| ws->frag_active = 0; |
| 534 |
2114120 |
| ws->fraglen = 0; |
| 535 |
|
| /* a large fragmented message leaves a large |
| 536 |
|
| * buffer; give it back once it is handed on */ |
| 537 |
2114120 |
▶ | if (ws->fragcap > PW_BUF_KEEP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->fragcap > PW_BUF_KEEP) { |
0 |
2114120 |
2114120 |
|
| 538 |
0 |
| free(ws->frag); |
| 539 |
0 |
| ws->frag = NULL; ws->fragcap = 0; |
| 540 |
|
| } |
| 541 |
2114120 |
▶ | (void)pw_emit(aTHX_ ws, |
Branch: 1 branch
| Branch | True | False | Total |
| (void)pw_emit(aTHX_ ws, |
2102118 |
12002 |
2114120 |
|
| 542 |
|
| is_text ? "message" : "binary", a, 1); |
| 543 |
|
| /* Released here, not left to a temporaries |
| 544 |
|
| * frame: this loop hands on every message a fast |
| 545 |
|
| * sender has ready before it returns, and as |
| 546 |
|
| * mortals they would all be alive at once. */ |
| 547 |
2114120 |
| SvREFCNT_dec(msg); |
| 548 |
|
| } |
| 549 |
|
| } |
| 550 |
|
| } |
| 551 |
|
| } |
| 552 |
|
| } |
| 553 |
2116226 |
| off += (size_t)n; |
| 554 |
|
| } |
| 555 |
170 |
| consumed: |
| 556 |
26871 |
▶ | if (off && ws->rbuf) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (off && ws->rbuf) { |
9108 |
17763 |
26871 |
| if (off && ws->rbuf) { |
9108 |
0 |
9108 |
|
| 557 |
9108 |
▶ | if (off < ws->rlen) memmove(ws->rbuf, ws->rbuf + off, ws->rlen - off); |
Branch: 1 branch
| Branch | True | False | Total |
| if (off < ws->rlen) memmove(ws->rbuf, ws->rbuf + off, ws->rlen - off); |
8937 |
171 |
9108 |
|
| 558 |
9108 |
| ws->rlen -= off; |
| 559 |
|
| /* an empty read buffer that grew for one large message shrinks |
| 560 |
|
| * back, so an idle connection does not keep it for its whole life */ |
| 561 |
9108 |
▶ | if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) { |
171 |
8937 |
9108 |
| if (ws->rlen == 0 && ws->rcap > PW_BUF_KEEP) { |
11 |
160 |
171 |
|
| 562 |
11 |
| free(ws->rbuf); |
| 563 |
11 |
| ws->rbuf = NULL; ws->rcap = 0; |
| 564 |
|
| } |
| 565 |
|
| } |
| 566 |
26871 |
▶ | if (guard) SvREFCNT_dec(guard); |
Branch: 1 branch
| Branch | True | False | Total |
| if (guard) SvREFCNT_dec(guard); |
26871 |
0 |
26871 |
|
| 567 |
26871 |
| } |
| 568 |
|
| |
| 569 |
|
| /* Bytes handed up by the server on an Extended CONNECT stream. The socket |
| 570 |
|
| * read loop's job, without the socket: append and let the codec run. */ |
| 571 |
5 |
| static void pw_feed(pTHX_ punk_wsconn *ws, const char *buf, size_t len, |
| 572 |
|
| int fin) { |
| 573 |
5 |
▶ | if (ws->state == PW_ST_CLOSED) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->state == PW_ST_CLOSED) return; |
0 |
5 |
5 |
|
| 574 |
5 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
4 |
1 |
5 |
|
| 575 |
8 |
| size_t cap = ws->max_message_size |
| 576 |
4 |
▶ | ? ws->max_message_size + PW_READ_CHUNK : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ? ws->max_message_size + PW_READ_CHUNK : 0; |
4 |
0 |
4 |
|
| 577 |
4 |
▶ | if (cap && ws->rlen + len > cap) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cap && ws->rlen + len > cap) { |
4 |
0 |
4 |
| if (cap && ws->rlen + len > cap) { |
0 |
4 |
4 |
|
| 578 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big"); |
| 579 |
0 |
| return; |
| 580 |
|
| } |
| 581 |
4 |
| pw_buf_reserve(&ws->rbuf, &ws->rcap, ws->rlen + len); |
| 582 |
4 |
| memcpy(ws->rbuf + ws->rlen, buf, len); |
| 583 |
4 |
| ws->rlen += len; |
| 584 |
|
| } |
| 585 |
5 |
| pw_process(aTHX_ ws); |
| 586 |
|
| /* The peer half-closed its side of the stream: the same thing a read of |
| 587 |
|
| * zero means on a socket. */ |
| 588 |
5 |
▶ | if (fin && ws->state != PW_ST_CLOSED) |
Branch: 2 branches
| Branch | True | False | Total |
| if (fin && ws->state != PW_ST_CLOSED) |
1 |
4 |
5 |
| if (fin && ws->state != PW_ST_CLOSED) |
1 |
0 |
1 |
|
| 589 |
1 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); |
| 590 |
|
| } |
| 591 |
|
| |
| 592 |
|
| /* The ABI's read half (v8) delivers here. */ |
| 593 |
5 |
| static void pw_on_stream_data_in(pTHX_ void *h, const char *buf, STRLEN len, |
| 594 |
|
| int fin, void *ud) { |
| 595 |
5 |
| punk_wsconn *ws = (punk_wsconn *)ud; |
| 596 |
|
| PERL_UNUSED_ARG(h); |
| 597 |
5 |
| pw_feed(aTHX_ ws, buf, (size_t)len, fin); |
| 598 |
5 |
| } |
| 599 |
|
| |
| 600 |
5 |
| static void pw_on_stream_data(pTHX_ void *h, const char *buf, STRLEN len, |
| 601 |
|
| int fin, void *ud) { |
| 602 |
5 |
| punk_wsconn *ws_ = (punk_wsconn *)ud; |
| 603 |
5 |
▶ | SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
5 |
0 |
5 |
|
| 604 |
5 |
| PK_LOOP_ENTER; |
| 605 |
5 |
| pw_on_stream_data_in(aTHX_ h, buf, len, fin, ud); |
| 606 |
5 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
2 |
3 |
5 |
|
| 607 |
5 |
▶ | if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
5 |
0 |
5 |
|
| 608 |
5 |
| } |
| 609 |
|
| |
| 610 |
|
| /* The stream died under us - an h2 RST_STREAM, or the connection going. */ |
| 611 |
0 |
| static void pw_on_stream_abort_in(pTHX_ void *h, void *ud) { |
| 612 |
0 |
| punk_wsconn *ws = (punk_wsconn *)ud; |
| 613 |
|
| PERL_UNUSED_ARG(h); |
| 614 |
0 |
▶ | if (ws->state != PW_ST_CLOSED) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->state != PW_ST_CLOSED) |
0 |
0 |
0 |
|
| 615 |
0 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); |
| 616 |
0 |
| } |
| 617 |
|
| |
| 618 |
0 |
| static void pw_on_stream_abort(pTHX_ void *h, void *ud) { |
| 619 |
0 |
| punk_wsconn *ws_ = (punk_wsconn *)ud; |
| 620 |
0 |
▶ | SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
0 |
0 |
0 |
|
| 621 |
0 |
| PK_LOOP_ENTER; |
| 622 |
0 |
| pw_on_stream_abort_in(aTHX_ h, ud); |
| 623 |
0 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
0 |
0 |
|
| 624 |
0 |
▶ | if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
0 |
0 |
0 |
|
| 625 |
0 |
| } |
| 626 |
|
| |
| 627 |
|
| /* How many reads one readiness callback may do before it hands the loop |
| 628 |
|
| * back. A peer that keeps its socket non-empty would otherwise be read and |
| 629 |
|
| * processed until it stopped, and every other connection and timer on the |
| 630 |
|
| * worker waited for it. The watcher is level-triggered, so what is left |
| 631 |
|
| * fires it again. */ |
| 632 |
|
| #define PW_READ_BUDGET 16 |
| 633 |
|
| |
| 634 |
1717 |
| static void pw_on_readable_in(pTHX_ int fd, int mask, void *ud) { |
| 635 |
1717 |
| punk_wsconn *ws = (punk_wsconn *)ud; |
| 636 |
1717 |
| int budget = PW_READ_BUDGET; |
| 637 |
|
| PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask); |
| 638 |
26863 |
| for (;;) { |
| 639 |
|
| ssize_t n; |
| 640 |
28580 |
▶ | if (budget-- <= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (budget-- <= 0) return; |
1670 |
26910 |
28580 |
|
| 641 |
53820 |
| size_t cap = ws->max_message_size |
| 642 |
26910 |
▶ | ? ws->max_message_size + PW_READ_CHUNK : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ? ws->max_message_size + PW_READ_CHUNK : 0; |
26910 |
0 |
26910 |
|
| 643 |
26910 |
▶ | if (cap && ws->rlen >= cap) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cap && ws->rlen >= cap) { |
26910 |
0 |
26910 |
| if (cap && ws->rlen >= cap) { |
0 |
26910 |
26910 |
|
| 644 |
0 |
| pw_fail(aTHX_ ws, PW_CLOSE_MESSAGE_TOO_BIG, "message too big"); |
| 645 |
0 |
| return; |
| 646 |
|
| } |
| 647 |
26910 |
| pw_buf_reserve(&ws->rbuf, &ws->rcap, ws->rlen + PW_READ_CHUNK); |
| 648 |
26910 |
| n = read(ws->fd, ws->rbuf + ws->rlen, PW_READ_CHUNK); |
| 649 |
26910 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
26866 |
44 |
26910 |
|
| 650 |
|
| /* Processed as it arrives, not once the socket runs dry. The cap |
| 651 |
|
| * above is on what is buffered, and buffering everything a fast |
| 652 |
|
| * sender had ready counted whole frames that only needed handing |
| 653 |
|
| * on: megabytes of small valid messages were closed as one |
| 654 |
|
| * message too big. */ |
| 655 |
26866 |
| ws->rlen += (size_t)n; |
| 656 |
26866 |
| pw_process(aTHX_ ws); |
| 657 |
26866 |
▶ | if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; |
26863 |
3 |
26866 |
| if (ws->state == PW_ST_CLOSED || ws->fd < 0) return; |
0 |
26863 |
26863 |
|
| 658 |
26863 |
| continue; |
| 659 |
|
| } |
| 660 |
44 |
▶ | if (n == 0) { /* peer went away */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { /* peer went away */ |
15 |
29 |
44 |
|
| 661 |
15 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); |
| 662 |
15 |
| return; |
| 663 |
|
| } |
| 664 |
29 |
▶ | if (errno == EINTR) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) continue; |
0 |
29 |
29 |
|
| 665 |
29 |
▶ | if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
0 |
29 |
29 |
| if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
0 |
0 |
0 |
|
| 666 |
0 |
| pw_teardown(aTHX_ ws, PW_CLOSE_ABNORMAL, NULL, 0); |
| 667 |
0 |
| return; |
| 668 |
|
| } |
| 669 |
|
| } |
| 670 |
|
| |
| 671 |
1717 |
| static void pw_on_readable(pTHX_ int fd, int mask, void *ud) { |
| 672 |
1717 |
| punk_wsconn *ws_ = (punk_wsconn *)ud; |
| 673 |
1717 |
▶ | SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = ws_->self_rv ? SvREFCNT_inc(ws_->self_rv) : NULL; |
1717 |
0 |
1717 |
|
| 674 |
1717 |
| PK_LOOP_ENTER; |
| 675 |
1717 |
| pw_on_readable_in(aTHX_ fd, mask, ud); |
| 676 |
1717 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
18 |
1699 |
1717 |
|
| 677 |
1717 |
▶ | if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* ws may be gone after this */ |
1717 |
0 |
1717 |
|
| 678 |
1717 |
| } |
| 679 |
|
| |
| 680 |
|
| /* ---- construction and destruction ---------------------------------------- */ |
| 681 |
|
| |
| 682 |
20 |
| static punk_wsconn *pw_new(pTHX_ int fd, HV *opts) { |
| 683 |
|
| punk_wsconn *ws; |
| 684 |
|
| SV **e; |
| 685 |
20 |
| Newxz(ws, 1, punk_wsconn); |
| 686 |
20 |
| ws->fd = fd; |
| 687 |
20 |
| ws->state = PW_ST_CONNECTING; |
| 688 |
20 |
| ws->cbs = newHV(); |
| 689 |
20 |
| ws->max_message_size = PW_DEFAULT_MAX_MESSAGE; |
| 690 |
20 |
| ws->write_buffer_limit = PW_DEFAULT_WRITE_LIMIT; |
| 691 |
20 |
| pw_utf8_reset(&ws->utf8); |
| 692 |
20 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
20 |
0 |
20 |
|
| 693 |
|
| /* zero keeps the default: it used to mean no bound at all, and a |
| 694 |
|
| * connection with no bound could grow the worker without limit */ |
| 695 |
20 |
▶ | if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) |
0 |
20 |
20 |
| if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) |
0 |
0 |
0 |
| if ((e = hv_fetchs(opts, "max_message_size", 0)) && *e && SvOK(*e) |
0 |
0 |
0 |
|
| 696 |
0 |
▶ | && SvUV(*e)) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvUV(*e)) |
0 |
0 |
0 |
|
| 697 |
0 |
| ws->max_message_size = (size_t)SvUV(*e); |
| 698 |
20 |
▶ | if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) |
0 |
20 |
20 |
| if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) |
0 |
0 |
0 |
| if ((e = hv_fetchs(opts, "write_buffer_limit", 0)) && *e && SvOK(*e)) |
0 |
0 |
0 |
|
| 699 |
0 |
| ws->write_buffer_limit = (size_t)SvUV(*e); |
| 700 |
20 |
▶ | if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((e = hv_fetchs(opts, "protocol", 0)) && *e && SvOK(*e)) |
2 |
18 |
20 |
| 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 |
|
| 701 |
2 |
| ws->protocol = newSVsv(*e); |
| 702 |
20 |
▶ | if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) |
0 |
20 |
20 |
| if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) |
0 |
0 |
0 |
| if ((e = hv_fetchs(opts, "blocking", 0)) && *e && SvTRUE(*e)) |
0 |
0 |
0 |
|
| 703 |
0 |
| ws->blocking = 1; |
| 704 |
|
| } |
| 705 |
20 |
| return ws; |
| 706 |
|
| } |
| 707 |
|
| |
| 708 |
|
| /* The blessed IV-ref -> the struct, guarded like Punk::Router's handles. */ |
| 709 |
105 |
| static punk_wsconn *punk_ws_of(pTHX_ SV *self) { |
| 710 |
105 |
▶ | if (!SvROK(self) || !SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
105 |
0 |
105 |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
0 |
105 |
105 |
|
| 711 |
0 |
| croak("Punk::WebSocket: not a connection"); |
| 712 |
105 |
| return (punk_wsconn *)INT2PTR(void *, SvIV(SvRV(self))); |
| 713 |
|
| } |
| 714 |
|
| |
| 715 |
20 |
| static void pw_free(pTHX_ punk_wsconn *ws) { |
| 716 |
20 |
▶ | if (!ws) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ws) return; |
0 |
20 |
20 |
|
| 717 |
20 |
▶ | if (ws->fd >= 0) close(ws->fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->fd >= 0) close(ws->fd); |
0 |
20 |
20 |
|
| 718 |
20 |
▶ | if (ws->cbs) SvREFCNT_dec((SV *)ws->cbs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->cbs) SvREFCNT_dec((SV *)ws->cbs); |
0 |
20 |
20 |
|
| 719 |
20 |
▶ | if (ws->protocol) SvREFCNT_dec(ws->protocol); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->protocol) SvREFCNT_dec(ws->protocol); |
2 |
18 |
20 |
|
| 720 |
20 |
▶ | if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->peer_reason) SvREFCNT_dec(ws->peer_reason); |
2 |
18 |
20 |
|
| 721 |
20 |
▶ | if (ws->rbuf) free(ws->rbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->rbuf) free(ws->rbuf); |
20 |
0 |
20 |
|
| 722 |
20 |
▶ | if (ws->wbuf) free(ws->wbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->wbuf) free(ws->wbuf); |
20 |
0 |
20 |
|
| 723 |
20 |
▶ | if (ws->frag) free(ws->frag); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws->frag) free(ws->frag); |
1 |
19 |
20 |
|
| 724 |
20 |
| Safefree(ws); |
| 725 |
|
| } |
| 726 |
|
| |
| 727 |
|
| #endif /* PUNK_WSCONN_H */ |