include/punk/punk_response.h

filter: Statementstmt 95.3% 41 / 43 Branchbran 75.0% 30 / 40 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.5% 71 / 83 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/computed.xs ↑ Summary include/punk/punk_i18n.h »
1 /* punk_response.h - C response assembly.
2 *
3 * Builds the PSGI triplet - status, header AV with Content-Type and
4 * Content-Length, single-element body AV - in one pass, and gives the
5 * data-return coercion a JSON fast path straight through
6 * File::Raw::JSON's C ABI (the encode returns an owned SV that drops
7 * into the body slot untouched - no lexical hop, no PV steal).
8 */
9  
10 #ifndef PUNK_RESPONSE_H
11 #define PUNK_RESPONSE_H
12  
13 /* extra: NULL, or an AV of header pairs appended after CT/CL.
14 * ct is the content type SV, taken as an owned (+1) value. */
15 218327 static SV *punk_triplet_own(pTHX_ IV status, SV *ct, SV *body, AV *extra) {
16 218327 AV *resp = newAV();
17 218327 AV *headers = newAV();
18 218327 AV *bodyav = newAV();
19 218327 av_extend(resp, 2);
20 218327 ▶ av_extend(headers, extra ? 3 + av_len(extra) + 1 : 3);
Branch: 1 branch
BranchTrueFalseTotal
av_extend(headers, extra ? 3 + av_len(extra) + 1 : 3); 220 218107 218327
21 218327 av_store(resp, 0, newSViv(status));
22 218327 av_store(headers, 0, newSVpvs("Content-Type"));
23 218327 av_store(headers, 1, ct);
24 218327 av_store(headers, 2, newSVpvs("Content-Length"));
25 218327 ▶ av_store(headers, 3, newSViv((IV)(SvOK(body) ? sv_len(body) : 0))); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_store(headers, 3, newSViv((IV)(SvOK(body) ? sv_len(body) : 0))); /* # uncoverable condition right count:1 class:ignore_covered_err */ 218327 0 218327
26 218327 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 220 218107 218327
27 220 SSize_t i, n = av_len(extra) + 1;
28 648 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 428 220 648
29 428 SV **e = av_fetch(extra, i, 0);
30 /*
31 # uncoverable condition right count:1 class:ignore_covered_err
32 # uncoverable condition right count:2 class:ignore_covered_err */
33 428 ▶ av_push(headers, e && *e ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(headers, e && *e ? newSVsv(*e) : newSV(0)); 428 0 428
av_push(headers, e && *e ? newSVsv(*e) : newSV(0)); 428 0 428
34 }
35 }
36 218327 av_store(resp, 1, newRV_noinc((SV *)headers));
37 218327 av_store(bodyav, 0, body); /* takes ownership */
38 218327 av_store(resp, 2, newRV_noinc((SV *)bodyav));
39 218327 return newRV_noinc((SV *)resp);
40 }
41  
42 /* the same with the content type borrowed (copied once) */
43 2401 static SV *punk_triplet(pTHX_ IV status, SV *ct, SV *body, AV *extra) {
44 2401 return punk_triplet_own(aTHX_ status, newSVsv(ct), body, extra);
45 }
46  
47 /* and with the content type as a C string: no mortal, one SV */
48 215926 static SV *punk_triplet_ct(pTHX_ IV status, const char *ct, STRLEN ctl,
49 SV *body, AV *extra) {
50 215926 return punk_triplet_own(aTHX_ status, newSVpvn(ct, ctl), body, extra);
51 }
52  
53 /* ---- the Punk::Response builder ------------------------------------------ *
54 * A plain blessed AV; lib/Punk/Response.pm is documentation only. Slots: */
55 enum {
56 PS_STATUS = 0,
57 PS_HEADERS = 1, /* live header-pair AV ref */
58 PS_TYPE = 2,
59 PS_BODY = 3
60 };
61  
62 183 static AV *punk_res_av(pTHX_ SV *self) {
63 /*
64 # uncoverable branch true
65 # uncoverable condition left count:1 class:ignore_covered_err
66 # uncoverable condition left count:2 class:ignore_covered_err */
67 183 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 183 0 183
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 0 183 183
68 0 croak("Punk::Response: not a response object"); /* # uncoverable statement */
69 183 return (AV *)SvRV(self);
70 }
71  
72 235 static AV *punk_res_headers(pTHX_ AV *res) {
73 235 SV **e = av_fetch(res, PS_HEADERS, 0);
74 /*
75 # uncoverable condition right count:2 class:ignore_covered_err
76 # uncoverable condition right count:3 class:ignore_covered_err
77 # uncoverable condition right count:4 class:ignore_covered_err */
78 235 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 32 203 235
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 32 0 32
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 32 0 32
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 32 0 32
79 32 return (AV *)SvRV(*e);
80 {
81 203 AV *h = newAV();
82 203 (void)av_store(res, PS_HEADERS, newRV_noinc((SV *)h));
83 203 return h;
84 }
85 }
86  
87 /* A status a handler asked for, as the number to send: 100 to 599, or 0 for
88 * "none given" (undef, 0, the empty string), which the caller turns into its
89 * default. Anything else croaks. It used to be passed through, and a status
90 * the dispatcher did not recognise made the finished response look like data:
91 * it was JSON-encoded, headers and all, and sent as a 200. */
92 110961 static IV ps_status_arg(pTHX_ SV *sv) {
93 IV st;
94 110961 ▶ if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 110961 0 110961
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 173 110788 110961
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 6 167 173
95 /*
96 # uncoverable branch true
97 # uncoverable condition left count:1 class:ignore_covered_err */
98 167 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 167 167
99 0 croak("Punk: a response status must be a number from 100 to 599"); /* # uncoverable statement */
100 167 ▶ st = (looks_like_number(sv) && (NV)SvIV(sv) == SvNV(sv)) ? SvIV(sv) : -1;
Branch: 2 branches
BranchTrueFalseTotal
st = (looks_like_number(sv) && (NV)SvIV(sv) == SvNV(sv)) ? SvIV(sv) : -1; 157 10 167
st = (looks_like_number(sv) && (NV)SvIV(sv) == SvNV(sv)) ? SvIV(sv) : -1; 152 5 157
101 167 ▶ if (st < 100 || st > 599)
Branch: 2 branches
BranchTrueFalseTotal
if (st < 100 || st > 599) 137 30 167
if (st < 100 || st > 599) 12 125 137
102 42 croak("Punk: a response status must be a number from 100 to 599, "
103 "not '%" SVf "'", SVfARG(sv));
104 125 return st;
105 }
106  
107 #endif /* PUNK_RESPONSE_H */
« xs/computed.xs ↑ Summary include/punk/punk_i18n.h »