include/pmcp/pmcp_reg.h

filter: Statementstmt 97.2% 211 / 217 Branchbran 80.0% 232 / 290 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.3% 443 / 507 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_res.h ↑ Summary include/pmcp/pmcp_sess.h »
1 /* pmcp_reg.h - reading the plugin's options, and the calls into Punk's
2 * registrar that install a keyword, a to_app callback and a middleware.
3 *
4 * Needs: pmcp_clos.h.
5 * Gives: pmcp_opts (validate `plugin 'MCP' => {...}`), pmcp_keyword,
6 * pmcp_at_compile, pmcp_middleware, pmcp_can, pmcp_hget.
7 *
8 * Every registration is a method call. Punk installs pk_abi.h, an observer
9 * table with no install_kw, route or on_compile, so a plugin reaches the
10 * registrar through its Perl surface as Punk-Feed does.
11 */
12  
13 #ifndef PMCP_REG_H
14 #define PMCP_REG_H
15  
16 #define PMCP_WHO "Punk::Plugin::MCP"
17  
18 /* A declaration record, as the `mcp` keyword stores it (pmcp_state.h). */
19 enum { PMCP_D_KIND = 0, PMCP_D_KEY = 1, PMCP_D_SPEC = 2, PMCP_D_TARGET = 3 };
20  
21 /* ---- small readers -------------------------------------------------------- */
22  
23 /* One key of a hash, borrowed, or NULL when absent or undef. */
24 27048 static SV *pmcp_hget(pTHX_ HV *h, const char *k)
25 {
26 27048 ▶ SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; 27048 0 27048
27 27048 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 12127 14921 27048
return (e && *e && SvOK(*e)) ? *e : NULL; 12127 0 12127
return (e && *e && SvOK(*e)) ? *e : NULL; 12116 11 12127
28 }
29  
30 /* Does this class or object have that method? */
31 223 static int pmcp_can(pTHX_ SV *obj, const char *meth)
32 {
33 223 SV *m = sv_2mortal(newSVpv(meth, 0));
34 223 SV *r = pmcp_call_method(aTHX_ obj, "can", &m, 1, G_SCALAR, NULL);
35 223 ▶ int yes = r && SvTRUE(r);
Branch: 2 branches
BranchTrueFalseTotal
int yes = r && SvTRUE(r); 223 0 223
int yes = r && SvTRUE(r); 221 2 223
36 223 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 223 0 223
37 223 return yes;
38 }
39  
40 /* A non-empty string that is not a reference. */
41 1613 static int pmcp_is_text(SV *sv)
42 {
43 1613 ▶ return sv && SvOK(sv) && !SvROK(sv) && SvCUR(sv) > 0;
Branch: 4 branches
BranchTrueFalseTotal
return sv && SvOK(sv) && !SvROK(sv) && SvCUR(sv) > 0; 1611 2 1613
return sv && SvOK(sv) && !SvROK(sv) && SvCUR(sv) > 0; 1610 1 1611
return sv && SvOK(sv) && !SvROK(sv) && SvCUR(sv) > 0; 1592 18 1610
return sv && SvOK(sv) && !SvROK(sv) && SvCUR(sv) > 0; 1589 3 1592
44 }
45  
46 /* A code ref, or a 'Controller#method' target. Either is what a route takes. */
47 431 static int pmcp_is_target(pTHX_ SV *sv)
48 {
49 PERL_UNUSED_CONTEXT;
50 431 ▶ return pmcp_is_code(sv) || pmcp_is_text(sv);
Branch: 2 branches
BranchTrueFalseTotal
return pmcp_is_code(sv) || pmcp_is_text(sv); 371 60 431
return pmcp_is_code(sv) || pmcp_is_text(sv); 361 10 371
51 }
52  
53 /* ---- the options ----------------------------------------------------------- */
54  
55 static const char *const PMCP_OPTS[] = {
56 "path", "name", "title", "version", "description", "website", "icons",
57 "instructions", "auth", "oauth", "principal", "openapi", "ops",
58 "session", "session_ttl", "max_sessions", "stream", "origins",
59 "page_size", "max_body", "logging", "timeout", "stdio_trust", NULL
60 };
61  
62 8 static SV *pmcp_name_list(pTHX_ const char *const *names)
63 {
64 8 SV *out = sv_2mortal(newSVpvs(""));
65 int i;
66 76 ▶ for (i = 0; names[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; names[i]; i++) { 68 8 76
67 68 ▶ if (i) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, ", "); 60 8 68
68 68 sv_catpv(out, names[i]);
69 }
70 8 return out;
71 }
72  
73 /* Every key of `h` must be in `known`. A misspelled option is a setting that
74 * silently did not apply, so it croaks naming the key and what was meant. */
75 729 static void pmcp_check_keys(pTHX_ const char *noun, HV *h,
76 const char *const *known)
77 {
78 HE *he;
79 729 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 729 729
80 729 hv_iterinit(h);
81 1820 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 1099 721 1820
82 STRLEN kl;
83 1099 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 1099 1099
84 1099 int i, ok = 0;
85 7253 ▶ for (i = 0; known[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) 7245 8 7253
86 7245 ▶ if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } 2049 5196 7245
if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } 1091 958 2049
87 1099 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 8 1091 1099
88 16 croak("%s: unknown %s '%.*s' (known: %s)", PMCP_WHO, noun,
89 (int)kl, k, SvPV_nolen(pmcp_name_list(aTHX_ known)));
90 }
91 }
92  
93 23 static void pmcp_opt_croak(pTHX_ const char *opt, const char *want)
94 {
95 23 croak("%s: option '%s' must be %s", PMCP_WHO, opt, want);
96 }
97  
98 /* A positive whole number, or the default when absent. */
99 695 static IV pmcp_opt_count(pTHX_ HV *in, const char *k, IV dflt, IV max)
100 {
101 695 SV *v = pmcp_hget(aTHX_ in, k);
102 IV n;
103 695 ▶ if (!v) return dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return dflt; 678 17 695
104 17 ▶ if (SvROK(v) || !looks_like_number(v) || SvNV(v) != (NV)SvIV(v)
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(v) || !looks_like_number(v) || SvNV(v) != (NV)SvIV(v) 17 0 17
if (SvROK(v) || !looks_like_number(v) || SvNV(v) != (NV)SvIV(v) 16 1 17
if (SvROK(v) || !looks_like_number(v) || SvNV(v) != (NV)SvIV(v) 15 1 16
105 15 ▶ || (n = SvIV(v)) < 1 || (max && n > max)) {
Branch: 3 branches
BranchTrueFalseTotal
|| (n = SvIV(v)) < 1 || (max && n > max)) { 13 2 15
|| (n = SvIV(v)) < 1 || (max && n > max)) { 5 8 13
|| (n = SvIV(v)) < 1 || (max && n > max)) { 1 4 5
106 5 ▶ if (max) croak("%s: option '%s' must be a whole number from 1 to %"
Branch: 1 branch
BranchTrueFalseTotal
if (max) croak("%s: option '%s' must be a whole number from 1 to %" 1 4 5
107 IVdf, PMCP_WHO, k, max);
108 4 pmcp_opt_croak(aTHX_ k, "a positive whole number");
109 }
110 12 return n;
111 }
112  
113 /* An origin: scheme://host[:port], no path. */
114 9 static int pmcp_is_origin(const char *p, STRLEN l)
115 {
116 9 const char *h, *e = p + l;
117 9 ▶ if (l > 8 && memEQ(p, "https://", 8)) h = p + 8;
Branch: 2 branches
BranchTrueFalseTotal
if (l > 8 && memEQ(p, "https://", 8)) h = p + 8; 8 1 9
if (l > 8 && memEQ(p, "https://", 8)) h = p + 8; 7 1 8
118 2 ▶ else if (l > 7 && memEQ(p, "http://", 7)) h = p + 7;
Branch: 2 branches
BranchTrueFalseTotal
else if (l > 7 && memEQ(p, "http://", 7)) h = p + 7; 1 1 2
else if (l > 7 && memEQ(p, "http://", 7)) h = p + 7; 1 0 1
119 1 else return 0;
120 8 ▶ if (h >= e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (h >= e) return 0; 0 8 8
121 102 ▶ for (; h < e; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (; h < e; h++) 95 7 102
122 95 ▶ if (*h == '/' || *h == '?' || *h == '#' || *h == ' ') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (*h == '/' || *h == '?' || *h == '#' || *h == ' ') return 0; 94 1 95
if (*h == '/' || *h == '?' || *h == '#' || *h == ' ') return 0; 94 0 94
if (*h == '/' || *h == '?' || *h == '#' || *h == ' ') return 0; 94 0 94
if (*h == '/' || *h == '?' || *h == '#' || *h == ' ') return 0; 0 94 94
123 7 return 1;
124 }
125  
126 /* An icon list: [{src, mimeType?, sizes?}, ...], src https or data:. */
127 8 static void pmcp_check_icons(pTHX_ SV *v)
128 {
129 AV *av;
130 SSize_t i, n;
131 static const char *const keys[] = { "src", "mimeType", "sizes", "theme", NULL };
132 8 ▶ if (!pmcp_is_array(v))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(v)) 1 7 8
133 1 pmcp_opt_croak(aTHX_ "icons", "a list of {src, mimeType, sizes}");
134 7 av = (AV *)SvRV(v);
135 7 n = av_len(av) + 1;
136 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 4 11
137 7 SV **e = av_fetch(av, i, 0);
138 SV *src;
139 STRLEN l;
140 const char *p;
141 7 ▶ if (!(e && pmcp_is_hash(*e)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_hash(*e))) 7 0 7
if (!(e && pmcp_is_hash(*e))) 0 7 7
142 0 pmcp_opt_croak(aTHX_ "icons", "a list of {src, mimeType, sizes}");
143 7 pmcp_check_keys(aTHX_ "icon key", (HV *)SvRV(*e), keys);
144 6 src = pmcp_hget(aTHX_ (HV *)SvRV(*e), "src");
145 6 ▶ if (!pmcp_is_text(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(src)) 0 6 6
146 0 pmcp_opt_croak(aTHX_ "icons", "a list of icons that each have a src");
147 6 p = SvPV_const(src, l);
148 6 ▶ if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 5 && memEQ(p, "data:", 5))))
Branch: 4 branches
BranchTrueFalseTotal
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 5 && memEQ(p, "data:", 5)))) 5 1 6
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 5 && memEQ(p, "data:", 5)))) 1 4 5
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 5 && memEQ(p, "data:", 5)))) 2 0 2
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 5 && memEQ(p, "data:", 5)))) 2 0 2
149 2 croak("%s: an icon src must be an https or data: URI, not '%.*s'",
150 PMCP_WHO, (int)l, p);
151 }
152 4 }
153  
154 /* A guard: a code ref or a 'Controller#method' string, or a list of them. */
155 52 static void pmcp_check_guards(pTHX_ const char *opt, SV *v)
156 {
157 52 ▶ if (pmcp_is_target(aTHX_ v)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_target(aTHX_ v)) return; 44 8 52
158 8 ▶ if (pmcp_is_array(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_array(v)) { 5 3 8
159 5 AV *av = (AV *)SvRV(v);
160 5 SSize_t i, n = av_len(av) + 1;
161 5 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 3 2 5
162 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 3 9
163 6 SV **e = av_fetch(av, i, 0);
164 6 ▶ if (!(e && pmcp_is_target(aTHX_ *e))) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_target(aTHX_ *e))) break; 6 0 6
if (!(e && pmcp_is_target(aTHX_ *e))) break; 6 0 6
165 }
166 3 ▶ if (i == n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (i == n) return; 3 0 3
167 }
168 }
169 5 pmcp_opt_croak(aTHX_ opt, "a guard (a code ref or 'Controller#method') "
170 "or a list of them");
171 }
172  
173 /* Validate `plugin 'MCP' => {...}` and fill in the defaults. Returns a new HV
174 * (+1). `class` is the application class, for the name and version defaults. */
175 198 static HV *pmcp_opts(pTHX_ SV *class, SV *optsv)
176 {
177 198 HV *in, *out = newHV();
178 SV *v;
179  
180 198 ▶ if (SvOK(optsv) && !pmcp_is_hash(optsv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(optsv) && !pmcp_is_hash(optsv)) { 198 0 198
if (SvOK(optsv) && !pmcp_is_hash(optsv)) { 1 197 198
181 1 SvREFCNT_dec((SV *)out);
182 1 croak("%s: the options must be a hash", PMCP_WHO);
183 }
184 197 ▶ in = SvOK(optsv) ? (HV *)SvRV(optsv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
in = SvOK(optsv) ? (HV *)SvRV(optsv) : NULL; 197 0 197
185 197 sv_2mortal((SV *)out); /* until it is safely built */
186 197 pmcp_check_keys(aTHX_ "option", in, PMCP_OPTS);
187  
188 /* path: rooted, concrete, no trailing slash */
189 196 v = pmcp_hget(aTHX_ in, "path");
190 196 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 6 190 196
191 STRLEN l;
192 const char *p;
193 6 ▶ if (!pmcp_is_text(v)) pmcp_opt_croak(aTHX_ "path", "a path such as /mcp");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(v)) pmcp_opt_croak(aTHX_ "path", "a path such as /mcp"); 1 5 6
194 5 p = SvPV_const(v, l);
195 5 ▶ if (p[0] != '/' || (l > 1 && p[l - 1] == '/') || memchr(p, ':', l)
Branch: 4 branches
BranchTrueFalseTotal
if (p[0] != '/' || (l > 1 && p[l - 1] == '/') || memchr(p, ':', l) 4 1 5
if (p[0] != '/' || (l > 1 && p[l - 1] == '/') || memchr(p, ':', l) 4 0 4
if (p[0] != '/' || (l > 1 && p[l - 1] == '/') || memchr(p, ':', l) 3 1 4
if (p[0] != '/' || (l > 1 && p[l - 1] == '/') || memchr(p, ':', l) 2 1 3
196 2 ▶ || memchr(p, '*', l) || memchr(p, '?', l) || memchr(p, '#', l))
Branch: 3 branches
BranchTrueFalseTotal
|| memchr(p, '*', l) || memchr(p, '?', l) || memchr(p, '#', l)) 2 0 2
|| memchr(p, '*', l) || memchr(p, '?', l) || memchr(p, '#', l)) 2 0 2
|| memchr(p, '*', l) || memchr(p, '?', l) || memchr(p, '#', l)) 0 2 2
197 3 croak("%s: option 'path' must be a rooted, concrete path with no "
198 "trailing slash, not '%.*s'", PMCP_WHO, (int)l, p);
199 2 (void)hv_stores(out, "path", newSVsv(v));
200 }
201 190 else (void)hv_stores(out, "path", newSVpvs("/mcp"));
202  
203 /* name and version default to the application's own */
204 192 v = pmcp_hget(aTHX_ in, "name");
205 192 ▶ if (v && !pmcp_is_text(v)) pmcp_opt_croak(aTHX_ "name", "a non-empty string");
Branch: 2 branches
BranchTrueFalseTotal
if (v && !pmcp_is_text(v)) pmcp_opt_croak(aTHX_ "name", "a non-empty string"); 3 189 192
if (v && !pmcp_is_text(v)) pmcp_opt_croak(aTHX_ "name", "a non-empty string"); 1 2 3
206 191 ▶ (void)hv_stores(out, "name", newSVsv(v ? v : class));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "name", newSVsv(v ? v : class)); 2 189 191
207  
208 191 v = pmcp_hget(aTHX_ in, "version");
209 191 ▶ if (v && (SvROK(v) || !SvCUR(v)))
Branch: 3 branches
BranchTrueFalseTotal
if (v && (SvROK(v) || !SvCUR(v))) 2 189 191
if (v && (SvROK(v) || !SvCUR(v))) 2 0 2
if (v && (SvROK(v) || !SvCUR(v))) 1 1 2
210 1 pmcp_opt_croak(aTHX_ "version", "a non-empty string");
211 190 ▶ if (!v) {
Branch: 1 branch
BranchTrueFalseTotal
if (!v) { 189 1 190
212 189 SV *av = pmcp_call_method(aTHX_ class, "VERSION", NULL, 0, G_SCALAR, NULL);
213 189 ▶ if (av && SvOK(av)) (void)hv_stores(out, "version", av);
Branch: 2 branches
BranchTrueFalseTotal
if (av && SvOK(av)) (void)hv_stores(out, "version", av); 189 0 189
if (av && SvOK(av)) (void)hv_stores(out, "version", av); 26 163 189
214 else {
215 163 ▶ if (av) SvREFCNT_dec(av);
Branch: 1 branch
BranchTrueFalseTotal
if (av) SvREFCNT_dec(av); 163 0 163
216 163 (void)hv_stores(out, "version", newSVpvs("0"));
217 }
218 }
219 1 else (void)hv_stores(out, "version", newSVsv(v));
220  
221 { /* plain strings */
222 static const char *const text[] = { "title", "description", "instructions",
223 NULL };
224 int i;
225 757 ▶ for (i = 0; text[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; text[i]; i++) { 568 189 757
226 568 v = pmcp_hget(aTHX_ in, text[i]);
227 568 ▶ if (!v) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) continue; 558 10 568
228 10 ▶ if (!pmcp_is_text(v)) pmcp_opt_croak(aTHX_ text[i], "a non-empty string");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(v)) pmcp_opt_croak(aTHX_ text[i], "a non-empty string"); 1 9 10
229 9 (void)hv_store(out, text[i], (I32)strlen(text[i]), newSVsv(v), 0);
230 }
231 }
232  
233 189 v = pmcp_hget(aTHX_ in, "website");
234 189 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 3 186 189
235 STRLEN l;
236 6 ▶ const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL; 3 0 3
237 3 ▶ if (!p || !((l > 8 && memEQ(p, "https://", 8))
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !((l > 8 && memEQ(p, "https://", 8)) 3 0 3
if (!p || !((l > 8 && memEQ(p, "https://", 8)) 3 0 3
if (!p || !((l > 8 && memEQ(p, "https://", 8)) 1 2 3
238 1 ▶ || (l > 7 && memEQ(p, "http://", 7))))
Branch: 2 branches
BranchTrueFalseTotal
|| (l > 7 && memEQ(p, "http://", 7)))) 1 0 1
|| (l > 7 && memEQ(p, "http://", 7)))) 1 0 1
239 1 pmcp_opt_croak(aTHX_ "website", "an http or https URL");
240 2 (void)hv_stores(out, "website", newSVsv(v));
241 }
242  
243 188 ▶ if ((v = pmcp_hget(aTHX_ in, "icons"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "icons"))) { 4 184 188
244 4 pmcp_check_icons(aTHX_ v);
245 1 (void)hv_stores(out, "icons", newSVsv(v));
246 }
247  
248 185 ▶ if ((v = pmcp_hget(aTHX_ in, "auth"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "auth"))) { 16 169 185
249 16 pmcp_check_guards(aTHX_ "auth", v);
250 12 (void)hv_stores(out, "auth", newSVsv(v));
251 }
252  
253 /* the shape only, the keys are 11's */
254 181 ▶ if ((v = pmcp_hget(aTHX_ in, "oauth"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "oauth"))) { 15 166 181
255 15 ▶ if (!pmcp_is_hash(v)) pmcp_opt_croak(aTHX_ "oauth", "a hash");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(v)) pmcp_opt_croak(aTHX_ "oauth", "a hash"); 1 14 15
256 14 (void)hv_stores(out, "oauth", newSVsv(v));
257 }
258  
259 180 ▶ if ((v = pmcp_hget(aTHX_ in, "principal"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "principal"))) { 9 171 180
260 9 ▶ if (!pmcp_is_target(aTHX_ v))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_target(aTHX_ v)) 1 8 9
261 1 pmcp_opt_croak(aTHX_ "principal",
262 "a code ref or a 'Controller#method' target");
263 8 (void)hv_stores(out, "principal", newSVsv(v));
264 }
265  
266 /* openapi: an api mount, '*', a spec or a prefix, or a list of them;
267 * ops needs it. Strings are matched to mounts at to_app (12). */
268 179 ▶ if ((v = pmcp_hget(aTHX_ in, "openapi"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "openapi"))) { 21 158 179
269 21 AV *names = newAV();
270 21 ▶ if (pmcp_is_text(v) || (SvROK(v) && sv_derived_from(v, "Punk::Mount::OpenAPI")))
Branch: 3 branches
BranchTrueFalseTotal
if (pmcp_is_text(v) || (SvROK(v) && sv_derived_from(v, "Punk::Mount::OpenAPI"))) 3 18 21
if (pmcp_is_text(v) || (SvROK(v) && sv_derived_from(v, "Punk::Mount::OpenAPI"))) 3 0 3
if (pmcp_is_text(v) || (SvROK(v) && sv_derived_from(v, "Punk::Mount::OpenAPI"))) 1 2 3
271 19 av_push(names, newSVsv(v));
272 3 ▶ else if (pmcp_is_array(v) && av_len((AV *)SvRV(v)) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (pmcp_is_array(v) && av_len((AV *)SvRV(v)) >= 0) { 1 1 2
else if (pmcp_is_array(v) && av_len((AV *)SvRV(v)) >= 0) { 1 0 1
273 1 AV *src = (AV *)SvRV(v);
274 1 SSize_t i, n = av_len(src) + 1;
275 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
276 1 SV **e = av_fetch(src, i, 0);
277 1 ▶ if (!(e && (pmcp_is_text(*e)
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && (pmcp_is_text(*e) 1 0 1
if (!(e && (pmcp_is_text(*e) 0 1 1
278 0 ▶ || (SvROK(*e) && sv_derived_from(*e, "Punk::Mount::OpenAPI"))))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (SvROK(*e) && sv_derived_from(*e, "Punk::Mount::OpenAPI"))))) { 0 0 0
|| (SvROK(*e) && sv_derived_from(*e, "Punk::Mount::OpenAPI"))))) { 0 0 0
279 0 SvREFCNT_dec((SV *)names);
280 0 pmcp_opt_croak(aTHX_ "openapi", "an api mount, '*', a spec or a prefix, or a list of them");
281 }
282 1 av_push(names, newSVsv(*e));
283 }
284 }
285 else {
286 1 SvREFCNT_dec((SV *)names);
287 1 pmcp_opt_croak(aTHX_ "openapi", "an api mount, '*', a spec or a prefix, or a list of them");
288 }
289 20 (void)hv_stores(out, "openapi", newRV_noinc((SV *)names));
290 }
291 178 ▶ if ((v = pmcp_hget(aTHX_ in, "ops"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "ops"))) { 4 174 178
292 AV *src;
293 SSize_t i, n;
294 4 ▶ if (!pmcp_hget(aTHX_ out, "openapi"))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_hget(aTHX_ out, "openapi")) 1 3 4
295 1 croak("%s: option 'ops' needs 'openapi', the mount the operations "
296 "come from", PMCP_WHO);
297 3 ▶ if (!pmcp_is_array(v)) pmcp_opt_croak(aTHX_ "ops", "a list of operationIds");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(v)) pmcp_opt_croak(aTHX_ "ops", "a list of operationIds"); 0 3 3
298 3 src = (AV *)SvRV(v);
299 3 n = av_len(src) + 1;
300 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 3 8
301 5 SV **e = av_fetch(src, i, 0);
302 5 ▶ if (!(e && pmcp_is_text(*e)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_text(*e))) 5 0 5
if (!(e && pmcp_is_text(*e))) 0 5 5
303 0 pmcp_opt_croak(aTHX_ "ops", "a list of operationIds");
304 }
305 3 (void)hv_stores(out, "ops", newSVsv(v));
306 }
307  
308 /* session: 0 for stateless, or the name of a Punk::Cache store */
309 177 v = pmcp_hget(aTHX_ in, "session");
310 177 ▶ if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0)
Branch: 5 branches
BranchTrueFalseTotal
if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0) 160 17 177
if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0) 159 1 160
if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0) 144 15 159
if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0) 144 0 144
if (v && !SvROK(v) && looks_like_number(v) && SvIV(v) == 0 && SvNV(v) == 0) 144 0 144
311 144 (void)hv_stores(out, "session", newSViv(0));
312 33 ▶ else if (v && !pmcp_is_text(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (v && !pmcp_is_text(v)) 16 17 33
else if (v && !pmcp_is_text(v)) 1 15 16
313 1 pmcp_opt_croak(aTHX_ "session", "0, or the name of a Punk::Cache store");
314 32 ▶ else (void)hv_stores(out, "session", v ? newSVsv(v) : newSViv(0));
Branch: 1 branch
BranchTrueFalseTotal
else (void)hv_stores(out, "session", v ? newSVsv(v) : newSViv(0)); 15 17 32
315  
316 176 (void)hv_stores(out, "session_ttl",
317 newSViv(pmcp_opt_count(aTHX_ in, "session_ttl", 3600, 0)));
318 174 (void)hv_stores(out, "max_sessions",
319 newSViv(pmcp_opt_count(aTHX_ in, "max_sessions", 10000, 0)));
320 173 (void)hv_stores(out, "page_size",
321 newSViv(pmcp_opt_count(aTHX_ in, "page_size", 50, 1000)));
322 172 (void)hv_stores(out, "max_body",
323 newSViv(pmcp_opt_count(aTHX_ in, "max_body", 1048576, 0)));
324  
325 171 v = pmcp_hget(aTHX_ in, "stream");
326 171 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 4 167 171
327 STRLEN l;
328 8 ▶ const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL; 4 0 4
329 4 ▶ if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4))
Branch: 5 branches
BranchTrueFalseTotal
if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4)) 4 0 4
if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4)) 2 2 4
if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4)) 2 0 2
if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4)) 2 2 4
if (!p || !((l == 4 && memEQ(p, "auto", 4)) || (l == 4 && memEQ(p, "json", 4)) 0 2 2
330 2 ▶ || (l == 6 && memEQ(p, "always", 6))))
Branch: 2 branches
BranchTrueFalseTotal
|| (l == 6 && memEQ(p, "always", 6)))) 1 1 2
|| (l == 6 && memEQ(p, "always", 6)))) 0 1 1
331 1 pmcp_opt_croak(aTHX_ "stream", "'auto', 'json' or 'always'");
332 }
333 170 ▶ (void)hv_stores(out, "stream", v ? newSVsv(v) : newSVpvs("auto"));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "stream", v ? newSVsv(v) : newSVpvs("auto")); 3 167 170
334  
335 170 ▶ if ((v = pmcp_hget(aTHX_ in, "origins"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "origins"))) { 9 161 170
336 AV *src;
337 SSize_t i, n;
338 9 ▶ if (!pmcp_is_array(v))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(v)) 1 8 9
339 1 pmcp_opt_croak(aTHX_ "origins", "a list of origins such as https://acme.test");
340 8 src = (AV *)SvRV(v);
341 8 n = av_len(src) + 1;
342 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 6 15
343 9 SV **e = av_fetch(src, i, 0);
344 STRLEN l;
345 18 ▶ const char *p = (e && pmcp_is_text(*e)) ? SvPV_const(*e, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *p = (e && pmcp_is_text(*e)) ? SvPV_const(*e, l) : NULL; 9 0 9
const char *p = (e && pmcp_is_text(*e)) ? SvPV_const(*e, l) : NULL; 9 0 9
346 9 ▶ if (!p || !pmcp_is_origin(p, l))
Branch: 2 branches
BranchTrueFalseTotal
if (!p || !pmcp_is_origin(p, l)) 9 0 9
if (!p || !pmcp_is_origin(p, l)) 2 7 9
347 2 ▶ croak("%s: option 'origins' holds '%s', which is not an origin "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: option 'origins' holds '%s', which is not an origin " 2 0 2
348 "such as https://acme.test", PMCP_WHO,
349 p ? p : "a value that is not a string");
350 }
351 6 (void)hv_stores(out, "origins", newSVsv(v));
352 }
353  
354 167 ▶ (void)hv_stores(out, "logging",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(out, "logging", 25 142 167
(void)hv_stores(out, "logging", 25 0 25
355 newSViv((v = pmcp_hget(aTHX_ in, "logging")) && SvTRUE(v)));
356 167 ▶ (void)hv_stores(out, "stdio_trust",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(out, "stdio_trust", 6 161 167
(void)hv_stores(out, "stdio_trust", 2 4 6
357 newSViv((v = pmcp_hget(aTHX_ in, "stdio_trust")) && SvTRUE(v)));
358  
359 /* timeout: seconds, fractional allowed, for a call that returns a Future */
360 167 ▶ if ((v = pmcp_hget(aTHX_ in, "timeout"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ in, "timeout"))) { 3 164 167
361 3 ▶ if (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0)) 3 0 3
if (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0)) 2 1 3
if (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0)) 1 1 2
362 2 pmcp_opt_croak(aTHX_ "timeout", "a number of seconds above 0");
363 1 (void)hv_stores(out, "timeout", newSVnv(SvNV(v)));
364 }
365  
366 165 return (HV *)SvREFCNT_inc_simple_NN((SV *)out);
367 }
368  
369 /* ---- installing ------------------------------------------------------------ */
370  
371 /* A keyword on the application class. Idempotent per owner, so import and
372 * register may both ask and only the first does anything. */
373 329 static void pmcp_keyword(pTHX_ SV *app, const char *name, XSUBADDR_t body,
374 AV *cap)
375 {
376 SV *argv[3];
377 SV *r;
378 329 argv[0] = sv_2mortal(newSVpv(name, 0));
379 329 argv[1] = sv_2mortal(pmcp_closure(aTHX_ body, cap));
380 329 argv[2] = sv_2mortal(newSVpvs(PMCP_WHO));
381 329 r = pmcp_call_method(aTHX_ app, "install_kw", argv, 3, G_SCALAR, NULL);
382 329 SvREFCNT_dec(r);
383 329 }
384  
385 /* A to_app callback: after every keyword has recorded, before compiling. */
386 165 static void pmcp_at_compile(pTHX_ SV *app, XSUBADDR_t body, AV *cap)
387 {
388 SV *argv[2];
389 SV *r;
390 165 argv[0] = sv_2mortal(pmcp_closure(aTHX_ body, cap));
391 165 argv[1] = sv_2mortal(newSVpvs(PMCP_WHO));
392 165 r = pmcp_call_method(aTHX_ app, "on_compile", argv, 2, G_SCALAR, NULL);
393 165 SvREFCNT_dec(r);
394 165 }
395  
396 /* A context helper, $c->NAME. Idempotent per name in Punk. */
397 825 static void pmcp_helper(pTHX_ SV *app, const char *name, XSUBADDR_t body)
398 {
399 SV *argv[2];
400 SV *r;
401 825 argv[0] = sv_2mortal(newSVpv(name, 0));
402 825 argv[1] = sv_2mortal(pmcp_closure(aTHX_ body, newAV()));
403 825 r = pmcp_call_method(aTHX_ app, "helper", argv, 2, G_SCALAR, NULL);
404 825 SvREFCNT_dec(r);
405 825 }
406  
407 /* A middleware. Its body runs once at to_app with the inner app, after the
408 * router has compiled, so it can read the route records. A croak from it is a
409 * to_app croak. */
410 187 static void pmcp_middleware(pTHX_ SV *app, XSUBADDR_t body, AV *cap)
411 {
412 SV *argv[1];
413 SV *r;
414 187 argv[0] = sv_2mortal(pmcp_closure(aTHX_ body, cap));
415 187 r = pmcp_call_method(aTHX_ app, "middleware", argv, 1, G_SCALAR, NULL);
416 187 SvREFCNT_dec(r);
417 187 }
418  
419 #endif /* PMCP_REG_H */
« include/pmcp/pmcp_res.h ↑ Summary include/pmcp/pmcp_sess.h »