| 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 |
1016 |
| static punk_stream *pst_of(pTHX_ SV *self) { |
| 67 |
|
| /* |
| 68 |
|
| # uncoverable branch true |
| 69 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 70 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 71 |
1016 |
▶ | if (!SvROK(self) || !SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
1016 |
0 |
1016 |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
0 |
1016 |
1016 |
|
| 72 |
0 |
| croak("Punk::Stream: not a stream"); /* # uncoverable statement */ |
| 73 |
1016 |
| return (punk_stream *)INT2PTR(void *, SvIV(SvRV(self))); |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| static void pst_teardown(pTHX_ punk_stream *st); |
| 77 |
|
| static void pst_on_writable(pTHX_ int fd, int mask, void *ud); |
| 78 |
|
| static void pst_on_hdrain(pTHX_ void *h, void *ud); |
| 79 |
|
| static void pst_on_habort(pTHX_ void *h, void *ud); |
| 80 |
|
| |
| 81 |
|
| /* settle the pending drain future, if any. ok is 1 (drained) or 0 (closed |
| 82 |
|
| * first). The local copy matters: settling fires reactions that can call back |
| 83 |
|
| * into this stream and ask for another drain. */ |
| 84 |
741 |
| static void pst_settle_drain(pTHX_ punk_stream *st, int ok) { |
| 85 |
741 |
| SV *f = st->drain_f; |
| 86 |
|
| AV *vals; |
| 87 |
741 |
▶ | if (!f) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) return; |
740 |
1 |
741 |
|
| 88 |
1 |
| st->drain_f = NULL; |
| 89 |
1 |
| vals = newAV(); |
| 90 |
1 |
| av_push(vals, newSViv(ok)); |
| 91 |
1 |
| pf_settle(aTHX_ pf_of(aTHX_ f), f, PF_DONE, vals); |
| 92 |
1 |
| SvREFCNT_dec(f); |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /* ---- writing (DETACH / BLOCK): the punk_sse.h flush, plus two endings ------ */ |
| 96 |
|
| |
| 97 |
716 |
| static void pst_flush(pTHX_ punk_stream *st) { |
| 98 |
1421 |
▶ | while (st->woff < st->wlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (st->woff < st->wlen) { |
708 |
713 |
1421 |
|
| 99 |
708 |
| ssize_t n = write(st->fd, st->wbuf + st->woff, st->wlen - st->woff); |
| 100 |
708 |
▶ | if (n > 0) { st->woff += (size_t)n; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { st->woff += (size_t)n; continue; } |
705 |
3 |
708 |
|
| 101 |
|
| /* |
| 102 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 103 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 104 |
3 |
▶ | if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
3 |
0 |
3 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
1 |
2 |
3 |
| if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
0 |
1 |
1 |
|
| 105 |
|
| /* |
| 106 |
|
| # uncoverable branch false |
| 107 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 108 |
2 |
▶ | if (st->mode == PST_MODE_DETACH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->mode == PST_MODE_DETACH) { |
2 |
0 |
2 |
|
| 109 |
|
| /* |
| 110 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 111 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 112 |
2 |
▶ | if (!st->writing && st->abi && st->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!st->writing && st->abi && st->loop) { |
1 |
1 |
2 |
| if (!st->writing && st->abi && st->loop) { |
1 |
0 |
1 |
| if (!st->writing && st->abi && st->loop) { |
1 |
0 |
1 |
|
| 113 |
1 |
| st->abi->io_watch(aTHX_ st->loop, st->fd, HM_ABI_WRITE, |
| 114 |
|
| pst_on_writable, st); |
| 115 |
1 |
| st->writing = 1; |
| 116 |
|
| } |
| 117 |
2 |
| return; |
| 118 |
|
| } |
| 119 |
|
| /* |
| 120 |
|
| # uncoverable statement */ |
| 121 |
0 |
| { /* blocking transport over a socket the server left |
| 122 |
|
| * non-blocking: wait for writability rather than spinning. |
| 123 |
|
| * SSE's writes were small enough never to meet this; an |
| 124 |
|
| * export is not. */ |
| 125 |
|
| fd_set wf; |
| 126 |
0 |
▶ | FD_ZERO(&wf); /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| FD_ZERO(&wf); /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 127 |
0 |
| FD_SET(st->fd, &wf); /* # uncoverable statement */ |
| 128 |
0 |
| (void)select(st->fd + 1, NULL, &wf, NULL, NULL); /* # uncoverable statement */ |
| 129 |
0 |
| continue; /* # uncoverable statement */ |
| 130 |
|
| } |
| 131 |
|
| } |
| 132 |
|
| /* |
| 133 |
|
| # uncoverable branch true |
| 134 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 135 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 136 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 137 |
1 |
▶ | if (n < 0 && errno == EINTR) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 && errno == EINTR) continue; |
1 |
0 |
1 |
| if (n < 0 && errno == EINTR) continue; |
0 |
1 |
1 |
|
| 138 |
1 |
| pst_teardown(aTHX_ st); /* write error: client gone */ |
| 139 |
1 |
| return; |
| 140 |
|
| } |
| 141 |
713 |
| st->wlen = st->woff = 0; |
| 142 |
|
| /* |
| 143 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 144 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 145 |
713 |
▶ | if (st->writing && st->abi && st->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->writing && st->abi && st->loop) { |
1 |
712 |
713 |
| if (st->writing && st->abi && st->loop) { |
1 |
0 |
1 |
| if (st->writing && st->abi && st->loop) { |
1 |
0 |
1 |
|
| 146 |
1 |
| st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); |
| 147 |
1 |
| st->writing = 0; |
| 148 |
|
| } |
| 149 |
713 |
| pst_settle_drain(aTHX_ st, 1); |
| 150 |
713 |
▶ | if (st->state == PST_CLOSING) pst_teardown(aTHX_ st); /* fully sent */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state == PST_CLOSING) pst_teardown(aTHX_ st); /* fully sent */ |
18 |
695 |
713 |
|
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
1 |
| static void pst_on_writable_in(pTHX_ int fd, int mask, void *ud) { |
| 154 |
|
| PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask); |
| 155 |
1 |
| pst_flush(aTHX_ (punk_stream *)ud); |
| 156 |
1 |
| } |
| 157 |
|
| |
| 158 |
1 |
| static void pst_on_writable(pTHX_ int fd, int mask, void *ud) { |
| 159 |
1 |
| PK_LOOP_ENTER; |
| 160 |
1 |
| pst_on_writable_in(aTHX_ fd, mask, ud); |
| 161 |
1 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
1 |
1 |
|
| 162 |
1 |
| } |
| 163 |
|
| |
| 164 |
|
| /* ---- the stream handle's two callbacks ------------------------------------- */ |
| 165 |
|
| |
| 166 |
|
| /* Output that was over the high-water mark has gone out. This is what makes |
| 167 |
|
| * $w->drain mean the same thing on the handle as on the detach transport: |
| 168 |
|
| * settled with 1 when everything written so far has actually left. */ |
| 169 |
0 |
| static void pst_on_hdrain_in(pTHX_ void *h, void *ud) { |
| 170 |
0 |
| punk_stream *st = (punk_stream *)ud; |
| 171 |
|
| PERL_UNUSED_ARG(h); |
| 172 |
0 |
| st->full = 0; |
| 173 |
0 |
| pst_settle_drain(aTHX_ st, 1); |
| 174 |
0 |
| } |
| 175 |
|
| |
| 176 |
0 |
| static void pst_on_hdrain(pTHX_ void *h, void *ud) { |
| 177 |
0 |
| PK_LOOP_ENTER; |
| 178 |
0 |
| pst_on_hdrain_in(aTHX_ h, ud); |
| 179 |
0 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
0 |
0 |
|
| 180 |
0 |
| } |
| 181 |
|
| |
| 182 |
|
| /* The stream died before it was closed - an HTTP/2 RST_STREAM on this one |
| 183 |
|
| * stream of many, or the connection going away. Detach had no way to report |
| 184 |
|
| * the first of those, because on HTTP/1 a reset stream IS a dead connection; |
| 185 |
|
| * on a multiplexed transport it is not, and a producer that is not told goes |
| 186 |
|
| * on generating a body for nobody. Teardown settles the drain future with 0, |
| 187 |
|
| * so a handler awaiting it stops rather than blocking for ever. */ |
| 188 |
|
| /* |
| 189 |
|
| # uncoverable statement |
| 190 |
|
| # uncoverable subroutine */ |
| 191 |
0 |
| static void pst_on_habort_in(pTHX_ void *h, void *ud) { |
| 192 |
0 |
| punk_stream *st = (punk_stream *)ud; /* # uncoverable statement */ |
| 193 |
|
| PERL_UNUSED_ARG(h); |
| 194 |
0 |
| pst_teardown(aTHX_ st); /* # uncoverable statement */ |
| 195 |
0 |
| } |
| 196 |
|
| |
| 197 |
|
| /* |
| 198 |
|
| # uncoverable statement |
| 199 |
|
| # uncoverable subroutine */ |
| 200 |
0 |
| static void pst_on_habort(pTHX_ void *h, void *ud) { |
| 201 |
0 |
| PK_LOOP_ENTER; /* # uncoverable statement */ |
| 202 |
0 |
| pst_on_habort_in(aTHX_ h, ud); /* # uncoverable statement */ |
| 203 |
0 |
▶ | PK_LOOP_LEAVE; /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 204 |
0 |
| } |
| 205 |
|
| |
| 206 |
|
| /* the client closing early is the only thing we read for */ |
| 207 |
|
| /* |
| 208 |
|
| # uncoverable statement |
| 209 |
|
| # uncoverable subroutine */ |
| 210 |
0 |
| static void pst_on_readable_in(pTHX_ int fd, int mask, void *ud) { |
| 211 |
0 |
| punk_stream *st = (punk_stream *)ud; /* # uncoverable statement */ |
| 212 |
|
| char scratch[512]; |
| 213 |
|
| ssize_t n; |
| 214 |
|
| PERL_UNUSED_ARG(fd); PERL_UNUSED_ARG(mask); |
| 215 |
0 |
| n = read(st->fd, scratch, sizeof scratch); /* # uncoverable statement */ |
| 216 |
|
| /* |
| 217 |
|
| # uncoverable statement |
| 218 |
|
| # uncoverable branch true |
| 219 |
|
| # uncoverable branch false |
| 220 |
|
| # uncoverable condition left count:1 |
| 221 |
|
| # uncoverable condition right count:1 */ |
| 222 |
0 |
▶ | if (n == 0) { pst_teardown(aTHX_ st); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { pst_teardown(aTHX_ st); return; } |
0 |
0 |
0 |
|
| 223 |
|
| /* |
| 224 |
|
| # uncoverable statement |
| 225 |
|
| # uncoverable branch true |
| 226 |
|
| # uncoverable branch false |
| 227 |
|
| # uncoverable condition left count:1 |
| 228 |
|
| # uncoverable condition right count:1 |
| 229 |
|
| # uncoverable condition left count:2 |
| 230 |
|
| # uncoverable condition right count:2 |
| 231 |
|
| # uncoverable condition left count:3 |
| 232 |
|
| # uncoverable condition right count:3 |
| 233 |
|
| # uncoverable condition left count:4 |
| 234 |
|
| # uncoverable condition right count:4 */ |
| 235 |
0 |
▶ | if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 236 |
0 |
| return; /* # uncoverable statement */ |
| 237 |
|
| /* |
| 238 |
|
| # uncoverable statement |
| 239 |
|
| # uncoverable branch true |
| 240 |
|
| # uncoverable branch false |
| 241 |
|
| # uncoverable condition left count:1 |
| 242 |
|
| # uncoverable condition right count:1 */ |
| 243 |
0 |
▶ | if (n < 0) pst_teardown(aTHX_ st); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) pst_teardown(aTHX_ st); |
0 |
0 |
0 |
|
| 244 |
|
| /* n > 0: bytes on a response stream are the client's mistake; ignored */ |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| /* |
| 248 |
|
| # uncoverable statement |
| 249 |
|
| # uncoverable subroutine */ |
| 250 |
0 |
| static void pst_on_readable(pTHX_ int fd, int mask, void *ud) { |
| 251 |
0 |
| PK_LOOP_ENTER; /* # uncoverable statement */ |
| 252 |
0 |
| pst_on_readable_in(aTHX_ fd, mask, ud); /* # uncoverable statement */ |
| 253 |
0 |
▶ | PK_LOOP_LEAVE; /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 254 |
0 |
| } |
| 255 |
|
| |
| 256 |
|
| /* queue raw bytes (already framed) to the socket transports */ |
| 257 |
40 |
| static void pst_write_raw(pTHX_ punk_stream *st, const char *bytes, size_t len) { |
| 258 |
|
| /* |
| 259 |
|
| # uncoverable branch true |
| 260 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 261 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 262 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 263 |
40 |
▶ | if (st->write_buffer_limit |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->write_buffer_limit |
0 |
40 |
40 |
|
| 264 |
0 |
▶ | && st->wlen - st->woff + len > st->write_buffer_limit) { /* # uncoverable statement class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| && st->wlen - st->woff + len > st->write_buffer_limit) { /* # uncoverable statement class:ignore_covered_err */ |
0 |
0 |
0 |
|
| 265 |
0 |
| pst_teardown(aTHX_ st); /* a client that will not read */ /* # uncoverable statement */ |
| 266 |
0 |
| return; /* # uncoverable statement */ |
| 267 |
|
| } |
| 268 |
40 |
| pw_append(&st->wbuf, &st->wlen, &st->wcap, bytes, len); |
| 269 |
40 |
| pst_flush(aTHX_ st); |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
|
| /* one body chunk, framed for the transport */ |
| 273 |
690 |
| static void pst_write_body(pTHX_ punk_stream *st, const char *bytes, size_t len) { |
| 274 |
690 |
▶ | if (st->state != PST_OPEN || len == 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->state != PST_OPEN || len == 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
687 |
3 |
690 |
| if (st->state != PST_OPEN || len == 0) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
687 |
687 |
|
| 275 |
687 |
▶ | if (st->mode == PST_MODE_HSTREAM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->mode == PST_MODE_HSTREAM) { |
0 |
687 |
687 |
|
| 276 |
|
| /* Unframed: the transport frames it. HTTP/2 puts the bytes in a DATA |
| 277 |
|
| * frame and HTTP/1.1 delimits by close, so a chunk header written |
| 278 |
|
| * here would be body content on one and a lie on the other. |
| 279 |
|
| * |
| 280 |
|
| * The reference is held across the call because ending the body can |
| 281 |
|
| * close the connection, and Hyperman fires the abort callback from |
| 282 |
|
| * there - which tears this stream down and drops its own last |
| 283 |
|
| * reference. */ |
| 284 |
0 |
▶ | SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
0 |
0 |
0 |
|
| 285 |
0 |
| int r = st->abi->stream_write(aTHX_ st->sh, bytes, len); |
| 286 |
|
| /* |
| 287 |
|
| # uncoverable branch true |
| 288 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 289 |
0 |
▶ | if (r < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r < 0) { |
0 |
0 |
0 |
|
| 290 |
|
| /* |
| 291 |
|
| # uncoverable statement |
| 292 |
|
| # uncoverable branch true |
| 293 |
|
| # uncoverable branch false |
| 294 |
|
| # uncoverable condition left count:1 |
| 295 |
|
| # uncoverable condition right count:1 */ |
| 296 |
0 |
▶ | if (st->state == PST_OPEN) pst_teardown(aTHX_ st); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state == PST_OPEN) pst_teardown(aTHX_ st); |
0 |
0 |
0 |
|
| 297 |
|
| } |
| 298 |
0 |
▶ | else if (r == HM_ABI_STREAM_FULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (r == HM_ABI_STREAM_FULL) { |
0 |
0 |
0 |
|
| 299 |
|
| /* over the high-water mark: on_drain settles the drain future, |
| 300 |
|
| * which is how $w->drain becomes real backpressure here */ |
| 301 |
0 |
| st->full = 1; |
| 302 |
0 |
| (void)st->abi->stream_on_drain(aTHX_ st->sh, pst_on_hdrain, st); |
| 303 |
|
| } |
| 304 |
|
| else { /* all of it is away */ |
| 305 |
0 |
| st->full = 0; |
| 306 |
0 |
| pst_settle_drain(aTHX_ st, 1); |
| 307 |
|
| } |
| 308 |
|
| /* |
| 309 |
|
| # uncoverable branch false |
| 310 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 311 |
0 |
▶ | if (keep) SvREFCNT_dec(keep); /* st may be gone now */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* st may be gone now */ |
0 |
0 |
0 |
|
| 312 |
0 |
| return; |
| 313 |
|
| } |
| 314 |
687 |
▶ | if (st->mode == PST_MODE_STREAM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->mode == PST_MODE_STREAM) { |
12 |
675 |
687 |
|
| 315 |
12 |
| dSP; |
| 316 |
12 |
| ENTER; SAVETMPS; |
| 317 |
12 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
12 |
12 |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
12 |
12 |
|
| 318 |
12 |
▶ | PUSHs(st->writer ? st->writer : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(st->writer ? st->writer : &PL_sv_undef); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
12 |
0 |
12 |
|
| 319 |
12 |
| PUSHs(sv_2mortal(newSVpvn(bytes, len))); |
| 320 |
12 |
| PUTBACK; |
| 321 |
12 |
| call_method("write", G_DISCARD | G_EVAL); |
| 322 |
12 |
| SPAGAIN; |
| 323 |
|
| /* |
| 324 |
|
| # uncoverable branch true |
| 325 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 326 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 327 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 328 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 329 |
12 |
▶ | if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } |
12 |
0 |
12 |
| if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } |
0 |
12 |
12 |
| if (SvTRUE(ERRSV)) { FREETMPS; LEAVE; pst_teardown(aTHX_ st); return; } |
0 |
0 |
0 |
|
| 330 |
12 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
12 |
0 |
12 |
|
| 331 |
12 |
| return; |
| 332 |
|
| } |
| 333 |
675 |
▶ | if (st->chunked) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->chunked) { |
657 |
18 |
675 |
|
| 334 |
|
| char pre[24]; |
| 335 |
|
| /* the size goes through its own variable: where perl expands |
| 336 |
|
| * my_snprintf to a gcc statement expression, that expression declares |
| 337 |
|
| * its own `len' from the snprintf return, and a `len' written in the |
| 338 |
|
| * argument list resolves to that one - still uninitialised inside its |
| 339 |
|
| * own initialiser. Every chunk header then reads the stack. */ |
| 340 |
657 |
| UV chunk_len = (UV)len; |
| 341 |
657 |
| int n = my_snprintf(pre, sizeof pre, "%" UVxf "\r\n", chunk_len); |
| 342 |
|
| /* |
| 343 |
|
| # uncoverable branch true |
| 344 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 345 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 346 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 347 |
657 |
▶ | if (st->write_buffer_limit |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->write_buffer_limit |
0 |
657 |
657 |
|
| 348 |
0 |
▶ | && st->wlen - st->woff + len + (size_t)n + 2 > st->write_buffer_limit) { /* # uncoverable statement class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| && st->wlen - st->woff + len + (size_t)n + 2 > st->write_buffer_limit) { /* # uncoverable statement class:ignore_covered_err */ |
0 |
0 |
0 |
|
| 349 |
0 |
| pst_teardown(aTHX_ st); /* # uncoverable statement */ |
| 350 |
0 |
| return; /* # uncoverable statement */ |
| 351 |
|
| } |
| 352 |
657 |
| pw_append(&st->wbuf, &st->wlen, &st->wcap, pre, (size_t)n); |
| 353 |
657 |
| pw_append(&st->wbuf, &st->wlen, &st->wcap, bytes, len); |
| 354 |
657 |
| pw_append(&st->wbuf, &st->wlen, &st->wcap, "\r\n", 2); |
| 355 |
657 |
| pst_flush(aTHX_ st); |
| 356 |
657 |
| return; |
| 357 |
|
| } |
| 358 |
18 |
| pst_write_raw(aTHX_ st, bytes, len); |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
|
| /* ---- teardown and the two closes ------------------------------------------- */ |
| 362 |
|
| |
| 363 |
|
| /* the hard stop: nothing more is sent, so on a chunked transport the client |
| 364 |
|
| * sees a missing terminal chunk - truncation, not a short success. |
| 365 |
|
| * |
| 366 |
|
| * The stream handle says the same thing through stream_abort (hm_abi.h v7), |
| 367 |
|
| * which is why `ending_clean` exists: pst_close sets it and every other way |
| 368 |
|
| * of getting here leaves it clear. A streamed body has no declared length, so |
| 369 |
|
| * ending cleanly IS the claim that it is whole - the two endings are the only |
| 370 |
|
| * way a client can tell an export that finished from one whose query died on |
| 371 |
|
| * the last page. */ |
| 372 |
28 |
| static void pst_teardown(pTHX_ punk_stream *st) { |
| 373 |
|
| /* |
| 374 |
|
| # uncoverable branch true |
| 375 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 376 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 377 |
28 |
▶ | if (st->in_teardown || st->state == PST_CLOSED) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->in_teardown || st->state == PST_CLOSED) return; |
28 |
0 |
28 |
| if (st->in_teardown || st->state == PST_CLOSED) return; |
0 |
28 |
28 |
|
| 378 |
28 |
| st->in_teardown = 1; |
| 379 |
|
| /* |
| 380 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 381 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 382 |
28 |
▶ | if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
26 |
28 |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
0 |
2 |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
0 |
2 |
|
| 383 |
|
| /* |
| 384 |
|
| # uncoverable branch false |
| 385 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 386 |
2 |
▶ | if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); |
2 |
0 |
2 |
|
| 387 |
|
| /* |
| 388 |
|
| # uncoverable branch true |
| 389 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 390 |
2 |
▶ | if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); |
0 |
2 |
2 |
|
| 391 |
|
| } |
| 392 |
|
| /* |
| 393 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 394 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 395 |
28 |
▶ | if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) { |
0 |
28 |
28 |
| 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 |
|
| 396 |
|
| /* NULLed first: ending the body can close the connection, and the |
| 397 |
|
| * teardown that re-enters from there must not end it twice */ |
| 398 |
0 |
| void *h = st->sh; |
| 399 |
0 |
| st->sh = NULL; |
| 400 |
0 |
▶ | if (st->ending_clean) (void)st->abi->stream_close(aTHX_ h); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->ending_clean) (void)st->abi->stream_close(aTHX_ h); |
0 |
0 |
0 |
|
| 401 |
0 |
| else (void)st->abi->stream_abort(aTHX_ h); |
| 402 |
|
| } |
| 403 |
28 |
| st->reading = st->writing = 0; |
| 404 |
28 |
▶ | if (st->mode == PST_MODE_STREAM && st->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_STREAM && st->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
6 |
22 |
28 |
| if (st->mode == PST_MODE_STREAM && st->writer) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
6 |
0 |
6 |
|
| 405 |
6 |
| dSP; |
| 406 |
6 |
| ENTER; SAVETMPS; |
| 407 |
6 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK; |
0 |
6 |
6 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(st->writer); PUTBACK; |
0 |
6 |
6 |
|
| 408 |
6 |
| call_method("close", G_DISCARD | G_EVAL); |
| 409 |
6 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 410 |
|
| } |
| 411 |
28 |
▶ | if (st->fd >= 0) { close(st->fd); st->fd = -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fd >= 0) { close(st->fd); st->fd = -1; } |
22 |
6 |
28 |
|
| 412 |
28 |
| st->state = PST_CLOSED; |
| 413 |
28 |
| pst_settle_drain(aTHX_ st, 0); |
| 414 |
28 |
| st->in_teardown = 0; |
| 415 |
|
| /* |
| 416 |
|
| # uncoverable branch false |
| 417 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 418 |
28 |
▶ | if (st->self_rv) { SV *s = st->self_rv; st->self_rv = NULL; SvREFCNT_dec(s); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->self_rv) { SV *s = st->self_rv; st->self_rv = NULL; SvREFCNT_dec(s); } |
28 |
0 |
28 |
|
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
|
| /* the clean end: terminal chunk, then teardown once every byte is out. On the |
| 422 |
|
| * detach transport a still-loaded buffer keeps draining on the loop (the |
| 423 |
|
| * stream holds itself alive until it has); the blocking transports flush here. */ |
| 424 |
28 |
| static void pst_close(pTHX_ punk_stream *st) { |
| 425 |
28 |
▶ | if (st->state != PST_OPEN) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state != PST_OPEN) return; |
4 |
24 |
28 |
|
| 426 |
24 |
▶ | if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) { |
18 |
6 |
24 |
| if (st->mode == PST_MODE_STREAM || st->mode == PST_MODE_HSTREAM) { |
0 |
18 |
18 |
|
| 427 |
|
| /* teardown does the ending; this is the one caller that gets to call |
| 428 |
|
| * it a clean one, and there is no half-sent buffer of ours to drain */ |
| 429 |
6 |
| st->ending_clean = 1; |
| 430 |
6 |
| pst_teardown(aTHX_ st); |
| 431 |
6 |
| return; |
| 432 |
|
| } |
| 433 |
18 |
| st->state = PST_CLOSING; |
| 434 |
18 |
▶ | if (st->chunked) |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->chunked) |
9 |
9 |
18 |
|
| 435 |
9 |
| pw_append(&st->wbuf, &st->wlen, &st->wcap, "0\r\n\r\n", 5); |
| 436 |
18 |
| pst_flush(aTHX_ st); /* teardown fires from here once drained */ |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
28 |
| static void pst_free(pTHX_ punk_stream *st) { |
| 440 |
|
| /* |
| 441 |
|
| # uncoverable branch true |
| 442 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 443 |
28 |
▶ | if (!st) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!st) return; |
0 |
28 |
28 |
|
| 444 |
|
| /* |
| 445 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 446 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 447 |
28 |
▶ | if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
26 |
28 |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
0 |
2 |
| if (st->mode == PST_MODE_DETACH && st->abi && st->loop) { |
2 |
0 |
2 |
|
| 448 |
|
| /* watchers name this struct, so they go before it - and before the |
| 449 |
|
| * fd, or the loop watches a number the next connection gets */ |
| 450 |
|
| /* |
| 451 |
|
| # uncoverable branch true |
| 452 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 453 |
2 |
▶ | if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->reading) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_READ); |
0 |
2 |
2 |
|
| 454 |
|
| /* |
| 455 |
|
| # uncoverable branch true |
| 456 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 457 |
2 |
▶ | if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->writing) st->abi->io_unwatch(aTHX_ st->loop, st->fd, HM_ABI_WRITE); |
0 |
2 |
2 |
|
| 458 |
|
| } |
| 459 |
|
| /* |
| 460 |
|
| # uncoverable branch true |
| 461 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 462 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 463 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 464 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 465 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 466 |
28 |
▶ | if (st->defer_tw && st->abi && st->loop) |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->defer_tw && st->abi && st->loop) |
0 |
28 |
28 |
| if (st->defer_tw && st->abi && st->loop) |
0 |
0 |
0 |
| if (st->defer_tw && st->abi && st->loop) |
0 |
0 |
0 |
|
| 467 |
0 |
| st->abi->timer_cancel(aTHX_ st->loop, st->defer_tw); /* # uncoverable statement */ |
| 468 |
|
| /* The handle's callbacks name this struct, so it goes before the struct |
| 469 |
|
| * does. Reaching here with one still open means nobody ended the body on |
| 470 |
|
| * purpose, so it is aborted rather than closed: an ending nobody asked |
| 471 |
|
| * for is not a claim that the response is whole. */ |
| 472 |
|
| /* |
| 473 |
|
| # uncoverable branch true |
| 474 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 475 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 476 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 477 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 478 |
28 |
▶ | if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->mode == PST_MODE_HSTREAM && st->abi && st->sh) |
0 |
28 |
28 |
| 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 |
|
| 479 |
0 |
| (void)st->abi->stream_abort(aTHX_ st->sh); /* # uncoverable statement */ |
| 480 |
|
| /* |
| 481 |
|
| # uncoverable branch true |
| 482 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 483 |
28 |
▶ | if (st->fd >= 0) close(st->fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fd >= 0) close(st->fd); |
0 |
28 |
28 |
|
| 484 |
28 |
▶ | if (st->wbuf) free(st->wbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->wbuf) free(st->wbuf); |
22 |
6 |
28 |
|
| 485 |
28 |
▶ | if (st->writer) SvREFCNT_dec(st->writer); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->writer) SvREFCNT_dec(st->writer); |
6 |
22 |
28 |
|
| 486 |
|
| /* |
| 487 |
|
| # uncoverable branch true |
| 488 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 489 |
28 |
▶ | if (st->drain_f) SvREFCNT_dec(st->drain_f); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->drain_f) SvREFCNT_dec(st->drain_f); |
0 |
28 |
28 |
|
| 490 |
28 |
▶ | if (st->reqid) SvREFCNT_dec(st->reqid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->reqid) SvREFCNT_dec(st->reqid); |
3 |
25 |
28 |
|
| 491 |
|
| /* |
| 492 |
|
| # uncoverable branch true |
| 493 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 494 |
28 |
▶ | if (st->defer_c) SvREFCNT_dec(st->defer_c); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->defer_c) SvREFCNT_dec(st->defer_c); |
0 |
28 |
28 |
|
| 495 |
|
| /* |
| 496 |
|
| # uncoverable branch true |
| 497 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 498 |
28 |
▶ | if (st->defer_code) SvREFCNT_dec(st->defer_code); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->defer_code) SvREFCNT_dec(st->defer_code); |
0 |
28 |
28 |
|
| 499 |
|
| /* |
| 500 |
|
| # uncoverable branch true |
| 501 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 502 |
28 |
▶ | if (st->defer_head) SvREFCNT_dec(st->defer_head); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->defer_head) SvREFCNT_dec(st->defer_head); |
0 |
28 |
28 |
|
| 503 |
|
| /* |
| 504 |
|
| # uncoverable branch true |
| 505 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 506 |
28 |
▶ | if (st->self_rv) SvREFCNT_dec(st->self_rv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->self_rv) SvREFCNT_dec(st->self_rv); |
0 |
28 |
28 |
|
| 507 |
28 |
| Safefree(st); |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| /* ---- construction, the head, the handler ---------------------------------- */ |
| 511 |
|
| |
| 512 |
28 |
| static punk_stream *pst_new(pTHX_ int mode, HV *opts) { |
| 513 |
|
| punk_stream *st; |
| 514 |
28 |
| Newxz(st, 1, punk_stream); |
| 515 |
28 |
| st->fd = -1; |
| 516 |
28 |
| st->mode = mode; |
| 517 |
28 |
| st->state = PST_OPEN; |
| 518 |
28 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
23 |
5 |
28 |
|
| 519 |
23 |
| SV **w = hv_fetchs(opts, "write_buffer_limit", 0); |
| 520 |
|
| /* |
| 521 |
|
| # uncoverable branch true |
| 522 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 523 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 524 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 525 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 526 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 527 |
23 |
▶ | if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w); |
Branch: 3 branches
| Branch | True | False | Total |
| if (w && *w && SvOK(*w)) st->write_buffer_limit = (size_t)SvUV(*w); |
0 |
23 |
23 |
| 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 |
|
| 528 |
|
| } |
| 529 |
28 |
| return st; |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
32 |
| static IV pst_opt_status(pTHX_ HV *opts) { |
| 533 |
32 |
▶ | SV **s = opts ? hv_fetchs(opts, "status", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **s = opts ? hv_fetchs(opts, "status", 0) : NULL; |
27 |
5 |
32 |
|
| 534 |
|
| /* |
| 535 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 536 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 537 |
32 |
▶ | return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; |
Branch: 3 branches
| Branch | True | False | Total |
| return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; |
7 |
25 |
32 |
| return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; |
7 |
0 |
7 |
| return (s && *s && SvOK(*s)) ? SvIV(*s) : 200; |
7 |
0 |
7 |
|
| 538 |
|
| } |
| 539 |
32 |
| static AV *pst_opt_headers(pTHX_ HV *opts) { |
| 540 |
32 |
▶ | SV **h = opts ? hv_fetchs(opts, "headers", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **h = opts ? hv_fetchs(opts, "headers", 0) : NULL; |
27 |
5 |
32 |
|
| 541 |
7 |
▶ | return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) |
Branch: 3 branches
| Branch | True | False | Total |
| return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) |
7 |
0 |
7 |
| return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) |
7 |
0 |
7 |
| return (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) |
7 |
0 |
7 |
|
| 542 |
39 |
▶ | ? (AV *)SvRV(*h) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(*h) : NULL; |
7 |
25 |
32 |
|
| 543 |
|
| } |
| 544 |
|
| |
| 545 |
|
| /* an empty reason-phrase is valid HTTP; 200 keeps its customary one */ |
| 546 |
22 |
| static const char *pst_phrase(IV status) { |
| 547 |
22 |
▶ | return status == 200 ? "OK" : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| return status == 200 ? "OK" : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
22 |
0 |
22 |
|
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
|
| /* the raw response head for the socket transports */ |
| 551 |
22 |
| static SV *pst_head(pTHX_ punk_stream *st, SV *ct, HV *opts) { |
| 552 |
22 |
| IV status = pst_opt_status(aTHX_ opts); |
| 553 |
22 |
| AV *extra = pst_opt_headers(aTHX_ opts); |
| 554 |
22 |
| SV *head = sv_2mortal(newSVpvf("HTTP/1.1 %" IVdf " %s\r\nContent-Type: ", |
| 555 |
|
| status, pst_phrase(status))); |
| 556 |
22 |
| sv_catsv(head, ct); |
| 557 |
22 |
| sv_catpvs(head, "\r\n"); |
| 558 |
|
| /* |
| 559 |
|
| # uncoverable branch true |
| 560 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 561 |
22 |
▶ | if (extra) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (extra) { |
0 |
22 |
22 |
|
| 562 |
0 |
| SSize_t i, n = av_len(extra) + 1; /* # uncoverable statement */ |
| 563 |
|
| /* |
| 564 |
|
| # uncoverable statement |
| 565 |
|
| # uncoverable branch true |
| 566 |
|
| # uncoverable branch false |
| 567 |
|
| # uncoverable condition left count:1 |
| 568 |
|
| # uncoverable condition right count:1 */ |
| 569 |
0 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
0 |
0 |
0 |
|
| 570 |
0 |
| SV **k = av_fetch(extra, i, 0); /* # uncoverable statement */ |
| 571 |
0 |
| SV **v = av_fetch(extra, i + 1, 0); /* # uncoverable statement */ |
| 572 |
|
| /* |
| 573 |
|
| # uncoverable statement |
| 574 |
|
| # uncoverable branch true |
| 575 |
|
| # uncoverable branch false |
| 576 |
|
| # uncoverable condition left count:1 |
| 577 |
|
| # uncoverable condition right count:1 |
| 578 |
|
| # uncoverable condition left count:2 |
| 579 |
|
| # uncoverable condition right count:2 |
| 580 |
|
| # uncoverable condition left count:3 |
| 581 |
|
| # uncoverable condition right count:3 |
| 582 |
|
| # uncoverable condition left count:4 |
| 583 |
|
| # uncoverable condition right count:4 */ |
| 584 |
0 |
▶ | if (!(k && *k && v && *v)) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 585 |
0 |
| sv_catsv(head, *k); /* # uncoverable statement */ |
| 586 |
0 |
| sv_catpvs(head, ": "); /* # uncoverable statement */ |
| 587 |
0 |
| sv_catsv(head, *v); /* # uncoverable statement */ |
| 588 |
0 |
| sv_catpvs(head, "\r\n"); /* # uncoverable statement */ |
| 589 |
|
| } |
| 590 |
|
| } |
| 591 |
22 |
▶ | if (st->chunked) sv_catpvs(head, "Transfer-Encoding: chunked\r\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->chunked) sv_catpvs(head, "Transfer-Encoding: chunked\r\n"); |
13 |
9 |
22 |
|
| 592 |
22 |
| sv_catpvs(head, "Connection: close\r\nX-Accel-Buffering: no\r\n\r\n"); |
| 593 |
22 |
| return head; |
| 594 |
|
| } |
| 595 |
|
| |
| 596 |
|
| /* Run $code->($c, $w). The callback returning is the clean end; a die is the |
| 597 |
|
| * hard one - the head is already on the wire, so there is no error response |
| 598 |
|
| * to send, and on a chunked transport the missing terminal chunk is the |
| 599 |
|
| * client's evidence. The report carries the request id when one exists. */ |
| 600 |
28 |
| static void pst_run_handler(pTHX_ SV *code, SV *c, SV *self) { |
| 601 |
28 |
| dSP; int died; |
| 602 |
|
| punk_stream *st; |
| 603 |
28 |
| ENTER; SAVETMPS; |
| 604 |
28 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; |
0 |
28 |
28 |
| PUSHMARK(SP); EXTEND(SP, 2); PUSHs(c); PUSHs(self); PUTBACK; |
0 |
28 |
28 |
|
| 605 |
28 |
| call_sv(code, G_DISCARD | G_EVAL); |
| 606 |
28 |
| SPAGAIN; |
| 607 |
28 |
▶ | died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
28 |
0 |
28 |
|
| 608 |
28 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
28 |
28 |
|
| 609 |
28 |
| st = pst_of(aTHX_ self); |
| 610 |
28 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
3 |
25 |
28 |
|
| 611 |
3 |
▶ | STRLEN el; const char *ep = SvPV_const(ERRSV, el); |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN el; const char *ep = SvPV_const(ERRSV, el); |
3 |
0 |
3 |
|
| 612 |
3 |
▶ | SV *plain = pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)); /* one line */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *plain = pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)); /* one line */ |
3 |
0 |
3 |
|
| 613 |
3 |
▶ | if (st->reqid) |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->reqid) |
3 |
0 |
3 |
|
| 614 |
3 |
| warn("Punk::Stream: handler died (request %" SVf "): %" SVf, |
| 615 |
|
| SVfARG(st->reqid), SVfARG(plain)); |
| 616 |
|
| else |
| 617 |
0 |
| warn("Punk::Stream: handler died: %" SVf, SVfARG(plain)); |
| 618 |
3 |
| pst_teardown(aTHX_ st); |
| 619 |
3 |
| return; |
| 620 |
|
| } |
| 621 |
25 |
| pst_close(aTHX_ st); |
| 622 |
|
| } |
| 623 |
|
| |
| 624 |
|
| /* The deferred half of the detach transport. Hyperman's detach is two-phase: |
| 625 |
|
| * hm_detach disarms the server's watchers at once, but the connection slot is |
| 626 |
|
| * only released after the app frame returns its sentinel - and the event |
| 627 |
|
| * dispatch routes an fd to the application's watchers only once that slot is |
| 628 |
|
| * empty. A handler that produced the whole body synchronously would arm its |
| 629 |
|
| * write watcher against a slot still naming the server's connection, and the |
| 630 |
|
| * first EAGAIN would stall the stream forever. So the detach transport starts |
| 631 |
|
| * on the next loop tick, after the sentinel has made it home. |
| 632 |
|
| * |
| 633 |
|
| * The self-reference is held across the run: the handler can tear the stream |
| 634 |
|
| * down (client gone), and teardown drops the stream's own reference - the |
| 635 |
|
| * last one, here. The SSE heartbeat learned this the hard way. */ |
| 636 |
2 |
| static void pst_start_cb_in(pTHX_ void *ud) { |
| 637 |
2 |
| punk_stream *st = (punk_stream *)ud; |
| 638 |
2 |
▶ | SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *keep = st->self_rv ? SvREFCNT_inc(st->self_rv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2 |
0 |
2 |
|
| 639 |
2 |
| SV *c = st->defer_c, *code = st->defer_code, *head = st->defer_head; |
| 640 |
2 |
| st->defer_tw = NULL; /* it fired; nothing to cancel */ |
| 641 |
2 |
| st->defer_c = st->defer_code = st->defer_head = NULL; |
| 642 |
|
| /* |
| 643 |
|
| # uncoverable branch false |
| 644 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 645 |
2 |
▶ | if (st->state == PST_OPEN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state == PST_OPEN) { |
2 |
0 |
2 |
|
| 646 |
2 |
| pst_write_raw(aTHX_ st, SvPVX(head), SvCUR(head)); |
| 647 |
|
| /* |
| 648 |
|
| # uncoverable branch false |
| 649 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 650 |
2 |
▶ | if (st->state == PST_OPEN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state == PST_OPEN) { |
2 |
0 |
2 |
|
| 651 |
2 |
| st->abi->io_watch(aTHX_ st->loop, st->fd, HM_ABI_READ, |
| 652 |
|
| pst_on_readable, st); |
| 653 |
2 |
| st->reading = 1; |
| 654 |
2 |
▶ | pst_run_handler(aTHX_ code, c, st->self_rv ? st->self_rv : keep); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| pst_run_handler(aTHX_ code, c, st->self_rv ? st->self_rv : keep); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2 |
0 |
2 |
|
| 655 |
|
| } |
| 656 |
|
| } |
| 657 |
2 |
| SvREFCNT_dec(c); |
| 658 |
2 |
| SvREFCNT_dec(code); |
| 659 |
2 |
| SvREFCNT_dec(head); |
| 660 |
|
| /* |
| 661 |
|
| # uncoverable branch false |
| 662 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 663 |
2 |
▶ | if (keep) SvREFCNT_dec(keep); /* st may be gone after this */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep) SvREFCNT_dec(keep); /* st may be gone after this */ |
2 |
0 |
2 |
|
| 664 |
2 |
| } |
| 665 |
|
| |
| 666 |
2 |
| static void pst_start_cb(pTHX_ void *ud) { |
| 667 |
2 |
| PK_LOOP_ENTER; |
| 668 |
2 |
| pst_start_cb_in(aTHX_ ud); |
| 669 |
2 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
0 |
2 |
2 |
|
| 670 |
2 |
| } |
| 671 |
|
| |
| 672 |
|
| /* The response head as a structured header list rather than the pst_head byte |
| 673 |
|
| * string. Content-Type and the caller's own headers carry over; Connection |
| 674 |
|
| * and Transfer-Encoding do NOT. Both are hop-by-hop, HTTP/2 and HTTP/3 forbid |
| 675 |
|
| * them, and framing is the transport's job on every version this branch |
| 676 |
|
| * serves - HTTP/1.1 over TLS included, where Hyperman delimits by close. |
| 677 |
|
| * pst_phrase has nothing to do here either: a header list carries the status |
| 678 |
|
| * and never a reason phrase. Owned (+1). */ |
| 679 |
10 |
| static AV *pst_header_av(pTHX_ SV *ct, HV *opts) { |
| 680 |
10 |
| AV *hdrs = newAV(), *extra = pst_opt_headers(aTHX_ opts); |
| 681 |
10 |
| av_push(hdrs, newSVpvs("Content-Type")); av_push(hdrs, newSVsv(ct)); |
| 682 |
10 |
▶ | if (extra) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (extra) { |
7 |
3 |
10 |
|
| 683 |
7 |
| SSize_t i, n = av_len(extra) + 1; |
| 684 |
14 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
7 |
7 |
14 |
|
| 685 |
7 |
| SV **k = av_fetch(extra, i, 0); |
| 686 |
7 |
| SV **v = av_fetch(extra, i + 1, 0); |
| 687 |
|
| /* |
| 688 |
|
| # uncoverable branch true |
| 689 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 690 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 691 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 692 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 693 |
7 |
▶ | if (!(k && *k && v && *v)) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(k && *k && v && *v)) continue; |
7 |
0 |
7 |
| if (!(k && *k && v && *v)) continue; |
7 |
0 |
7 |
| if (!(k && *k && v && *v)) continue; |
7 |
0 |
7 |
| if (!(k && *k && v && *v)) continue; |
0 |
7 |
7 |
|
| 694 |
7 |
| av_push(hdrs, newSVsv(*k)); |
| 695 |
7 |
| av_push(hdrs, newSVsv(*v)); |
| 696 |
|
| } |
| 697 |
|
| } |
| 698 |
10 |
| av_push(hdrs, newSVpvs("X-Accel-Buffering")); av_push(hdrs, newSVpvs("no")); |
| 699 |
10 |
| return hdrs; |
| 700 |
|
| } |
| 701 |
|
| |
| 702 |
|
| /* the psgi.streaming responder: capture [ $c, $code, $ct, $opts, $reqid, $env ]. |
| 703 |
|
| * The responder IS the deferral a stream handle needs, so the handle is tried |
| 704 |
|
| * first here and the portable writer is the fallback. */ |
| 705 |
|
| XS_INTERNAL(pst_stream_cb); |
| 706 |
6 |
| XS_INTERNAL(pst_stream_cb) { |
| 707 |
6 |
| dXSARGS; |
| 708 |
6 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 709 |
6 |
| SV *c = *av_fetch(cap, 0, 0); |
| 710 |
6 |
| SV *code = *av_fetch(cap, 1, 0); |
| 711 |
6 |
| SV *ct = *av_fetch(cap, 2, 0); |
| 712 |
6 |
| SV *osv = *av_fetch(cap, 3, 0); |
| 713 |
6 |
| SV *rid = *av_fetch(cap, 4, 0); |
| 714 |
6 |
| SV *esv = *av_fetch(cap, 5, 0); |
| 715 |
6 |
▶ | HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
3 |
6 |
| HV *opts = (SvROK(osv) && SvTYPE(SvRV(osv)) == SVt_PVHV) ? (HV *)SvRV(osv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
0 |
3 |
|
| 716 |
|
| /* |
| 717 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 718 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 719 |
6 |
▶ | HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; |
6 |
0 |
6 |
| HV *envh = (SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV) ? (HV *)SvRV(esv) : NULL; |
6 |
0 |
6 |
|
| 720 |
6 |
▶ | SV *responder = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *responder = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
6 |
0 |
6 |
|
| 721 |
6 |
| AV *hdrs = pst_header_av(aTHX_ ct, opts); |
| 722 |
6 |
| IV status = pst_opt_status(aTHX_ opts); |
| 723 |
|
| SV *self; |
| 724 |
|
| punk_stream *st; |
| 725 |
6 |
| const hm_abi *A = NULL; |
| 726 |
6 |
| void *loop = NULL, *h = NULL; |
| 727 |
|
| |
| 728 |
|
| /* |
| 729 |
|
| # uncoverable branch false |
| 730 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 731 |
6 |
▶ | if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, (int)status, hdrs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (envh) h = punk_hm_stream_open(aTHX_ envh, &A, &loop, (int)status, hdrs); |
6 |
0 |
6 |
|
| 732 |
6 |
▶ | if (h) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h) { |
0 |
6 |
6 |
|
| 733 |
0 |
| st = pst_new(aTHX_ PST_MODE_HSTREAM, opts); |
| 734 |
0 |
| st->abi = A; st->loop = loop; st->sh = h; |
| 735 |
0 |
| SvREFCNT_dec((SV *)hdrs); |
| 736 |
|
| /* |
| 737 |
|
| # uncoverable branch true |
| 738 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 739 |
0 |
▶ | if (SvOK(rid)) st->reqid = newSVsv(rid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(rid)) st->reqid = newSVsv(rid); |
0 |
0 |
0 |
|
| 740 |
0 |
| self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st))); |
| 741 |
0 |
| st->self_rv = newSVsv(self); |
| 742 |
|
| /* before the handler produces anything: a reset while it is still |
| 743 |
|
| * writing must reach it, and registering after the event still fires */ |
| 744 |
0 |
| (void)A->stream_on_abort(aTHX_ h, pst_on_habort, st); |
| 745 |
0 |
| pst_run_handler(aTHX_ code, c, self); |
| 746 |
0 |
| XSRETURN_EMPTY; |
| 747 |
|
| } |
| 748 |
|
| |
| 749 |
|
| { |
| 750 |
6 |
| AV *sh = newAV(); |
| 751 |
|
| SV *writer; |
| 752 |
6 |
| dSP; int n; |
| 753 |
6 |
| av_push(sh, newSViv(status)); |
| 754 |
6 |
| av_push(sh, newRV_noinc((SV *)hdrs)); |
| 755 |
6 |
| ENTER; SAVETMPS; |
| 756 |
6 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); |
0 |
6 |
6 |
| PUSHMARK(SP); EXTEND(SP, 1); |
0 |
6 |
6 |
|
| 757 |
6 |
| PUSHs(sv_2mortal(newRV_noinc((SV *)sh))); |
| 758 |
6 |
| PUTBACK; |
| 759 |
6 |
| n = call_sv(responder, G_SCALAR); |
| 760 |
6 |
| SPAGAIN; |
| 761 |
6 |
▶ | writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| writer = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
6 |
0 |
6 |
|
| 762 |
6 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 763 |
6 |
| st = pst_new(aTHX_ PST_MODE_STREAM, opts); |
| 764 |
6 |
| st->writer = writer; /* +1 owned */ |
| 765 |
|
| } |
| 766 |
|
| /* |
| 767 |
|
| # uncoverable branch true |
| 768 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 769 |
6 |
▶ | if (SvOK(rid)) st->reqid = newSVsv(rid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(rid)) st->reqid = newSVsv(rid); |
0 |
6 |
6 |
|
| 770 |
6 |
| self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st))); |
| 771 |
6 |
| st->self_rv = newSVsv(self); |
| 772 |
6 |
| pst_run_handler(aTHX_ code, c, self); |
| 773 |
6 |
| XSRETURN_EMPTY; |
| 774 |
|
| } |
| 775 |
|
| |
| 776 |
|
| /* ---- $c->stream ------------------------------------------------------------ */ |
| 777 |
|
| |
| 778 |
35 |
| static SV *punk_stream_start(pTHX_ SV *c, SV *ct, HV *opts, SV *code) { |
| 779 |
35 |
| AV *cav = pcx_av(aTHX_ c); |
| 780 |
35 |
| SV *envsv = pcx_get(aTHX_ cav, PCX_ENV); |
| 781 |
35 |
▶ | HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) |
35 |
0 |
35 |
| HV *envh = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) |
35 |
0 |
35 |
|
| 782 |
70 |
▶ | ? (HV *)SvRV(envsv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(envsv) : NULL; |
35 |
0 |
35 |
|
| 783 |
|
| SV **x, *rid; |
| 784 |
35 |
| int chunked = 1; |
| 785 |
|
| /* |
| 786 |
|
| # uncoverable branch true |
| 787 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 788 |
35 |
▶ | if (!envh) return pw_err(aTHX_ 500, "bad stream dispatch\n", NULL, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!envh) return pw_err(aTHX_ 500, "bad stream dispatch\n", NULL, NULL); |
0 |
35 |
35 |
|
| 789 |
|
| |
| 790 |
|
| /* HEAD is the headers and nothing else. The streaming transports write |
| 791 |
|
| * to the connection themselves, where nothing blanks a body the way the |
| 792 |
|
| * dispatcher does for an ordinary response, so the handler is not run. */ |
| 793 |
35 |
| x = hv_fetchs(envh, "REQUEST_METHOD", 0); |
| 794 |
|
| /* |
| 795 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 796 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 797 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 798 |
|
| # uncoverable condition right count:5 class:ignore_covered_err */ |
| 799 |
35 |
▶ | if (x && *x && SvOK(*x) && SvCUR(*x) == 4 |
Branch: 4 branches
| Branch | True | False | Total |
| if (x && *x && SvOK(*x) && SvCUR(*x) == 4 |
35 |
0 |
35 |
| if (x && *x && SvOK(*x) && SvCUR(*x) == 4 |
35 |
0 |
35 |
| if (x && *x && SvOK(*x) && SvCUR(*x) == 4 |
35 |
0 |
35 |
| if (x && *x && SvOK(*x) && SvCUR(*x) == 4 |
4 |
31 |
35 |
|
| 800 |
4 |
▶ | && memEQ(SvPVX(*x), "HEAD", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && memEQ(SvPVX(*x), "HEAD", 4)) { |
4 |
0 |
4 |
|
| 801 |
4 |
| AV *resp = newAV(); |
| 802 |
4 |
| av_push(resp, newSViv(pst_opt_status(aTHX_ opts))); |
| 803 |
4 |
| av_push(resp, newRV_noinc((SV *)pst_header_av(aTHX_ ct, opts))); |
| 804 |
4 |
| av_push(resp, newRV_noinc((SV *)newAV())); |
| 805 |
4 |
| return newRV_noinc((SV *)resp); |
| 806 |
|
| } |
| 807 |
|
| |
| 808 |
|
| /* Chunked framing is an HTTP/1.1 thing and only an HTTP/1.1 thing. |
| 809 |
|
| * HTTP/1.0 has no chunked transfer coding; HTTP/2 and HTTP/3 forbid the |
| 810 |
|
| * header and frame the body themselves. So the test names the one version |
| 811 |
|
| * that wants it rather than the one that does not - "anything but |
| 812 |
|
| * HTTP/1.0" put a Transfer-Encoding on an h2 response, and Hyperman spells |
| 813 |
|
| * its h2 protocol "HTTP/2" (six bytes, not "HTTP/2.0"), so no amount of |
| 814 |
|
| * widening the excluded literal would have caught it. |
| 815 |
|
| * |
| 816 |
|
| * A missing SERVER_PROTOCOL is HTTP/1.1: PSGI requires the key, and a |
| 817 |
|
| * synthetic environment that omits it is a test writing HTTP/1.1. */ |
| 818 |
31 |
| x = hv_fetchs(envh, "SERVER_PROTOCOL", 0); |
| 819 |
|
| /* |
| 820 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 821 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 822 |
31 |
▶ | if (x && *x && SvOK(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvOK(*x)) { |
28 |
3 |
31 |
| if (x && *x && SvOK(*x)) { |
28 |
0 |
28 |
| if (x && *x && SvOK(*x)) { |
28 |
0 |
28 |
|
| 823 |
|
| STRLEN pl; |
| 824 |
28 |
| const char *p = SvPV_const(*x, pl); |
| 825 |
28 |
▶ | chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8)); |
Branch: 2 branches
| Branch | True | False | Total |
| chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8)); |
22 |
6 |
28 |
| chunked = (pl == 8 && memEQ(p, "HTTP/1.1", 8)); |
19 |
3 |
22 |
|
| 826 |
|
| } |
| 827 |
31 |
| x = hv_fetchs(envh, "psgix.request_id", 0); |
| 828 |
|
| /* |
| 829 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 830 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 831 |
31 |
▶ | rid = (x && *x && SvOK(*x)) ? *x : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| rid = (x && *x && SvOK(*x)) ? *x : NULL; |
3 |
28 |
31 |
| rid = (x && *x && SvOK(*x)) ? *x : NULL; |
3 |
0 |
3 |
| rid = (x && *x && SvOK(*x)) ? *x : NULL; |
3 |
0 |
3 |
|
| 832 |
|
| |
| 833 |
|
| /* 1. Hyperman detach: stream on the worker loop. |
| 834 |
|
| * |
| 835 |
|
| * A refusal falls through rather than answering 503: conn_detach says no |
| 836 |
|
| * to TLS, and the stream handle below is exactly what serves that case. */ |
| 837 |
31 |
| x = hv_fetchs(envh, "psgix.hyperman.conn", 0); |
| 838 |
|
| /* |
| 839 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 840 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 841 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 842 |
31 |
▶ | if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { |
2 |
29 |
31 |
| 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 |
|
| 843 |
2 |
| const hm_abi *A = punk_hm(aTHX); |
| 844 |
|
| /* |
| 845 |
|
| # uncoverable branch false |
| 846 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 847 |
2 |
▶ | if (A) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (A) { |
2 |
0 |
2 |
|
| 848 |
2 |
| AV *cid = (AV *)SvRV(*x); |
| 849 |
2 |
| SV **fsv = av_fetch(cid, 0, 0), **isv = av_fetch(cid, 1, 0); |
| 850 |
2 |
| void *loop = A->cur_loop(aTHX); |
| 851 |
|
| int fd; int fl; |
| 852 |
|
| punk_stream *st; SV *self; |
| 853 |
|
| /* |
| 854 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 855 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 856 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 857 |
|
| # uncoverable condition right count:4 class:ignore_covered_err |
| 858 |
|
| # uncoverable condition right count:5 class:ignore_covered_err |
| 859 |
|
| # uncoverable condition right count:6 class:ignore_covered_err */ |
| 860 |
2 |
▶ | if (fsv && *fsv && isv && *isv && loop |
Branch: 5 branches
| Branch | True | False | Total |
| 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 |
|
| 861 |
2 |
▶ | && (fd = (int)SvIV(*fsv)) >= 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && (fd = (int)SvIV(*fsv)) >= 0 |
2 |
0 |
2 |
|
| 862 |
2 |
▶ | && A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| && A->conn_detach(aTHX_ loop, fd, SvUV(*isv)) == 0) { |
2 |
0 |
2 |
|
| 863 |
2 |
| fl = fcntl(fd, F_GETFL, 0); |
| 864 |
|
| /* |
| 865 |
|
| # uncoverable branch false |
| 866 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 867 |
2 |
▶ | if (fl >= 0) (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
Branch: 1 branch
| Branch | True | False | Total |
| if (fl >= 0) (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
2 |
0 |
2 |
|
| 868 |
2 |
| st = pst_new(aTHX_ PST_MODE_DETACH, opts); |
| 869 |
2 |
| st->fd = fd; st->abi = A; st->loop = loop; |
| 870 |
2 |
| st->chunked = (unsigned char)chunked; |
| 871 |
|
| /* |
| 872 |
|
| # uncoverable branch true |
| 873 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 874 |
2 |
▶ | if (rid) st->reqid = newSVsv(rid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rid) st->reqid = newSVsv(rid); |
0 |
2 |
2 |
|
| 875 |
2 |
| self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st))); |
| 876 |
2 |
| st->self_rv = newSVsv(self); |
| 877 |
2 |
| st->defer_c = newSVsv(c); |
| 878 |
2 |
| st->defer_code = newSVsv(code); |
| 879 |
2 |
| st->defer_head = newSVsv(pst_head(aTHX_ st, ct, opts)); |
| 880 |
2 |
| st->defer_tw = A->timer(aTHX_ loop, 0.0, pst_start_cb, st); |
| 881 |
2 |
| return pw_empty(aTHX_ 200); /* the socket is ours now */ |
| 882 |
|
| } |
| 883 |
|
| } |
| 884 |
|
| } |
| 885 |
|
| |
| 886 |
|
| /* 2. the deferred branch: a Hyperman v6 stream handle when the server |
| 887 |
|
| * offers one (HTTP/2, and HTTP/1.1 over TLS), else the portable |
| 888 |
|
| * psgi.streaming writer. pst_stream_cb decides, because a handle can |
| 889 |
|
| * only be opened once the response has been deferred. */ |
| 890 |
29 |
| x = hv_fetchs(envh, "psgi.streaming", 0); |
| 891 |
|
| /* |
| 892 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 893 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 894 |
29 |
▶ | if (x && *x && SvTRUE(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvTRUE(*x)) { |
6 |
23 |
29 |
| if (x && *x && SvTRUE(*x)) { |
6 |
0 |
6 |
| if (x && *x && SvTRUE(*x)) { |
6 |
0 |
6 |
|
| 895 |
6 |
| AV *cap = newAV(); |
| 896 |
6 |
| av_push(cap, newSVsv(c)); |
| 897 |
6 |
| av_push(cap, newSVsv(code)); |
| 898 |
6 |
| av_push(cap, newSVsv(ct)); |
| 899 |
6 |
▶ | av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(cap, opts ? newRV_inc((SV *)opts) : newSV(0)); |
3 |
3 |
6 |
|
| 900 |
6 |
▶ | av_push(cap, rid ? newSVsv(rid) : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(cap, rid ? newSVsv(rid) : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
6 |
6 |
|
| 901 |
6 |
| av_push(cap, newRV_inc((SV *)envh)); |
| 902 |
6 |
| return punk_closure(aTHX_ pst_stream_cb, cap); /* the responder */ |
| 903 |
|
| } |
| 904 |
|
| |
| 905 |
|
| /* 3. blocking psgix.io: stream inside the handler (pins a worker) */ |
| 906 |
23 |
▶ | x = opts ? hv_fetchs(opts, "blocking", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| x = opts ? hv_fetchs(opts, "blocking", 0) : NULL; |
20 |
3 |
23 |
|
| 907 |
|
| /* |
| 908 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 909 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 910 |
23 |
▶ | if (x && *x && SvTRUE(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvTRUE(*x)) { |
20 |
3 |
23 |
| if (x && *x && SvTRUE(*x)) { |
20 |
0 |
20 |
| if (x && *x && SvTRUE(*x)) { |
20 |
0 |
20 |
|
| 911 |
20 |
| SV **iop = hv_fetchs(envh, "psgix.io", 0); |
| 912 |
|
| /* |
| 913 |
|
| # uncoverable branch false |
| 914 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 915 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 916 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 917 |
20 |
▶ | if (iop && *iop && SvOK(*iop)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (iop && *iop && SvOK(*iop)) { |
20 |
0 |
20 |
| if (iop && *iop && SvOK(*iop)) { |
20 |
0 |
20 |
| if (iop && *iop && SvOK(*iop)) { |
20 |
0 |
20 |
|
| 918 |
20 |
| IO *io = sv_2io(*iop); |
| 919 |
|
| /* |
| 920 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 921 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 922 |
20 |
▶ | int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; |
20 |
0 |
20 |
| int fd = (io && IoIFP(io)) ? PerlIO_fileno(IoIFP(io)) : -1; |
20 |
0 |
20 |
|
| 923 |
|
| /* |
| 924 |
|
| # uncoverable branch false |
| 925 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 926 |
20 |
▶ | if (fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { |
20 |
0 |
20 |
|
| 927 |
20 |
| punk_stream *st = pst_new(aTHX_ PST_MODE_BLOCK, opts); |
| 928 |
|
| SV *self, *head; |
| 929 |
20 |
| st->fd = fd; st->chunked = (unsigned char)chunked; |
| 930 |
20 |
▶ | if (rid) st->reqid = newSVsv(rid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rid) st->reqid = newSVsv(rid); |
3 |
17 |
20 |
|
| 931 |
20 |
| self = sv_2mortal(sv_setref_iv(newSV(0), "Punk::Stream", PTR2IV(st))); |
| 932 |
20 |
| st->self_rv = newSVsv(self); |
| 933 |
20 |
| head = pst_head(aTHX_ st, ct, opts); |
| 934 |
20 |
| pst_write_raw(aTHX_ st, SvPVX(head), SvCUR(head)); |
| 935 |
20 |
| pst_run_handler(aTHX_ code, c, self); |
| 936 |
20 |
| return pw_empty(aTHX_ 200); |
| 937 |
|
| } |
| 938 |
|
| } |
| 939 |
|
| } |
| 940 |
|
| |
| 941 |
3 |
| return pw_err(aTHX_ 501, |
| 942 |
|
| "this server cannot stream a response (needs Hyperman 0.11+, a " |
| 943 |
|
| "psgi.streaming server, or blocking => 1 with psgix.io)\n", |
| 944 |
|
| NULL, NULL); |
| 945 |
|
| } |
| 946 |
|
| |
| 947 |
|
| #endif /* PUNK_STREAM_H */ |