| 1 |
|
| MODULE = Punk PACKAGE = Punk::Context |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| # $c->stream($content_type, $cb) / $c->stream($content_type, \%opts, $cb): |
| 6 |
|
| # a streamed response of unknown length (punk_stream.h). The handler returns |
| 7 |
|
| # the value this returns; the callback gets ($c, $w) and writes the body as it |
| 8 |
|
| # is produced. Options: status, headers, write_buffer_limit, blocking. |
| 9 |
|
| SV * |
| 10 |
|
| stream(self, content_type, ...) |
| 11 |
|
| SV *self |
| 12 |
|
| SV *content_type |
| 13 |
|
| CODE: |
| 14 |
|
| { |
| 15 |
53 |
| HV *opts = NULL; |
| 16 |
|
| SV *cb; |
| 17 |
|
| STRLEN ctl, i; |
| 18 |
|
| const char *ctp; |
| 19 |
53 |
▶ | if (items == 3) cb = ST(2); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 3) cb = ST(2); |
14 |
39 |
53 |
|
| 20 |
39 |
▶ | # uncoverable branch false |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch false |
39 |
0 |
39 |
|
| 21 |
39 |
▶ | # uncoverable condition right count:1 class:ignore_covered_err |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable condition right count:1 class:ignore_covered_err |
39 |
0 |
39 |
| # uncoverable condition right count:1 class:ignore_covered_err |
0 |
39 |
39 |
|
| 22 |
0 |
| else if (items == 4) { |
| 23 |
39 |
| # uncoverable branch true |
| 24 |
39 |
| # uncoverable condition left count:1 class:ignore_covered_err |
| 25 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 26 |
0 |
| if (!(SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV)) |
| 27 |
53 |
▶ | croak("Punk::Context: stream options must be a hashref"); /* # uncoverable statement */ |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk::Context: stream options must be a hashref"); /* # uncoverable statement */ |
50 |
3 |
53 |
| croak("Punk::Context: stream options must be a hashref"); /* # uncoverable statement */ |
0 |
50 |
50 |
|
| 28 |
3 |
| opts = (HV *)SvRV(ST(2)); |
| 29 |
50 |
▶ | cb = ST(3); |
Branch: 2 branches
| Branch | True | False | Total |
| cb = ST(3); |
50 |
0 |
50 |
| cb = ST(3); |
0 |
50 |
50 |
|
| 30 |
0 |
| } |
| 31 |
50 |
| else croak("usage: $c->stream($content_type, [\\%%opts,] $cb)"); /* # uncoverable statement */ |
| 32 |
50 |
▶ | if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
50 |
50 |
|
| 33 |
0 |
| croak("Punk::Context: stream needs a coderef"); |
| 34 |
456 |
▶ | # uncoverable branch true |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch true |
409 |
47 |
456 |
|
| 35 |
409 |
▶ | # uncoverable condition left count:1 class:ignore_covered_err |
Branch: 3 branches
| Branch | True | False | Total |
| # uncoverable condition left count:1 class:ignore_covered_err |
406 |
3 |
409 |
| # uncoverable condition left count:1 class:ignore_covered_err |
406 |
0 |
406 |
| # uncoverable condition left count:1 class:ignore_covered_err |
0 |
406 |
406 |
|
| 36 |
3 |
| # uncoverable condition left count:2 class:ignore_covered_err |
| 37 |
47 |
▶ | if (!SvOK(content_type) || SvROK(content_type)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(content_type) || SvROK(content_type)) |
39 |
8 |
47 |
|
| 38 |
|
| croak("Punk::Context: stream needs a content type"); /* # uncoverable statement */ |
| 39 |
39 |
| ctp = SvPV_const(content_type, ctl); |
| 40 |
82 |
▶ | # uncoverable branch true |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch true |
46 |
36 |
82 |
|
| 41 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 42 |
46 |
| if (ctl == 0) |
| 43 |
46 |
▶ | croak("Punk::Context: stream needs a content type"); /* # uncoverable statement */ |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk::Context: stream needs a content type"); /* # uncoverable statement */ |
10 |
36 |
46 |
| croak("Punk::Context: stream needs a content type"); /* # uncoverable statement */ |
0 |
10 |
10 |
|
| 44 |
36 |
▶ | for (i = 0; i < ctl; i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < ctl; i++) |
13 |
23 |
36 |
| for (i = 0; i < ctl; i++) |
0 |
13 |
13 |
|
| 45 |
23 |
▶ | # uncoverable condition left count:2 class:ignore_covered_err |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable condition left count:2 class:ignore_covered_err |
0 |
23 |
23 |
| # uncoverable condition left count:2 class:ignore_covered_err |
0 |
0 |
0 |
|
| 46 |
23 |
▶ | # uncoverable condition left count:3 class:ignore_covered_err |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable condition left count:3 class:ignore_covered_err |
20 |
3 |
23 |
| # uncoverable condition left count:3 class:ignore_covered_err |
0 |
20 |
20 |
|
| 47 |
3 |
| if (ctp[i] == '\r' || ctp[i] == '\n' || ctp[i] == '\0') |
| 48 |
|
| croak("Punk::Context: stream content type carries a control byte"); |
| 49 |
|
| if (opts) { |
| 50 |
|
| HE *he; |
| 51 |
|
| hv_iterinit(opts); |
| 52 |
36 |
| while ((he = hv_iternext(opts))) { |
| 53 |
36 |
▶ | I32 kl; |
Branch: 3 branches
| Branch | True | False | Total |
| I32 kl; |
10 |
26 |
36 |
| I32 kl; |
10 |
0 |
10 |
| I32 kl; |
10 |
0 |
10 |
|
| 54 |
10 |
| const char *k = hv_iterkey(he, &kl); |
| 55 |
10 |
▶ | # uncoverable condition left count:2 class:ignore_covered_err |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable condition left count:2 class:ignore_covered_err |
7 |
3 |
10 |
| # uncoverable condition left count:2 class:ignore_covered_err |
0 |
7 |
7 |
|
| 56 |
3 |
| # uncoverable condition left count:4 class:ignore_covered_err |
| 57 |
|
| # uncoverable condition right count:5 class:ignore_covered_err |
| 58 |
|
| # uncoverable condition left count:6 class:ignore_covered_err |
| 59 |
|
| # uncoverable condition right count:6 class:ignore_covered_err |
| 60 |
|
| # uncoverable condition left count:8 class:ignore_covered_err |
| 61 |
33 |
| if (!((kl == 6 && memEQ(k, "status", 6)) |
| 62 |
33 |
▶ | || (kl == 7 && memEQ(k, "headers", 7)) |
Branch: 3 branches
| Branch | True | False | Total |
| || (kl == 7 && memEQ(k, "headers", 7)) |
13 |
20 |
33 |
| || (kl == 7 && memEQ(k, "headers", 7)) |
13 |
0 |
13 |
| || (kl == 7 && memEQ(k, "headers", 7)) |
13 |
0 |
13 |
|
| 63 |
|
| || (kl == 18 && memEQ(k, "write_buffer_limit", 18)) |
| 64 |
|
| || (kl == 8 && memEQ(k, "blocking", 8)))) |
| 65 |
13 |
▶ | croak("Punk::Context: unknown stream option '%.*s' " |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk::Context: unknown stream option '%.*s' " |
13 |
0 |
13 |
| croak("Punk::Context: unknown stream option '%.*s' " |
0 |
13 |
13 |
|
| 66 |
0 |
| "(status, headers, write_buffer_limit, blocking)", |
| 67 |
|
| (int)kl, k); |
| 68 |
13 |
| } |
| 69 |
13 |
| { |
| 70 |
13 |
▶ | SV **s = hv_fetchs(opts, "status", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV **s = hv_fetchs(opts, "status", 0); |
3 |
10 |
13 |
|
| 71 |
3 |
| # uncoverable condition right count:2 class:ignore_covered_err |
| 72 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 73 |
27 |
▶ | if (s && *s && SvOK(*s)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s && *s && SvOK(*s)) { |
20 |
7 |
27 |
|
| 74 |
20 |
| IV st = SvIV(*s); |
| 75 |
|
| if (st < 100 || st > 599) /* # uncoverable condition left count:2 class:ignore_covered_err */ |
| 76 |
|
| croak("Punk::Context: stream status %" IVdf |
| 77 |
20 |
▶ | " is not an HTTP status", st); |
Branch: 4 branches
| Branch | True | False | Total |
| " is not an HTTP status", st); |
20 |
0 |
20 |
| " is not an HTTP status", st); |
20 |
0 |
20 |
| " is not an HTTP status", st); |
20 |
0 |
20 |
| " is not an HTTP status", st); |
0 |
20 |
20 |
|
| 78 |
0 |
| } |
| 79 |
|
| } |
| 80 |
20 |
| { |
| 81 |
195 |
▶ | SV **h = hv_fetchs(opts, "headers", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV **h = hv_fetchs(opts, "headers", 0); |
178 |
17 |
195 |
|
| 82 |
178 |
▶ | # uncoverable condition right count:2 class:ignore_covered_err |
Branch: 3 branches
| Branch | True | False | Total |
| # uncoverable condition right count:2 class:ignore_covered_err |
175 |
3 |
178 |
| # uncoverable condition right count:2 class:ignore_covered_err |
175 |
0 |
175 |
| # uncoverable condition right count:2 class:ignore_covered_err |
0 |
175 |
175 |
|
| 83 |
3 |
| # uncoverable condition right count:3 class:ignore_covered_err |
| 84 |
|
| if (h && *h && SvOK(*h)) { |
| 85 |
|
| AV *ha; |
| 86 |
|
| SSize_t j, n; |
| 87 |
|
| # uncoverable branch true |
| 88 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 89 |
35 |
| # uncoverable condition left count:2 class:ignore_covered_err |
| 90 |
|
| if (!(SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) |
| 91 |
|
| croak("Punk::Context: stream headers must be an " /* # uncoverable statement */ |
| 92 |
|
| "arrayref of pairs"); |
| 93 |
|
| ha = (AV *)SvRV(*h); |
| 94 |
|
| n = av_len(ha) + 1; |
| 95 |
|
| if (n % 2) |
| 96 |
|
| croak("Punk::Context: stream headers must be an " |
| 97 |
|
| "arrayref of pairs"); |
| 98 |
|
| for (j = 0; j < n; j++) { |
| 99 |
|
| SV **e = av_fetch(ha, j, 0); |
| 100 |
|
| STRLEN vl, vi; |
| 101 |
|
| const char *vp; |
| 102 |
|
| # uncoverable branch true |
| 103 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 104 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 105 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 106 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 107 |
|
| if (!(e && *e && SvOK(*e)) || SvROK(*e)) |
| 108 |
|
| croak("Punk::Context: stream headers must be " /* # uncoverable statement */ |
| 109 |
|
| "plain strings"); |
| 110 |
|
| vp = SvPV_const(*e, vl); |
| 111 |
|
| for (vi = 0; vi < vl; vi++) |
| 112 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 113 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 114 |
|
| if (vp[vi] == '\r' || vp[vi] == '\n' || vp[vi] == '\0') |
| 115 |
|
| croak("Punk::Context: stream header carries " |
| 116 |
|
| "a control byte"); |
| 117 |
|
| } |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| } |
| 121 |
|
| RETVAL = punk_stream_start(aTHX_ self, content_type, opts, cb); |
| 122 |
|
| } |
| 123 |
|
| OUTPUT: |
| 124 |
|
| RETVAL |
| 125 |
|
| |
| 126 |
|
| MODULE = Punk PACKAGE = Punk::Stream |
| 127 |
|
| |
| 128 |
|
| # write($bytes): queue one chunk of the body. Chainable. Bytes, as PSGI bodies |
| 129 |
|
| # are; a wide-character string should be encoded first. |
| 130 |
|
| SV * |
| 131 |
|
| write(self, bytes) |
| 132 |
|
| SV *self |
| 133 |
|
| SV *bytes |
| 134 |
|
| CODE: |
| 135 |
|
| { |
| 136 |
|
| STRLEN l; |
| 137 |
1380 |
▶ | const char *p = SvOK(bytes) ? SvPV_const(bytes, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = SvOK(bytes) ? SvPV_const(bytes, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
690 |
0 |
690 |
|
| 138 |
690 |
▶ | # uncoverable branch false |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch false |
690 |
0 |
690 |
|
| 139 |
690 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 140 |
|
| if (p) pst_write_body(aTHX_ pst_of(aTHX_ self), p, l); |
| 141 |
|
| RETVAL = newSVsv(self); |
| 142 |
|
| } |
| 143 |
|
| OUTPUT: |
| 144 |
|
| RETVAL |
| 145 |
|
| |
| 146 |
|
| # drain: a Punk::Future settled with 1 once everything written so far has |
| 147 |
|
| # reached the kernel, or 0 when the stream closed first. Awaiting it after |
| 148 |
|
| # each write bounds the buffer to one chunk; on the synchronous transports it |
| 149 |
|
| # is already settled. |
| 150 |
|
| SV * |
| 151 |
|
| drain(self) |
| 152 |
|
| SV *self |
| 153 |
|
| CODE: |
| 154 |
|
| { |
| 155 |
264 |
| punk_stream *st = pst_of(aTHX_ self); |
| 156 |
|
| /* Pending while there is something the transport has not taken: our |
| 157 |
|
| * own buffer on a detached socket, the server's on a stream handle, |
| 158 |
|
| * which says so by answering a write with "full". Without the second |
| 159 |
|
| * half every TLS and HTTP/2 response drained at once, and a slow |
| 160 |
|
| * client had the whole export buffered for it. */ |
| 161 |
264 |
▶ | if (st->state == PST_OPEN |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->state == PST_OPEN |
260 |
4 |
264 |
|
| 162 |
260 |
▶ | && ((st->mode == PST_MODE_DETACH && st->wlen > st->woff) |
Branch: 2 branches
| Branch | True | False | Total |
| && ((st->mode == PST_MODE_DETACH && st->wlen > st->woff) |
257 |
3 |
260 |
| && ((st->mode == PST_MODE_DETACH && st->wlen > st->woff) |
256 |
1 |
257 |
|
| 163 |
259 |
▶ | || (st->mode == PST_MODE_HSTREAM && st->full))) { |
Branch: 2 branches
| Branch | True | False | Total |
| || (st->mode == PST_MODE_HSTREAM && st->full))) { |
0 |
259 |
259 |
| || (st->mode == PST_MODE_HSTREAM && st->full))) { |
0 |
0 |
0 |
|
| 164 |
1 |
▶ | # uncoverable branch false |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch false |
1 |
0 |
1 |
|
| 165 |
1 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 166 |
1 |
| if (!st->drain_f) { |
| 167 |
|
| punk_future *pf = pf_new(aTHX); |
| 168 |
1 |
| st->drain_f = pf_bless(aTHX_ pf, "Punk::Future"); |
| 169 |
|
| } |
| 170 |
|
| RETVAL = newSVsv(st->drain_f); |
| 171 |
263 |
| } |
| 172 |
263 |
| else { |
| 173 |
263 |
| punk_future *pf = pf_new(aTHX); |
| 174 |
263 |
| SV *f = pf_bless(aTHX_ pf, "Punk::Future"); |
| 175 |
263 |
| AV *vals = newAV(); |
| 176 |
263 |
| av_push(vals, newSViv(st->state == PST_OPEN ? 1 : 0)); |
| 177 |
|
| pf_settle(aTHX_ pf, f, PF_DONE, vals); |
| 178 |
|
| RETVAL = f; |
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
|
| OUTPUT: |
| 182 |
|
| RETVAL |
| 183 |
|
| |
| 184 |
|
| # close: end the response cleanly (the terminal chunk, then teardown once the |
| 185 |
|
| # buffer drains). The callback returning does this for you. Chainable. |
| 186 |
|
| SV * |
| 187 |
|
| close(self) |
| 188 |
|
| SV *self |
| 189 |
|
| CODE: |
| 190 |
3 |
| pst_close(aTHX_ pst_of(aTHX_ self)); |
| 191 |
3 |
| RETVAL = newSVsv(self); |
| 192 |
|
| OUTPUT: |
| 193 |
|
| RETVAL |
| 194 |
|
| |
| 195 |
|
| IV |
| 196 |
|
| is_open(self) |
| 197 |
|
| SV *self |
| 198 |
|
| CODE: |
| 199 |
3 |
▶ | RETVAL = pst_of(aTHX_ self)->state == PST_OPEN ? 1 : 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = pst_of(aTHX_ self)->state == PST_OPEN ? 1 : 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
3 |
3 |
|
| 200 |
|
| OUTPUT: |
| 201 |
|
| RETVAL |
| 202 |
|
| |
| 203 |
|
| void |
| 204 |
|
| DESTROY(self) |
| 205 |
|
| SV *self |
| 206 |
|
| CODE: |
| 207 |
28 |
| pst_free(aTHX_ pst_of(aTHX_ self)); |