include/fetch/ft_ws.h

filter: Statementstmt 86.6% 194 / 224 Branchbran 56.5% 86 / 152 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.4% 280 / 376 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_ua.h ↑ Summary include/fetch/ft_loop.h »
1 #ifndef FT_WS_H
2 #define FT_WS_H
3  
4 /* WebSocket (RFC 6455) client, native in C to match the rest of Fetch: the
5 * HTTP/1.1 Upgrade handshake is verified here (SHA-1 + base64, no external
6 * lib), then the connection switches to WebSocket framing - masked text/binary
7 * frames out, reassembled messages in, with ping/pong and close handled
8 * automatically. Runs on the same non-blocking connection and loop as an
9 * ordinary request; the upgrade Future resolves to a Fetch::WebSocket.
10 *
11 * Included from ft_http.h after ft_recv/ft_send and the ft_conn struct. */
12  
13 #include <stdint.h>
14  
15 /* --- SHA-1 (public-domain style, for Sec-WebSocket-Accept) ---------------- */
16  
17 typedef struct { uint32_t h[5]; uint64_t nbits; unsigned char buf[64]; size_t n; } ft_sha1;
18  
19 2 static void ft_sha1_block(ft_sha1 *s, const unsigned char *p) {
20 uint32_t w[80], a, b, c, d, e, t;
21 int i;
22 34 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 32 2 34
23 32 w[i] = (uint32_t)p[i*4] << 24 | (uint32_t)p[i*4+1] << 16 |
24 32 (uint32_t)p[i*4+2] << 8 | (uint32_t)p[i*4+3];
25 130 ▶ for (i = 16; i < 80; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 16; i < 80; i++) { 128 2 130
26 128 t = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
27 128 w[i] = (t << 1) | (t >> 31);
28 }
29 2 a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; e = s->h[4];
30 162 ▶ for (i = 0; i < 80; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 80; i++) { 160 2 162
31 uint32_t f, k;
32 160 ▶ if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
Branch: 1 branch
BranchTrueFalseTotal
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; } 40 120 160
33 120 ▶ else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } 40 80 120
34 80 ▶ else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
Branch: 1 branch
BranchTrueFalseTotal
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } 40 40 80
35 40 else { f = b ^ c ^ d; k = 0xCA62C1D6; }
36 160 t = ((a << 5) | (a >> 27)) + f + e + k + w[i];
37 160 e = d; d = c; c = (b << 30) | (b >> 2); b = a; a = t;
38 }
39 2 s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; s->h[4] += e;
40 2 }
41  
42 1 static void ft_sha1_init(ft_sha1 *s) {
43 1 s->h[0] = 0x67452301; s->h[1] = 0xEFCDAB89; s->h[2] = 0x98BADCFE;
44 1 s->h[3] = 0x10325476; s->h[4] = 0xC3D2E1F0; s->nbits = 0; s->n = 0;
45 1 }
46  
47 70 static void ft_sha1_update(ft_sha1 *s, const void *data, size_t len) {
48 70 const unsigned char *p = (const unsigned char *)data;
49 70 s->nbits += (uint64_t)len * 8;
50 140 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 70 70 140
51 70 size_t take = 64 - s->n;
52 70 ▶ if (take > len) take = len;
Branch: 1 branch
BranchTrueFalseTotal
if (take > len) take = len; 68 2 70
53 70 memcpy(s->buf + s->n, p, take);
54 70 s->n += take; p += take; len -= take;
55 70 ▶ if (s->n == 64) { ft_sha1_block(s, s->buf); s->n = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->n == 64) { ft_sha1_block(s, s->buf); s->n = 0; } 2 68 70
56 }
57 70 }
58  
59 1 static void ft_sha1_final(ft_sha1 *s, unsigned char out[20]) {
60 int i;
61 1 unsigned char pad = 0x80;
62 1 uint64_t nbits = s->nbits;
63 1 ft_sha1_update(s, &pad, 1);
64 60 ▶ { unsigned char z = 0; while (s->n != 56) ft_sha1_update(s, &z, 1); }
Branch: 1 branch
BranchTrueFalseTotal
{ unsigned char z = 0; while (s->n != 56) ft_sha1_update(s, &z, 1); } 59 1 60
65 9 ▶ for (i = 7; i >= 0; i--) { unsigned char b = (unsigned char)(nbits >> (i*8)); ft_sha1_update(s, &b, 1); }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 7; i >= 0; i--) { unsigned char b = (unsigned char)(nbits >> (i*8)); ft_sha1_update(s, &b, 1); } 8 1 9
66 6 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 5 1 6
67 5 out[i*4] = (unsigned char)(s->h[i] >> 24);
68 5 out[i*4+1] = (unsigned char)(s->h[i] >> 16);
69 5 out[i*4+2] = (unsigned char)(s->h[i] >> 8);
70 5 out[i*4+3] = (unsigned char)(s->h[i]);
71 }
72 1 }
73  
74 /* --- base64 encode -------------------------------------------------------- */
75  
76 static const char ft_b64tab[] =
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78  
79 /* encode `len` bytes into `out` (must hold 4*ceil(len/3)+1); NUL-terminates */
80 2 static void ft_b64enc(const unsigned char *in, size_t len, char *out) {
81 2 size_t i = 0, o = 0;
82 13 ▶ while (i + 3 <= len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 3 <= len) { 11 2 13
83 11 uint32_t v = in[i] << 16 | in[i+1] << 8 | in[i+2];
84 11 out[o++] = ft_b64tab[(v >> 18) & 63];
85 11 out[o++] = ft_b64tab[(v >> 12) & 63];
86 11 out[o++] = ft_b64tab[(v >> 6) & 63];
87 11 out[o++] = ft_b64tab[v & 63];
88 11 i += 3;
89 }
90 2 ▶ if (len - i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (len - i == 1) { 1 1 2
91 1 uint32_t v = in[i] << 16;
92 1 out[o++] = ft_b64tab[(v >> 18) & 63];
93 1 out[o++] = ft_b64tab[(v >> 12) & 63];
94 1 out[o++] = '='; out[o++] = '=';
95 1 ▶ } else if (len - i == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len - i == 2) { 1 0 1
96 1 uint32_t v = in[i] << 16 | in[i+1] << 8;
97 1 out[o++] = ft_b64tab[(v >> 18) & 63];
98 1 out[o++] = ft_b64tab[(v >> 12) & 63];
99 1 out[o++] = ft_b64tab[(v >> 6) & 63];
100 1 out[o++] = '=';
101 }
102 2 out[o] = '\0';
103 2 }
104  
105 #define FT_WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
106  
107 /* Sec-WebSocket-Accept for a given client key: base64(sha1(key . GUID)). */
108 1 static void ft_ws_accept(const char *key, char out[30]) {
109 ft_sha1 s;
110 unsigned char dg[20];
111 1 ft_sha1_init(&s);
112 1 ft_sha1_update(&s, key, strlen(key));
113 1 ft_sha1_update(&s, FT_WS_GUID, sizeof(FT_WS_GUID) - 1);
114 1 ft_sha1_final(&s, dg);
115 1 ft_b64enc(dg, 20, out);
116 1 }
117  
118 /* --- randomness (client key + frame masks; not crypto-critical) ---------- */
119  
120 /* portable random bytes (defined per-platform in ft_win.h) */
121 #define ft_ws_random(out, n) ft_os_random((out), (n))
122  
123 /* A fresh 16-byte client key, base64-encoded (24 chars) into out[25]. */
124 1 static void ft_ws_genkey(char out[25]) {
125 unsigned char k[16];
126 1 ft_ws_random(k, 16);
127 1 ft_b64enc(k, 16, out);
128 1 }
129  
130 /* --- outgoing frames ------------------------------------------------------ */
131  
132 6 static void ft_ws_wbuf_need(ft_conn *c, size_t extra) {
133 6 ▶ if (c->ws_wlen + extra > c->ws_wcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_wlen + extra > c->ws_wcap) { 2 4 6
134 2 c->ws_wcap = (c->ws_wlen + extra) * 2 + 64;
135 2 Renew(c->ws_wbuf, c->ws_wcap, char);
136 }
137 6 }
138  
139 /* Queue one masked frame (client -> server frames MUST be masked). */
140 6 static void ft_ws_queue(ft_conn *c, int opcode, const unsigned char *data, size_t len) {
141 unsigned char hdr[14], mask[4];
142 6 size_t hl = 0, i;
143 6 hdr[hl++] = (unsigned char)(0x80 | (opcode & 0x0f)); /* FIN + opcode */
144 6 ▶ if (len < 126) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 126) { 5 1 6
145 5 hdr[hl++] = (unsigned char)(0x80 | len);
146 1 ▶ } else if (len < 65536) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len < 65536) { 1 0 1
147 1 hdr[hl++] = 0x80 | 126;
148 1 hdr[hl++] = (unsigned char)(len >> 8);
149 1 hdr[hl++] = (unsigned char)(len);
150 } else {
151 int s;
152 0 hdr[hl++] = 0x80 | 127;
153 0 ▶ for (s = 56; s >= 0; s -= 8) hdr[hl++] = (unsigned char)((uint64_t)len >> s);
Branch: 1 branch
BranchTrueFalseTotal
for (s = 56; s >= 0; s -= 8) hdr[hl++] = (unsigned char)((uint64_t)len >> s); 0 0 0
154 }
155 6 ft_ws_random(mask, 4);
156 6 memcpy(hdr + hl, mask, 4); hl += 4;
157  
158 6 ft_ws_wbuf_need(c, hl + len);
159 6 memcpy(c->ws_wbuf + c->ws_wlen, hdr, hl);
160 6 c->ws_wlen += hl;
161 1057 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 1051 6 1057
162 1051 c->ws_wbuf[c->ws_wlen + i] = (char)(data[i] ^ mask[i & 3]);
163 6 c->ws_wlen += len;
164 6 }
165  
166 /* Push whatever is queued through the socket; arm WRITE if it would block. */
167 11 static int ft_ws_flush(pTHX_ ft_conn *c) {
168 17 ▶ while (c->ws_woff < c->ws_wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (c->ws_woff < c->ws_wlen) { 6 11 17
169 6 int want = 0;
170 6 ssize_t n = ft_send(c, c->ws_wbuf + c->ws_woff, c->ws_wlen - c->ws_woff, &want);
171 6 ▶ if (n > 0) { c->ws_woff += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { c->ws_woff += (size_t)n; continue; } 6 0 6
172 0 ▶ if (n < 0 && want) return 1; /* would block: keep buffer */
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && want) return 1; /* would block: keep buffer */ 0 0 0
if (n < 0 && want) return 1; /* would block: keep buffer */ 0 0 0
173 0 ▶ if (n < 0 && errno == EINTR) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && errno == EINTR) continue; 0 0 0
if (n < 0 && errno == EINTR) continue; 0 0 0
174 0 return -1; /* fatal */
175 }
176 11 c->ws_woff = c->ws_wlen = 0; /* drained */
177 11 return 0;
178 }
179  
180 /* --- inbound dispatch to Perl -------------------------------------------- */
181  
182 /* Deliver a complete application message: resolve a pending next_message
183 * future, else call on_message, else buffer it in the inbox. */
184 5 static void ft_ws_deliver(pTHX_ ft_conn *c, int is_binary, const char *data, size_t len) {
185 5 SV *msg = newSVpvn(data, len);
186 5 ▶ if (is_binary) SvUTF8_off(msg); else sv_utf8_decode(msg);
Branch: 1 branch
BranchTrueFalseTotal
if (is_binary) SvUTF8_off(msg); else sv_utf8_decode(msg); 1 4 5
187 5 ▶ if (c->ws_waiter) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_waiter) { 4 1 5
188 4 SV *w = c->ws_waiter; c->ws_waiter = NULL;
189 4 hmf_settle(aTHX_ w, HMF_DONE, &msg, 1);
190 4 SvREFCNT_dec(w);
191 1 ▶ } else if (c->ws_on_message) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->ws_on_message) { 1 0 1
192 1 dSP;
193 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
194 1 ▶ XPUSHs(sv_2mortal(SvREFCNT_inc(msg)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(SvREFCNT_inc(msg))); 0 1 1
195 1 PUTBACK;
196 1 call_sv(c->ws_on_message, G_DISCARD | G_EVAL);
197 1 ▶ if (SvTRUE(ERRSV)) warn("Fetch: on_message callback died: %s", SvPV_nolen(ERRSV));
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) warn("Fetch: on_message callback died: %s", SvPV_nolen(ERRSV)); 1 0 1
if (SvTRUE(ERRSV)) warn("Fetch: on_message callback died: %s", SvPV_nolen(ERRSV)); 0 1 1
if (SvTRUE(ERRSV)) warn("Fetch: on_message callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
198 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
199 } else {
200 0 ▶ if (!c->ws_inbox) c->ws_inbox = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ws_inbox) c->ws_inbox = newAV(); 0 0 0
201 0 av_push(c->ws_inbox, SvREFCNT_inc(msg));
202 }
203 5 SvREFCNT_dec(msg);
204 5 }
205  
206 /* Mark the socket closed and notify; the Fetch::WebSocket object frees the
207 * conn on DESTROY, so we only release the fd here. */
208 1 static void ft_ws_shutdown(pTHX_ ft_conn *c, const char *why) {
209 1 ▶ if (c->ws_closed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_closed) return; 0 1 1
210 1 c->ws_closed = 1;
211 1 ▶ if (c->armed) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->armed) { 1 0 1
212 1 ▶ if (c->loop_sv) ft_loop_arm(aTHX_ c, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (c->loop_sv) ft_loop_arm(aTHX_ c, 0); 0 1 1
213 1 else ft_unwatch_io(aTHX_ c->loop, c->fd, c->armed);
214 1 c->armed = 0;
215 }
216 1 ▶ if (c->fd >= 0) { close(c->fd); c->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->fd >= 0) { close(c->fd); c->fd = -1; } 1 0 1
217 1 ▶ if (c->ws_waiter) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_waiter) { 0 1 1
218 0 SV *w = c->ws_waiter; c->ws_waiter = NULL;
219 0 ▶ SV *e = sv_2mortal(newSVpvf("Fetch: websocket %s", why ? why : "closed"));
Branch: 1 branch
BranchTrueFalseTotal
SV *e = sv_2mortal(newSVpvf("Fetch: websocket %s", why ? why : "closed")); 0 0 0
220 0 hmf_settle(aTHX_ w, HMF_FAILED, &e, 1);
221 0 SvREFCNT_dec(w);
222 }
223 1 ▶ if (c->ws_on_close) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_on_close) { 0 1 1
224 0 dSP;
225 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 0 0
226 0 call_sv(c->ws_on_close, G_DISCARD | G_EVAL);
227 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
228 }
229 }
230  
231 /* Parse and act on as many complete frames as are buffered in rbuf. */
232 6 static void ft_ws_feed(pTHX_ ft_conn *c) {
233 5 for (;;) {
234 11 unsigned char *b = (unsigned char *)c->rbuf + c->ws_pos;
235 11 size_t avail = c->rlen - c->ws_pos;
236 int fin, opcode, masked;
237 uint64_t len;
238 size_t hl;
239 11 ▶ if (avail < 2) break;
Branch: 1 branch
BranchTrueFalseTotal
if (avail < 2) break; 6 5 11
240 5 fin = b[0] & 0x80;
241 5 opcode = b[0] & 0x0f;
242 5 masked = b[1] & 0x80;
243 5 len = b[1] & 0x7f;
244 5 hl = 2;
245 5 ▶ if (len == 126) { if (avail < 4) break; len = ((uint64_t)b[2] << 8) | b[3]; hl = 4; }
Branch: 2 branches
BranchTrueFalseTotal
if (len == 126) { if (avail < 4) break; len = ((uint64_t)b[2] << 8) | b[3]; hl = 4; } 1 4 5
if (len == 126) { if (avail < 4) break; len = ((uint64_t)b[2] << 8) | b[3]; hl = 4; } 0 1 1
246 4 ▶ else if (len == 127) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len == 127) { 0 4 4
247 0 ▶ int i; if (avail < 10) break;
Branch: 1 branch
BranchTrueFalseTotal
int i; if (avail < 10) break; 0 0 0
248 0 ▶ len = 0; for (i = 0; i < 8; i++) len = (len << 8) | b[2 + i]; hl = 10;
Branch: 1 branch
BranchTrueFalseTotal
len = 0; for (i = 0; i < 8; i++) len = (len << 8) | b[2 + i]; hl = 10; 0 0 0
249 }
250 5 ▶ if (masked) hl += 4;
Branch: 1 branch
BranchTrueFalseTotal
if (masked) hl += 4; 0 5 5
251 5 ▶ if (len > (uint64_t)64 * 1024 * 1024) { ft_ws_shutdown(aTHX_ c, "frame too large"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (len > (uint64_t)64 * 1024 * 1024) { ft_ws_shutdown(aTHX_ c, "frame too large"); return; } 0 5 5
252 5 ▶ if (avail < hl + len) break; /* need the whole frame */
Branch: 1 branch
BranchTrueFalseTotal
if (avail < hl + len) break; /* need the whole frame */ 0 5 5
253  
254 {
255 5 unsigned char *payload = b + hl;
256 5 ▶ if (masked) {
Branch: 1 branch
BranchTrueFalseTotal
if (masked) { 0 5 5
257 0 unsigned char *mk = b + hl - 4;
258 0 ▶ uint64_t i; for (i = 0; i < len; i++) payload[i] ^= mk[i & 3];
Branch: 1 branch
BranchTrueFalseTotal
uint64_t i; for (i = 0; i < len; i++) payload[i] ^= mk[i & 3]; 0 0 0
259 }
260 5 ▶ if (opcode == 0x8) { /* close */
Branch: 1 branch
BranchTrueFalseTotal
if (opcode == 0x8) { /* close */ 0 5 5
261 0 ft_ws_queue(c, 0x8, NULL, 0); /* echo close */
262 0 ft_ws_flush(aTHX_ c);
263 0 c->ws_pos += hl + (size_t)len;
264 0 ft_ws_shutdown(aTHX_ c, "closed by peer");
265 0 return;
266 5 ▶ } else if (opcode == 0x9) { /* ping -> pong */
Branch: 1 branch
BranchTrueFalseTotal
} else if (opcode == 0x9) { /* ping -> pong */ 0 5 5
267 0 ft_ws_queue(c, 0xA, payload, (size_t)len);
268 5 ▶ } else if (opcode == 0xA) { /* pong: ignore */
Branch: 1 branch
BranchTrueFalseTotal
} else if (opcode == 0xA) { /* pong: ignore */ 5 0 5
269 } else { /* 0x0/0x1/0x2 data */
270 5 ▶ if (opcode != 0x0) c->ws_msg_opcode = opcode;
Branch: 1 branch
BranchTrueFalseTotal
if (opcode != 0x0) c->ws_msg_opcode = opcode; 5 0 5
271 5 ▶ if (c->ws_mlen + len > c->ws_mcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_mlen + len > c->ws_mcap) { 2 3 5
272 2 c->ws_mcap = (c->ws_mlen + (size_t)len) * 2 + 64;
273 2 Renew(c->ws_msg, c->ws_mcap, char);
274 }
275 5 memcpy(c->ws_msg + c->ws_mlen, payload, (size_t)len);
276 5 c->ws_mlen += (size_t)len;
277 5 ▶ if (fin) {
Branch: 1 branch
BranchTrueFalseTotal
if (fin) { 5 0 5
278 5 ft_ws_deliver(aTHX_ c, c->ws_msg_opcode == 0x2, c->ws_msg, c->ws_mlen);
279 5 c->ws_mlen = 0;
280 }
281 }
282 }
283 5 c->ws_pos += hl + (size_t)len;
284 }
285 /* compact consumed bytes */
286 6 ▶ if (c->ws_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_pos > 0) { 5 1 6
287 5 memmove(c->rbuf, c->rbuf + c->ws_pos, c->rlen - c->ws_pos);
288 5 c->rlen -= c->ws_pos;
289 5 c->ws_pos = 0;
290 }
291 }
292  
293 /* Arm READ (always, for inbound frames) plus WRITE when output is pending. */
294 11 static void ft_ws_rearm(pTHX_ ft_conn *c) {
295 11 int mask = HM_EV_READ;
296 11 ▶ if (c->ws_woff < c->ws_wlen) mask |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_woff < c->ws_wlen) mask |= HM_EV_WRITE; 0 11 11
297 11 ft_arm(aTHX_ c, mask);
298 11 }
299  
300 /* One readiness pass while in WebSocket mode. */
301 5 static void ft_ws_step(pTHX_ ft_conn *c) {
302 int fr;
303 5 ▶ if (c->ws_closed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_closed) return; 0 5 5
304 5 fr = ft_ws_flush(aTHX_ c);
305 5 ▶ if (fr < 0) { ft_ws_shutdown(aTHX_ c, "send failed"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (fr < 0) { ft_ws_shutdown(aTHX_ c, "send failed"); return; } 0 5 5
306 5 for (;;) {
307 10 int want = 0; ssize_t n;
308 10 ▶ if (c->rlen + 65536 > c->rcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen + 65536 > c->rcap) { 0 10 10
309 0 ▶ c->rcap = c->rcap ? c->rcap * 2 : 65536;
Branch: 1 branch
BranchTrueFalseTotal
c->rcap = c->rcap ? c->rcap * 2 : 65536; 0 0 0
310 0 Renew(c->rbuf, c->rcap, char);
311 }
312 10 n = ft_recv(c, c->rbuf + c->rlen, c->rcap - c->rlen, &want);
313 10 ▶ if (n > 0) { c->rlen += (size_t)n; ft_ws_feed(aTHX_ c); if (c->ws_closed) return; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0) { c->rlen += (size_t)n; ft_ws_feed(aTHX_ c); if (c->ws_closed) return; continue; } 5 5 10
if (n > 0) { c->rlen += (size_t)n; ft_ws_feed(aTHX_ c); if (c->ws_closed) return; continue; } 0 5 5
314 5 ▶ if (n == 0) { ft_ws_shutdown(aTHX_ c, "connection closed"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { ft_ws_shutdown(aTHX_ c, "connection closed"); return; } 0 5 5
315 5 ▶ if (want) break;
Branch: 1 branch
BranchTrueFalseTotal
if (want) break; 5 0 5
316 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
317 0 ft_ws_shutdown(aTHX_ c, strerror(errno));
318 0 return;
319 }
320 5 ft_ws_rearm(aTHX_ c);
321 }
322  
323 /* Public send helpers used from xs/websocket.xs. opcode: 1 text, 2 binary. */
324 5 static void ft_ws_send(pTHX_ ft_conn *c, int opcode, const char *data, size_t len) {
325 5 ▶ if (c->ws_closed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_closed) return; 0 5 5
326 5 ft_ws_queue(c, opcode, (const unsigned char *)data, len);
327 5 ▶ if (ft_ws_flush(aTHX_ c) < 0) { ft_ws_shutdown(aTHX_ c, "send failed"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ws_flush(aTHX_ c) < 0) { ft_ws_shutdown(aTHX_ c, "send failed"); return; } 0 5 5
328 5 ft_ws_rearm(aTHX_ c);
329 }
330  
331 1 static void ft_ws_close(pTHX_ ft_conn *c) {
332 1 ▶ if (c->ws_closed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_closed) return; 0 1 1
333 1 ft_ws_queue(c, 0x8, NULL, 0);
334 1 ft_ws_flush(aTHX_ c);
335 1 ft_ws_shutdown(aTHX_ c, "closed locally");
336 }
337  
338 #endif /* FT_WS_H */
« include/fetch/ft_ua.h ↑ Summary include/fetch/ft_loop.h »