xs/response.xs

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