include/punk/punk_stream.h

filter: Statementstmt 60.5% 230 / 380 Branchbran 39.9% 155 / 388 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 50.1% 385 / 768 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/url.xs ↑ Summary include/punk/punk_csp.h »
1 /* punk_stream.h - a streamed response for an ordinary route, in C.
2 *
3 * $c->stream($content_type, $cb) hands $cb a writer and emits the body as it
4 * is produced: a CSV export walking a large query, an NDJSON dump - a response
5 * of unknown length that should not cost its size in memory. This is the SSE
6 * transport machinery (punk_sse.h) with the event framing removed: the same
7 * four transports in the same order - Hyperman detach (stream on the loop),
8 * the Hyperman stream handle (ABI v6, which is what serves HTTP/2 and TLS),
9 * psgi.streaming (the portable delayed-response writer), and blocking
10 * psgix.io - and the same pw_append / io_watch WRITE flush discipline.
11 * punk_sse.h's own header explains why the handle sits under detach.
12 *
13 * What is new here is the ending. An SSE stream lives until someone closes
14 * it; a streamed response ENDS, and how it ends must be visible to the
15 * client. On the raw-socket transports the body is chunk-framed (HTTP/1.1),
16 * so a clean close sends the terminal chunk and a handler that dies closes
17 * the socket without one - the client sees truncation, never a valid-looking
18 * short body. The contract is: the callback returning closes the stream
19 * cleanly; a die closes it hard. The stream handle cannot say the second of
20 * those - see pst_teardown.
21 *
22 * Backpressure is $w->drain: a Punk::Future settled with 1 when everything
23 * written so far has reached the kernel, or 0 when the stream closed first.
24 * On the detach transport a pending drain settles from the writable watcher;
25 * on the stream handle it settles from the ABI's on_drain, or at once when a
26 * write was taken without hitting the high-water mark; the blocking
27 * transports flush synchronously, so it settles immediately.
28 * Awaiting it after each write bounds the buffer to one chunk.
29 *
30 * Must be included after punk_wsconn.h (punk_hm, pw_append), punk_wshandshake.h
31 * (pw_err / pw_empty), punk_context.h (pcx_*), punk_static.h (punk_closure)
32 * and punk_future.h (pf_new / pf_settle).
33 */
34  
35 #ifndef PUNK_STREAM_H
36 #define PUNK_STREAM_H
37  
38 #include <fcntl.h>
39  
40 enum { PST_MODE_DETACH = 0, PST_MODE_STREAM = 1, PST_MODE_BLOCK = 2,
41 PST_MODE_HSTREAM = 3 };
42 enum { PST_OPEN = 0, PST_CLOSING = 1, PST_CLOSED = 2 };
43  
44 typedef struct punk_stream {
45 int fd; /* detach / block; -1 for a psgi writer */
46 int mode;
47 int state;
48 unsigned char reading, writing, in_teardown, chunked;
49 unsigned char ending_clean; /* pst_close set this; anything else is a
50 * body that stopped rather than finished */
51 char *wbuf; size_t wlen, woff, wcap;
52 size_t write_buffer_limit; /* 0 = unbounded; over it, teardown */
53 SV *writer; /* the psgi.streaming $writer (STREAM mode) */
54 SV *self_rv; /* strong self while live */
55 SV *drain_f; /* the pending drain future, or NULL */
56 int full; /* HSTREAM: the server said stop, and
57 has not yet said go */
58 SV *reqid; /* psgix.request_id for the death report */
59 SV *defer_c, *defer_code, *defer_head; /* the deferred detach start */
60 hm_abi_timer *defer_tw;
61 void *loop;
62 const hm_abi *abi;
63 void *sh; /* the v6 stream handle (HSTREAM mode) */
64 } punk_stream;
65  
66 102 static punk_stream *pst_of(pTHX_ SV *self) {
67 102 ▶ if (!SvROK(self) || !SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) 102 0 102
if (!SvROK(self) || !SvIOK(SvRV(self))) 0 102 102
68 0 croak("Punk::Stream: not a stream");
69 102 return (punk_stream *)INT2PTR(void *, SvIV(SvRV(self)));
70 }
71  
72 static void pst_teardown(pTHX_ punk_stream *st);
73 static void pst_on_writable(pTHX_ int fd, int mask, void *ud);
74 static void pst_on_hdrain(pTHX_ void *h, void *ud);
75 static void pst_on_habort(pTHX_ void *h, void *ud);
76  
77 /* settle the pending drain future, if any. ok is 1 (drained) or 0 (closed
78 * first). The local copy matters: settling fires reactions that can call back
79 * into this stream and ask for another drain. */
80 97 static void pst_settle_drain(pTHX_ punk_stream *st, int ok) {
81 97 SV *f = st->drain_f;
82 AV *vals;
83 97 ▶ if (!f) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return; 97 0 97
84 0 st->drain_f = NULL;
85 0 vals = newAV();
86 0 av_push(vals, newSViv(ok));
87 0 pf_settle(aTHX_ pf_of(aTHX_ f), f, PF_DONE, vals);
88 0 SvREFCNT_dec(f);
89 }
90  
91 /* ---- writing (DETACH / BLOCK): the punk_sse.h flush, plus two endings ------ */
92  
93 88 static void pst_flush(pTHX_ punk_stream *st) {
94 173 ▶ while (st->woff < st->wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (st->woff < st->wlen) { 85 88 173
95 85 ssize_t n = write(st->fd, st->wbuf + st->woff, st->wlen - st->woff);
96 85 ▶ if (n > 0) { st->woff += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { st->woff += (size_t)n; continue; } 85 0 85
97 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
98 0 ▶ if (st->mode == PST_MODE_DETACH) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mode == PST_MODE_DETACH) { 0 0 0
99 0 ▶ if (!st->writing && st->abi && st->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (!st->writing && st->abi && st->loop) { 0 0 0
if (!st->writing && st->abi && st->loop) { 0 0 0
if (!st->writing && st->abi && st->loop) { 0 0 0
100 0 st->abi->io_watch(aTHX_ st->loop, st->fd, HM_ABI_WRITE,
101 pst_on_writable, st);
102 0 st->writing = 1;
103 }
104 0 return;
105 }
106 0 { /* blocking transport over a socket the server left
107 * non-blocking: wait for writability rather than spinning.
108 * SSE's writes were small enough never to meet this; an
109 * export is not. */
110 fd_set wf;
111 0 ▶ FD_ZERO(&wf);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&wf); 0 0 0
112 0 FD_SET(st->fd, &wf);
113 0 (void)select(st->fd + 1, NULL, &wf, NULL, NULL);
114 0 continue;
115 }
116 }
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 pst_teardown(aTHX_ st); /* write error: client gone */
119 0 return;
120 }
121 88 st->wlen = st->woff = 0;
122 88 ▶ if (st->writing && st->abi && st->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (st->writing && st->abi && st->loop) { 0 88 88
if (st->writing && st->abi && st->loop) { 0 0 0
if (st->writing && st->abi && st->loop) { 0 0 0
123 0 st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE);
124 0 st->writing = 0;
125 }
126 88 pst_settle_drain(aTHX_ st, 1);
127 88 ▶ if (st->state == PST_CLOSING) pst_teardown(aTHX_ st); /* fully sent */
Branch: 1 branch
BranchTrueFalseTotal
if (st->state == PST_CLOSING) pst_teardown(aTHX_ st); /* fully sent */ 6 82 88
128 }
129  
130 0 static void pst_on_writable_in(pTHX_ int fd, int mask, void *ud) {
131 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
132 0 pst_flush(aTHX_ (punk_stream *)ud);
133 0 }
134  
135 0 static void pst_on_writable(pTHX_ int fd, int mask, void *ud) {
136 0 PK_LOOP_ENTER;
137 0 pst_on_writable_in(aTHX_ fd, mask, ud);
138 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
139 0 }
140  
141 /* ---- the stream handle's two callbacks ------------------------------------- */
142  
143 /* Output that was over the high-water mark has gone out. This is what makes
144 * $w->drain mean the same thing on the handle as on the detach transport:
145 * settled with 1 when everything written so far has actually left. */
146 0 static void pst_on_hdrain_in(pTHX_ void *h, void *ud) {
147 0 punk_stream *st = (punk_stream *)ud;
148 PERL_UNUSED_ARG(h);
149 0 st->full = 0;
150 0 pst_settle_drain(aTHX_ st, 1);
151 0 }
152  
153 0 static void pst_on_hdrain(pTHX_ void *h, void *ud) {
154 0 PK_LOOP_ENTER;
155 0 pst_on_hdrain_in(aTHX_ h, ud);
156 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
157 0 }
158  
159 /* The stream died before it was closed - an HTTP/2 RST_STREAM on this one
160 * stream of many, or the connection going away. Detach had no way to report
161 * the first of those, because on HTTP/1 a reset stream IS a dead connection;
162 * on a multiplexed transport it is not, and a producer that is not told goes
163 * on generating a body for nobody. Teardown settles the drain future with 0,
164 * so a handler awaiting it stops rather than blocking for ever. */
165 0 static void pst_on_habort_in(pTHX_ void *h, void *ud) {
166 0 punk_stream *st = (punk_stream *)ud;
167 PERL_UNUSED_ARG(h);
168 0 pst_teardown(aTHX_ st);
169 0 }
170  
171 0 static void pst_on_habort(pTHX_ void *h, void *ud) {
172 0 PK_LOOP_ENTER;
173 0 pst_on_habort_in(aTHX_ h, ud);
174 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
175 0 }
176  
177 /* the client closing early is the only thing we read for */
178 0 static void pst_on_readable_in(pTHX_ int fd, int mask, void *ud) {
179 0 punk_stream *st = (punk_stream *)ud;
180 char scratch[512];
181 ssize_t n;
182 PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask);
183 0 n = read(st->fd, scratch, sizeof scratch);
184 0 ▶ if (n == 0) { pst_teardown(aTHX_ st); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { pst_teardown(aTHX_ st); return; } 0 0 0
185 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
186 0 return;
187 0 ▶ if (n < 0) pst_teardown(aTHX_ st);
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) pst_teardown(aTHX_ st); 0 0 0
188 /* n > 0: bytes on a response stream are the client's mistake; ignored */
189 }
190  
191 0 static void pst_on_readable(pTHX_ int fd, int mask, void *ud) {
192 0 PK_LOOP_ENTER;
193 0 pst_on_readable_in(aTHX_ fd, mask, ud);
194 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
195 0 }
196  
197 /* queue raw bytes (already framed) to the socket transports */
198 13 static void pst_write_raw(pTHX_ punk_stream *st, const char *bytes, size_t len) {
199 13 ▶ if (st->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (st->write_buffer_limit 0 13 13
200 0 ▶ && st->wlen - st->woff + len > st->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& st->wlen - st->woff + len > st->write_buffer_limit) { 0 0 0
201 0 pst_teardown(aTHX_ st); /* a client that will not read */
202 0 return;
203 }
204 13 pw_append(&st->wbuf, &st->wlen, &st->wcap, bytes, len);
205 13 pst_flush(aTHX_ st);
206 }
207  
208 /* one body chunk, framed for the transport */
209 80 static void pst_write_body(pTHX_ punk_stream *st, const char *bytes, size_t len) {
210 80 ▶ if (st->state != PST_OPEN || len == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (st->state != PST_OPEN || len == 0) return; 79 1 80
if (st->state != PST_OPEN || len == 0) return; 0 79 79
211 79 ▶ if (st->mode == PST_MODE_HSTREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mode == PST_MODE_HSTREAM) { 0 79 79
212 /* Unframed: the transport frames it. HTTP/2 puts the bytes in a DATA
213 * frame and HTTP/1.1 delimits by close, so a chunk header written
214 * here would be body content on one and a lie on the other.
215 *
216 * The reference is held across the call because ending the body can
217 * close the connection, and Hyperman fires the abort callback from
218 * there - which tears this stream down and drops its own last
219 * reference. */
220 0 ▶ SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; 0 0 0
221 0 int r = st->abi->stream_write(aTHX_ st->sh, bytes, len);
222 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
223 0 ▶ if (st->state == PST_OPEN) pst_teardown(aTHX_ st);
Branch: 1 branch
BranchTrueFalseTotal
if (st->state == PST_OPEN) pst_teardown(aTHX_ st); 0 0 0
224 }
225 0 ▶ else if (r == HM_ABI_STREAM_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r == HM_ABI_STREAM_FULL) { 0 0 0
226 /* over the high-water mark: on_drain settles the drain future,
227 * which is how $w->drain becomes real backpressure here */
228 0 st->full = 1;
229 0 (void)st->abi->stream_on_drain(aTHX_ st->sh, pst_on_hdrain, st);
230 }
231 else { /* all of it is away */
232 0 st->full = 0;
233 0 pst_settle_drain(aTHX_ st, 1);
234 }
235 0 ▶ if (keep) SvREFCNT_dec(keep); /* st may be gone now */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* st may be gone now */ 0 0 0
236 0 return;
237 }
238 79 ▶ if (st->mode == PST_MODE_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mode == PST_MODE_STREAM) { 4 75 79
239 4 dSP;
240 4 ENTER; SAVETMPS;
241 4 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 4 4
PUSHMARK(SP); EXTEND(SP, 2); 0 4 4
242 4 ▶ PUSHs(st->writer ? st->writer : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(st->writer ? st->writer : &PL_sv_undef); 4 0 4
243 4 PUSHs(sv_2mortal(newSVpvn(bytes, len)));
244 4 PUTBACK;
245 4 call_method("write", G_DISCARD | G_EVAL);
246 4 SPAGAIN;
247 4 ▶ if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } 4 0 4
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } 0 4 4
if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } 0 0 0
248 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4 0 4
249 4 return;
250 }
251 75 ▶ if (st->chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->chunked) { 69 6 75
252 char pre[24];
253 /* the size goes through its own variable: where perl expands
254 * my_snprintf to a gcc statement expression, that expression declares
255 * its own `len' from the snprintf return, and a `len' written in the
256 * argument list resolves to that one - still uninitialised inside its
257 * own initialiser. Every chunk header then reads the stack. */
258 69 UV chunk_len = (UV)len;
259 69 int n = my_snprintf(pre, sizeof pre, "%" UVxf "\r\n", chunk_len);
260 69 ▶ if (st->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (st->write_buffer_limit 0 69 69
261 0 ▶ && st->wlen - st->woff + len + (size_t)n + 2 > st->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& st->wlen - st->woff + len + (size_t)n + 2 > st->write_buffer_limit) { 0 0 0
262 0 pst_teardown(aTHX_ st);
263 0 return;
264 }
265 69 pw_append(&st->wbuf, &st->wlen, &st->wcap, pre, (size_t)n);
266 69 pw_append(&st->wbuf, &st->wlen, &st->wcap, bytes, len);
267 69 pw_append(&st->wbuf, &st->wlen, &st->wcap, "\r\n", 2);
268 69 pst_flush(aTHX_ st);
269 69 return;
270 }
271 6 pst_write_raw(aTHX_ st, bytes, len);
272 }
273  
274 /* ---- teardown and the two closes ------------------------------------------- */
275  
276 /* the hard stop: nothing more is sent, so on a chunked transport the client
277 * sees a missing terminal chunk - truncation, not a short success.
278 *
279 * The stream handle says the same thing through stream_abort (hm_abi.h v7),
280 * which is why `ending_clean` exists: pst_close sets it and every other way
281 * of getting here leaves it clear. A streamed body has no declared length, so
282 * ending cleanly IS the claim that it is whole - the two endings are the only
283 * way a client can tell an export that finished from one whose query died on
284 * the last page. */
285 9 static void pst_teardown(pTHX_ punk_stream *st) {
286 9 ▶ if (st->in_teardown || st->state == PST_CLOSED) return;
Branch: 2 branches
BranchTrueFalseTotal
if (st->in_teardown || st->state == PST_CLOSED) return; 9 0 9
if (st->in_teardown || st->state == PST_CLOSED) return; 0 9 9
287 9 st->in_teardown = 1;
288 9 ▶ if (st->mode == PST_MODE_DETACH && st->abi && st->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 9 9
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 0 0
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 0 0
289 0 ▶ if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); 0 0 0
290 0 ▶ if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); 0 0 0
291 }
292 9 ▶ if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) {
Branch: 3 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) { 0 9 9
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) { 0 0 0
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) { 0 0 0
293 /* NULLed first: ending the body can close the connection, and the
294 * teardown that re-enters from there must not end it twice */
295 0 void *h = st->sh;
296 0 st->sh = NULL;
297 0 ▶ if (st->ending_clean) (void)st->abi->stream_close(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (st->ending_clean) (void)st->abi->stream_close(aTHX_ h); 0 0 0
298 0 else (void)st->abi->stream_abort(aTHX_ h);
299 }
300 9 st->reading = st->writing = 0;
301 9 ▶ if (st->mode == PST_MODE_STREAM && st->writer) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_STREAM && st->writer) { 2 7 9
if (st->mode == PST_MODE_STREAM && st->writer) { 2 0 2
302 2 dSP;
303 2 ENTER; SAVETMPS;
304 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK; 0 2 2
305 2 call_method("close", G_DISCARD | G_EVAL);
306 2 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 2 0 2
307 }
308 9 ▶ if (st->fd >= 0) { close(st->fd); st->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (st->fd >= 0) { close(st->fd); st->fd = -1; } 7 2 9
309 9 st->state = PST_CLOSED;
310 9 pst_settle_drain(aTHX_ st, 0);
311 9 st->in_teardown = 0;
312 9 ▶ if (st->self_rv) { SV *s = st->self_rv; st->self_rv = NULL; SvREFCNT_dec(s); }
Branch: 1 branch
BranchTrueFalseTotal
if (st->self_rv) { SV *s = st->self_rv; st->self_rv = NULL; SvREFCNT_dec(s); } 9 0 9
313 }
314  
315 /* the clean end: terminal chunk, then teardown once every byte is out. On the
316 * detach transport a still-loaded buffer keeps draining on the loop (the
317 * stream holds itself alive until it has); the blocking transports flush here. */
318 9 static void pst_close(pTHX_ punk_stream *st) {
319 9 ▶ if (st->state != PST_OPEN) return;
Branch: 1 branch
BranchTrueFalseTotal
if (st->state != PST_OPEN) return; 1 8 9
320 8 ▶ if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) { 6 2 8
if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) { 0 6 6
321 /* teardown does the ending; this is the one caller that gets to call
322 * it a clean one, and there is no half-sent buffer of ours to drain */
323 2 st->ending_clean = 1;
324 2 pst_teardown(aTHX_ st);
325 2 return;
326 }
327 6 st->state = PST_CLOSING;
328 6 ▶ if (st->chunked)
Branch: 1 branch
BranchTrueFalseTotal
if (st->chunked) 3 3 6
329 3 pw_append(&st->wbuf, &st->wlen, &st->wcap, "0\r\n\r\n", 5);
330 6 pst_flush(aTHX_ st); /* teardown fires from here once drained */
331 }
332  
333 9 static void pst_free(pTHX_ punk_stream *st) {
334 9 ▶ if (!st) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) return; 0 9 9
335 9 ▶ if (st->mode == PST_MODE_DETACH && st->abi && st->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 9 9
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 0 0
if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { 0 0 0
336 /* watchers name this struct, so they go before it - and before the
337 * fd, or the loop watches a number the next connection gets */
338 0 ▶ if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); 0 0 0
339 0 ▶ if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); 0 0 0
340 }
341 9 ▶ if (st->defer_tw && st->abi && st->loop)
Branch: 3 branches
BranchTrueFalseTotal
if (st->defer_tw && st->abi && st->loop) 0 9 9
if (st->defer_tw && st->abi && st->loop) 0 0 0
if (st->defer_tw && st->abi && st->loop) 0 0 0
342 0 st->abi->timer_cancel(aTHX_ st->loop, st->defer_tw);
343 /* The handle's callbacks name this struct, so it goes before the struct
344 * does. Reaching here with one still open means nobody ended the body on
345 * purpose, so it is aborted rather than closed: an ending nobody asked
346 * for is not a claim that the response is whole. */
347 9 ▶ if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh)
Branch: 3 branches
BranchTrueFalseTotal
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) 0 9 9
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) 0 0 0
if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) 0 0 0
348 0 (void)st->abi->stream_abort(aTHX_ st->sh);
349 9 ▶ if (st->fd >= 0) close(st->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->fd >= 0) close(st->fd); 0 9 9
350 9 ▶ if (st->wbuf) free(st->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (st->wbuf) free(st->wbuf); 7 2 9
351 9 ▶ if (st->writer) SvREFCNT_dec(st->writer);
Branch: 1 branch
BranchTrueFalseTotal
if (st->writer) SvREFCNT_dec(st->writer); 2 7 9
352 9 ▶ if (st->drain_f) SvREFCNT_dec(st->drain_f);
Branch: 1 branch
BranchTrueFalseTotal
if (st->drain_f) SvREFCNT_dec(st->drain_f); 0 9 9
353 9 ▶ if (st->reqid) SvREFCNT_dec(st->reqid);
Branch: 1 branch
BranchTrueFalseTotal
if (st->reqid) SvREFCNT_dec(st->reqid); 1 8 9
354 9 ▶ if (st->defer_c) SvREFCNT_dec(st->defer_c);
Branch: 1 branch
BranchTrueFalseTotal
if (st->defer_c) SvREFCNT_dec(st->defer_c); 0 9 9
355 9 ▶ if (st->defer_code) SvREFCNT_dec(st->defer_code);
Branch: 1 branch
BranchTrueFalseTotal
if (st->defer_code) SvREFCNT_dec(st->defer_code); 0 9 9
356 9 ▶ if (st->defer_head) SvREFCNT_dec(st->defer_head);
Branch: 1 branch
BranchTrueFalseTotal
if (st->defer_head) SvREFCNT_dec(st->defer_head); 0 9 9
357 9 ▶ if (st->self_rv) SvREFCNT_dec(st->self_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (st->self_rv) SvREFCNT_dec(st->self_rv); 0 9 9
358 9 Safefree(st);
359 }
360  
361 /* ---- construction, the head, the handler ---------------------------------- */
362  
363 9 static punk_stream *pst_new(pTHX_ int mode, HV *opts) {
364 punk_stream *st;
365 9 Newxz(st, 1, punk_stream);
366 9 st->fd = -1;
367 9 st->mode = mode;
368 9 st->state = PST_OPEN;
369 9 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 8 1 9
370 8 SV **w = hv_fetchs(opts, "write_buffer_limit", 0);
371 8 ▶ if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w);
Branch: 3 branches
BranchTrueFalseTotal
if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w); 0 8 8
if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w); 0 0 0
if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w); 0 0 0
372 }
373 9 return st;
374 }
375  
376 11 static IV pst_opt_status(pTHX_ HV *opts) {
377 11 ▶ SV **s = opts ? hv_fetchs(opts, "status", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **s = opts ? hv_fetchs(opts, "status", 0) : NULL; 10 1 11
378 11 ▶ return (s && *s && SvOK(*s)) ? SvIV(*s) : 200;
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; 3 8 11
return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; 3 0 3
return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; 3 0 3
379 }
380 11 static AV *pst_opt_headers(pTHX_ HV *opts) {
381 11 ▶ SV **h = opts ? hv_fetchs(opts, "headers", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **h = opts ? hv_fetchs(opts, "headers", 0) : NULL; 10 1 11
382 3 ▶ return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) 3 0 3
return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) 3 0 3
return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) 3 0 3
383 14 ▶ ? (AV *)SvRV(*h) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*h) : NULL; 3 8 11
384 }
385  
386 /* an empty reason-phrase is valid HTTP; 200 keeps its customary one */
387 7 static const char *pst_phrase(IV status) {
388 7 ▶ return status == 200 ? "OK" : "";
Branch: 1 branch
BranchTrueFalseTotal
return status == 200 ? "OK" : ""; 7 0 7
389 }
390  
391 /* the raw response head for the socket transports */
392 7 static SV *pst_head(pTHX_ punk_stream *st, SV *ct, HV *opts) {
393 7 IV status = pst_opt_status(aTHX_ opts);
394 7 AV *extra = pst_opt_headers(aTHX_ opts);
395 7 SV *head = sv_2mortal(newSVpvf("HTTP/1.1 %" IVdf " %s\r\nContent-Type: ",
396 status, pst_phrase(status)));
397 7 sv_catsv(head, ct);
398 7 sv_catpvs(head, "\r\n");
399 7 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 0 7 7
400 0 SSize_t i, n = av_len(extra) + 1;
401 0 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 0 0 0
402 0 SV **k = av_fetch(extra, i, 0);
403 0 SV **v = av_fetch(extra, i + 1, 0);
404 0 ▶ if (!(k && *k && v && *v)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(k && *k && v && *v)) continue; 0 0 0
if (!(k && *k && v && *v)) continue; 0 0 0
if (!(k && *k && v && *v)) continue; 0 0 0
if (!(k && *k && v && *v)) continue; 0 0 0
405 0 sv_catsv(head, *k);
406 0 sv_catpvs(head, ": ");
407 0 sv_catsv(head, *v);
408 0 sv_catpvs(head, "\r\n");
409 }
410 }
411 7 ▶ if (st->chunked) sv_catpvs(head, "Transfer-Encoding: chunked\r\n");
Branch: 1 branch
BranchTrueFalseTotal
if (st->chunked) sv_catpvs(head, "Transfer-Encoding: chunked\r\n"); 4 3 7
412 7 sv_catpvs(head, "Connection: close\r\nX-Accel-Buffering: no\r\n\r\n");
413 7 return head;
414 }
415  
416 /* Run $code->($c, $w). The callback returning is the clean end; a die is the
417 * hard one - the head is already on the wire, so there is no error response
418 * to send, and on a chunked transport the missing terminal chunk is the
419 * client's evidence. The report carries the request id when one exists. */
420 9 static void pst_run_handler(pTHX_ SV *code, SV *c, SV *self) {
421 9 dSP; int died;
422 punk_stream *st;
423 9 ENTER; SAVETMPS;
424 9 ▶ 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 9 9
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; 0 9 9
425 9 call_sv(code, G_DISCARD | G_EVAL);
426 9 SPAGAIN;
427 9 ▶ died = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; 9 0 9
428 9 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 9 9
429 9 st = pst_of(aTHX_ self);
430 9 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 1 8 9
431 1 ▶ STRLEN el; const char *ep = SvPV_const(ERRSV, el);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN el; const char *ep = SvPV_const(ERRSV, el); 1 0 1
432 1 ▶ SV *plain = pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)); /* one line */
Branch: 1 branch
BranchTrueFalseTotal
SV *plain = pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)); /* one line */ 1 0 1
433 1 ▶ if (st->reqid)
Branch: 1 branch
BranchTrueFalseTotal
if (st->reqid) 1 0 1
434 1 warn("Punk::Stream: handler died (request %" SVf "): %" SVf,
435 SVfARG(st->reqid), SVfARG(plain));
436 else
437 0 warn("Punk::Stream: handler died: %" SVf, SVfARG(plain));
438 1 pst_teardown(aTHX_ st);
439 1 return;
440 }
441 8 pst_close(aTHX_ st);
442 }
443  
444 /* The deferred half of the detach transport. Hyperman's detach is two-phase:
445 * hm_detach disarms the server's watchers at once, but the connection slot is
446 * only released after the app frame returns its sentinel - and the event
447 * dispatch routes an fd to the application's watchers only once that slot is
448 * empty. A handler that produced the whole body synchronously would arm its
449 * write watcher against a slot still naming the server's connection, and the
450 * first EAGAIN would stall the stream forever. So the detach transport starts
451 * on the next loop tick, after the sentinel has made it home.
452 *
453 * The self-reference is held across the run: the handler can tear the stream
454 * down (client gone), and teardown drops the stream's own reference - the
455 * last one, here. The SSE heartbeat learned this the hard way. */
456 0 static void pst_start_cb_in(pTHX_ void *ud) {
457 0 punk_stream *st = (punk_stream *)ud;
458 0 ▶ SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; 0 0 0
459 0 SV *c = st->defer_c, *code = st->defer_code, *head = st->defer_head;
460 0 st->defer_tw = NULL; /* it fired; nothing to cancel */
461 0 st->defer_c = st->defer_code = st->defer_head = NULL;
462 0 ▶ if (st->state == PST_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->state == PST_OPEN) { 0 0 0
463 0 pst_write_raw(aTHX_ st, SvPVX(head), SvCUR(head));
464 0 ▶ if (st->state == PST_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->state == PST_OPEN) { 0 0 0
465 0 st->abi->io_watch(aTHX_ st->loop, st->fd, HM_ABI_READ,
466 pst_on_readable, st);
467 0 st->reading = 1;
468 0 ▶ pst_run_handler(aTHX_ code, c, st->self_rv ? st->self_rv : keep);
Branch: 1 branch
BranchTrueFalseTotal
pst_run_handler(aTHX_ code, c, st->self_rv ? st->self_rv : keep); 0 0 0
469 }
470 }
471 0 SvREFCNT_dec(c);
472 0 SvREFCNT_dec(code);
473 0 SvREFCNT_dec(head);
474 0 ▶ if (keep) SvREFCNT_dec(keep); /* st may be gone after this */
Branch: 1 branch
BranchTrueFalseTotal
if (keep) SvREFCNT_dec(keep); /* st may be gone after this */ 0 0 0
475 0 }
476  
477 0 static void pst_start_cb(pTHX_ void *ud) {
478 0 PK_LOOP_ENTER;
479 0 pst_start_cb_in(aTHX_ ud);
480 0 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 0 0
481 0 }
482  
483 /* The response head as a structured header list rather than the pst_head byte
484 * string. Content-Type and the caller's own headers carry over; Connection
485 * and Transfer-Encoding do NOT. Both are hop-by-hop, HTTP/2 and HTTP/3 forbid
486 * them, and framing is the transport's job on every version this branch
487 * serves - HTTP/1.1 over TLS included, where Hyperman delimits by close.
488 * pst_phrase has nothing to do here either: a header list carries the status
489 * and never a reason phrase. Owned (+1). */
490 4 static AV *pst_header_av(pTHX_ SV *ct, HV *opts) {
491 4 AV *hdrs = newAV(), *extra = pst_opt_headers(aTHX_ opts);
492 4 av_push(hdrs, newSVpvs("Content-Type")); av_push(hdrs, newSVsv(ct));
493 4 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 3 1 4
494 3 SSize_t i, n = av_len(extra) + 1;
495 6 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 3 3 6
496 3 SV **k = av_fetch(extra, i, 0);
497 3 SV **v = av_fetch(extra, i + 1, 0);
498 3 ▶ if (!(k && *k && v && *v)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(k && *k && v && *v)) continue; 3 0 3
if (!(k && *k && v && *v)) continue; 3 0 3
if (!(k && *k && v && *v)) continue; 3 0 3
if (!(k && *k && v && *v)) continue; 0 3 3
499 3 av_push(hdrs, newSVsv(*k));
500 3 av_push(hdrs, newSVsv(*v));
501 }
502 }
503 4 av_push(hdrs, newSVpvs("X-Accel-Buffering")); av_push(hdrs, newSVpvs("no"));
504 4 return hdrs;
505 }
506  
507 /* the psgi.streaming responder: capture [ $c, $code, $ct, $opts, $reqid, $env ].
508 * The responder IS the deferral a stream handle needs, so the handle is tried
509 * first here and the portable writer is the fallback. */
510 XS_INTERNAL(pst_stream_cb);
511 2 XS_INTERNAL(pst_stream_cb) {
512 2 dXSARGS;
513 2 AV *cap = punk_clos_cap(aTHX_ cv);
514 2 SV *c = *av_fetch(cap, 0, 0);
515 2 SV *code = *av_fetch(cap, 1, 0);
516 2 SV *ct = *av_fetch(cap, 2, 0);
517 2 SV *osv = *av_fetch(cap, 3, 0);
518 2 SV *rid = *av_fetch(cap, 4, 0);
519 2 SV *esv = *av_fetch(cap, 5, 0);
520 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; 1 1 2
HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; 1 0 1
521 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
522 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
523 2 AV *hdrs = pst_header_av(aTHX_ ct, opts);
524 2 IV status = pst_opt_status(aTHX_ opts);
525 SV *self;
526 punk_stream *st;
527 2 const hm_abi *A = NULL;
528 2 void *loop = NULL, *h = NULL;
529  
530 2 ▶ if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, (int)status, hdrs);
Branch: 1 branch
BranchTrueFalseTotal
if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, (int)status, hdrs); 2 0 2
531 2 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 0 2 2
532 0 st = pst_new(aTHX_ PST_MODE_HSTREAM, opts);
533 0 st->abi = A; st->loop = loop; st->sh = h;
534 0 SvREFCNT_dec((SV *)hdrs);
535 0 ▶ if (SvOK(rid)) st->reqid = newSVsv(rid);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rid)) st->reqid = newSVsv(rid); 0 0 0
536 0 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st)));
537 0 st->self_rv = newSVsv(self);
538 /* before the handler produces anything: a reset while it is still
539 * writing must reach it, and registering after the event still fires */
540 0 (void)A->stream_on_abort(aTHX_ h, pst_on_habort, st);
541 0 pst_run_handler(aTHX_ code, c, self);
542 0 XSRETURN_EMPTY;
543 }
544  
545 {
546 2 AV *sh = newAV();
547 SV *writer;
548 2 dSP; int n;
549 2 av_push(sh, newSViv(status));
550 2 av_push(sh, newRV_noinc((SV *)hdrs));
551 2 ENTER; SAVETMPS;
552 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
553 2 PUSHs(sv_2mortal(newRV_noinc((SV *)sh)));
554 2 PUTBACK;
555 2 n = call_sv(responder, G_SCALAR);
556 2 SPAGAIN;
557 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
558 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
559 2 st = pst_new(aTHX_ PST_MODE_STREAM, opts);
560 2 st->writer = writer; /* +1 owned */
561 }
562 2 ▶ if (SvOK(rid)) st->reqid = newSVsv(rid);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rid)) st->reqid = newSVsv(rid); 0 2 2
563 2 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st)));
564 2 st->self_rv = newSVsv(self);
565 2 pst_run_handler(aTHX_ code, c, self);
566 2 XSRETURN_EMPTY;
567 }
568  
569 /* ---- $c->stream ------------------------------------------------------------ */
570  
571 12 static SV *punk_stream_start(pTHX_ SV *c, SV *ct, HV *opts, SV *code) {
572 12 AV *cav = pcx_av(aTHX_ c);
573 12 SV *envsv = pcx_get(aTHX_ cav, PCX_ENV);
574 12 ▶ HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 12 0 12
HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 12 0 12
575 24 ▶ ? (HV *)SvRV(envsv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(envsv) : NULL; 12 0 12
576 SV **x, *rid;
577 12 int chunked = 1;
578 12 ▶ if (!envh) return pw_err(aTHX_ 500, "bad stream dispatch\n", NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (!envh) return pw_err(aTHX_ 500, "bad stream dispatch\n", NULL, NULL); 0 12 12
579  
580 /* HEAD is the headers and nothing else. The streaming transports write
581 * to the connection themselves, where nothing blanks a body the way the
582 * dispatcher does for an ordinary response, so the handler is not run. */
583 12 x = hv_fetchs(envh, "REQUEST_METHOD", 0);
584 12 ▶ if (x && *x && SvOK(*x) && SvCUR(*x) == 4
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 12 0 12
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 12 0 12
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 12 0 12
if (x && *x && SvOK(*x) && SvCUR(*x) == 4 2 10 12
585 2 ▶ && memEQ(SvPVX(*x), "HEAD", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(SvPVX(*x), "HEAD", 4)) { 2 0 2
586 2 AV *resp = newAV();
587 2 av_push(resp, newSViv(pst_opt_status(aTHX_ opts)));
588 2 av_push(resp, newRV_noinc((SV *)pst_header_av(aTHX_ ct, opts)));
589 2 av_push(resp, newRV_noinc((SV *)newAV()));
590 2 return newRV_noinc((SV *)resp);
591 }
592  
593 /* Chunked framing is an HTTP/1.1 thing and only an HTTP/1.1 thing.
594 * HTTP/1.0 has no chunked transfer coding; HTTP/2 and HTTP/3 forbid the
595 * header and frame the body themselves. So the test names the one version
596 * that wants it rather than the one that does not - "anything but
597 * HTTP/1.0" put a Transfer-Encoding on an h2 response, and Hyperman spells
598 * its h2 protocol "HTTP/2" (six bytes, not "HTTP/2.0"), so no amount of
599 * widening the excluded literal would have caught it.
600 *
601 * A missing SERVER_PROTOCOL is HTTP/1.1: PSGI requires the key, and a
602 * synthetic environment that omits it is a test writing HTTP/1.1. */
603 10 x = hv_fetchs(envh, "SERVER_PROTOCOL", 0);
604 10 ▶ if (x && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { 9 1 10
if (x && *x && SvOK(*x)) { 9 0 9
if (x && *x && SvOK(*x)) { 9 0 9
605 STRLEN pl;
606 9 const char *p = SvPV_const(*x, pl);
607 9 ▶ chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8));
Branch: 2 branches
BranchTrueFalseTotal
chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8)); 7 2 9
chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8)); 6 1 7
608 }
609 10 x = hv_fetchs(envh, "psgix.request_id", 0);
610 10 ▶ rid = (x && *x && SvOK(*x)) ? *x : NULL;
Branch: 3 branches
BranchTrueFalseTotal
rid = (x && *x && SvOK(*x)) ? *x : NULL; 1 9 10
rid = (x && *x && SvOK(*x)) ? *x : NULL; 1 0 1
rid = (x && *x && SvOK(*x)) ? *x : NULL; 1 0 1
611  
612 /* 1. Hyperman detach: stream on the worker loop.
613 *
614 * A refusal falls through rather than answering 503: conn_detach says no
615 * to TLS, and the stream handle below is exactly what serves that case. */
616 10 x = hv_fetchs(envh, "psgix.hyperman.conn", 0);
617 10 ▶ 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 10 10
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
618 0 const hm_abi *A = punk_hm(aTHX);
619 0 ▶ if (A) {
Branch: 1 branch
BranchTrueFalseTotal
if (A) { 0 0 0
620 0 AV *cid = (AV *)SvRV(*x);
621 0 SV **fsv = av_fetch(cid, 0, 0), **isv = av_fetch(cid, 1, 0);
622 0 void *loop = A->cur_loop(aTHX);
623 int fd; int fl;
624 punk_stream *st; SV *self;
625 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
626 0 ▶ && (fd = (int)SvIV(*fsv)) >= 0
Branch: 1 branch
BranchTrueFalseTotal
&& (fd = (int)SvIV(*fsv)) >= 0 0 0 0
627 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
628 0 fl = fcntl(fd, F_GETFL, 0);
629 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
630 0 st = pst_new(aTHX_ PST_MODE_DETACH, opts);
631 0 st->fd = fd; st->abi = A; st->loop = loop;
632 0 st->chunked = (unsigned char)chunked;
633 0 ▶ if (rid) st->reqid = newSVsv(rid);
Branch: 1 branch
BranchTrueFalseTotal
if (rid) st->reqid = newSVsv(rid); 0 0 0
634 0 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st)));
635 0 st->self_rv = newSVsv(self);
636 0 st->defer_c = newSVsv(c);
637 0 st->defer_code = newSVsv(code);
638 0 st->defer_head = newSVsv(pst_head(aTHX_ st, ct, opts));
639 0 st->defer_tw = A->timer(aTHX_ loop, 0.0, pst_start_cb, st);
640 0 return pw_empty(aTHX_ 200); /* the socket is ours now */
641 }
642 }
643 }
644  
645 /* 2. the deferred branch: a Hyperman v6 stream handle when the server
646 * offers one (HTTP/2, and HTTP/1.1 over TLS), else the portable
647 * psgi.streaming writer. pst_stream_cb decides, because a handle can
648 * only be opened once the response has been deferred. */
649 10 x = hv_fetchs(envh, "psgi.streaming", 0);
650 10 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 2 8 10
if (x && *x && SvTRUE(*x)) { 2 0 2
if (x && *x && SvTRUE(*x)) { 2 0 2
651 2 AV *cap = newAV();
652 2 av_push(cap, newSVsv(c));
653 2 av_push(cap, newSVsv(code));
654 2 av_push(cap, newSVsv(ct));
655 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)); 1 1 2
656 2 ▶ av_push(cap, rid ? newSVsv(rid) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, rid ? newSVsv(rid) : newSV(0)); 0 2 2
657 2 av_push(cap, newRV_inc((SV *)envh));
658 2 return punk_closure(aTHX_ pst_stream_cb, cap); /* the responder */
659 }
660  
661 /* 3. blocking psgix.io: stream inside the handler (pins a worker) */
662 8 ▶ x = opts ? hv_fetchs(opts, "blocking", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
x = opts ? hv_fetchs(opts, "blocking", 0) : NULL; 7 1 8
663 8 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 7 1 8
if (x && *x && SvTRUE(*x)) { 7 0 7
if (x && *x && SvTRUE(*x)) { 7 0 7
664 7 SV **iop = hv_fetchs(envh, "psgix.io", 0);
665 7 ▶ if (iop && *iop && SvOK(*iop)) {
Branch: 3 branches
BranchTrueFalseTotal
if (iop && *iop && SvOK(*iop)) { 7 0 7
if (iop && *iop && SvOK(*iop)) { 7 0 7
if (iop && *iop && SvOK(*iop)) { 7 0 7
666 7 IO *io = sv_2io(*iop);
667 7 ▶ int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1;
Branch: 2 branches
BranchTrueFalseTotal
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 7 0 7
int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; 7 0 7
668 7 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 7 0 7
669 7 punk_stream *st = pst_new(aTHX_ PST_MODE_BLOCK, opts);
670 SV *self, *head;
671 7 st->fd = fd; st->chunked = (unsigned char)chunked;
672 7 ▶ if (rid) st->reqid = newSVsv(rid);
Branch: 1 branch
BranchTrueFalseTotal
if (rid) st->reqid = newSVsv(rid); 1 6 7
673 7 self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st)));
674 7 st->self_rv = newSVsv(self);
675 7 head = pst_head(aTHX_ st, ct, opts);
676 7 pst_write_raw(aTHX_ st, SvPVX(head), SvCUR(head));
677 7 pst_run_handler(aTHX_ code, c, self);
678 7 return pw_empty(aTHX_ 200);
679 }
680 }
681 }
682  
683 1 return pw_err(aTHX_ 501,
684 "this server cannot stream a response (needs Hyperman 0.11+, a "
685 "psgi.streaming server, or blocking => 1 with psgix.io)\n",
686 NULL, NULL);
687 }
688  
689 #endif /* PUNK_STREAM_H */
« xs/url.xs ↑ Summary include/punk/punk_csp.h »