xs/stream.xs

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