include/punk/punk_oamount.h

filter: Statementstmt 96.6% 260 / 269 Branchbran 64.8% 205 / 316 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.4% 465 / 585 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_state.h ↑ Summary include/punk/punk_host.h »
1 /* punk_oamount.h - the `api` mount, in C.
2 *
3 * One `api` mount is an OpenAPI document compiled by Open::API and dispatched
4 * to controller methods named after its operationIds. The request half was
5 * already C (punk_dispatch.h, over Open::API's oa_abi table); this is the boot
6 * half - controller discovery, the security-requirements-to-guards compiler,
7 * the per-spec-prefix guards, and the operation table they all fold into.
8 *
9 * It is boot-time work, so the win is not speed: it is that the mount no
10 * longer straddles two languages. The generated security guards used to be
11 * Perl closures built by a Perl compiler and then called from the C
12 * dispatcher; now they are magic CVs (punk_static.h's closure pattern)
13 * carrying their alternatives, so the whole path from route to controller is
14 * C calling the checkers the application registered.
15 *
16 * Two things still cross into Perl at boot, because the ABI has no C form for
17 * them: $api->operations and $api->spec. Both are single calls per mount.
18 *
19 * Must be included after punk_context.h (pcx_call_meth), punk_dispatch.h
20 * (punk_oa, the oa_abi table) and punk_static.h (punk_closure).
21 */
22  
23 #ifndef PUNK_OAMOUNT_H
24 #define PUNK_OAMOUNT_H
25  
26 /* The 401 body. Its Content-Length is taken from the string rather than
27 * written out beside it: the two had drifted, and a length two bytes short
28 * truncated the JSON on the wire - a parse error for every client refused. */
29 #define POA_UNAUTHORIZED "{\"errors\":[{\"message\":\"unauthorized\"}]}"
30  
31 198 static HV *poa_hv(pTHX_ SV *self) {
32 198 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 198 0 198
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 198 198
33 0 croak("Punk::Mount::OpenAPI: not a mount object");
34 198 return (HV *)SvRV(self);
35 }
36  
37 1019 static SV *poa_get(pTHX_ HV *h, const char *k) {
38 1019 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
39 1019 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 796 223 1019
return (e && *e) ? *e : NULL; 796 0 796
40 }
41  
42 632 static HV *poa_hash_of(pTHX_ SV *v) {
43 632 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 546 86 632
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 546 0 546
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 546 0 546
44 }
45 166 static AV *poa_av_of(pTHX_ SV *v) {
46 166 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 108 58 166
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 108 0 108
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 106 2 108
47 }
48  
49 /* $resolve->($target, $what) - the boot-time target resolver Punk::App hands
50 * every subsystem. Returns the coderef (+1). */
51 48 static SV *poa_resolve(pTHX_ SV *resolve, SV *target, const char *what) {
52 48 dSP; SV *r; int count;
53 48 ENTER; SAVETMPS;
54 48 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 48 48
PUSHMARK(SP); EXTEND(SP, 2); 0 48 48
55 48 PUSHs(target);
56 48 mPUSHp(what, strlen(what));
57 48 PUTBACK;
58 48 count = call_sv(resolve, G_SCALAR);
59 48 SPAGAIN;
60 48 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : NULL; 48 0 48
61 48 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 48 0 48
62 48 ▶ if (!r) croak("Punk: %s did not resolve", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Punk: %s did not resolve", what); 0 48 48
63 48 return r;
64 }
65  
66 /* $code->(@args) in scalar context. No G_EVAL: a checker that dies should
67 * propagate to the guard chain's own eval, exactly as the Perl did. +1. */
68 25 static SV *poa_call_args(pTHX_ SV *code, SV **argv, int n) {
69 25 dSP; SV *r; int count, i;
70 25 ENTER; SAVETMPS;
71 25 ▶ PUSHMARK(SP); EXTEND(SP, n);
Branch: 3 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, n); 0 25 25
PUSHMARK(SP); EXTEND(SP, n); 25 0 25
PUSHMARK(SP); EXTEND(SP, n); 0 25 25
72 125 ▶ for (i = 0; i < n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(argv[i]); 100 25 125
73 25 PUTBACK;
74 25 count = call_sv(code, G_SCALAR);
75 25 SPAGAIN;
76 25 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : NULL; 25 0 25
77 25 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 25 0 25
78 25 return r;
79 }
80  
81 /* ---- base64 (HTTP Basic credentials) --------------------------------------
82 * Decode only, and standard alphabet: the url-safe spelling never appears in
83 * an Authorization header. Returns NULL when the input is not base64. */
84 4 static SV *poa_b64_decode(pTHX_ const char *s, STRLEN len) {
85 4 SV *out = newSV(len + 1);
86 char *d;
87 4 unsigned v = 0;
88 4 int bits = 0;
89 STRLEN i;
90 4 SvPOK_on(out);
91 4 d = SvPVX(out);
92 44 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 43 1 44
93 43 char c = s[i];
94 int x;
95 43 ▶ if (c == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '=') { 2 41 43
96 /* padding ends the data: at most two, nothing after them, and
97 * the bits they pad must be zero - anything looser decodes two
98 * different headers to one credential */
99 2 STRLEN pad = 0;
100 6 ▶ while (i < len && s[i] == '=') { pad++; i++; }
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && s[i] == '=') { pad++; i++; } 4 2 6
while (i < len && s[i] == '=') { pad++; i++; } 4 0 4
101 2 ▶ if (i != len || pad > 2 || (bits && (v & ((1u << bits) - 1)))) {
Branch: 4 branches
BranchTrueFalseTotal
if (i != len || pad > 2 || (bits && (v & ((1u << bits) - 1)))) { 2 0 2
if (i != len || pad > 2 || (bits && (v & ((1u << bits) - 1)))) { 2 0 2
if (i != len || pad > 2 || (bits && (v & ((1u << bits) - 1)))) { 2 0 2
if (i != len || pad > 2 || (bits && (v & ((1u << bits) - 1)))) { 0 2 2
102 0 SvREFCNT_dec(out); return NULL;
103 }
104 2 break;
105 }
106 41 ▶ if (c >= 'A' && c <= 'Z') x = c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') x = c - 'A'; 34 7 41
if (c >= 'A' && c <= 'Z') x = c - 'A'; 19 15 34
107 22 ▶ else if (c >= 'a' && c <= 'z') x = c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; 15 7 22
else if (c >= 'a' && c <= 'z') x = c - 'a' + 26; 15 0 15
108 7 ▶ else if (c >= '0' && c <= '9') x = c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= '0' && c <= '9') x = c - '0' + 52; 6 1 7
else if (c >= '0' && c <= '9') x = c - '0' + 52; 6 0 6
109 1 ▶ else if (c == '+') x = 62;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '+') x = 62; 0 1 1
110 1 ▶ else if (c == '/') x = 63;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '/') x = 63; 0 1 1
111 1 else { SvREFCNT_dec(out); return NULL; }
112 40 v = (v << 6) | (unsigned)x;
113 40 bits += 6;
114 40 ▶ if (bits >= 8) { bits -= 8; *d++ = (char)((v >> bits) & 0xFF); }
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { bits -= 8; *d++ = (char)((v >> bits) & 0xFF); } 29 11 40
115 }
116 3 *d = '\0';
117 3 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
118 3 return out;
119 }
120  
121 /* case-insensitive "<prefix> " at the front; returns the rest, or NULL.
122 * Mirrors /\A<prefix> (.+)\z/i - so the remainder must be non-empty and, since
123 * '.' does not match a newline, must not contain one. */
124 11 static const char *poa_auth_param(const char *s, STRLEN len,
125 const char *word, STRLEN wlen,
126 STRLEN *out_len) {
127 STRLEN i;
128 11 ▶ if (len <= wlen + 1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= wlen + 1) return NULL; 0 11 11
129 68 ▶ for (i = 0; i < wlen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < wlen; i++) 58 10 68
130 58 ▶ if (toLOWER((U8)s[i]) != toLOWER((U8)word[i])) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (toLOWER((U8)s[i]) != toLOWER((U8)word[i])) return NULL; 10 48 58
if (toLOWER((U8)s[i]) != toLOWER((U8)word[i])) return NULL; 11 47 58
if (toLOWER((U8)s[i]) != toLOWER((U8)word[i])) return NULL; 1 57 58
131 10 ▶ if (s[wlen] != ' ') return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (s[wlen] != ' ') return NULL; 0 10 10
132 112 ▶ for (i = wlen + 1; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = wlen + 1; i < len; i++) 102 10 112
133 102 ▶ if (s[i] == '\n' || s[i] == '\r') return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '\n' || s[i] == '\r') return NULL; 102 0 102
if (s[i] == '\n' || s[i] == '\r') return NULL; 0 102 102
134 10 *out_len = len - wlen - 1;
135 10 return s + wlen + 1;
136 }
137  
138 /* The credential a securityScheme describes, pulled off the request the way
139 * the scheme says to. Mortal, or NULL. */
140 35 static SV *poa_credential(pTHX_ SV *c, HV *def) {
141 35 SV *type = poa_get(aTHX_ def, "type");
142 35 SV *req = pcx_call_meth(aTHX_ c, "req", NULL, 0, 1);
143 35 SV *out = NULL;
144 35 STRLEN tl = 0;
145 70 ▶ const char *t = (type && SvOK(type)) ? SvPV_const(type, tl) : "";
Branch: 2 branches
BranchTrueFalseTotal
const char *t = (type && SvOK(type)) ? SvPV_const(type, tl) : ""; 35 0 35
const char *t = (type && SvOK(type)) ? SvPV_const(type, tl) : ""; 35 0 35
146  
147 35 ▶ if (!req) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!req) return NULL; 0 35 35
148 35 sv_2mortal(req);
149  
150 35 ▶ if (tl == 6 && memEQ(t, "apiKey", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(t, "apiKey", 6)) { 21 14 35
if (tl == 6 && memEQ(t, "apiKey", 6)) { 21 0 21
151 21 SV *in = poa_get(aTHX_ def, "in");
152 21 SV *name = poa_get(aTHX_ def, "name");
153 21 STRLEN il = 0;
154 42 ▶ const char *ins = (in && SvOK(in)) ? SvPV_const(in, il) : "header";
Branch: 2 branches
BranchTrueFalseTotal
const char *ins = (in && SvOK(in)) ? SvPV_const(in, il) : "header"; 21 0 21
const char *ins = (in && SvOK(in)) ? SvPV_const(in, il) : "header"; 21 0 21
155 21 ▶ if (!(in && SvOK(in))) il = 6;
Branch: 2 branches
BranchTrueFalseTotal
if (!(in && SvOK(in))) il = 6; 21 0 21
if (!(in && SvOK(in))) il = 6; 0 21 21
156 21 ▶ if (!name) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return NULL; 0 21 21
157 38 ▶ if (il == 6 && memEQ(ins, "header", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (il == 6 && memEQ(ins, "header", 6)) { 19 2 21
if (il == 6 && memEQ(ins, "header", 6)) { 17 2 19
158 17 SV *argv[1]; argv[0] = name;
159 17 out = pcx_call_meth(aTHX_ req, "header", argv, 1, 1);
160 }
161 6 ▶ else if (il == 5 && memEQ(ins, "query", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (il == 5 && memEQ(ins, "query", 5)) { 2 2 4
else if (il == 5 && memEQ(ins, "query", 5)) { 2 0 2
162 2 SV *q = pcx_call_meth(aTHX_ req, "query", NULL, 0, 1);
163 2 HV *qh = poa_hash_of(aTHX_ q);
164 2 ▶ if (qh) {
Branch: 1 branch
BranchTrueFalseTotal
if (qh) { 2 0 2
165 2 HE *he = hv_fetch_ent(qh, name, 0, 0);
166 2 ▶ if (he) out = newSVsv(HeVAL(he));
Branch: 1 branch
BranchTrueFalseTotal
if (he) out = newSVsv(HeVAL(he)); 2 0 2
167 }
168 2 ▶ if (q) SvREFCNT_dec(q);
Branch: 1 branch
BranchTrueFalseTotal
if (q) SvREFCNT_dec(q); 2 0 2
169 }
170 2 ▶ else if (il == 6 && memEQ(ins, "cookie", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (il == 6 && memEQ(ins, "cookie", 6)) { 2 0 2
else if (il == 6 && memEQ(ins, "cookie", 6)) { 2 0 2
171 2 SV *argv[1]; argv[0] = name;
172 2 out = pcx_call_meth(aTHX_ req, "cookie", argv, 1, 1);
173 }
174 21 ▶ return out ? sv_2mortal(out) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return out ? sv_2mortal(out) : NULL; 21 0 21
175 }
176  
177 {
178 SV *argv[1], *auth;
179 STRLEN al;
180 const char *as;
181 14 argv[0] = sv_2mortal(newSVpvs("authorization"));
182 14 auth = pcx_call_meth(aTHX_ req, "header", argv, 1, 1);
183 14 ▶ if (!(auth && SvOK(auth))) { if (auth) SvREFCNT_dec(auth); return NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (!(auth && SvOK(auth))) { if (auth) SvREFCNT_dec(auth); return NULL; } 14 0 14
if (!(auth && SvOK(auth))) { if (auth) SvREFCNT_dec(auth); return NULL; } 3 11 14
if (!(auth && SvOK(auth))) { if (auth) SvREFCNT_dec(auth); return NULL; } 3 0 3
184 11 sv_2mortal(auth);
185 11 as = SvPV_const(auth, al);
186  
187 11 ▶ if (tl == 4 && memEQ(t, "http", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 4 && memEQ(t, "http", 4)) { 11 0 11
if (tl == 4 && memEQ(t, "http", 4)) { 11 0 11
188 11 SV *scheme = poa_get(aTHX_ def, "scheme");
189 11 STRLEN sl = 0;
190 11 ▶ const char *ss = (scheme && SvOK(scheme))
Branch: 1 branch
BranchTrueFalseTotal
const char *ss = (scheme && SvOK(scheme)) 11 0 11
191 22 ▶ ? SvPV_const(scheme, sl) : "";
Branch: 1 branch
BranchTrueFalseTotal
? SvPV_const(scheme, sl) : ""; 11 0 11
192 11 ▶ if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a'
Branch: 5 branches
BranchTrueFalseTotal
if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a' 5 6 11
if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a' 0 5 5
if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a' 5 0 5
if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a' 0 5 5
if (sl == 5 && toLOWER((U8)ss[0]) == 'b' && toLOWER((U8)ss[1]) == 'a' 5 0 5
193 5 ▶ && toLOWER((U8)ss[2]) == 's' && toLOWER((U8)ss[3]) == 'i'
Branch: 4 branches
BranchTrueFalseTotal
&& toLOWER((U8)ss[2]) == 's' && toLOWER((U8)ss[3]) == 'i' 0 5 5
&& toLOWER((U8)ss[2]) == 's' && toLOWER((U8)ss[3]) == 'i' 5 0 5
&& toLOWER((U8)ss[2]) == 's' && toLOWER((U8)ss[3]) == 'i' 0 5 5
&& toLOWER((U8)ss[2]) == 's' && toLOWER((U8)ss[3]) == 'i' 5 0 5
194 5 ▶ && toLOWER((U8)ss[4]) == 'c') {
Branch: 2 branches
BranchTrueFalseTotal
&& toLOWER((U8)ss[4]) == 'c') { 0 5 5
&& toLOWER((U8)ss[4]) == 'c') { 5 0 5
195 STRLEN pl;
196 5 const char *p = poa_auth_param(as, al, "Basic", 5, &pl);
197 5 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 1 4 5
198 4 out = poa_b64_decode(aTHX_ p, pl);
199 4 ▶ return out ? sv_2mortal(out) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return out ? sv_2mortal(out) : NULL; 3 1 4
200 }
201 }
202 /* http bearer, oauth2 and openIdConnect all verify an issued token */
203 {
204 STRLEN pl;
205 6 const char *p = poa_auth_param(as, al, "Bearer", 6, &pl);
206 6 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 6 6
207 6 return sv_2mortal(newSVpvn(p, pl));
208 }
209 }
210 }
211  
212 /* ---- the generated per-operation security guard ----------------------------
213 *
214 * Alternatives are OR, the schemes within one alternative are AND, and the
215 * eldest passing alternative authorizes (its results land in
216 * $c->stash->{auth}); none passing answers 401 before any body is read.
217 *
218 * Captured: [ $op_id, \@alts ] where each alt is an AV of
219 * [ $name, \%def, $checker, $scopes ]. */
220  
221 XS_INTERNAL(punk_oa_security_cb);
222 29 XS_INTERNAL(punk_oa_security_cb) {
223 29 dXSARGS;
224 29 AV *cap = punk_clos_cap(aTHX_ cv);
225 29 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 29 0 29
226 SV *op_id, *alts_sv;
227 AV *alts;
228 SSize_t ai, an;
229  
230 29 ▶ if (!cap || items < 1) croak("Punk: a security guard lost its captures");
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) croak("Punk: a security guard lost its captures"); 29 0 29
if (!cap || items < 1) croak("Punk: a security guard lost its captures"); 0 29 29
231 29 op_id = *av_fetch(cap, 0, 0);
232 29 alts_sv = *av_fetch(cap, 1, 0);
233 29 alts = (AV *)SvRV(alts_sv);
234 29 an = av_len(alts) + 1;
235  
236 46 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 32 14 46
237 32 AV *alt = (AV *)SvRV(*av_fetch(alts, ai, 0));
238 32 SSize_t pi, pn = av_len(alt) + 1;
239 32 HV *auth = newHV();
240 32 int ok = 1;
241  
242 50 ▶ for (pi = 0; pi < pn; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < pn; pi++) { 35 15 50
243 35 AV *part = (AV *)SvRV(*av_fetch(alt, pi, 0));
244 35 SV *name = *av_fetch(part, 0, 0);
245 35 HV *def = (HV *)SvRV(*av_fetch(part, 1, 0));
246 35 SV *checker = *av_fetch(part, 2, 0);
247 35 SV **scp = av_fetch(part, 3, 0);
248 35 SV *cred = poa_credential(aTHX_ c, def);
249 SV *got;
250  
251 /* a repeated query parameter arrives as an arrayref: that is
252 * no credential, not a string whose length is a pointer's */
253 35 ▶ if (!(cred && SvOK(cred) && !SvROK(cred) && SvCUR(cred))) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(cred && SvOK(cred) && !SvROK(cred) && SvCUR(cred))) { 30 5 35
if (!(cred && SvOK(cred) && !SvROK(cred) && SvCUR(cred))) { 25 5 30
if (!(cred && SvOK(cred) && !SvROK(cred) && SvCUR(cred))) { 25 0 25
if (!(cred && SvOK(cred) && !SvROK(cred) && SvCUR(cred))) { 0 25 25
254 10 ok = 0; break;
255 }
256 {
257 SV *argv[4];
258 25 argv[0] = cred;
259 25 argv[1] = c;
260 25 argv[2] = op_id;
261 25 ▶ argv[3] = (scp && *scp) ? *scp : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[3] = (scp && *scp) ? *scp : &PL_sv_undef; 25 0 25
argv[3] = (scp && *scp) ? *scp : &PL_sv_undef; 25 0 25
262 25 got = poa_call_args(aTHX_ checker, argv, 4);
263 }
264 25 ▶ if (!(got && SvTRUE(got))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(got && SvTRUE(got))) { 25 0 25
if (!(got && SvTRUE(got))) { 7 18 25
265 7 ▶ if (got) SvREFCNT_dec(got);
Branch: 1 branch
BranchTrueFalseTotal
if (got) SvREFCNT_dec(got); 7 0 7
266 7 ok = 0; break;
267 }
268 18 (void)hv_store_ent(auth, name, got, 0); /* takes the +1 */
269 }
270  
271 32 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 15 17 32
272 15 SV *stash = pcx_call_meth(aTHX_ c, "stash", NULL, 0, 1);
273 15 HV *sh = poa_hash_of(aTHX_ stash);
274 15 ▶ if (sh) (void)hv_stores(sh, "auth", newRV_noinc((SV *)auth));
Branch: 1 branch
BranchTrueFalseTotal
if (sh) (void)hv_stores(sh, "auth", newRV_noinc((SV *)auth)); 15 0 15
275 0 else SvREFCNT_dec((SV *)auth);
276 15 ▶ if (stash) SvREFCNT_dec(stash);
Branch: 1 branch
BranchTrueFalseTotal
if (stash) SvREFCNT_dec(stash); 15 0 15
277 15 XSRETURN_EMPTY; /* authorized: the chain continues */
278 }
279 17 SvREFCNT_dec((SV *)auth);
280 }
281  
282 {
283 14 SV *body = newSVpvs(POA_UNAUTHORIZED);
284 14 AV *hdrs = newAV();
285 14 AV *bod = newAV();
286 14 AV *trip = newAV();
287 14 av_push(hdrs, newSVpvs("Content-Type"));
288 14 av_push(hdrs, newSVpvs("application/json"));
289 14 av_push(hdrs, newSVpvs("Content-Length"));
290 14 av_push(hdrs, newSViv((IV)SvCUR(body)));
291 14 av_push(bod, body);
292 14 av_push(trip, newSViv(401));
293 14 av_push(trip, newRV_noinc((SV *)hdrs));
294 14 av_push(trip, newRV_noinc((SV *)bod));
295 14 ST(0) = sv_2mortal(newRV_noinc((SV *)trip));
296 14 XSRETURN(1);
297 }
298 }
299  
300 /* ---- controller discovery --------------------------------------------------
301 *
302 * Every .pm under the namespace directory, across @INC, loaded; the class
303 * names come back for the operationId search. */
304  
305 /* A loadable class name: the only files that can define one are the ones whose
306 * path is made of identifiers, so anything else is skipped rather than
307 * required. It also makes the name safe to interpolate into the eval below. */
308 11 static int poa_ident_path(const char *s, STRLEN len) {
309 11 STRLEN i = 0;
310 11 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 11 11
311 44 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 44 0 44
312 44 STRLEN start = i;
313 44 ▶ if (!(isALPHA((U8)s[i]) || s[i] == '_')) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(isALPHA((U8)s[i]) || s[i] == '_')) return 0; 0 44 44
if (!(isALPHA((U8)s[i]) || s[i] == '_')) return 0; 0 0 0
314 311 ▶ while (i < len && (isWORDCHAR((U8)s[i]))) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && (isWORDCHAR((U8)s[i]))) i++; 300 11 311
while (i < len && (isWORDCHAR((U8)s[i]))) i++; 267 33 300
315 44 ▶ if (i == start) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i == start) return 0; 0 44 44
316 44 ▶ if (i == len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) return 1; 11 33 44
317 33 ▶ if (!(s[i] == ':' && i + 1 < len && s[i + 1] == ':')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(s[i] == ':' && i + 1 < len && s[i + 1] == ':')) return 0; 33 0 33
if (!(s[i] == ':' && i + 1 < len && s[i + 1] == ':')) return 0; 33 0 33
if (!(s[i] == ':' && i + 1 < len && s[i + 1] == ':')) return 0; 0 33 33
318 33 i += 2;
319 }
320 0 return 0;
321 }
322  
323 231 static void poa_walk_dir(pTHX_ SV *inc, SV *rel, HV *seen, AV *out) {
324 231 SV *dir = sv_2mortal(newSVsv(inc));
325 DIR *dh;
326 Direntry_t *de;
327 231 AV *ents = (AV *)sv_2mortal((SV *)newAV());
328 SSize_t i, n;
329  
330 231 sv_catpvs(dir, "/");
331 231 sv_catsv(dir, rel);
332 231 dh = PerlDir_open(SvPV_nolen(dir));
333 231 ▶ if (!dh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!dh) return; 224 7 231
334 32 ▶ while ((de = PerlDir_read(dh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((de = PerlDir_read(dh))) { 25 7 32
335 25 const char *nm = de->d_name;
336 25 ▶ if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
Branch: 4 branches
BranchTrueFalseTotal
if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 14 11 25
if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 7 7 14
if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 7 0 7
if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 7 0 7
337 14 continue;
338 11 av_push(ents, newSVpv(nm, 0));
339 }
340 7 PerlDir_close(dh);
341  
342 /* sorted so the traversal is stable across filesystems */
343 7 n = av_len(ents) + 1;
344 7 ▶ if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp); 2 5 7
345  
346 18 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 7 18
347 11 SV *ent = *av_fetch(ents, i, 0);
348 STRLEN el;
349 11 const char *es = SvPV_const(ent, el);
350 11 SV *child = sv_2mortal(newSVsv(rel));
351 11 SV *full = sv_2mortal(newSVsv(dir));
352 Stat_t st;
353 11 sv_catpvs(child, "/"); sv_catsv(child, ent);
354 11 sv_catpvs(full, "/"); sv_catsv(full, ent);
355  
356 11 ▶ if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) { 11 0 11
if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) { 0 11 11
357 0 poa_walk_dir(aTHX_ inc, child, seen, out);
358 0 continue;
359 }
360 11 ▶ if (!(el > 3 && memEQ(es + el - 3, ".pm", 3))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(el > 3 && memEQ(es + el - 3, ".pm", 3))) continue; 11 0 11
if (!(el > 3 && memEQ(es + el - 3, ".pm", 3))) continue; 0 11 11
361  
362 {
363 /* the @INC-relative path minus .pm, as a class name */
364 11 SV *mod = sv_2mortal(newSVpvn(SvPVX(child), SvCUR(child) - 3));
365 11 char *p = SvPVX(mod);
366 11 STRLEN j, ml = SvCUR(mod);
367 11 SV *colons = sv_2mortal(newSVpvs(""));
368 311 ▶ for (j = 0; j < ml; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ml; j++) { 300 11 311
369 300 ▶ if (p[j] == '/') sv_catpvs(colons, "::");
Branch: 1 branch
BranchTrueFalseTotal
if (p[j] == '/') sv_catpvs(colons, "::"); 33 267 300
370 267 else sv_catpvn(colons, p + j, 1);
371 }
372 11 ▶ if (!poa_ident_path(SvPVX(colons), SvCUR(colons))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!poa_ident_path(SvPVX(colons), SvCUR(colons))) continue; 0 11 11
373 11 ▶ if (hv_exists_ent(seen, colons, 0)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(seen, colons, 0)) continue; 0 11 11
374 11 (void)hv_store_ent(seen, colons, newSViv(1), 0);
375  
376 /* Require by module name, which is the same %INC key
377 * `require Foo::Bar` and Punk::App's target resolver both use.
378 * Loading by absolute path instead made one file two %INC
379 * entries and ran it twice - "Subroutine redefined" for every
380 * controller reachable both ways, and two generations of the
381 * same coderefs. */
382 {
383 11 SV *code = sv_2mortal(newSVpvs("require "));
384 11 sv_catsv(code, colons);
385 11 sv_catpvs(code, "; 1");
386 11 eval_sv(code, G_SCALAR | G_DISCARD);
387 11 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 11 0 11
if (SvTRUE(ERRSV)) 0 11 11
388 0 ▶ croak("Punk: controller %s failed to load: %s",
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk: controller %s failed to load: %s", 0 0 0
croak("Punk: controller %s failed to load: %s", 0 0 0
389 SvPV_nolen(colons), SvPV_nolen(ERRSV));
390 }
391 11 av_push(out, newSVsv(colons));
392 }
393 }
394 }
395  
396 /* The classes under $ns, loaded, sorted. Mortal AV. */
397 28 static AV *poa_load_namespace(pTHX_ SV *ns) {
398 28 AV *out = (AV *)sv_2mortal((SV *)newAV());
399 28 HV *seen = (HV *)sv_2mortal((SV *)newHV());
400 28 AV *inc = GvAV(PL_incgv);
401 28 ▶ SSize_t i, n = inc ? av_len(inc) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = inc ? av_len(inc) + 1 : 0; 28 0 28
402 STRLEN nl, j;
403 28 const char *ns_s = SvPV_const(ns, nl);
404 28 SV *rel = sv_2mortal(newSVpvs(""));
405  
406 636 ▶ for (j = 0; j < nl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { 608 28 636
407 608 ▶ if (ns_s[j] == ':' && j + 1 < nl && ns_s[j + 1] == ':') {
Branch: 3 branches
BranchTrueFalseTotal
if (ns_s[j] == ':' && j + 1 < nl && ns_s[j + 1] == ':') { 56 552 608
if (ns_s[j] == ':' && j + 1 < nl && ns_s[j + 1] == ':') { 56 0 56
if (ns_s[j] == ':' && j + 1 < nl && ns_s[j + 1] == ':') { 56 0 56
408 56 sv_catpvs(rel, "/"); j++;
409 }
410 552 else sv_catpvn(rel, ns_s + j, 1);
411 }
412  
413 259 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 231 28 259
414 231 SV **ip = av_fetch(inc, i, 0);
415 231 ▶ if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 231 0 231
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 231 0 231
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 231 0 231
if (!ip || !*ip || SvROK(*ip) || !SvOK(*ip)) continue; 0 231 231
416 231 poa_walk_dir(aTHX_ *ip, rel, seen, out);
417 }
418 28 n = av_len(out) + 1;
419 28 ▶ if (n > 1) sortsv(AvARRAY(out), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(out), (STRLEN)n, Perl_sv_cmp); 2 26 28
420 28 return out;
421 }
422  
423 /* operationId -> coderef: an explicit handler first, then the one discovered
424 * class implementing it. Two implementers is an ambiguity the application has
425 * to resolve; none is only allowed under `stub`. Returns +1, or NULL. */
426 71 static SV *poa_find_handler(pTHX_ SV *id, HV *handlers, AV *classes,
427 SV *resolve) {
428 71 ▶ HE *he = handlers ? hv_fetch_ent(handlers, id, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = handlers ? hv_fetch_ent(handlers, id, 0, 0) : NULL; 38 33 71
429 71 SSize_t i, n = av_len(classes) + 1;
430 71 SV *found = NULL, *names = NULL;
431 71 int hits = 0;
432  
433 71 ▶ if (he && SvOK(HeVAL(he))) {
Branch: 2 branches
BranchTrueFalseTotal
if (he && SvOK(HeVAL(he))) { 33 38 71
if (he && SvOK(HeVAL(he))) { 33 0 33
434 33 SV *what = sv_2mortal(newSVpvs("handler for '"));
435 33 sv_catsv(what, id);
436 33 sv_catpvs(what, "'");
437 33 return poa_resolve(aTHX_ resolve, HeVAL(he), SvPV_nolen(what));
438 }
439  
440 71 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 33 38 71
441 33 SV *cls = *av_fetch(classes, i, 0);
442 33 HV *stash = gv_stashsv(cls, 0);
443 33 GV *gv = stash ? gv_fetchmethod_autoload(stash, SvPV_nolen(id), 0)
444 33 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 33 0 33
445 33 ▶ if (!(gv && GvCV(gv))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(gv && GvCV(gv))) continue; 21 12 33
if (!(gv && GvCV(gv))) continue; 0 21 21
446 21 hits++;
447 21 ▶ if (!found) found = newRV_inc((SV *)GvCV(gv));
Branch: 1 branch
BranchTrueFalseTotal
if (!found) found = newRV_inc((SV *)GvCV(gv)); 21 0 21
448 21 ▶ if (!names) names = sv_2mortal(newSVsv(cls));
Branch: 1 branch
BranchTrueFalseTotal
if (!names) names = sv_2mortal(newSVsv(cls)); 21 0 21
449 0 else { sv_catpvs(names, ", "); sv_catsv(names, cls); }
450 }
451 38 ▶ if (hits > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (hits > 1) 0 38 38
452 0 ▶ croak("Punk: operation '%s' is implemented by more than one "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: operation '%s' is implemented by more than one " 0 0 0
453 "controller (%s)", SvPV_nolen(id), SvPV_nolen(names));
454 38 return found;
455 }
456  
457 #endif /* PUNK_OAMOUNT_H */
« include/punk/punk_state.h ↑ Summary include/punk/punk_host.h »