include/punk/punk_sse.h

filter: Statementstmt 85.9% 282 / 334 (5 uncoverable) Branchbran 59.3% 198 / 384 (41 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.7% 480 / 718 (46 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/dbil.xs ↑ Summary include/punk/punk_stream.h »
1 /* punk_sse.h - Server-Sent Events, in C.
2 *
3 * The lighter sibling of the WebSocket connection: an `sse` route hands the
4 * socket to a stream the handler pushes text/event-stream events onto. No
5 * upgrade handshake and no frame codec - just a kept-open 200 response - so it
6 * reuses the WS connection's byte machinery (pw_append / a mirrored flush with
7 * io_watch WRITE backpressure / teardown) and drops the rest.
8 *
9 * Four transports, chosen in order: Hyperman detach (stream on the loop), the
10 * Hyperman stream handle (ABI v6), psgi.streaming (the portable
11 * delayed-response writer), and blocking psgix.io.
12 *
13 * The stream handle sits under detach rather than over it, which is not where
14 * an earlier sketch of this put it. Two things decide the order. On HTTP/1.1
15 * in clear, detach already works and its bytes are what every existing test
16 * asserts, so nothing may be taken off it. And a handle can only be opened
17 * from a DEFERRED response - hm_stream_open refuses an HTTP/1.1 connection
18 * whose handler has not yet returned, because a synchronous return value
19 * would be serialised on top of the body - so the branch lives inside the
20 * psgi.streaming responder, which is the deferral. What it buys is exactly
21 * the cases detach refuses: HTTP/2, where a stream is one of many on a shared
22 * connection and there is no fd to hand over, and TLS, where the session
23 * state belongs to the server. Both used to end at a 503 or at a
24 * psgi.streaming writer that buffers to close - which for an SSE stream that
25 * never closes means nothing is ever delivered.
26 *
27 * Must be included after punk_wsconn.h (punk_hm, pw_append) and
28 * punk_wshandshake.h (pw_err / pw_empty), and punk_context.h (pcx_* / frj).
29 */
30  
31 #ifndef PUNK_SSE_H
32 #define PUNK_SSE_H
33  
34 #include <fcntl.h>
35  
36 enum { SSE_MODE_DETACH = 0, SSE_MODE_STREAM = 1, SSE_MODE_BLOCK = 2,
37 SSE_MODE_HSTREAM = 3 };
38 enum { SSE_OPEN = 0, SSE_CLOSED = 1 };
39  
40 typedef struct punk_sse {
41 int fd; /* detach / block; -1 for a psgi writer */
42 int mode;
43 int state;
44 unsigned char reading, writing, in_teardown;
45 char *wbuf; size_t wlen, woff, wcap;
46 size_t write_buffer_limit;
47 SV *writer; /* the psgi.streaming $writer (STREAM mode) */
48 HV *cbs; /* event name -> coderef (close) */
49 SV *self_rv; /* strong self while live */
50 void *loop;
51 const hm_abi *abi;
52 void *sh; /* the v6 stream handle (HSTREAM mode) */
53 hm_abi_timer *hb_tw; /* heartbeat timer */
54 double heartbeat; /* seconds; 0 = off */
55 } punk_sse;
56  
57 /* The transports that run on the worker's event loop, and so have a timer to
58 * arm a heartbeat on. The two differ in what they write through, not in when
59 * they get to run. */
60 #define SE_ON_LOOP(sse) ((sse)->mode == SSE_MODE_DETACH \
61 || (sse)->mode == SSE_MODE_HSTREAM)
62  
63 static const char SSE_RESPONSE[] =
64 "HTTP/1.1 200 OK\r\n"
65 "Content-Type: text/event-stream\r\n"
66 "Cache-Control: no-cache\r\n"
67 "Connection: keep-alive\r\n"
68 "X-Accel-Buffering: no\r\n"
69 "\r\n";
70  
71 235 static punk_sse *se_of(pTHX_ SV *self) {
72 /*
73 # uncoverable branch true
74 # uncoverable condition left count:1 class:ignore_covered_err
75 # uncoverable condition left count:2 class:ignore_covered_err */
76 235 ▶ if (!SvROK(self) || !SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) 235 0 235
if (!SvROK(self) || !SvIOK(SvRV(self))) 0 235 235
77 0 croak("Punk::SSE: not a stream"); /* # uncoverable statement */
78 235 return (punk_sse *)INT2PTR(void *, SvIV(SvRV(self)));
79 }
80  
81 static void se_teardown(pTHX_ punk_sse *sse);
82 static void se_on_writable(pTHX_ int fd, int mask, void *ud);
83  
84 /* ---- writing (DETACH / BLOCK): mirror punk_wsconn.h's flush --------------- */
85  
86 65 static void se_flush(pTHX_ punk_sse *sse) {
87 115 ▶ while (sse->woff < sse->wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (sse->woff < sse->wlen) { 67 48 115
88 67 ssize_t n = write(sse->fd, sse->wbuf + sse->woff, sse->wlen - sse->woff);
89 67 ▶ if (n > 0) { sse->woff += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { sse->woff += (size_t)n; continue; } 50 17 67
90 /*
91 # uncoverable branch false
92 # uncoverable condition right count:1 class:ignore_covered_err
93 # uncoverable condition right count:2 class:ignore_covered_err
94 # uncoverable condition left count:3 class:ignore_covered_err
95 # uncoverable condition right count:3 class:ignore_covered_err */
96 17 ▶ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 17 0 17
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 0 17 17
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 0 0 0
97 /*
98 # uncoverable condition right count:2 class:ignore_covered_err
99 # uncoverable condition right count:3 class:ignore_covered_err
100 # uncoverable condition right count:4 class:ignore_covered_err */
101 17 ▶ if (!sse->writing && sse->mode == SSE_MODE_DETACH
Branch: 2 branches
BranchTrueFalseTotal
if (!sse->writing && sse->mode == SSE_MODE_DETACH 1 16 17
if (!sse->writing && sse->mode == SSE_MODE_DETACH 1 0 1
102 1 ▶ && sse->abi && sse->loop) {
Branch: 2 branches
BranchTrueFalseTotal
&& sse->abi && sse->loop) { 1 0 1
&& sse->abi && sse->loop) { 1 0 1
103 1 sse->abi->io_watch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE,
104 se_on_writable, sse);
105 1 sse->writing = 1;
106 }
107 17 return;
108 }
109 /*
110 # uncoverable statement
111 # uncoverable branch true
112 # uncoverable branch false
113 # uncoverable condition left count:1
114 # uncoverable condition right count:1
115 # uncoverable condition left count:2
116 # uncoverable condition right count:2 */
117 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
118 0 se_teardown(aTHX_ sse); /* write error: client gone */ /* # uncoverable statement */
119 0 return; /* # uncoverable statement */
120 }
121 48 sse->wlen = sse->woff = 0;
122 /*
123 # uncoverable branch true
124 # uncoverable condition left count:1 class:ignore_covered_err
125 # uncoverable condition left count:2 class:ignore_covered_err
126 # uncoverable condition right count:2 class:ignore_covered_err
127 # uncoverable condition left count:3 class:ignore_covered_err
128 # uncoverable condition right count:3 class:ignore_covered_err */
129 48 ▶ if (sse->writing && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->writing && sse->abi && sse->loop) { 0 48 48
if (sse->writing && sse->abi && sse->loop) { 0 0 0
if (sse->writing && sse->abi && sse->loop) { 0 0 0
130 0 sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE); /* # uncoverable statement */
131 0 sse->writing = 0; /* # uncoverable statement */
132 }
133 }
134  
135 /*
136 # uncoverable statement
137 # uncoverable subroutine */
138 0 static void se_on_writable_in(pTHX_ int fd, int mask, void *ud) {
139 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
140 0 se_flush(aTHX_ (punk_sse *)ud); /* # uncoverable statement */
141 0 }
142  
143 /*
144 # uncoverable statement
145 # uncoverable subroutine */
146 0 static void se_on_writable(pTHX_ int fd, int mask, void *ud) {
147 0 PK_LOOP_ENTER; /* # uncoverable statement */
148 0 se_on_writable_in(aTHX_ fd, mask, ud); /* # uncoverable statement */
149 0 ▶ PK_LOOP_LEAVE; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; /* # uncoverable statement */ 0 0 0
150 0 }
151  
152 /* the client closing is the only thing we read for; a read of 0 tears down */
153 1 static void se_on_readable_in(pTHX_ int fd, int mask, void *ud) {
154 1 punk_sse *sse = (punk_sse *)ud;
155 char scratch[512];
156 ssize_t n;
157 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
158 1 n = read(sse->fd, scratch, sizeof scratch);
159 /*
160 # uncoverable branch false class:ignore_covered_err
161 # uncoverable branch true class:ignore_covered_err
162 # uncoverable condition right count:1 class:ignore_covered_err */
163 1 ▶ if (n == 0) { se_teardown(aTHX_ sse); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { se_teardown(aTHX_ sse); return; } 1 0 1
164 /*
165 # uncoverable statement class:ignore_covered_err
166 # uncoverable branch true class:ignore_covered_err
167 # uncoverable branch false class:ignore_covered_err
168 # uncoverable condition left count:1
169 # uncoverable condition right count:1
170 # uncoverable condition left count:2
171 # uncoverable condition right count:2
172 # uncoverable condition left count:3
173 # uncoverable condition right count:3
174 # uncoverable condition left count:4
175 # uncoverable condition right count:4 */
176 0 ▶ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR))
Branch: 4 branches
BranchTrueFalseTotal
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) 0 0 0
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) 0 0 0
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) 0 0 0
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) 0 0 0
177 0 return; /* # uncoverable statement */
178 /*
179 # uncoverable statement class:ignore_covered_err
180 # uncoverable branch true class:ignore_covered_err
181 # uncoverable branch false class:ignore_covered_err
182 # uncoverable condition left count:1
183 # uncoverable condition right count:1 */
184 0 ▶ if (n < 0) se_teardown(aTHX_ sse);
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) se_teardown(aTHX_ sse); 0 0 0
185 /* n > 0: a client should not send on an SSE stream; ignore it */
186 }
187  
188 1 static void se_on_readable(pTHX_ int fd, int mask, void *ud) {
189 1 PK_LOOP_ENTER;
190 1 se_on_readable_in(aTHX_ fd, mask, ud);
191 1 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 1 1
192 1 }
193  
194 /* queue bytes to the active backend */
195 138 static void se_write(pTHX_ punk_sse *sse, const char *bytes, size_t len) {
196 /*
197 # uncoverable branch true
198 # uncoverable condition left count:1 class:ignore_covered_err */
199 138 ▶ if (sse->state != SSE_OPEN) return;
Branch: 1 branch
BranchTrueFalseTotal
if (sse->state != SSE_OPEN) return; 0 138 138
200 138 ▶ if (sse->mode == SSE_MODE_HSTREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_HSTREAM) { 0 138 138
201 /* The write can close the connection under us, and Hyperman fires
202 * the abort callback from there - which tears this stream down and
203 * drops its own last reference. So the reference is held across the
204 * call, exactly as the heartbeat holds one. */
205 0 ▶ SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
206 0 int r = sse->abi->stream_write(aTHX_ sse->sh, bytes, len);
207 /* HM_ABI_STREAM_FULL is not an error: the bytes were taken and the
208 * connection is merely backed up. An event stream's writes are small
209 * and there is nothing useful to drop, so the bound that matters is
210 * Hyperman's own connection buffer rather than one kept here. */
211 /*
212 # uncoverable branch true
213 # uncoverable condition left count:1 class:ignore_covered_err
214 # uncoverable condition left count:2 class:ignore_covered_err
215 # uncoverable condition right count:2 class:ignore_covered_err */
216 0 ▶ if (r < 0 && sse->state == SSE_OPEN) se_teardown(aTHX_ sse);
Branch: 2 branches
BranchTrueFalseTotal
if (r < 0 && sse->state == SSE_OPEN) se_teardown(aTHX_ sse); 0 0 0
if (r < 0 && sse->state == SSE_OPEN) se_teardown(aTHX_ sse); 0 0 0
217 /*
218 # uncoverable branch false
219 # uncoverable condition right count:1 class:ignore_covered_err */
220 0 ▶ if (keep) SvREFCNT_dec(keep); /* sse may be gone now */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* sse may be gone now */ 0 0 0
221 0 return;
222 }
223 138 ▶ if (sse->mode == SSE_MODE_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_STREAM) { 72 66 138
224 72 dSP;
225 72 ENTER; SAVETMPS;
226 72 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 72 72
PUSHMARK(SP); EXTEND(SP, 2); 0 72 72
227 72 ▶ PUSHs(sse->writer ? sse->writer : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sse->writer ? sse->writer : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */ 72 0 72
228 72 PUSHs(sv_2mortal(newSVpvn(bytes, len)));
229 72 PUTBACK;
230 72 call_method("write", G_DISCARD | G_EVAL);
231 72 SPAGAIN;
232 /*
233 # uncoverable branch true
234 # uncoverable condition right count:1 class:ignore_covered_err
235 # uncoverable condition left count:2 class:ignore_covered_err
236 # uncoverable condition left count:3 class:ignore_covered_err
237 # uncoverable condition right count:3 class:ignore_covered_err */
238 72 ▶ if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 72 0 72
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 0 72 72
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 0 0 0
239 72 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 72 0 72
240 72 return;
241 }
242 66 ▶ if (sse->write_buffer_limit /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (sse->write_buffer_limit /* # uncoverable condition right count:1 class:ignore_covered_err */ 66 0 66
243 66 ▶ && sse->wlen - sse->woff + len > sse->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& sse->wlen - sse->woff + len > sse->write_buffer_limit) { 1 65 66
244 1 se_teardown(aTHX_ sse); /* a client that will not read */
245 1 return;
246 }
247 65 pw_append(&sse->wbuf, &sse->wlen, &sse->wcap, bytes, len);
248 65 se_flush(aTHX_ sse);
249 }
250  
251 /* ---- SSE field formatting ------------------------------------------------- */
252  
253 /* append `data:` lines for one payload (a ref -> JSON; a string -> one line
254 * per line break), no terminating blank line.
255 *
256 * A line break is CR, LF or CRLF, because that is what ends a line for the
257 * EventSource parser reading this. Splitting on LF alone left a CR inside a
258 * `data:` line, where the client reads what follows it as a new field: text
259 * relayed from one user could set the event name and the last event id for
260 * everyone listening. */
261 101 static void se_append_data(pTHX_ SV *out, SV *data) {
262 101 ▶ if (SvROK(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(data)) { 33 68 101
263 33 SV *json = punk_frj(aTHX)->encode(aTHX_ data, NULL);
264 33 sv_catpvs(out, "data: ");
265 33 sv_catsv(out, json);
266 33 sv_catpvs(out, "\n");
267 33 SvREFCNT_dec(json);
268 }
269 else {
270 68 STRLEN l = 0, i, start = 0;
271 136 ▶ const char *p = SvOK(data) ? SvPV_const(data, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvOK(data) ? SvPV_const(data, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 68 0 68
272 20000367 ▶ for (i = 0; i <= l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= l; i++) { 20000299 68 20000367
273 20000299 ▶ if (i == l || p[i] == '\n' || p[i] == '\r') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == l || p[i] == '\n' || p[i] == '\r') { 20000231 68 20000299
if (i == l || p[i] == '\n' || p[i] == '\r') { 20000220 11 20000231
if (i == l || p[i] == '\n' || p[i] == '\r') { 14 20000206 20000220
274 93 sv_catpvs(out, "data: ");
275 93 sv_catpvn(out, p + start, i - start);
276 93 sv_catpvs(out, "\n");
277 93 ▶ if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++;
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++; 23 70 93
if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++; 12 11 23
if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++; 2 10 12
278 93 start = i + 1;
279 }
280 }
281 }
282 101 }
283  
284 81 static void se_send(pTHX_ punk_sse *sse, SV *data) {
285 81 SV *out = sv_2mortal(newSVpvs(""));
286 81 se_append_data(aTHX_ out, data);
287 81 sv_catpvs(out, "\n");
288 81 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
289 81 }
290  
291 20 static void se_event(pTHX_ punk_sse *sse, SV *name, SV *data) {
292 20 SV *out = sv_2mortal(newSVpvs("event: "));
293 40 ▶ STRLEN nl; const char *n = SvOK(name) ? SvPV_const(name, nl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
STRLEN nl; const char *n = SvOK(name) ? SvPV_const(name, nl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 0 20
294 STRLEN i;
295 /*
296 # uncoverable branch true class:ignore_covered_err
297 # uncoverable condition left count:1 class:ignore_covered_err
298 # uncoverable condition left count:2 class:ignore_covered_err */
299 100 ▶ for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */ 80 0 80
for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */ 80 0 80
for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */ 80 20 100
300 20 sv_catpvn(out, n, i);
301 20 sv_catpvs(out, "\n");
302 20 se_append_data(aTHX_ out, data);
303 20 sv_catpvs(out, "\n");
304 20 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
305 20 }
306  
307 26 static void se_field(pTHX_ punk_sse *sse, const char *field, SV *val) {
308 26 SV *out = sv_2mortal(newSVpv(field, 0));
309 52 ▶ STRLEN vl; const char *v = SvOK(val) ? SvPV_const(val, vl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
STRLEN vl; const char *v = SvOK(val) ? SvPV_const(val, vl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 26 0 26
310 STRLEN i;
311 /*
312 # uncoverable branch true class:ignore_covered_err
313 # uncoverable condition left count:1 class:ignore_covered_err
314 # uncoverable condition left count:2 class:ignore_covered_err */
315 81 ▶ for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break; 55 0 55
for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break; 55 0 55
for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break; 55 26 81
316 26 sv_catpvn(out, v, i);
317 26 sv_catpvs(out, "\n");
318 26 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
319 26 }
320  
321 /* ---- heartbeat, teardown -------------------------------------------------- */
322  
323 static void se_arm_heartbeat(pTHX_ punk_sse *sse);
324  
325 /* The keep-alive write is how a stream with nothing to say finds out its
326 * client has gone, so tearing down from inside it is the normal case, not the
327 * unlikely one - and teardown drops the stream's own reference, which for a
328 * handler that has already returned is the LAST one. So the reference is held
329 * across the write: without it the re-arm below reads freed memory and hands
330 * the loop a timer pointed at it. */
331 5 static void se_heartbeat_cb_in(pTHX_ void *ud) {
332 5 punk_sse *sse = (punk_sse *)ud;
333 5 ▶ SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
334 5 sse->hb_tw = NULL;
335 /*
336 # uncoverable branch false
337 # uncoverable condition right count:1 class:ignore_covered_err */
338 5 ▶ if (sse->state == SSE_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->state == SSE_OPEN) { 5 0 5
339 5 se_write(aTHX_ sse, ":\n\n", 3); /* a keep-alive comment */
340 5 se_arm_heartbeat(aTHX_ sse);
341 }
342 /*
343 # uncoverable branch false
344 # uncoverable condition right count:1 class:ignore_covered_err */
345 5 ▶ if (keep) SvREFCNT_dec(keep); /* sse may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* sse may be gone after this */ 5 0 5
346 5 }
347  
348 5 static void se_heartbeat_cb(pTHX_ void *ud) {
349 5 PK_LOOP_ENTER;
350 5 se_heartbeat_cb_in(aTHX_ ud);
351 5 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 5 5
352 5 }
353  
354 7 static void se_arm_heartbeat(pTHX_ punk_sse *sse) {
355 7 ▶ if (sse->state == SSE_OPEN && sse->heartbeat > 0
Branch: 2 branches
BranchTrueFalseTotal
if (sse->state == SSE_OPEN && sse->heartbeat > 0 7 0 7
if (sse->state == SSE_OPEN && sse->heartbeat > 0 6 1 7
356 6 ▶ && SE_ON_LOOP(sse) && sse->abi && sse->loop)
Branch: 4 branches
BranchTrueFalseTotal
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 6 6
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 0 0
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 6 0 6
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 6 0 6
357 6 sse->hb_tw = sse->abi->timer(aTHX_ sse->loop, sse->heartbeat,
358 se_heartbeat_cb, sse);
359 7 }
360  
361 /* The stream died before it was closed - an HTTP/2 RST_STREAM on this one
362 * stream of many, or the connection going away underneath it.
363 *
364 * This has no HTTP/1 analogue and is the reason the handle beats detach
365 * rather than merely matching it: on a multiplexed transport a peer can kill
366 * one stream while the connection carries on, and a producer that never hears
367 * about it goes on generating events for nobody. The handle stays valid to
368 * close, which is what teardown does with it. */
369 0 static void se_on_abort_in(pTHX_ void *h, void *ud) {
370 0 punk_sse *sse = (punk_sse *)ud;
371 PERL_UNUSED_ARG(h);
372 0 se_teardown(aTHX_ sse);
373 0 }
374  
375 0 static void se_on_abort(pTHX_ void *h, void *ud) {
376 0 PK_LOOP_ENTER;
377 0 se_on_abort_in(aTHX_ h, ud);
378 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
379 0 }
380  
381 37 static SV *se_cb(pTHX_ punk_sse *sse, const char *name) {
382 37 ▶ SV **e = sse->cbs ? hv_fetch(sse->cbs, name, (I32)strlen(name), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = sse->cbs ? hv_fetch(sse->cbs, name, (I32)strlen(name), 0) : NULL; 2 35 37
383 /*
384 # uncoverable condition right count:2 class:ignore_covered_err
385 # uncoverable condition right count:3 class:ignore_covered_err */
386 37 ▶ return (e && *e && SvROK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e)) ? *e : NULL; 2 35 37
return (e && *e && SvROK(*e)) ? *e : NULL; 2 0 2
return (e && *e && SvROK(*e)) ? *e : NULL; 2 0 2
387 }
388  
389 37 static void se_teardown(pTHX_ punk_sse *sse) {
390 SV *cb;
391 /*
392 # uncoverable branch true
393 # uncoverable condition left count:1 class:ignore_covered_err
394 # uncoverable condition left count:2 class:ignore_covered_err */
395 37 ▶ if (sse->in_teardown || sse->state == SSE_CLOSED) return;
Branch: 2 branches
BranchTrueFalseTotal
if (sse->in_teardown || sse->state == SSE_CLOSED) return; 37 0 37
if (sse->in_teardown || sse->state == SSE_CLOSED) return; 0 37 37
396 37 sse->in_teardown = 1;
397 /*
398 # uncoverable condition right count:2 class:ignore_covered_err
399 # uncoverable condition right count:3 class:ignore_covered_err */
400 37 ▶ if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 35 37
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 0 2
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 0 2
401 /*
402 # uncoverable branch false
403 # uncoverable condition right count:1 class:ignore_covered_err */
404 2 ▶ if (sse->reading) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->reading) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_READ); 2 0 2
405 2 ▶ if (sse->writing) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->writing) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE); 1 1 2
406 }
407 37 ▶ if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) {
Branch: 5 branches
BranchTrueFalseTotal
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 35 2 37
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 0 35 35
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 2 0 2
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 2 0 2
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 1 1 2
408 1 sse->abi->timer_cancel(aTHX_ sse->loop, sse->hb_tw);
409 1 sse->hb_tw = NULL;
410 }
411 /*
412 # uncoverable condition right count:2 class:ignore_covered_err
413 # uncoverable condition right count:3 class:ignore_covered_err */
414 37 ▶ if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) { 0 37 37
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) { 0 0 0
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) { 0 0 0
415 /* NULLed first: closing ends the body, which on an EOF-delimited
416 * HTTP/1.1 response closes the connection, and a re-entrant teardown
417 * from that must not close the same handle twice. */
418 0 void *h = sse->sh;
419 0 sse->sh = NULL;
420 0 (void)sse->abi->stream_close(aTHX_ h);
421 }
422 37 sse->reading = sse->writing = 0;
423 37 ▶ if (sse->mode == SSE_MODE_STREAM && sse->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_STREAM && sse->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 31 6 37
if (sse->mode == SSE_MODE_STREAM && sse->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 31 0 31
424 31 dSP;
425 31 ENTER; SAVETMPS;
426 31 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK; 0 31 31
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK; 0 31 31
427 31 call_method("close", G_DISCARD | G_EVAL);
428 31 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 31 0 31
429 }
430 37 ▶ if (sse->fd >= 0) { close(sse->fd); sse->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (sse->fd >= 0) { close(sse->fd); sse->fd = -1; } 6 31 37
431 37 sse->state = SSE_CLOSED;
432 37 cb = se_cb(aTHX_ sse, "close");
433 37 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 2 35 37
434 2 dSP;
435 2 ENTER; SAVETMPS;
436 2 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); 0 2 2
437 2 ▶ PUSHs(sse->self_rv ? sse->self_rv : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sse->self_rv ? sse->self_rv : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
438 2 PUTBACK;
439 2 call_sv(cb, G_DISCARD | G_EVAL);
440 2 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 0 2 2
441 }
442 37 ▶ if (sse->cbs) { SvREFCNT_dec((SV *)sse->cbs); sse->cbs = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (sse->cbs) { SvREFCNT_dec((SV *)sse->cbs); sse->cbs = NULL; } 2 35 37
443 37 sse->in_teardown = 0;
444 /*
445 # uncoverable branch false
446 # uncoverable condition right count:1 class:ignore_covered_err */
447 37 ▶ if (sse->self_rv) { SV *s = sse->self_rv; sse->self_rv = NULL; SvREFCNT_dec(s); }
Branch: 1 branch
BranchTrueFalseTotal
if (sse->self_rv) { SV *s = sse->self_rv; sse->self_rv = NULL; SvREFCNT_dec(s); } 37 0 37
448 }
449  
450 37 static void se_free(pTHX_ punk_sse *sse) {
451 /*
452 # uncoverable branch true
453 # uncoverable condition left count:1 class:ignore_covered_err */
454 37 ▶ if (!sse) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sse) return; 0 37 37
455 /*
456 # uncoverable condition right count:2 class:ignore_covered_err
457 # uncoverable condition right count:3 class:ignore_covered_err */
458 37 ▶ if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 35 37
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 0 2
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 2 0 2
459 /* the watchers name this struct, so they must go before it does -
460 * and before the fd, or the loop is left watching a number the next
461 * connection will be given */
462 /*
463 # uncoverable branch true
464 # uncoverable condition left count:1 class:ignore_covered_err */
465 2 ▶ if (sse->reading) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->reading) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_READ); 0 2 2
466 /*
467 # uncoverable branch true
468 # uncoverable condition left count:1 class:ignore_covered_err */
469 2 ▶ if (sse->writing) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->writing) sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE); 0 2 2
470 }
471 37 ▶ if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */
Branch: 5 branches
BranchTrueFalseTotal
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */ 35 2 37
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */ 0 35 35
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */ 2 0 2
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */ 2 0 2
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) /* # uncoverable branch true */ 0 2 2
472 0 sse->abi->timer_cancel(aTHX_ sse->loop, sse->hb_tw); /* # uncoverable statement */
473 /* The handle names this struct through its callbacks, so it goes first;
474 * stream_close is the only entry point that releases one, and a stream
475 * aborted underneath us still has to be closed. */
476 /*
477 # uncoverable branch true
478 # uncoverable condition left count:1 class:ignore_covered_err
479 # uncoverable condition left count:2 class:ignore_covered_err
480 # uncoverable condition right count:2 class:ignore_covered_err
481 # uncoverable condition left count:3 class:ignore_covered_err */
482 37 ▶ if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh)
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) 0 37 37
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) 0 0 0
if (sse->mode == SSE_MODE_HSTREAM && sse->abi && sse->sh) 0 0 0
483 0 (void)sse->abi->stream_close(aTHX_ sse->sh); /* # uncoverable statement */
484 /*
485 # uncoverable branch true
486 # uncoverable condition left count:1 class:ignore_covered_err */
487 37 ▶ if (sse->fd >= 0) close(sse->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->fd >= 0) close(sse->fd); 0 37 37
488 37 ▶ if (sse->wbuf) free(sse->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->wbuf) free(sse->wbuf); 6 31 37
489 37 ▶ if (sse->writer) SvREFCNT_dec(sse->writer);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->writer) SvREFCNT_dec(sse->writer); 31 6 37
490 /*
491 # uncoverable branch true
492 # uncoverable condition left count:1 class:ignore_covered_err */
493 37 ▶ if (sse->cbs) SvREFCNT_dec((SV *)sse->cbs);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->cbs) SvREFCNT_dec((SV *)sse->cbs); 0 37 37
494 /*
495 # uncoverable branch true
496 # uncoverable condition left count:1 class:ignore_covered_err */
497 37 ▶ if (sse->self_rv) SvREFCNT_dec(sse->self_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->self_rv) SvREFCNT_dec(sse->self_rv); 0 37 37
498 37 Safefree(sse);
499 }
500  
501 /* ---- construction + the handler ------------------------------------------- */
502  
503 37 static punk_sse *se_new(pTHX_ int mode, HV *opts) {
504 punk_sse *sse;
505 37 Newxz(sse, 1, punk_sse);
506 37 sse->fd = -1;
507 37 sse->mode = mode;
508 37 sse->state = SSE_OPEN;
509 37 sse->heartbeat = 15.0;
510 /* bounded unless told otherwise: a client that opens a stream and stops
511 * reading would otherwise have every event kept for it for ever */
512 37 sse->write_buffer_limit = PW_DEFAULT_WRITE_LIMIT;
513 /*
514 # uncoverable branch false
515 # uncoverable condition right count:1 class:ignore_covered_err */
516 37 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 37 0 37
517 37 SV **h = hv_fetchs(opts, "heartbeat", 0);
518 37 SV **w = hv_fetchs(opts, "write_buffer_limit", 0);
519 /*
520 # uncoverable condition right count:2 class:ignore_covered_err
521 # uncoverable condition right count:3 class:ignore_covered_err */
522 37 ▶ if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h);
Branch: 3 branches
BranchTrueFalseTotal
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 2 35 37
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 2 0 2
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 2 0 2
523 /*
524 # uncoverable branch true
525 # uncoverable condition left count:1 class:ignore_covered_err
526 # uncoverable condition left count:2 class:ignore_covered_err
527 # uncoverable condition right count:2 class:ignore_covered_err
528 # uncoverable condition left count:3 class:ignore_covered_err
529 # uncoverable condition right count:3 class:ignore_covered_err */
530 37 ▶ if (w && *w && SvOK(*w)) sse->write_buffer_limit = (size_t)SvUV(*w);
Branch: 3 branches
BranchTrueFalseTotal
if (w && *w && SvOK(*w)) sse->write_buffer_limit = (size_t)SvUV(*w); 0 37 37
if (w && *w && SvOK(*w)) sse->write_buffer_limit = (size_t)SvUV(*w); 0 0 0
if (w && *w && SvOK(*w)) sse->write_buffer_limit = (size_t)SvUV(*w); 0 0 0
531 }
532 37 return sse;
533 }
534  
535 /* run $code->($c, $stream); a die closes the stream (the 200 is already on the
536 * wire, so there is no error response to send) and is reported */
537 37 static void se_run_handler(pTHX_ SV *code, SV *c, SV *self) {
538 37 dSP; int died;
539 37 ENTER; SAVETMPS;
540 37 ▶ PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; 0 37 37
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; 0 37 37
541 37 call_sv(code, G_DISCARD | G_EVAL);
542 37 SPAGAIN;
543 37 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 37 0 37
544 37 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 37 37
545 /*
546 # uncoverable branch true
547 # uncoverable condition left count:1 class:ignore_covered_err */
548 37 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 0 37 37
549 { /* escaped: a die text is request-shaped, and a newline in it
550 * would be a second log line (the 0.59 pl_plain rule) */
551 0 ▶ STRLEN el; const char *ep = SvPV_const(ERRSV, el); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
STRLEN el; const char *ep = SvPV_const(ERRSV, el); /* # uncoverable statement */ 0 0 0
552 0 ▶ warn("Punk::SSE: handler died: %" SVf, /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::SSE: handler died: %" SVf, /* # uncoverable statement */ 0 0 0
553 SVfARG(pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)))); /* # uncoverable statement */
554 }
555 0 se_teardown(aTHX_ se_of(aTHX_ self)); /* # uncoverable statement */
556 }
557 37 }
558  
559 /* The SSE response head as a structured header list rather than the
560 * SSE_RESPONSE byte string.
561 *
562 * Content-Type and Cache-Control carry over unchanged. Connection does NOT:
563 * it is hop-by-hop, HTTP/2 and HTTP/3 forbid it outright, and there is no
564 * connection to keep alive when the stream is one of many on a shared one.
565 * X-Accel-Buffering is kept because it costs a header and an application may
566 * still be behind nginx - though it means nothing when Hyperman is itself the
567 * edge, which is the point of serving h2 and h3 natively. Owned (+1). */
568 35 static AV *se_header_av(pTHX) {
569 35 AV *hdrs = newAV();
570 35 av_push(hdrs, newSVpvs("Content-Type")); av_push(hdrs, newSVpvs("text/event-stream"));
571 35 av_push(hdrs, newSVpvs("Cache-Control")); av_push(hdrs, newSVpvs("no-cache"));
572 35 av_push(hdrs, newSVpvs("X-Accel-Buffering")); av_push(hdrs, newSVpvs("no"));
573 35 return hdrs;
574 }
575  
576 /* The `retry: N` field, which has to go out before anything the handler
577 * writes and is the same three lines on every transport. */
578 37 static void se_send_retry(pTHX_ punk_sse *sse, HV *opts) {
579 37 ▶ SV **r = opts ? hv_fetchs(opts, "retry", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **r = opts ? hv_fetchs(opts, "retry", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 37 0 37
580 /*
581 # uncoverable branch true
582 # uncoverable condition left count:1 class:ignore_covered_err
583 # uncoverable condition left count:2 class:ignore_covered_err
584 # uncoverable condition right count:2 class:ignore_covered_err
585 # uncoverable condition left count:3 class:ignore_covered_err
586 # uncoverable condition right count:3 class:ignore_covered_err */
587 37 ▶ if (r && *r && SvOK(*r)) {
Branch: 3 branches
BranchTrueFalseTotal
if (r && *r && SvOK(*r)) { 0 37 37
if (r && *r && SvOK(*r)) { 0 0 0
if (r && *r && SvOK(*r)) { 0 0 0
588 0 se_field(aTHX_ sse, "retry: ", *r); /* retry: N */ /* # uncoverable statement */
589 0 se_write(aTHX_ sse, "\n", 1); /* + blank = dispatch */ /* # uncoverable statement */
590 }
591 37 }
592  
593 /* the psgi.streaming responder: capture [ $c, $code, $opts ]. The server calls
594 * it with the responder, which is the deferral a stream handle needs - so the
595 * handle is tried first and the writer is the fallback. Either way the
596 * handler sees one Punk::SSE and never learns which it got. */
597 XS_INTERNAL(sse_stream_cb);
598 31 XS_INTERNAL(sse_stream_cb) {
599 31 dXSARGS;
600 31 AV *cap = punk_clos_cap(aTHX_ cv);
601 31 SV *c = *av_fetch(cap, 0, 0);
602 31 SV *code = *av_fetch(cap, 1, 0);
603 31 SV *osv = *av_fetch(cap, 2, 0);
604 31 SV *esv = *av_fetch(cap, 3, 0);
605 /*
606 # uncoverable condition right count:1 class:ignore_covered_err
607 # uncoverable condition right count:2 class:ignore_covered_err */
608 31 ▶ HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; 31 0 31
HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; 31 0 31
609 /*
610 # uncoverable condition right count:1 class:ignore_covered_err
611 # uncoverable condition right count:2 class:ignore_covered_err */
612 31 ▶ HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; 31 0 31
HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; 31 0 31
613 31 ▶ SV *responder = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *responder = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
614 31 AV *hdrs = se_header_av(aTHX);
615 SV *self;
616 punk_sse *sse;
617 31 const hm_abi *A = NULL;
618 31 void *loop = NULL, *h = NULL;
619  
620 /*
621 # uncoverable branch false
622 # uncoverable condition right count:1 class:ignore_covered_err */
623 31 ▶ if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 200, hdrs);
Branch: 1 branch
BranchTrueFalseTotal
if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 200, hdrs); 31 0 31
624 31 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 0 31 31
625 0 sse = se_new(aTHX_ SSE_MODE_HSTREAM, opts);
626 0 sse->abi = A; sse->loop = loop; sse->sh = h;
627 0 SvREFCNT_dec((SV *)hdrs);
628 0 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE", PTR2IV(sse)));
629 0 sse->self_rv = newSVsv(self);
630 /* before the handler writes anything: a stream reset while the
631 * handler is still producing must reach it, and registering after
632 * the event still fires, so there is no race to lose */
633 0 (void)A->stream_on_abort(aTHX_ h, se_on_abort, sse);
634 0 se_send_retry(aTHX_ sse, opts);
635 0 se_arm_heartbeat(aTHX_ sse);
636 0 se_run_handler(aTHX_ code, c, self);
637 0 XSRETURN_EMPTY;
638 }
639  
640 {
641 31 AV *sh = newAV();
642 SV *writer;
643 31 dSP; int n;
644 31 av_push(sh, newSViv(200));
645 31 av_push(sh, newRV_noinc((SV *)hdrs));
646 31 ENTER; SAVETMPS;
647 31 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 31 31
PUSHMARK(SP); EXTEND(SP, 1); 0 31 31
648 31 PUSHs(sv_2mortal(newRV_noinc((SV *)sh)));
649 31 PUTBACK;
650 31 n = call_sv(responder, G_SCALAR);
651 31 SPAGAIN;
652 31 ▶ writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
653 31 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 31 0 31
654 31 sse = se_new(aTHX_ SSE_MODE_STREAM, opts);
655 31 sse->writer = writer; /* +1 owned */
656 }
657 31 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE", PTR2IV(sse)));
658 31 sse->self_rv = newSVsv(self);
659 31 se_send_retry(aTHX_ sse, opts);
660 31 se_run_handler(aTHX_ code, c, self);
661 31 XSRETURN_EMPTY;
662 }
663  
664 /* ---- the dispatcher ------------------------------------------------------- */
665  
666 41 static SV *punk_sse_dispatch(pTHX_ SV *c, SV *rec, SV *env) {
667 /*
668 # uncoverable condition right count:1 class:ignore_covered_err
669 # uncoverable condition right count:2 class:ignore_covered_err */
670 41 ▶ HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; 41 0 41
HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; 41 0 41
671 /*
672 # uncoverable condition right count:1 class:ignore_covered_err
673 # uncoverable condition right count:2 class:ignore_covered_err */
674 41 ▶ HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; 41 0 41
HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; 41 0 41
675 41 HV *opts = NULL;
676 SV **x, *code;
677 /*
678 # uncoverable branch true
679 # uncoverable condition left count:1 class:ignore_covered_err
680 # uncoverable condition left count:2 class:ignore_covered_err */
681 41 ▶ if (!envh || !rech) return pw_err(aTHX_ 500, "bad sse dispatch\n", NULL, NULL);
Branch: 2 branches
BranchTrueFalseTotal
if (!envh || !rech) return pw_err(aTHX_ 500, "bad sse dispatch\n", NULL, NULL); 41 0 41
if (!envh || !rech) return pw_err(aTHX_ 500, "bad sse dispatch\n", NULL, NULL); 0 41 41
682 41 x = hv_fetchs(rech, "sse", 0);
683 /*
684 # uncoverable branch false
685 # uncoverable condition right count:1 class:ignore_covered_err
686 # uncoverable condition right count:2 class:ignore_covered_err
687 # uncoverable condition right count:3 class:ignore_covered_err
688 # uncoverable condition right count:4 class:ignore_covered_err */
689 41 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x);
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 41 0 41
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 41 0 41
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 41 0 41
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 41 0 41
690 41 x = hv_fetchs(rech, K_CODE, 0);
691 /*
692 # uncoverable condition right count:1 class:ignore_covered_err
693 # uncoverable condition right count:2 class:ignore_covered_err */
694 41 ▶ code = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
code = (x && *x) ? *x : &PL_sv_undef; 41 0 41
code = (x && *x) ? *x : &PL_sv_undef; 41 0 41
695  
696 /* HEAD is the headers and nothing else: no stream is opened and the
697 * handler is not run. */
698 41 x = hv_fetchs(envh, "REQUEST_METHOD", 0);
699 /*
700 # uncoverable condition right count:1 class:ignore_covered_err
701 # uncoverable condition right count:2 class:ignore_covered_err
702 # uncoverable condition right count:3 class:ignore_covered_err
703 # uncoverable condition right count:5 class:ignore_covered_err */
704 41 ▶ if (x && *x && SvOK(*x) && SvCUR(*x) == 4
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 41 0 41
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 41 0 41
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 41 0 41
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 4 37 41
705 4 ▶ && memEQ(SvPVX(*x), "HEAD", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(SvPVX(*x), "HEAD", 4)) { 4 0 4
706 4 AV *resp = newAV();
707 4 av_push(resp, newSViv(200));
708 4 av_push(resp, newRV_noinc((SV *)se_header_av(aTHX)));
709 4 av_push(resp, newRV_noinc((SV *)newAV()));
710 4 return newRV_noinc((SV *)resp);
711 }
712  
713 /* 1. Hyperman detach: stream on the worker loop.
714 *
715 * A refusal falls through rather than answering 503. conn_detach says no
716 * to TLS (-3) because the session state belongs to the server, and that
717 * used to be the end of it - which is why deploying this meant
718 * terminating TLS in front and speaking plain HTTP/1 to the application.
719 * The stream handle below takes exactly that case, so the refusal is now
720 * a reason to try the next transport rather than an answer to the
721 * client. */
722 37 x = hv_fetchs(envh, "psgix.hyperman.conn", 0);
723 /*
724 # uncoverable condition right count:2 class:ignore_covered_err
725 # uncoverable condition right count:3 class:ignore_covered_err
726 # uncoverable condition right count:4 class:ignore_covered_err */
727 37 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 2 35 37
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 2 0 2
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 2 0 2
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 2 0 2
728 2 const hm_abi *A = punk_hm(aTHX);
729 /*
730 # uncoverable branch false
731 # uncoverable condition right count:1 class:ignore_covered_err */
732 2 ▶ if (A) {
Branch: 1 branch
BranchTrueFalseTotal
if (A) { 2 0 2
733 2 AV *cid = (AV *)SvRV(*x);
734 2 SV **fsv = av_fetch(cid, 0, 0), **isv = av_fetch(cid, 1, 0);
735 2 void *loop = A->cur_loop(aTHX);
736 int fd; int fl;
737 punk_sse *sse; SV *self;
738 /*
739 # uncoverable condition right count:1 class:ignore_covered_err
740 # uncoverable condition right count:2 class:ignore_covered_err
741 # uncoverable condition right count:3 class:ignore_covered_err
742 # uncoverable condition right count:4 class:ignore_covered_err
743 # uncoverable condition right count:5 class:ignore_covered_err
744 # uncoverable condition right count:6 class:ignore_covered_err */
745 2 ▶ if (fsv && *fsv && isv && *isv && loop
Branch: 5 branches
BranchTrueFalseTotal
if (fsv && *fsv && isv && *isv && loop 2 0 2
if (fsv && *fsv && isv && *isv && loop 2 0 2
if (fsv && *fsv && isv && *isv && loop 2 0 2
if (fsv && *fsv && isv && *isv && loop 2 0 2
if (fsv && *fsv && isv && *isv && loop 2 0 2
746 2 ▶ && (fd = (int)SvIV(*fsv)) >= 0
Branch: 1 branch
BranchTrueFalseTotal
&& (fd = (int)SvIV(*fsv)) >= 0 2 0 2
747 2 ▶ && A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) { 2 0 2
748 2 fl = fcntl(fd, F_GETFL, 0);
749 /*
750 # uncoverable branch false
751 # uncoverable condition right count:1 class:ignore_covered_err */
752 2 ▶ if (fl >= 0) (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if (fl >= 0) (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK); 2 0 2
753 2 sse = se_new(aTHX_ SSE_MODE_DETACH, opts);
754 2 sse->fd = fd; sse->abi = A; sse->loop = loop;
755 2 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE",
756 PTR2IV(sse)));
757 2 sse->self_rv = newSVsv(self);
758 2 se_write(aTHX_ sse, SSE_RESPONSE, sizeof(SSE_RESPONSE) - 1);
759 2 se_send_retry(aTHX_ sse, opts);
760 2 A->io_watch(aTHX_ loop, fd, HM_ABI_READ, se_on_readable, sse);
761 2 sse->reading = 1;
762 2 se_arm_heartbeat(aTHX_ sse);
763 2 se_run_handler(aTHX_ code, c, self);
764 2 return pw_empty(aTHX_ 200); /* the socket is ours now */
765 }
766 }
767 }
768  
769 /* 2. the deferred branch: a Hyperman v6 stream handle when the server
770 * offers one (HTTP/2, and HTTP/1.1 over TLS), else the portable
771 * psgi.streaming writer. sse_stream_cb decides, because the choice can
772 * only be made once the response has been deferred. */
773 35 x = hv_fetchs(envh, "psgi.streaming", 0);
774 /*
775 # uncoverable condition right count:2 class:ignore_covered_err
776 # uncoverable condition right count:3 class:ignore_covered_err */
777 35 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 31 4 35
if (x && *x && SvTRUE(*x)) { 31 0 31
if (x && *x && SvTRUE(*x)) { 31 0 31
778 31 AV *cap = newAV();
779 31 av_push(cap, newSVsv(c));
780 31 av_push(cap, newSVsv(code));
781 31 ▶ av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
782 31 av_push(cap, newRV_inc((SV *)envh));
783 31 return punk_closure(aTHX_ sse_stream_cb, cap); /* the responder */
784 }
785  
786 /* 3. blocking psgix.io: stream inside the handler (pins a worker) */
787 4 ▶ x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
788 /*
789 # uncoverable branch false
790 # uncoverable condition right count:1 class:ignore_covered_err
791 # uncoverable condition right count:2 class:ignore_covered_err
792 # uncoverable condition right count:3 class:ignore_covered_err */
793 4 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 4 0 4
if (x && *x && SvTRUE(*x)) { 4 0 4
if (x && *x && SvTRUE(*x)) { 4 0 4
794 4 SV **iop = hv_fetchs(envh, "psgix.io", 0);
795 /*
796 # uncoverable branch false
797 # uncoverable condition right count:1 class:ignore_covered_err
798 # uncoverable condition right count:2 class:ignore_covered_err
799 # uncoverable condition right count:3 class:ignore_covered_err */
800 4 ▶ if (iop && *iop && SvOK(*iop)) {
Branch: 3 branches
BranchTrueFalseTotal
if (iop && *iop && SvOK(*iop)) { 4 0 4
if (iop && *iop && SvOK(*iop)) { 4 0 4
if (iop && *iop && SvOK(*iop)) { 4 0 4
801 4 IO *io = sv_2io(*iop);
802 /*
803 # uncoverable condition right count:1 class:ignore_covered_err
804 # uncoverable condition right count:2 class:ignore_covered_err */
805 4 ▶ int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1;
Branch: 2 branches
BranchTrueFalseTotal
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 4 0 4
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 4 0 4
806 /*
807 # uncoverable branch false
808 # uncoverable condition right count:1 class:ignore_covered_err */
809 4 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 4 0 4
810 4 punk_sse *sse = se_new(aTHX_ SSE_MODE_BLOCK, opts);
811 SV *self;
812 4 sse->fd = fd;
813 4 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE",
814 PTR2IV(sse)));
815 4 sse->self_rv = newSVsv(self);
816 4 se_write(aTHX_ sse, SSE_RESPONSE, sizeof(SSE_RESPONSE) - 1);
817 4 se_send_retry(aTHX_ sse, opts);
818 4 se_run_handler(aTHX_ code, c, self); /* a sync generator */
819 4 return pw_empty(aTHX_ 200);
820 }
821 }
822 }
823  
824 0 return pw_err(aTHX_ 501, /* # uncoverable statement */
825 "this server cannot stream server-sent events (needs Hyperman "
826 "0.11+, a psgi.streaming server, or blocking => 1 with psgix.io)\n",
827 NULL, NULL);
828 }
829  
830 #endif /* PUNK_SSE_H */
« xs/dbil.xs ↑ Summary include/punk/punk_stream.h »