xs/response.xs

filter: Statementstmt 86.2% 70 / 80 (1 uncoverable) Branchbran 62.2% 66 / 106 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.5% 136 / 186 (3 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_stream.h ↑ Summary xs/session.xs »
1 MODULE = Punk PACKAGE = Punk::Response
2  
3 PROTOTYPES: DISABLE
4  
5 # new(%args): status, headers (a live pair arrayref), body, type.
6 SV *
7 new(class, ...)
8 SV *class
9 CODE:
10 {
11 226 AV *res = newAV();
12 int i;
13 226 av_extend(res, PS_BODY);
14 243 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 25 218 243
15 STRLEN kl;
16 25 const char *k = SvPV_const(ST(i), kl);
17 25 SV *v = ST(i + 1);
18 25 ▶ if (kl == 6 && memEQ(k, "status", 6)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 6 && memEQ(k, "status", 6)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 9 25
if (kl == 6 && memEQ(k, "status", 6)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 0 16
19 16 sv_2mortal((SV *)res); /* freed if this croaks */
20 16 (void)ps_status_arg(aTHX_ v);
21 8 SvREFCNT_inc_simple_void_NN((SV *)res);
22 8 (void)av_store(res, PS_STATUS, newSVsv(v));
23 }
24 9 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 9 9
# uncoverable branch true 0 0 0
25 0 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
26 0 # uncoverable condition left count:2 class:ignore_covered_err
27 0 # uncoverable condition right count:2 class:ignore_covered_err
28 else if (kl == 7 && memEQ(k, "headers", 7)) {
29 9 ▶ # uncoverable statement
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement 9 0 9
# uncoverable statement 9 0 9
30 9 # uncoverable branch true
31 0 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 0 0 0
# uncoverable branch false 0 0 0
32 0 # uncoverable condition left count:1
33 # uncoverable condition right count:1
34 0 # uncoverable condition left count:2
35 # uncoverable condition right count:2
36 218 if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV)
37 croak("Punk::Response: headers must be an arrayref"); /* # uncoverable statement */
38 (void)av_store(res, PS_HEADERS, newSVsv(v)); /* # uncoverable statement */
39 }
40 # uncoverable branch false
41 # uncoverable condition right count:1 class:ignore_covered_err
42 # uncoverable condition right count:2 class:ignore_covered_err
43 else if (kl == 4 && memEQ(k, "body", 4))
44 (void)av_store(res, PS_BODY, newSVsv(v));
45 # uncoverable statement
46 # uncoverable branch true
47 # uncoverable branch false
48 # uncoverable condition left count:1
49 # uncoverable condition right count:1
50 # uncoverable condition left count:2
51 # uncoverable condition right count:2
52 else if (kl == 4 && memEQ(k, "type", 4))
53 (void)av_store(res, PS_TYPE, newSVsv(v)); /* # uncoverable statement */
54 else
55 croak("Punk::Response: unknown option '%.*s'", (int)kl, k); /* # uncoverable statement */
56 }
57 RETVAL = sv_bless(newRV_noinc((SV *)res),
58 gv_stashsv(class, GV_ADD));
59 }
60 OUTPUT:
61 RETVAL
62  
63 # get/set accessors; setters chain.
64 SV *
65 status(self, ...)
66 SV *self
67 ALIAS:
68 type = 1
69 body = 2
70 CODE:
71 {
72 static const I32 slot[] = { PS_STATUS, PS_TYPE, PS_BODY };
73 28 AV *res = punk_res_av(aTHX_ self);
74 28 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 28 0 28
75 28 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 18 10 28
76 28 if (items > 1) {
77 28 if (ix == 0) (void)ps_status_arg(aTHX_ ST(1));
78 (void)av_store(res, slot[ix], newSVsv(ST(1)));
79 RETVAL = newSVsv(self);
80 0 }
81 0 ▶ else {
Branch: 2 branches
BranchTrueFalseTotal
else { 0 0 0
else { 0 0 0
82 SV **e = av_fetch(res, slot[ix], 0); /* # uncoverable statement */
83 # uncoverable statement
84 # uncoverable condition left count:1
85 # uncoverable condition right count:1
86 # uncoverable condition left count:2
87 # uncoverable condition right count:2
88 RETVAL = e && *e ? newSVsv(*e) : newSV(0);
89 }
90 }
91 OUTPUT:
92 RETVAL
93  
94 # the live header-pair arrayref
95 SV *
96 # uncoverable statement
97 # uncoverable subroutine
98 headers(self)
99 SV *self
100 CODE:
101 0 RETVAL = newRV_inc((SV *)punk_res_headers(aTHX_ /* # uncoverable statement */
102 punk_res_av(aTHX_ self)));
103 OUTPUT:
104 RETVAL
105  
106 # header(Name => $v, ...) appends and chains; header('Name') reads the
107 # first case-insensitive match. A name or value carrying CR, LF or NUL is
108 # refused before anything is appended: those bytes end a header, and one
109 # built from request input would otherwise be a second header of the
110 # client's choosing.
111 SV *
112 header(self, ...)
113 SV *self
114 CODE:
115 {
116 138 AV *headers = punk_res_headers(aTHX_ punk_res_av(aTHX_ self));
117 138 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 133 5 138
118 int i;
119 393 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 265 128 393
120 STRLEN l, j;
121 const char *p;
122 265 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 265 265
123 265 # uncoverable condition left count:1 class:ignore_covered_err
124 4013 ▶ if (!SvOK(ST(i))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(i))) continue; 3753 260 4013
125 3753 ▶ p = SvPV_const(ST(i), l);
Branch: 3 branches
BranchTrueFalseTotal
p = SvPV_const(ST(i), l); 3750 3 3753
p = SvPV_const(ST(i), l); 3749 1 3750
p = SvPV_const(ST(i), l); 1 3748 3749
126 5 ▶ for (j = 0; j < l; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; j++) 1 4 5
127 if (p[j] == '\r' || p[j] == '\n' || p[j] == '\0')
128 croak("Punk::Response: a header %s may not contain "
129 384 ▶ "CR, LF or NUL", (i & 1) ? "name" : "value");
Branch: 1 branch
BranchTrueFalseTotal
"CR, LF or NUL", (i & 1) ? "name" : "value"); 256 128 384
130 256 }
131 128 for (i = 1; i < items; i++)
132 av_push(headers, newSVsv(ST(i)));
133 5 ▶ RETVAL = newSVsv(self);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVsv(self); 5 0 5
134 }
135 5 # uncoverable branch false
136 5 # uncoverable condition right count:1 class:ignore_covered_err
137 5 else if (items == 2) {
138 5 ▶ STRLEN wl;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN wl; 5 0 5
139 5 const char *want = SvPV_const(ST(1), wl);
140 SSize_t i, n = av_len(headers) + 1;
141 RETVAL = newSV(0);
142 5 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
# uncoverable branch false 0 5 5
143 5 # uncoverable condition right count:1 class:ignore_covered_err
144 5 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 5 0 5
for (i = 0; i + 1 < n; i += 2) { 5 0 5
145 5 SV **k = av_fetch(headers, i, 0);
146 5 ▶ STRLEN kl;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN kl; 5 0 5
STRLEN kl; 5 0 5
147 5 const char *kp;
148 # uncoverable branch true
149 # uncoverable condition left count:1 class:ignore_covered_err
150 # uncoverable condition left count:2 class:ignore_covered_err
151 0 if (!(k && *k)) continue;
152 kp = SvPV_const(*k, kl);
153 # uncoverable branch false
154 # uncoverable condition right count:1 class:ignore_covered_err
155 # uncoverable condition right count:2 class:ignore_covered_err
156 if (kl == wl && foldEQ(kp, want, (I32)wl)) {
157 SV **v = av_fetch(headers, i + 1, 0);
158 # uncoverable branch false
159 # uncoverable condition right count:1 class:ignore_covered_err
160 # uncoverable condition right count:2 class:ignore_covered_err
161 if (v && *v) sv_setsv(RETVAL, *v);
162 break;
163 }
164 }
165 }
166 else croak("Punk::Response: header needs a name"); /* # uncoverable statement */
167 }
168 OUTPUT:
169 RETVAL
170  
171 # The PSGI triplet: an XML document or node serialises through the frx ABI,
172 # any other reference body JSON-encodes through the frj ABI, a string is html
173 # unless type says otherwise, no body is text/plain.
174 SV *
175 finalize(self, ...)
176 SV *self
177 CODE:
178 {
179 /* finalize($canonical): the dispatcher passes true on an
180 * `etag => 1` route, so a JSON body is encoded with its keys in
181 * order and the tag is the same in every worker */
182 17 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 11 6 17
# uncoverable condition left count:1 class:ignore_covered_err 0 11 11
183 17 # uncoverable condition left count:2 class:ignore_covered_err
184 17 int canon = items > 1 && SvTRUE(ST(1));
185 17 AV *res = punk_res_av(aTHX_ self);
186 17 SV **b = av_fetch(res, PS_BODY, 0);
187 17 ▶ SV **t = av_fetch(res, PS_TYPE, 0);
Branch: 3 branches
BranchTrueFalseTotal
SV **t = av_fetch(res, PS_TYPE, 0); 3 14 17
SV **t = av_fetch(res, PS_TYPE, 0); 3 0 3
SV **t = av_fetch(res, PS_TYPE, 0); 3 0 3
188 17 ▶ SV **s = av_fetch(res, PS_STATUS, 0);
Branch: 3 branches
BranchTrueFalseTotal
SV **s = av_fetch(res, PS_STATUS, 0); 8 9 17
SV **s = av_fetch(res, PS_STATUS, 0); 8 0 8
SV **s = av_fetch(res, PS_STATUS, 0); 7 1 8
189 17 # uncoverable condition right count:2 class:ignore_covered_err
190 # uncoverable condition right count:3 class:ignore_covered_err
191 17 ▶ SV *type = (t && *t && SvOK(*t)) ? *t : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *type = (t && *t && SvOK(*t)) ? *t : NULL; 1 16 17
192 17 ▶ IV status = (s && *s && SvOK(*s)) ? SvIV(*s) : 200; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
IV status = (s && *s && SvOK(*s)) ? SvIV(*s) : 200; /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 1 17
IV status = (s && *s && SvOK(*s)) ? SvIV(*s) : 200; /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 0 16
IV status = (s && *s && SvOK(*s)) ? SvIV(*s) : 200; /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 16 16
193 1 AV *extra = punk_res_headers(aTHX_ res);
194 1 SV *bytes, *ct;
195 1 ▶ if (!status) status = 200; /* none given */
Branch: 1 branch
BranchTrueFalseTotal
if (!status) status = 200; /* none given */ 1 0 1
196 # uncoverable condition left count:2 class:ignore_covered_err
197 16 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 4 12 16
198 if (!(b && *b && SvOK(*b))) {
199 bytes = newSVpvs("");
200 ct = type ? type
201 4 : sv_2mortal(newSVpvs("text/plain; charset=utf-8"));
202 4 }
203 4 ▶ else if (punk_xml_is(aTHX_ *b)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (punk_xml_is(aTHX_ *b)) { 0 4 4
else if (punk_xml_is(aTHX_ *b)) { 0 0 0
204 4 ▶ /* before the reference arm below, which would otherwise hand a
Branch: 1 branch
BranchTrueFalseTotal
/* before the reference arm below, which would otherwise hand a 2 2 4
205 * document - a blessed reference like any other - to the JSON
206 12 ▶ * encoder */
Branch: 1 branch
BranchTrueFalseTotal
* encoder */ 2 10 12
207 2 SV *err = NULL;
208 bytes = punk_xml_bytes(aTHX_ *b, &err);
209 2 # uncoverable branch true
210 2 # uncoverable condition left count:1 class:ignore_covered_err
211 2 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 0 2 2
212 2 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 2 0 2
213 if (!bytes) croak("%s", err ? SvPV_nolen(err) : "Punk: xml");
214 ct = type ? type : sv_2mortal(newSVpvs(PK_XML_CT));
215 10 }
216 10 else if (SvROK(*b)) {
217 10 ▶ const frj_abi *J = punk_frj(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
const frj_abi *J = punk_frj(aTHX); 9 1 10
218 frj_opts o;
219 17 Zero(&o, 1, frj_opts);
220 o.canonical = 1;
221 bytes = J->encode(aTHX_ *b, canon ? &o : NULL); /* # uncoverable condition left count:1 class:ignore_covered_err */
222 ct = type ? type : sv_2mortal(newSVpvs("application/json")); /* # uncoverable condition right count:1 class:ignore_covered_err */
223 }
224 else {
225 bytes = newSVsv(*b);
226 ct = type ? type
227 : sv_2mortal(newSVpvs("text/html; charset=utf-8"));
228 }
229 RETVAL = punk_triplet(aTHX_ status, ct, bytes, extra);
230 }
231 OUTPUT:
232 RETVAL
« include/punk/punk_stream.h ↑ Summary xs/session.xs »