include/punk/punk_sse.h

filter: Statementstmt 63.4% 212 / 334 Branchbran 34.1% 131 / 384 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 47.7% 343 / 718 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/sse.xs ↑ Summary xs/url.xs »
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 27 static punk_sse *se_of(pTHX_ SV *self) {
72 27 ▶ if (!SvROK(self) || !SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) 27 0 27
if (!SvROK(self) || !SvIOK(SvRV(self))) 0 27 27
73 0 croak("Punk::SSE: not a stream");
74 27 return (punk_sse *)INT2PTR(void *, SvIV(SvRV(self)));
75 }
76  
77 static void se_teardown(pTHX_ punk_sse *sse);
78 static void se_on_writable(pTHX_ int fd, int mask, void *ud);
79  
80 /* ---- writing (DETACH / BLOCK): mirror punk_wsconn.h's flush --------------- */
81  
82 14 static void se_flush(pTHX_ punk_sse *sse) {
83 28 ▶ while (sse->woff < sse->wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (sse->woff < sse->wlen) { 14 14 28
84 14 ssize_t n = write(sse->fd, sse->wbuf + sse->woff, sse->wlen - sse->woff);
85 14 ▶ if (n > 0) { sse->woff += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { sse->woff += (size_t)n; continue; } 14 0 14
86 0 ▶ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
Branch: 3 branches
BranchTrueFalseTotal
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
87 0 ▶ if (!sse->writing && sse->mode == SSE_MODE_DETACH
Branch: 2 branches
BranchTrueFalseTotal
if (!sse->writing && sse->mode == SSE_MODE_DETACH 0 0 0
if (!sse->writing && sse->mode == SSE_MODE_DETACH 0 0 0
88 0 ▶ && sse->abi && sse->loop) {
Branch: 2 branches
BranchTrueFalseTotal
&& sse->abi && sse->loop) { 0 0 0
&& sse->abi && sse->loop) { 0 0 0
89 0 sse->abi->io_watch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE,
90 se_on_writable, sse);
91 0 sse->writing = 1;
92 }
93 0 return;
94 }
95 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
96 0 se_teardown(aTHX_ sse); /* write error: client gone */
97 0 return;
98 }
99 14 sse->wlen = sse->woff = 0;
100 14 ▶ if (sse->writing && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->writing && sse->abi && sse->loop) { 0 14 14
if (sse->writing && sse->abi && sse->loop) { 0 0 0
if (sse->writing && sse->abi && sse->loop) { 0 0 0
101 0 sse->abi->io_unwatch(aTHX_ sse->loop, sse->fd, HM_ABI_WRITE);
102 0 sse->writing = 0;
103 }
104 }
105  
106 0 static void se_on_writable_in(pTHX_ int fd, int mask, void *ud) {
107 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
108 0 se_flush(aTHX_ (punk_sse *)ud);
109 0 }
110  
111 0 static void se_on_writable(pTHX_ int fd, int mask, void *ud) {
112 0 PK_LOOP_ENTER;
113 0 se_on_writable_in(aTHX_ fd, mask, ud);
114 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
115 0 }
116  
117 /* the client closing is the only thing we read for; a read of 0 tears down */
118 0 static void se_on_readable_in(pTHX_ int fd, int mask, void *ud) {
119 0 punk_sse *sse = (punk_sse *)ud;
120 char scratch[512];
121 ssize_t n;
122 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
123 0 n = read(sse->fd, scratch, sizeof scratch);
124 0 ▶ if (n == 0) { se_teardown(aTHX_ sse); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { se_teardown(aTHX_ sse); return; } 0 0 0
125 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
126 0 return;
127 0 ▶ if (n < 0) se_teardown(aTHX_ sse);
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) se_teardown(aTHX_ sse); 0 0 0
128 /* n > 0: a client should not send on an SSE stream; ignore it */
129 }
130  
131 0 static void se_on_readable(pTHX_ int fd, int mask, void *ud) {
132 0 PK_LOOP_ENTER;
133 0 se_on_readable_in(aTHX_ fd, mask, ud);
134 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
135 0 }
136  
137 /* queue bytes to the active backend */
138 21 static void se_write(pTHX_ punk_sse *sse, const char *bytes, size_t len) {
139 21 ▶ if (sse->state != SSE_OPEN) return;
Branch: 1 branch
BranchTrueFalseTotal
if (sse->state != SSE_OPEN) return; 0 21 21
140 21 ▶ if (sse->mode == SSE_MODE_HSTREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_HSTREAM) { 0 21 21
141 /* The write can close the connection under us, and Hyperman fires
142 * the abort callback from there - which tears this stream down and
143 * drops its own last reference. So the reference is held across the
144 * call, exactly as the heartbeat holds one. */
145 0 ▶ SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; 0 0 0
146 0 int r = sse->abi->stream_write(aTHX_ sse->sh, bytes, len);
147 /* HM_ABI_STREAM_FULL is not an error: the bytes were taken and the
148 * connection is merely backed up. An event stream's writes are small
149 * and there is nothing useful to drop, so the bound that matters is
150 * Hyperman's own connection buffer rather than one kept here. */
151 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
152 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
153 0 return;
154 }
155 21 ▶ if (sse->mode == SSE_MODE_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_STREAM) { 7 14 21
156 7 dSP;
157 7 ENTER; SAVETMPS;
158 7 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 7 7
PUSHMARK(SP); EXTEND(SP, 2); 0 7 7
159 7 ▶ PUSHs(sse->writer ? sse->writer : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sse->writer ? sse->writer : &PL_sv_undef); 7 0 7
160 7 PUSHs(sv_2mortal(newSVpvn(bytes, len)));
161 7 PUTBACK;
162 7 call_method("write", G_DISCARD | G_EVAL);
163 7 SPAGAIN;
164 7 ▶ if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 7 0 7
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 0 7 7
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; se_teardown(aTHX_ sse); return; } 0 0 0
165 7 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 7 0 7
166 7 return;
167 }
168 14 ▶ if (sse->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (sse->write_buffer_limit 14 0 14
169 14 ▶ && sse->wlen - sse->woff + len > sse->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& sse->wlen - sse->woff + len > sse->write_buffer_limit) { 0 14 14
170 0 se_teardown(aTHX_ sse); /* a client that will not read */
171 0 return;
172 }
173 14 pw_append(&sse->wbuf, &sse->wlen, &sse->wcap, bytes, len);
174 14 se_flush(aTHX_ sse);
175 }
176  
177 /* ---- SSE field formatting ------------------------------------------------- */
178  
179 /* append `data:` lines for one payload (a ref -> JSON; a string -> one line
180 * per line break), no terminating blank line.
181 *
182 * A line break is CR, LF or CRLF, because that is what ends a line for the
183 * EventSource parser reading this. Splitting on LF alone left a CR inside a
184 * `data:` line, where the client reads what follows it as a new field: text
185 * relayed from one user could set the event name and the last event id for
186 * everyone listening. */
187 14 static void se_append_data(pTHX_ SV *out, SV *data) {
188 14 ▶ if (SvROK(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(data)) { 2 12 14
189 2 SV *json = punk_frj(aTHX)->encode(aTHX_ data, NULL);
190 2 sv_catpvs(out, "data: ");
191 2 sv_catsv(out, json);
192 2 sv_catpvs(out, "\n");
193 2 SvREFCNT_dec(json);
194 }
195 else {
196 12 STRLEN l = 0, i, start = 0;
197 24 ▶ const char *p = SvOK(data) ? SvPV_const(data, l) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvOK(data) ? SvPV_const(data, l) : ""; 12 0 12
198 110 ▶ for (i = 0; i <= l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= l; i++) { 98 12 110
199 98 ▶ if (i == l || p[i] == '\n' || p[i] == '\r') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == l || p[i] == '\n' || p[i] == '\r') { 86 12 98
if (i == l || p[i] == '\n' || p[i] == '\r') { 83 3 86
if (i == l || p[i] == '\n' || p[i] == '\r') { 7 76 83
200 22 sv_catpvs(out, "data: ");
201 22 sv_catpvn(out, p + start, i - start);
202 22 sv_catpvs(out, "\n");
203 22 ▶ 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++; 9 13 22
if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++; 6 3 9
if (i + 1 < l && p[i] == '\r' && p[i + 1] == '\n') i++; 1 5 6
204 22 start = i + 1;
205 }
206 }
207 }
208 14 }
209  
210 11 static void se_send(pTHX_ punk_sse *sse, SV *data) {
211 11 SV *out = sv_2mortal(newSVpvs(""));
212 11 se_append_data(aTHX_ out, data);
213 11 sv_catpvs(out, "\n");
214 11 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
215 11 }
216  
217 3 static void se_event(pTHX_ punk_sse *sse, SV *name, SV *data) {
218 3 SV *out = sv_2mortal(newSVpvs("event: "));
219 6 ▶ STRLEN nl; const char *n = SvOK(name) ? SvPV_const(name, nl) : "";
Branch: 1 branch
BranchTrueFalseTotal
STRLEN nl; const char *n = SvOK(name) ? SvPV_const(name, nl) : ""; 3 0 3
220 STRLEN i;
221 15 ▶ 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 */ 12 0 12
for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */ 12 0 12
for (i = 0; i < nl; i++) if (n[i] == '\n' || n[i] == '\r') break; /* one line */ 12 3 15
222 3 sv_catpvn(out, n, i);
223 3 sv_catpvs(out, "\n");
224 3 se_append_data(aTHX_ out, data);
225 3 sv_catpvs(out, "\n");
226 3 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
227 3 }
228  
229 5 static void se_field(pTHX_ punk_sse *sse, const char *field, SV *val) {
230 5 SV *out = sv_2mortal(newSVpv(field, 0));
231 10 ▶ STRLEN vl; const char *v = SvOK(val) ? SvPV_const(val, vl) : "";
Branch: 1 branch
BranchTrueFalseTotal
STRLEN vl; const char *v = SvOK(val) ? SvPV_const(val, vl) : ""; 5 0 5
232 STRLEN i;
233 16 ▶ 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; 11 0 11
for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break; 11 0 11
for (i = 0; i < vl; i++) if (v[i] == '\n' || v[i] == '\r') break; 11 5 16
234 5 sv_catpvn(out, v, i);
235 5 sv_catpvs(out, "\n");
236 5 se_write(aTHX_ sse, SvPVX(out), SvCUR(out));
237 5 }
238  
239 /* ---- heartbeat, teardown -------------------------------------------------- */
240  
241 static void se_arm_heartbeat(pTHX_ punk_sse *sse);
242  
243 /* The keep-alive write is how a stream with nothing to say finds out its
244 * client has gone, so tearing down from inside it is the normal case, not the
245 * unlikely one - and teardown drops the stream's own reference, which for a
246 * handler that has already returned is the LAST one. So the reference is held
247 * across the write: without it the re-arm below reads freed memory and hands
248 * the loop a timer pointed at it. */
249 0 static void se_heartbeat_cb_in(pTHX_ void *ud) {
250 0 punk_sse *sse = (punk_sse *)ud;
251 0 ▶ SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = sse->self_rv ? SvREFCNT_inc(sse->self_rv) : NULL; 0 0 0
252 0 sse->hb_tw = NULL;
253 0 ▶ if (sse->state == SSE_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (sse->state == SSE_OPEN) { 0 0 0
254 0 se_write(aTHX_ sse, ":\n\n", 3); /* a keep-alive comment */
255 0 se_arm_heartbeat(aTHX_ sse);
256 }
257 0 ▶ 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 */ 0 0 0
258 0 }
259  
260 0 static void se_heartbeat_cb(pTHX_ void *ud) {
261 0 PK_LOOP_ENTER;
262 0 se_heartbeat_cb_in(aTHX_ ud);
263 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
264 0 }
265  
266 0 static void se_arm_heartbeat(pTHX_ punk_sse *sse) {
267 0 ▶ if (sse->state == SSE_OPEN && sse->heartbeat > 0
Branch: 2 branches
BranchTrueFalseTotal
if (sse->state == SSE_OPEN && sse->heartbeat > 0 0 0 0
if (sse->state == SSE_OPEN && sse->heartbeat > 0 0 0 0
268 0 ▶ && SE_ON_LOOP(sse) && sse->abi && sse->loop)
Branch: 4 branches
BranchTrueFalseTotal
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 0 0
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 0 0
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 0 0
&& SE_ON_LOOP(sse) && sse->abi && sse->loop) 0 0 0
269 0 sse->hb_tw = sse->abi->timer(aTHX_ sse->loop, sse->heartbeat,
270 se_heartbeat_cb, sse);
271 0 }
272  
273 /* The stream died before it was closed - an HTTP/2 RST_STREAM on this one
274 * stream of many, or the connection going away underneath it.
275 *
276 * This has no HTTP/1 analogue and is the reason the handle beats detach
277 * rather than merely matching it: on a multiplexed transport a peer can kill
278 * one stream while the connection carries on, and a producer that never hears
279 * about it goes on generating events for nobody. The handle stays valid to
280 * close, which is what teardown does with it. */
281 0 static void se_on_abort_in(pTHX_ void *h, void *ud) {
282 0 punk_sse *sse = (punk_sse *)ud;
283 PERL_UNUSED_ARG(h);
284 0 se_teardown(aTHX_ sse);
285 0 }
286  
287 0 static void se_on_abort(pTHX_ void *h, void *ud) {
288 0 PK_LOOP_ENTER;
289 0 se_on_abort_in(aTHX_ h, ud);
290 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
291 0 }
292  
293 4 static SV *se_cb(pTHX_ punk_sse *sse, const char *name) {
294 4 ▶ 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; 0 4 4
295 4 ▶ return (e && *e && SvROK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e)) ? *e : NULL; 0 4 4
return (e && *e && SvROK(*e)) ? *e : NULL; 0 0 0
return (e && *e && SvROK(*e)) ? *e : NULL; 0 0 0
296 }
297  
298 4 static void se_teardown(pTHX_ punk_sse *sse) {
299 SV *cb;
300 4 ▶ if (sse->in_teardown || sse->state == SSE_CLOSED) return;
Branch: 2 branches
BranchTrueFalseTotal
if (sse->in_teardown || sse->state == SSE_CLOSED) return; 4 0 4
if (sse->in_teardown || sse->state == SSE_CLOSED) return; 0 4 4
301 4 sse->in_teardown = 1;
302 4 ▶ if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 4 4
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 0 0
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 0 0
303 0 ▶ 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 0 0
304 0 ▶ 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 0 0
305 }
306 4 ▶ 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) { 4 0 4
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 0 4 4
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 0 0 0
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 0 0 0
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) { 0 0 0
307 0 sse->abi->timer_cancel(aTHX_ sse->loop, sse->hb_tw);
308 0 sse->hb_tw = NULL;
309 }
310 4 ▶ 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 4 4
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
311 /* NULLed first: closing ends the body, which on an EOF-delimited
312 * HTTP/1.1 response closes the connection, and a re-entrant teardown
313 * from that must not close the same handle twice. */
314 0 void *h = sse->sh;
315 0 sse->sh = NULL;
316 0 (void)sse->abi->stream_close(aTHX_ h);
317 }
318 4 sse->reading = sse->writing = 0;
319 4 ▶ if (sse->mode == SSE_MODE_STREAM && sse->writer) {
Branch: 2 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_STREAM && sse->writer) { 2 2 4
if (sse->mode == SSE_MODE_STREAM && sse->writer) { 2 0 2
320 2 dSP;
321 2 ENTER; SAVETMPS;
322 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sse->writer); PUTBACK; 0 2 2
323 2 call_method("close", G_DISCARD | G_EVAL);
324 2 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 2 0 2
325 }
326 4 ▶ if (sse->fd >= 0) { close(sse->fd); sse->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (sse->fd >= 0) { close(sse->fd); sse->fd = -1; } 2 2 4
327 4 sse->state = SSE_CLOSED;
328 4 cb = se_cb(aTHX_ sse, "close");
329 4 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 4 4
330 0 dSP;
331 0 ENTER; SAVETMPS;
332 0 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 0 0
PUSHMARK(SP); EXTEND(SP, 1); 0 0 0
333 0 ▶ PUSHs(sse->self_rv ? sse->self_rv : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sse->self_rv ? sse->self_rv : &PL_sv_undef); 0 0 0
334 0 PUTBACK;
335 0 call_sv(cb, G_DISCARD | G_EVAL);
336 0 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 0 0 0
337 }
338 4 ▶ 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; } 0 4 4
339 4 sse->in_teardown = 0;
340 4 ▶ 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); } 4 0 4
341 }
342  
343 4 static void se_free(pTHX_ punk_sse *sse) {
344 4 ▶ if (!sse) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sse) return; 0 4 4
345 4 ▶ if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 4 4
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 0 0
if (sse->mode == SSE_MODE_DETACH && sse->abi && sse->loop) { 0 0 0
346 /* the watchers name this struct, so they must go before it does -
347 * and before the fd, or the loop is left watching a number the next
348 * connection will be given */
349 0 ▶ 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 0 0
350 0 ▶ 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 0 0
351 }
352 4 ▶ 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) 4 0 4
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) 0 4 4
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) 0 0 0
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) 0 0 0
if (SE_ON_LOOP(sse) && sse->abi && sse->loop && sse->hb_tw) 0 0 0
353 0 sse->abi->timer_cancel(aTHX_ sse->loop, sse->hb_tw);
354 /* The handle names this struct through its callbacks, so it goes first;
355 * stream_close is the only entry point that releases one, and a stream
356 * aborted underneath us still has to be closed. */
357 4 ▶ 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 4 4
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
358 0 (void)sse->abi->stream_close(aTHX_ sse->sh);
359 4 ▶ if (sse->fd >= 0) close(sse->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->fd >= 0) close(sse->fd); 0 4 4
360 4 ▶ if (sse->wbuf) free(sse->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->wbuf) free(sse->wbuf); 2 2 4
361 4 ▶ if (sse->writer) SvREFCNT_dec(sse->writer);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->writer) SvREFCNT_dec(sse->writer); 2 2 4
362 4 ▶ if (sse->cbs) SvREFCNT_dec((SV *)sse->cbs);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->cbs) SvREFCNT_dec((SV *)sse->cbs); 0 4 4
363 4 ▶ if (sse->self_rv) SvREFCNT_dec(sse->self_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (sse->self_rv) SvREFCNT_dec(sse->self_rv); 0 4 4
364 4 Safefree(sse);
365 }
366  
367 /* ---- construction + the handler ------------------------------------------- */
368  
369 4 static punk_sse *se_new(pTHX_ int mode, HV *opts) {
370 punk_sse *sse;
371 4 Newxz(sse, 1, punk_sse);
372 4 sse->fd = -1;
373 4 sse->mode = mode;
374 4 sse->state = SSE_OPEN;
375 4 sse->heartbeat = 15.0;
376 /* bounded unless told otherwise: a client that opens a stream and stops
377 * reading would otherwise have every event kept for it for ever */
378 4 sse->write_buffer_limit = PW_DEFAULT_WRITE_LIMIT;
379 4 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 4 0 4
380 4 SV **h = hv_fetchs(opts, "heartbeat", 0);
381 4 SV **w = hv_fetchs(opts, "write_buffer_limit", 0);
382 4 ▶ if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h);
Branch: 3 branches
BranchTrueFalseTotal
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 0 4 4
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 0 0 0
if (h && *h && SvOK(*h)) sse->heartbeat = SvNV(*h); 0 0 0
383 4 ▶ 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 4 4
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
384 }
385 4 return sse;
386 }
387  
388 /* run $code->($c, $stream); a die closes the stream (the 200 is already on the
389 * wire, so there is no error response to send) and is reported */
390 4 static void se_run_handler(pTHX_ SV *code, SV *c, SV *self) {
391 4 dSP; int died;
392 4 ENTER; SAVETMPS;
393 4 ▶ 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 4 4
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; 0 4 4
394 4 call_sv(code, G_DISCARD | G_EVAL);
395 4 SPAGAIN;
396 4 ▶ died = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; 4 0 4
397 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 4 4
398 4 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 0 4 4
399 { /* escaped: a die text is request-shaped, and a newline in it
400 * would be a second log line (the 0.59 pl_plain rule) */
401 0 ▶ STRLEN el; const char *ep = SvPV_const(ERRSV, el);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN el; const char *ep = SvPV_const(ERRSV, el); 0 0 0
402 0 ▶ warn("Punk::SSE: handler died: %" SVf,
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::SSE: handler died: %" SVf, 0 0 0
403 SVfARG(pl_plain(aTHX_ ep, el, SvUTF8(ERRSV))));
404 }
405 0 se_teardown(aTHX_ se_of(aTHX_ self));
406 }
407 4 }
408  
409 /* The SSE response head as a structured header list rather than the
410 * SSE_RESPONSE byte string.
411 *
412 * Content-Type and Cache-Control carry over unchanged. Connection does NOT:
413 * it is hop-by-hop, HTTP/2 and HTTP/3 forbid it outright, and there is no
414 * connection to keep alive when the stream is one of many on a shared one.
415 * X-Accel-Buffering is kept because it costs a header and an application may
416 * still be behind nginx - though it means nothing when Hyperman is itself the
417 * edge, which is the point of serving h2 and h3 natively. Owned (+1). */
418 4 static AV *se_header_av(pTHX) {
419 4 AV *hdrs = newAV();
420 4 av_push(hdrs, newSVpvs("Content-Type")); av_push(hdrs, newSVpvs("text/event-stream"));
421 4 av_push(hdrs, newSVpvs("Cache-Control")); av_push(hdrs, newSVpvs("no-cache"));
422 4 av_push(hdrs, newSVpvs("X-Accel-Buffering")); av_push(hdrs, newSVpvs("no"));
423 4 return hdrs;
424 }
425  
426 /* The `retry: N` field, which has to go out before anything the handler
427 * writes and is the same three lines on every transport. */
428 4 static void se_send_retry(pTHX_ punk_sse *sse, HV *opts) {
429 4 ▶ SV **r = opts ? hv_fetchs(opts, "retry", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **r = opts ? hv_fetchs(opts, "retry", 0) : NULL; 4 0 4
430 4 ▶ if (r && *r && SvOK(*r)) {
Branch: 3 branches
BranchTrueFalseTotal
if (r && *r && SvOK(*r)) { 0 4 4
if (r && *r && SvOK(*r)) { 0 0 0
if (r && *r && SvOK(*r)) { 0 0 0
431 0 se_field(aTHX_ sse, "retry: ", *r); /* retry: N */
432 0 se_write(aTHX_ sse, "\n", 1); /* + blank = dispatch */
433 }
434 4 }
435  
436 /* the psgi.streaming responder: capture [ $c, $code, $opts ]. The server calls
437 * it with the responder, which is the deferral a stream handle needs - so the
438 * handle is tried first and the writer is the fallback. Either way the
439 * handler sees one Punk::SSE and never learns which it got. */
440 XS_INTERNAL(sse_stream_cb);
441 2 XS_INTERNAL(sse_stream_cb) {
442 2 dXSARGS;
443 2 AV *cap = punk_clos_cap(aTHX_ cv);
444 2 SV *c = *av_fetch(cap, 0, 0);
445 2 SV *code = *av_fetch(cap, 1, 0);
446 2 SV *osv = *av_fetch(cap, 2, 0);
447 2 SV *esv = *av_fetch(cap, 3, 0);
448 2 ▶ 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; 2 0 2
HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; 2 0 2
449 2 ▶ 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; 2 0 2
HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; 2 0 2
450 2 ▶ SV *responder = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *responder = items > 0 ? ST(0) : &PL_sv_undef; 2 0 2
451 2 AV *hdrs = se_header_av(aTHX);
452 SV *self;
453 punk_sse *sse;
454 2 const hm_abi *A = NULL;
455 2 void *loop = NULL, *h = NULL;
456  
457 2 ▶ 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); 2 0 2
458 2 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 0 2 2
459 0 sse = se_new(aTHX_ SSE_MODE_HSTREAM, opts);
460 0 sse->abi = A; sse->loop = loop; sse->sh = h;
461 0 SvREFCNT_dec((SV *)hdrs);
462 0 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE", PTR2IV(sse)));
463 0 sse->self_rv = newSVsv(self);
464 /* before the handler writes anything: a stream reset while the
465 * handler is still producing must reach it, and registering after
466 * the event still fires, so there is no race to lose */
467 0 (void)A->stream_on_abort(aTHX_ h, se_on_abort, sse);
468 0 se_send_retry(aTHX_ sse, opts);
469 0 se_arm_heartbeat(aTHX_ sse);
470 0 se_run_handler(aTHX_ code, c, self);
471 0 XSRETURN_EMPTY;
472 }
473  
474 {
475 2 AV *sh = newAV();
476 SV *writer;
477 2 dSP; int n;
478 2 av_push(sh, newSViv(200));
479 2 av_push(sh, newRV_noinc((SV *)hdrs));
480 2 ENTER; SAVETMPS;
481 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
482 2 PUSHs(sv_2mortal(newRV_noinc((SV *)sh)));
483 2 PUTBACK;
484 2 n = call_sv(responder, G_SCALAR);
485 2 SPAGAIN;
486 2 ▶ writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; 2 0 2
487 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
488 2 sse = se_new(aTHX_ SSE_MODE_STREAM, opts);
489 2 sse->writer = writer; /* +1 owned */
490 }
491 2 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE", PTR2IV(sse)));
492 2 sse->self_rv = newSVsv(self);
493 2 se_send_retry(aTHX_ sse, opts);
494 2 se_run_handler(aTHX_ code, c, self);
495 2 XSRETURN_EMPTY;
496 }
497  
498 /* ---- the dispatcher ------------------------------------------------------- */
499  
500 6 static SV *punk_sse_dispatch(pTHX_ SV *c, SV *rec, SV *env) {
501 6 ▶ 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; 6 0 6
HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; 6 0 6
502 6 ▶ 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; 6 0 6
HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; 6 0 6
503 6 HV *opts = NULL;
504 SV **x, *code;
505 6 ▶ 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); 6 0 6
if (!envh || !rech) return pw_err(aTHX_ 500, "bad sse dispatch\n", NULL, NULL); 0 6 6
506 6 x = hv_fetchs(rech, "sse", 0);
507 6 ▶ 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); 6 0 6
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 6 0 6
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 6 0 6
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 6 0 6
508 6 x = hv_fetchs(rech, K_CODE, 0);
509 6 ▶ code = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
code = (x && *x) ? *x : &PL_sv_undef; 6 0 6
code = (x && *x) ? *x : &PL_sv_undef; 6 0 6
510  
511 /* HEAD is the headers and nothing else: no stream is opened and the
512 * handler is not run. */
513 6 x = hv_fetchs(envh, "REQUEST_METHOD", 0);
514 6 ▶ if (x && *x && SvOK(*x) && SvCUR(*x) == 4
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 6 0 6
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 6 0 6
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 6 0 6
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 2 4 6
515 2 ▶ && memEQ(SvPVX(*x), "HEAD", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(SvPVX(*x), "HEAD", 4)) { 2 0 2
516 2 AV *resp = newAV();
517 2 av_push(resp, newSViv(200));
518 2 av_push(resp, newRV_noinc((SV *)se_header_av(aTHX)));
519 2 av_push(resp, newRV_noinc((SV *)newAV()));
520 2 return newRV_noinc((SV *)resp);
521 }
522  
523 /* 1. Hyperman detach: stream on the worker loop.
524 *
525 * A refusal falls through rather than answering 503. conn_detach says no
526 * to TLS (-3) because the session state belongs to the server, and that
527 * used to be the end of it - which is why deploying this meant
528 * terminating TLS in front and speaking plain HTTP/1 to the application.
529 * The stream handle below takes exactly that case, so the refusal is now
530 * a reason to try the next transport rather than an answer to the
531 * client. */
532 4 x = hv_fetchs(envh, "psgix.hyperman.conn", 0);
533 4 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 0 4 4
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 0 0 0
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 0 0 0
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 0 0 0
534 0 const hm_abi *A = punk_hm(aTHX);
535 0 ▶ if (A) {
Branch: 1 branch
BranchTrueFalseTotal
if (A) { 0 0 0
536 0 AV *cid = (AV *)SvRV(*x);
537 0 SV **fsv = av_fetch(cid, 0, 0), **isv = av_fetch(cid, 1, 0);
538 0 void *loop = A->cur_loop(aTHX);
539 int fd; int fl;
540 punk_sse *sse; SV *self;
541 0 ▶ if (fsv && *fsv && isv && *isv && loop
Branch: 5 branches
BranchTrueFalseTotal
if (fsv && *fsv && isv && *isv && loop 0 0 0
if (fsv && *fsv && isv && *isv && loop 0 0 0
if (fsv && *fsv && isv && *isv && loop 0 0 0
if (fsv && *fsv && isv && *isv && loop 0 0 0
if (fsv && *fsv && isv && *isv && loop 0 0 0
542 0 ▶ && (fd = (int)SvIV(*fsv)) >= 0
Branch: 1 branch
BranchTrueFalseTotal
&& (fd = (int)SvIV(*fsv)) >= 0 0 0 0
543 0 ▶ && A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) { 0 0 0
544 0 fl = fcntl(fd, F_GETFL, 0);
545 0 ▶ 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); 0 0 0
546 0 sse = se_new(aTHX_ SSE_MODE_DETACH, opts);
547 0 sse->fd = fd; sse->abi = A; sse->loop = loop;
548 0 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE",
549 PTR2IV(sse)));
550 0 sse->self_rv = newSVsv(self);
551 0 se_write(aTHX_ sse, SSE_RESPONSE, sizeof(SSE_RESPONSE) - 1);
552 0 se_send_retry(aTHX_ sse, opts);
553 0 A->io_watch(aTHX_ loop, fd, HM_ABI_READ, se_on_readable, sse);
554 0 sse->reading = 1;
555 0 se_arm_heartbeat(aTHX_ sse);
556 0 se_run_handler(aTHX_ code, c, self);
557 0 return pw_empty(aTHX_ 200); /* the socket is ours now */
558 }
559 }
560 }
561  
562 /* 2. the deferred branch: a Hyperman v6 stream handle when the server
563 * offers one (HTTP/2, and HTTP/1.1 over TLS), else the portable
564 * psgi.streaming writer. sse_stream_cb decides, because the choice can
565 * only be made once the response has been deferred. */
566 4 x = hv_fetchs(envh, "psgi.streaming", 0);
567 4 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 2 2 4
if (x && *x && SvTRUE(*x)) { 2 0 2
if (x && *x && SvTRUE(*x)) { 2 0 2
568 2 AV *cap = newAV();
569 2 av_push(cap, newSVsv(c));
570 2 av_push(cap, newSVsv(code));
571 2 ▶ av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0)); 2 0 2
572 2 av_push(cap, newRV_inc((SV *)envh));
573 2 return punk_closure(aTHX_ sse_stream_cb, cap); /* the responder */
574 }
575  
576 /* 3. blocking psgix.io: stream inside the handler (pins a worker) */
577 2 ▶ x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; 2 0 2
578 2 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 2 0 2
if (x && *x && SvTRUE(*x)) { 2 0 2
if (x && *x && SvTRUE(*x)) { 2 0 2
579 2 SV **iop = hv_fetchs(envh, "psgix.io", 0);
580 2 ▶ if (iop && *iop && SvOK(*iop)) {
Branch: 3 branches
BranchTrueFalseTotal
if (iop && *iop && SvOK(*iop)) { 2 0 2
if (iop && *iop && SvOK(*iop)) { 2 0 2
if (iop && *iop && SvOK(*iop)) { 2 0 2
581 2 IO *io = sv_2io(*iop);
582 2 ▶ int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1;
Branch: 2 branches
BranchTrueFalseTotal
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 2 0 2
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 2 0 2
583 2 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 2 0 2
584 2 punk_sse *sse = se_new(aTHX_ SSE_MODE_BLOCK, opts);
585 SV *self;
586 2 sse->fd = fd;
587 2 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::SSE",
588 PTR2IV(sse)));
589 2 sse->self_rv = newSVsv(self);
590 2 se_write(aTHX_ sse, SSE_RESPONSE, sizeof(SSE_RESPONSE) - 1);
591 2 se_send_retry(aTHX_ sse, opts);
592 2 se_run_handler(aTHX_ code, c, self); /* a sync generator */
593 2 return pw_empty(aTHX_ 200);
594 }
595 }
596 }
597  
598 0 return pw_err(aTHX_ 501,
599 "this server cannot stream server-sent events (needs Hyperman "
600 "0.11+, a psgi.streaming server, or blocking => 1 with psgix.io)\n",
601 NULL, NULL);
602 }
603  
604 #endif /* PUNK_SSE_H */
« xs/sse.xs ↑ Summary xs/url.xs »