include/punk/punk_response.h

filter: Statementstmt 95.3% 41 / 43 Branchbran 75.0% 30 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.5% 71 / 83 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/markdown.xs ↑ Summary include/punk/punk_sendfile.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 216507 static SV *punk_triplet_own(pTHX_ IV status, SV *ct, SV *body, AV *extra) {
16 216507 AV *resp = newAV();
17 216507 AV *headers = newAV();
18 216507 AV *bodyav = newAV();
19 216507 av_extend(resp, 2);
20 216507 ▶ av_extend(headers, extra ? 3 + av_len(extra) + 1 : 3);
Branch: 1 branch
BranchTrueFalseTotal
av_extend(headers, extra ? 3 + av_len(extra) + 1 : 3); 213 216294 216507
21 216507 av_store(resp, 0, newSViv(status));
22 216507 av_store(headers, 0, newSVpvs("Content-Type"));
23 216507 av_store(headers, 1, ct);
24 216507 av_store(headers, 2, newSVpvs("Content-Length"));
25 216507 ▶ av_store(headers, 3, newSViv((IV)(SvOK(body) ? sv_len(body) : 0)));
Branch: 1 branch
BranchTrueFalseTotal
av_store(headers, 3, newSViv((IV)(SvOK(body) ? sv_len(body) : 0))); 216507 0 216507
26 216507 ▶ if (extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra) { 213 216294 216507
27 213 SSize_t i, n = av_len(extra) + 1;
28 627 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 414 213 627
29 414 SV **e = av_fetch(extra, i, 0);
30 414 ▶ av_push(headers, e && *e ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(headers, e && *e ? newSVsv(*e) : newSV(0)); 414 0 414
av_push(headers, e && *e ? newSVsv(*e) : newSV(0)); 414 0 414
31 }
32 }
33 216507 av_store(resp, 1, newRV_noinc((SV *)headers));
34 216507 av_store(bodyav, 0, body); /* takes ownership */
35 216507 av_store(resp, 2, newRV_noinc((SV *)bodyav));
36 216507 return newRV_noinc((SV *)resp);
37 }
38  
39 /* the same with the content type borrowed (copied once) */
40 848 static SV *punk_triplet(pTHX_ IV status, SV *ct, SV *body, AV *extra) {
41 848 return punk_triplet_own(aTHX_ status, newSVsv(ct), body, extra);
42 }
43  
44 /* and with the content type as a C string: no mortal, one SV */
45 215659 static SV *punk_triplet_ct(pTHX_ IV status, const char *ct, STRLEN ctl,
46 SV *body, AV *extra) {
47 215659 return punk_triplet_own(aTHX_ status, newSVpvn(ct, ctl), body, extra);
48 }
49  
50 /* ---- the Punk::Response builder ------------------------------------------ *
51 * A plain blessed AV; lib/Punk/Response.pm is documentation only. Slots: */
52 enum {
53 PS_STATUS = 0,
54 PS_HEADERS = 1, /* live header-pair AV ref */
55 PS_TYPE = 2,
56 PS_BODY = 3
57 };
58  
59 126 static AV *punk_res_av(pTHX_ SV *self) {
60 126 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 126 0 126
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 0 126 126
61 0 croak("Punk::Response: not a response object");
62 126 return (AV *)SvRV(self);
63 }
64  
65 178 static AV *punk_res_headers(pTHX_ AV *res) {
66 178 SV **e = av_fetch(res, PS_HEADERS, 0);
67 178 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 27 151 178
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 27 0 27
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 27 0 27
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 27 0 27
68 27 return (AV *)SvRV(*e);
69 {
70 151 AV *h = newAV();
71 151 (void)av_store(res, PS_HEADERS, newRV_noinc((SV *)h));
72 151 return h;
73 }
74 }
75  
76 /* A status a handler asked for, as the number to send: 100 to 599, or 0 for
77 * "none given" (undef, 0, the empty string), which the caller turns into its
78 * default. Anything else croaks. It used to be passed through, and a status
79 * the dispatcher did not recognise made the finished response look like data:
80 * it was JSON-encoded, headers and all, and sent as a 200. */
81 110699 static IV ps_status_arg(pTHX_ SV *sv) {
82 IV st;
83 110699 ▶ if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; 110699 0 110699
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; 166 110533 110699
if (!(sv && SvOK(sv)) || !SvTRUE(sv)) return 0; 6 160 166
84 160 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 160 160
85 0 croak("Punk: a response status must be a number from 100 to 599");
86 160 ▶ 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; 150 10 160
st = (looks_like_number(sv) && (NV)SvIV(sv) == SvNV(sv)) ? SvIV(sv) : -1; 145 5 150
87 160 ▶ if (st < 100 || st > 599)
Branch: 2 branches
BranchTrueFalseTotal
if (st < 100 || st > 599) 130 30 160
if (st < 100 || st > 599) 12 118 130
88 42 croak("Punk: a response status must be a number from 100 to 599, "
89 "not '%" SVf "'", SVfARG(sv));
90 118 return st;
91 }
92  
93 #endif /* PUNK_RESPONSE_H */
« xs/markdown.xs ↑ Summary include/punk/punk_sendfile.h »