| 1 |
|
| /* pmcp_oa.h - tools from an Open::API mount, called by re-entering the app. |
| 2 |
|
| * |
| 3 |
|
| * Needs: pmcp_decl.h, pmcp_call.h, pmcp_state.h. |
| 4 |
|
| * |
| 5 |
|
| * `openapi` names mounts: a mount object as `api` returns it, '*' for every |
| 6 |
|
| * mount, or a string matching a mount's spec or its prefix. Each operation |
| 7 |
|
| * becomes a tool, unless `ops` leaves it out or its `x-mcp` is false. The spec |
| 8 |
|
| * stays the source of truth, as it is for Maat, and the rules are Maat's |
| 9 |
|
| * (Maat/include/mt_mcp_tools.h): |
| 10 |
|
| * |
| 11 |
|
| * name the operationId lowercased, anything else made _, 64 at most, |
| 12 |
|
| * _N on a collision. A collision with a DSL tool croaks |
| 13 |
|
| * description the summary, then the description, clipped to 1024 |
| 14 |
|
| * annotations from the method alone: GET and HEAD read-only, PUT and DELETE |
| 15 |
|
| * idempotent, DELETE destructive, and open world |
| 16 |
|
| * inputSchema Open::API's operation_schema: the parameters flat, the body |
| 17 |
|
| * under `body`, with $defs |
| 18 |
|
| * x-mcp { name, title, description, annotations } overrides those. |
| 19 |
|
| * No guard: the mount's own guards run on the inner request. |
| 20 |
|
| * |
| 21 |
|
| * A call is a request the application answers: a synthetic env built in C as |
| 22 |
|
| * Maat's mt_mcp_call builds one, handed to the innermost app (a middleware |
| 23 |
|
| * added last at on_compile captures it). So the mount's security checker, |
| 24 |
|
| * its validation and its guards all run, and the answer comes back through |
| 25 |
|
| * 05's triplet branch. The env carries the caller's Authorization to this |
| 26 |
|
| * application's own mount and nowhere else, and is marked pmcp.inner and |
| 27 |
|
| * pmcp.tool so an observer or a rate limit can tell the inner request apart. |
| 28 |
|
| */ |
| 29 |
|
| |
| 30 |
|
| #ifndef PMCP_OA_H |
| 31 |
|
| #define PMCP_OA_H |
| 32 |
|
| |
| 33 |
|
| #define PMCP_OA_NAME_MAX 64 |
| 34 |
|
| #define PMCP_OA_DESC_MAX 1024 |
| 35 |
|
| |
| 36 |
|
| static const char *const PMCP_XMCP_KEYS[] = { |
| 37 |
|
| "name", "title", "description", "annotations", NULL |
| 38 |
|
| }; |
| 39 |
|
| |
| 40 |
|
| /* ---- the input handle ------------------------------------------------------------ */ |
| 41 |
|
| |
| 42 |
|
| static SV *PMCP_INPUT = NULL; /* sub { open my $fh, '<', \$_[0]; $fh } */ |
| 43 |
|
| |
| 44 |
|
| /* An in-memory read handle over `bytes`, for psgi.input: Punk reads a body |
| 45 |
|
| * through PerlIO, so it has to be a real handle. +1 */ |
| 46 |
17 |
| static SV *pmcp_input_handle(pTHX_ SV *bytes) |
| 47 |
|
| { |
| 48 |
|
| SV *fh, *copy; |
| 49 |
17 |
▶ | if (!PMCP_INPUT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PMCP_INPUT) { |
3 |
14 |
17 |
|
| 50 |
3 |
| PMCP_INPUT = eval_pv("sub { my $b = $_[0]; open my $fh, '<', \\$b " |
| 51 |
|
| "or die \"cannot open a body handle: $!\"; $fh }", TRUE); |
| 52 |
3 |
| SvREFCNT_inc_simple_void_NN(PMCP_INPUT); |
| 53 |
|
| } |
| 54 |
17 |
| copy = sv_2mortal(newSVsv(bytes)); |
| 55 |
17 |
| fh = pmcp_call_sv(aTHX_ PMCP_INPUT, ©, 1, G_SCALAR, NULL); |
| 56 |
17 |
| return fh; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| /* ---- percent encoding ------------------------------------------------------------- */ |
| 60 |
|
| |
| 61 |
|
| /* Everything outside RFC 3986's unreserved set is escaped: the same routine |
| 62 |
|
| * serves a path segment and a query value, and over-escaping is always safe |
| 63 |
|
| * where under-escaping smuggles a delimiter. A JSON boolean goes out as |
| 64 |
|
| * true or false, not as SCALAR(0x...). */ |
| 65 |
14 |
| static void pmcp_pct_cat(pTHX_ SV *out, SV *v) |
| 66 |
|
| { |
| 67 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 68 |
|
| STRLEN l, i; |
| 69 |
|
| const char *p; |
| 70 |
14 |
▶ | if (!v || !SvOK(v)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) return; |
14 |
0 |
14 |
| if (!v || !SvOK(v)) return; |
0 |
14 |
14 |
|
| 71 |
14 |
▶ | if (SvROK(v) && SvTYPE(SvRV(v)) < SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(v) && SvTYPE(SvRV(v)) < SVt_PVAV) { |
0 |
14 |
14 |
| if (SvROK(v) && SvTYPE(SvRV(v)) < SVt_PVAV) { |
0 |
0 |
0 |
|
| 72 |
0 |
▶ | sv_catpv(out, SvTRUE(SvRV(v)) ? "true" : "false"); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpv(out, SvTRUE(SvRV(v)) ? "true" : "false"); |
0 |
0 |
0 |
|
| 73 |
0 |
| return; |
| 74 |
|
| } |
| 75 |
14 |
▶ | if (SvROK(v)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(v)) return; |
0 |
14 |
14 |
|
| 76 |
14 |
| p = SvPVutf8(v, l); |
| 77 |
70 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
56 |
14 |
70 |
|
| 78 |
56 |
| unsigned char c = (unsigned char)p[i]; |
| 79 |
56 |
▶ | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
Branch: 6 branches
| Branch | True | False | Total |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
48 |
8 |
56 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
48 |
0 |
48 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
48 |
8 |
56 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
0 |
48 |
48 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
3 |
5 |
8 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') |
0 |
3 |
3 |
|
| 80 |
5 |
▶ | || c == '-' || c == '.' || c == '_' || c == '~') |
Branch: 4 branches
| Branch | True | False | Total |
| || c == '-' || c == '.' || c == '_' || c == '~') |
5 |
0 |
5 |
| || c == '-' || c == '.' || c == '_' || c == '~') |
5 |
0 |
5 |
| || c == '-' || c == '.' || c == '_' || c == '~') |
5 |
0 |
5 |
| || c == '-' || c == '.' || c == '_' || c == '~') |
0 |
5 |
5 |
|
| 81 |
51 |
| sv_catpvn(out, (const char *)&c, 1); |
| 82 |
|
| else { |
| 83 |
|
| char b[3]; |
| 84 |
5 |
| b[0] = '%'; b[1] = hex[c >> 4]; b[2] = hex[c & 15]; |
| 85 |
5 |
| sv_catpvn(out, b, 3); |
| 86 |
|
| } |
| 87 |
|
| } |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| /* ---- finding the mounts --------------------------------------------------------------- */ |
| 91 |
|
| |
| 92 |
|
| /* Does `want` (an `openapi` entry) name `mount`? */ |
| 93 |
18 |
| static int pmcp_oa_names(pTHX_ SV *want, SV *mount) |
| 94 |
|
| { |
| 95 |
|
| SV *spec, *prefix; |
| 96 |
18 |
▶ | if (SvROK(want)) return SvRV(want) == SvRV(mount); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(want)) return SvRV(want) == SvRV(mount); |
1 |
17 |
18 |
|
| 97 |
17 |
▶ | if (strEQ(SvPV_nolen(want), "*")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(SvPV_nolen(want), "*")) return 1; |
13 |
4 |
17 |
|
| 98 |
4 |
| spec = pmcp_hget(aTHX_ (HV *)SvRV(mount), "spec"); |
| 99 |
4 |
▶ | if (spec && !SvROK(spec) && sv_eq(spec, want)) return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (spec && !SvROK(spec) && sv_eq(spec, want)) return 1; |
4 |
0 |
4 |
| if (spec && !SvROK(spec) && sv_eq(spec, want)) return 1; |
4 |
0 |
4 |
| if (spec && !SvROK(spec) && sv_eq(spec, want)) return 1; |
1 |
3 |
4 |
|
| 100 |
3 |
| prefix = sv_2mortal(pmcp_call_method(aTHX_ mount, "prefix", NULL, 0, G_SCALAR, NULL)); |
| 101 |
3 |
▶ | return prefix && SvOK(prefix) && SvCUR(prefix) && sv_eq(prefix, want); |
Branch: 4 branches
| Branch | True | False | Total |
| return prefix && SvOK(prefix) && SvCUR(prefix) && sv_eq(prefix, want); |
3 |
0 |
3 |
| return prefix && SvOK(prefix) && SvCUR(prefix) && sv_eq(prefix, want); |
3 |
0 |
3 |
| return prefix && SvOK(prefix) && SvCUR(prefix) && sv_eq(prefix, want); |
2 |
1 |
3 |
| return prefix && SvOK(prefix) && SvCUR(prefix) && sv_eq(prefix, want); |
2 |
0 |
2 |
|
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* ---- deriving the tools ------------------------------------------------------------------ */ |
| 105 |
|
| |
| 106 |
|
| /* The tool name for an operationId, made unique against `used`. Mortal. */ |
| 107 |
227 |
| static SV *pmcp_oa_name(pTHX_ SV *raw, HV *used) |
| 108 |
|
| { |
| 109 |
227 |
| SV *base = sv_2mortal(newSVpvs("")), *name; |
| 110 |
|
| STRLEN l, i; |
| 111 |
227 |
| const char *p = SvPV_const(raw, l); |
| 112 |
227 |
| int n = 1; |
| 113 |
2148 |
▶ | for (i = 0; i < l && SvCUR(base) < PMCP_OA_NAME_MAX; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < l && SvCUR(base) < PMCP_OA_NAME_MAX; i++) { |
1921 |
227 |
2148 |
| for (i = 0; i < l && SvCUR(base) < PMCP_OA_NAME_MAX; i++) { |
1921 |
0 |
1921 |
|
| 114 |
1921 |
| char c = p[i]; |
| 115 |
1921 |
▶ | if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
1835 |
86 |
1921 |
| if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
227 |
1608 |
1835 |
|
| 116 |
1921 |
▶ | if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
Branch: 6 branches
| Branch | True | False | Total |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
1809 |
112 |
1921 |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
0 |
1809 |
1809 |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
26 |
86 |
112 |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
26 |
0 |
26 |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
86 |
26 |
112 |
| if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) |
58 |
28 |
86 |
|
| 117 |
58 |
| c = '_'; |
| 118 |
1921 |
| sv_catpvn(base, &c, 1); |
| 119 |
|
| } |
| 120 |
227 |
▶ | if (!SvCUR(base)) sv_catpvs(base, "op"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvCUR(base)) sv_catpvs(base, "op"); |
0 |
227 |
227 |
|
| 121 |
227 |
| name = sv_2mortal(newSVsv(base)); |
| 122 |
240 |
▶ | while (hv_exists_ent(used, name, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (hv_exists_ent(used, name, 0)) { |
13 |
227 |
240 |
|
| 123 |
|
| char suffix[16]; |
| 124 |
13 |
| int sl = my_snprintf(suffix, sizeof suffix, "_%d", ++n); |
| 125 |
13 |
| name = sv_2mortal(newSVsv(base)); |
| 126 |
13 |
▶ | if (SvCUR(name) > (STRLEN)(PMCP_OA_NAME_MAX - sl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(name) > (STRLEN)(PMCP_OA_NAME_MAX - sl)) |
0 |
13 |
13 |
|
| 127 |
0 |
| SvCUR_set(name, PMCP_OA_NAME_MAX - sl); |
| 128 |
13 |
| sv_catpvn(name, suffix, sl); |
| 129 |
|
| } |
| 130 |
227 |
| (void)hv_store_ent(used, name, newSViv(1), 0); |
| 131 |
227 |
| return name; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
31 |
| static SV *pmcp_clip(pTHX_ SV *t) |
| 135 |
|
| { |
| 136 |
31 |
▶ | if (!t || !SvOK(t) || !SvCUR(t)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!t || !SvOK(t) || !SvCUR(t)) return NULL; |
31 |
0 |
31 |
| if (!t || !SvOK(t) || !SvCUR(t)) return NULL; |
31 |
0 |
31 |
| if (!t || !SvOK(t) || !SvCUR(t)) return NULL; |
0 |
31 |
31 |
|
| 137 |
31 |
▶ | if (SvCUR(t) <= PMCP_OA_DESC_MAX) return sv_2mortal(newSVsv(t)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(t) <= PMCP_OA_DESC_MAX) return sv_2mortal(newSVsv(t)); |
31 |
0 |
31 |
|
| 138 |
|
| { |
| 139 |
0 |
| SV *c = sv_2mortal(newSVpvn(SvPVX(t), PMCP_OA_DESC_MAX - 4)); |
| 140 |
0 |
| sv_catpvs(c, " ..."); |
| 141 |
0 |
| return c; |
| 142 |
|
| } |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* argument name -> {in, name}, from $api->operation($id). Header, path and |
| 146 |
|
| * query, as Maat does. A cookie argument would have to be folded into the |
| 147 |
|
| * Cookie header, and no spec in front of this has used one: left out rather |
| 148 |
|
| * than guessed. +1 HV */ |
| 149 |
126 |
| static HV *pmcp_oa_where(pTHX_ SV *api, SV *op_id) |
| 150 |
|
| { |
| 151 |
|
| static const char *const locs[] = { "path", "query", "header", NULL }; |
| 152 |
126 |
| HV *w = newHV(); |
| 153 |
126 |
| SV *info = pmcp_call_method(aTHX_ api, "operation", &op_id, 1, G_SCALAR, NULL); |
| 154 |
|
| HV *params; |
| 155 |
|
| int li; |
| 156 |
126 |
| sv_2mortal(info); |
| 157 |
126 |
▶ | if (!pmcp_is_hash(info) || !pmcp_is_hash(pmcp_hget(aTHX_ (HV *)SvRV(info), "params"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pmcp_is_hash(info) || !pmcp_is_hash(pmcp_hget(aTHX_ (HV *)SvRV(info), "params"))) |
126 |
0 |
126 |
| if (!pmcp_is_hash(info) || !pmcp_is_hash(pmcp_hget(aTHX_ (HV *)SvRV(info), "params"))) |
0 |
126 |
126 |
|
| 158 |
0 |
| return w; |
| 159 |
126 |
| params = (HV *)SvRV(pmcp_hget(aTHX_ (HV *)SvRV(info), "params")); |
| 160 |
504 |
▶ | for (li = 0; locs[li]; li++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; locs[li]; li++) { |
378 |
126 |
504 |
|
| 161 |
378 |
| SV *list = pmcp_hget(aTHX_ params, locs[li]); |
| 162 |
|
| SSize_t i, n; |
| 163 |
378 |
▶ | if (!pmcp_is_array(list)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_array(list)) continue; |
0 |
378 |
378 |
|
| 164 |
378 |
| n = av_len((AV *)SvRV(list)) + 1; |
| 165 |
482 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
104 |
378 |
482 |
|
| 166 |
104 |
| SV **e = av_fetch((AV *)SvRV(list), i, 0); |
| 167 |
|
| SV *nm; |
| 168 |
|
| HV *rec; |
| 169 |
104 |
▶ | if (!(e && pmcp_is_hash(*e))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(e && pmcp_is_hash(*e))) continue; |
104 |
0 |
104 |
| if (!(e && pmcp_is_hash(*e))) continue; |
0 |
104 |
104 |
|
| 170 |
104 |
| nm = pmcp_hget(aTHX_ (HV *)SvRV(*e), "name"); |
| 171 |
104 |
▶ | if (!nm) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nm) continue; |
0 |
104 |
104 |
|
| 172 |
104 |
| rec = newHV(); |
| 173 |
104 |
| (void)hv_stores(rec, "in", newSVpv(locs[li], 0)); |
| 174 |
104 |
| (void)hv_stores(rec, "name", newSVsv(nm)); |
| 175 |
104 |
| (void)hv_store_ent(w, nm, newRV_noinc((SV *)rec), 0); |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
126 |
| return w; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| /* The tool spec 05's freeze understands, built from the operation. Mortal HV, |
| 182 |
|
| * or NULL when x-mcp says to leave the operation out. */ |
| 183 |
142 |
| static HV *pmcp_oa_spec(pTHX_ SV *api, HV *op, SV *opname) |
| 184 |
|
| { |
| 185 |
142 |
| SV *op_id = pmcp_hget(aTHX_ op, "operationId"); |
| 186 |
142 |
| SV *method = pmcp_hget(aTHX_ op, "method"); |
| 187 |
142 |
| SV *path = pmcp_hget(aTHX_ op, "path"); |
| 188 |
142 |
| HV *spec = (HV *)sv_2mortal((SV *)newHV()); |
| 189 |
142 |
| HV *x = NULL; |
| 190 |
|
| SV *doc, *schema, *v; |
| 191 |
|
| |
| 192 |
|
| { /* x-mcp, from the raw spec */ |
| 193 |
142 |
| SV *raw = sv_2mortal(pmcp_call_method(aTHX_ api, "spec", NULL, 0, G_SCALAR, NULL)); |
| 194 |
142 |
▶ | SV *paths = pmcp_is_hash(raw) ? pmcp_hget(aTHX_ (HV *)SvRV(raw), "paths") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *paths = pmcp_is_hash(raw) ? pmcp_hget(aTHX_ (HV *)SvRV(raw), "paths") : NULL; |
142 |
0 |
142 |
|
| 195 |
284 |
▶ | SV *item = (paths && pmcp_is_hash(paths)) ? pmcp_hget(aTHX_ (HV *)SvRV(paths), SvPV_nolen(path)) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *item = (paths && pmcp_is_hash(paths)) ? pmcp_hget(aTHX_ (HV *)SvRV(paths), SvPV_nolen(path)) : NULL; |
142 |
0 |
142 |
| SV *item = (paths && pmcp_is_hash(paths)) ? pmcp_hget(aTHX_ (HV *)SvRV(paths), SvPV_nolen(path)) : NULL; |
142 |
0 |
142 |
|
| 196 |
284 |
▶ | SV *rop = (item && pmcp_is_hash(item)) ? pmcp_hget(aTHX_ (HV *)SvRV(item), SvPV_nolen(method)) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *rop = (item && pmcp_is_hash(item)) ? pmcp_hget(aTHX_ (HV *)SvRV(item), SvPV_nolen(method)) : NULL; |
142 |
0 |
142 |
| SV *rop = (item && pmcp_is_hash(item)) ? pmcp_hget(aTHX_ (HV *)SvRV(item), SvPV_nolen(method)) : NULL; |
142 |
0 |
142 |
|
| 197 |
142 |
▶ | SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
142 |
0 |
142 |
| SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
142 |
0 |
142 |
|
| 198 |
142 |
▶ | if (xv && !SvROK(xv) && !SvTRUE(xv)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (xv && !SvROK(xv) && !SvTRUE(xv)) return NULL; |
28 |
114 |
142 |
| if (xv && !SvROK(xv) && !SvTRUE(xv)) return NULL; |
0 |
28 |
28 |
| if (xv && !SvROK(xv) && !SvTRUE(xv)) return NULL; |
0 |
0 |
0 |
|
| 199 |
142 |
▶ | if (xv && SvROK(xv) && SvTYPE(SvRV(xv)) < SVt_PVAV && !SvTRUE(SvRV(xv))) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (xv && SvROK(xv) && SvTYPE(SvRV(xv)) < SVt_PVAV && !SvTRUE(SvRV(xv))) return NULL; |
28 |
114 |
142 |
| if (xv && SvROK(xv) && SvTYPE(SvRV(xv)) < SVt_PVAV && !SvTRUE(SvRV(xv))) return NULL; |
28 |
0 |
28 |
| if (xv && SvROK(xv) && SvTYPE(SvRV(xv)) < SVt_PVAV && !SvTRUE(SvRV(xv))) return NULL; |
14 |
14 |
28 |
| if (xv && SvROK(xv) && SvTYPE(SvRV(xv)) < SVt_PVAV && !SvTRUE(SvRV(xv))) return NULL; |
14 |
0 |
14 |
|
| 200 |
128 |
▶ | if (xv && pmcp_is_hash(xv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (xv && pmcp_is_hash(xv)) { |
14 |
114 |
128 |
| if (xv && pmcp_is_hash(xv)) { |
14 |
0 |
14 |
|
| 201 |
14 |
| SV *what = sv_2mortal(newSVpvf("x-mcp key on operation '%" SVf "'", SVfARG(op_id))); |
| 202 |
14 |
| x = (HV *)SvRV(xv); |
| 203 |
14 |
| pmcp_check_keys(aTHX_ SvPV_nolen(what), x, PMCP_XMCP_KEYS); |
| 204 |
|
| } |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
128 |
| doc = sv_2mortal(pmcp_call_method(aTHX_ api, "operation_doc", &op_id, 1, G_SCALAR, NULL)); |
| 208 |
128 |
▶ | if ((v = x ? pmcp_hget(aTHX_ x, "title") : NULL)) (void)hv_stores(spec, "title", newSVsv(v)); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = x ? pmcp_hget(aTHX_ x, "title") : NULL)) (void)hv_stores(spec, "title", newSVsv(v)); |
14 |
114 |
128 |
| if ((v = x ? pmcp_hget(aTHX_ x, "title") : NULL)) (void)hv_stores(spec, "title", newSVsv(v)); |
14 |
114 |
128 |
|
| 209 |
128 |
▶ | if ((v = x ? pmcp_hget(aTHX_ x, "description") : NULL)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = x ? pmcp_hget(aTHX_ x, "description") : NULL)) { |
14 |
114 |
128 |
| if ((v = x ? pmcp_hget(aTHX_ x, "description") : NULL)) { |
0 |
128 |
128 |
|
| 210 |
0 |
| (void)hv_stores(spec, "description", newSVsv(pmcp_clip(aTHX_ v))); |
| 211 |
|
| } |
| 212 |
128 |
▶ | else if (pmcp_is_hash(doc)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (pmcp_is_hash(doc)) { |
128 |
0 |
128 |
|
| 213 |
128 |
| SV *s = pmcp_hget(aTHX_ (HV *)SvRV(doc), "summary"); |
| 214 |
128 |
| SV *d = pmcp_hget(aTHX_ (HV *)SvRV(doc), "description"); |
| 215 |
128 |
| SV *joined = sv_2mortal(newSVpvs("")); |
| 216 |
128 |
▶ | if (s) sv_catsv(joined, s); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) sv_catsv(joined, s); |
128 |
0 |
128 |
|
| 217 |
128 |
▶ | if (d && SvCUR(d)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (d && SvCUR(d)) { |
128 |
0 |
128 |
| if (d && SvCUR(d)) { |
17 |
111 |
128 |
|
| 218 |
17 |
▶ | if (SvCUR(joined)) sv_catpvs(joined, "\n\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(joined)) sv_catpvs(joined, "\n\n"); |
17 |
0 |
17 |
|
| 219 |
17 |
| sv_catsv(joined, d); |
| 220 |
|
| } |
| 221 |
128 |
▶ | if (SvCUR(joined)) (void)hv_stores(spec, "description", newSVsv(pmcp_clip(aTHX_ joined))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(joined)) (void)hv_stores(spec, "description", newSVsv(pmcp_clip(aTHX_ joined))); |
31 |
97 |
128 |
|
| 222 |
|
| } |
| 223 |
128 |
▶ | if ((v = x ? pmcp_hget(aTHX_ x, "annotations") : NULL)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = x ? pmcp_hget(aTHX_ x, "annotations") : NULL)) { |
14 |
114 |
128 |
| if ((v = x ? pmcp_hget(aTHX_ x, "annotations") : NULL)) { |
0 |
128 |
128 |
|
| 224 |
0 |
| (void)hv_stores(spec, "annotations", newSVsv(v)); |
| 225 |
|
| } |
| 226 |
|
| else { |
| 227 |
128 |
| HV *a = newHV(); |
| 228 |
128 |
| const char *m = SvPV_nolen(method); |
| 229 |
128 |
▶ | int read = pmcp_foldEQ(m, "get", 3) || pmcp_foldEQ(m, "head", 4); |
Branch: 2 branches
| Branch | True | False | Total |
| int read = pmcp_foldEQ(m, "get", 3) || pmcp_foldEQ(m, "head", 4); |
40 |
88 |
128 |
| int read = pmcp_foldEQ(m, "get", 3) || pmcp_foldEQ(m, "head", 4); |
0 |
40 |
40 |
|
| 230 |
128 |
▶ | int del = strlen(m) == 6 && pmcp_foldEQ(m, "delete", 6); |
Branch: 2 branches
| Branch | True | False | Total |
| int del = strlen(m) == 6 && pmcp_foldEQ(m, "delete", 6); |
13 |
115 |
128 |
| int del = strlen(m) == 6 && pmcp_foldEQ(m, "delete", 6); |
13 |
0 |
13 |
|
| 231 |
128 |
▶ | int put = strlen(m) == 3 && pmcp_foldEQ(m, "put", 3); |
Branch: 2 branches
| Branch | True | False | Total |
| int put = strlen(m) == 3 && pmcp_foldEQ(m, "put", 3); |
101 |
27 |
128 |
| int put = strlen(m) == 3 && pmcp_foldEQ(m, "put", 3); |
13 |
88 |
101 |
|
| 232 |
128 |
| (void)hv_stores(a, "readOnlyHint", newSViv(read)); |
| 233 |
128 |
| (void)hv_stores(a, "destructiveHint", newSViv(del)); |
| 234 |
128 |
▶ | (void)hv_stores(a, "idempotentHint", newSViv(read || put || del)); |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(a, "idempotentHint", newSViv(read || put || del)); |
40 |
88 |
128 |
| (void)hv_stores(a, "idempotentHint", newSViv(read || put || del)); |
27 |
13 |
40 |
| (void)hv_stores(a, "idempotentHint", newSViv(read || put || del)); |
13 |
14 |
27 |
|
| 235 |
128 |
| (void)hv_stores(a, "openWorldHint", newSViv(1)); |
| 236 |
128 |
| (void)hv_stores(spec, "annotations", newRV_noinc((SV *)a)); |
| 237 |
|
| } |
| 238 |
128 |
| schema = pmcp_call_method(aTHX_ api, "operation_schema", &op_id, 1, G_SCALAR, NULL); |
| 239 |
128 |
▶ | (void)hv_stores(spec, "input", schema ? schema : newRV_noinc((SV *)newHV())); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(spec, "input", schema ? schema : newRV_noinc((SV *)newHV())); |
0 |
128 |
128 |
|
| 240 |
|
| PERL_UNUSED_ARG(opname); |
| 241 |
128 |
| return spec; |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
|
| /* Every operation of every named mount, appended to the tool table. Runs in |
| 245 |
|
| * the innermost middleware, once the mounts have compiled. */ |
| 246 |
19 |
| static void pmcp_oa_derive(pTHX_ SV *app, pmcp_server *ps) |
| 247 |
|
| { |
| 248 |
19 |
| SV *names = pmcp_hget(aTHX_ ps->opts, "openapi"); |
| 249 |
19 |
| SV *allow = pmcp_hget(aTHX_ ps->opts, "ops"); |
| 250 |
19 |
| SV *mounts = pmcp_hget(aTHX_ (HV *)SvRV(app), "api_mounts"); |
| 251 |
19 |
| AV *wanted = (AV *)SvRV(names); |
| 252 |
19 |
| HV *used = (HV *)sv_2mortal((SV *)newHV()); |
| 253 |
19 |
| HV *allowed = NULL, *seen_ops = (HV *)sv_2mortal((SV *)newHV()); |
| 254 |
19 |
| pmcp_tools *ts = (pmcp_tools *)ps->srv.tools; |
| 255 |
19 |
▶ | AV *ms = pmcp_is_array(mounts) ? (AV *)SvRV(mounts) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| AV *ms = pmcp_is_array(mounts) ? (AV *)SvRV(mounts) : NULL; |
19 |
0 |
19 |
|
| 256 |
19 |
▶ | SSize_t wi, mi, nm = ms ? av_len(ms) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t wi, mi, nm = ms ? av_len(ms) + 1 : 0; |
19 |
0 |
19 |
|
| 257 |
19 |
| IV i, added = 0; |
| 258 |
|
| |
| 259 |
19 |
▶ | if (!ts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ts) { |
16 |
3 |
19 |
|
| 260 |
16 |
| Newxz(ts, 1, pmcp_tools); |
| 261 |
16 |
| Newxz(ts->t, 1, pmcp_tool); |
| 262 |
|
| { |
| 263 |
16 |
| SV *v = pmcp_hget(aTHX_ ps->opts, "page_size"); |
| 264 |
16 |
▶ | ts->page_size = v ? SvIV(v) : 50; |
Branch: 1 branch
| Branch | True | False | Total |
| ts->page_size = v ? SvIV(v) : 50; |
16 |
0 |
16 |
|
| 265 |
|
| } |
| 266 |
16 |
| ps->srv.tools = ts; |
| 267 |
|
| } |
| 268 |
22 |
▶ | for (i = 0; i < ts->n; i++) (void)hv_store_ent(used, ts->t[i].name, newSViv(2), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ts->n; i++) (void)hv_store_ent(used, ts->t[i].name, newSViv(2), 0); |
3 |
19 |
22 |
|
| 269 |
19 |
▶ | if (allow) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow) { |
2 |
17 |
19 |
|
| 270 |
2 |
| SSize_t k, n = av_len((AV *)SvRV(allow)) + 1; |
| 271 |
2 |
| allowed = (HV *)sv_2mortal((SV *)newHV()); |
| 272 |
6 |
▶ | for (k = 0; k < n; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < n; k++) |
4 |
2 |
6 |
|
| 273 |
4 |
| (void)hv_store_ent(allowed, *av_fetch((AV *)SvRV(allow), k, 0), newSViv(0), 0); |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
34 |
▶ | for (wi = 0; wi <= av_len(wanted); wi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (wi = 0; wi <= av_len(wanted); wi++) { |
19 |
15 |
34 |
|
| 277 |
19 |
| SV *want = *av_fetch(wanted, wi, 0); |
| 278 |
19 |
| int matched = 0; |
| 279 |
35 |
▶ | for (mi = 0; mi < nm; mi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mi = 0; mi < nm; mi++) { |
18 |
17 |
35 |
|
| 280 |
18 |
| SV *mount = *av_fetch(ms, mi, 0); |
| 281 |
|
| SV *api, *opsv, *prefix; |
| 282 |
|
| SSize_t oi, no; |
| 283 |
18 |
▶ | if (!pmcp_oa_names(aTHX_ want, mount)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_oa_names(aTHX_ want, mount)) continue; |
1 |
17 |
18 |
|
| 284 |
17 |
| matched = 1; |
| 285 |
17 |
| api = sv_2mortal(pmcp_call_method(aTHX_ mount, "api", NULL, 0, G_SCALAR, NULL)); |
| 286 |
17 |
| prefix = sv_2mortal(pmcp_call_method(aTHX_ mount, "prefix", NULL, 0, G_SCALAR, NULL)); |
| 287 |
17 |
| opsv = sv_2mortal(pmcp_call_method(aTHX_ api, "operations", NULL, 0, G_SCALAR, NULL)); |
| 288 |
17 |
▶ | if (!pmcp_is_array(opsv)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_array(opsv)) continue; |
0 |
17 |
17 |
|
| 289 |
17 |
| no = av_len((AV *)SvRV(opsv)) + 1; |
| 290 |
174 |
▶ | for (oi = 0; oi < no; oi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (oi = 0; oi < no; oi++) { |
159 |
15 |
174 |
|
| 291 |
159 |
| HV *op = (HV *)SvRV(*av_fetch((AV *)SvRV(opsv), oi, 0)); |
| 292 |
159 |
| SV *op_id = pmcp_hget(aTHX_ op, "operationId"); |
| 293 |
159 |
| HV *spec, *x = NULL; |
| 294 |
159 |
| SV *name, *xname = NULL, *full; |
| 295 |
|
| pmcp_tool *t; |
| 296 |
|
| int ver; |
| 297 |
159 |
▶ | if (!op_id || hv_exists_ent(seen_ops, op_id, 0)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!op_id || hv_exists_ent(seen_ops, op_id, 0)) continue; |
159 |
0 |
159 |
| if (!op_id || hv_exists_ent(seen_ops, op_id, 0)) continue; |
0 |
159 |
159 |
|
| 298 |
159 |
▶ | if (allowed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allowed) { |
20 |
139 |
159 |
|
| 299 |
20 |
| HE *ae = hv_fetch_ent(allowed, op_id, 0, 0); |
| 300 |
20 |
▶ | if (!ae) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ae) continue; |
17 |
3 |
20 |
|
| 301 |
3 |
| sv_setiv(HeVAL(ae), 1); |
| 302 |
|
| } |
| 303 |
142 |
| spec = pmcp_oa_spec(aTHX_ api, op, op_id); |
| 304 |
142 |
▶ | if (!spec) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!spec) continue; |
14 |
128 |
142 |
|
| 305 |
128 |
| (void)hv_store_ent(seen_ops, op_id, newSViv(1), 0); |
| 306 |
|
| { /* x-mcp's name, checked as a DSL name is */ |
| 307 |
128 |
| SV *raw = sv_2mortal(pmcp_call_method(aTHX_ api, "spec", NULL, 0, G_SCALAR, NULL)); |
| 308 |
128 |
| SV *paths = pmcp_hget(aTHX_ (HV *)SvRV(raw), "paths"); |
| 309 |
256 |
| SV *item = pmcp_hget(aTHX_ (HV *)SvRV(paths), SvPV_nolen(pmcp_hget(aTHX_ op, "path"))); |
| 310 |
256 |
▶ | SV *rop = item ? pmcp_hget(aTHX_ (HV *)SvRV(item), SvPV_nolen(pmcp_hget(aTHX_ op, "method"))) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *rop = item ? pmcp_hget(aTHX_ (HV *)SvRV(item), SvPV_nolen(pmcp_hget(aTHX_ op, "method"))) : NULL; |
128 |
0 |
128 |
|
| 311 |
128 |
▶ | SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
128 |
0 |
128 |
| SV *xv = (rop && pmcp_is_hash(rop)) ? pmcp_hget(aTHX_ (HV *)SvRV(rop), "x-mcp") : NULL; |
128 |
0 |
128 |
|
| 312 |
128 |
▶ | if (xv && pmcp_is_hash(xv)) x = (HV *)SvRV(xv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (xv && pmcp_is_hash(xv)) x = (HV *)SvRV(xv); |
14 |
114 |
128 |
| if (xv && pmcp_is_hash(xv)) x = (HV *)SvRV(xv); |
14 |
0 |
14 |
|
| 313 |
128 |
▶ | xname = x ? pmcp_hget(aTHX_ x, "name") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| xname = x ? pmcp_hget(aTHX_ x, "name") : NULL; |
14 |
114 |
128 |
|
| 314 |
|
| } |
| 315 |
128 |
▶ | if (xname) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (xname) { |
14 |
114 |
128 |
|
| 316 |
|
| STRLEN xl; |
| 317 |
14 |
| const char *xp = SvPV_const(xname, xl); |
| 318 |
14 |
▶ | if (!pmcp_tool_name_ok(xp, xl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_tool_name_ok(xp, xl)) |
0 |
14 |
14 |
|
| 319 |
0 |
| croak("%s: x-mcp name '%" SVf "' on operation '%" SVf "' is not 1 to 128 " |
| 320 |
|
| "of A-Z a-z 0-9 _ . -", PMCP_WHO, SVfARG(xname), SVfARG(op_id)); |
| 321 |
14 |
▶ | if (hv_exists_ent(used, xname, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(used, xname, 0)) |
1 |
13 |
14 |
|
| 322 |
1 |
| croak("%s: x-mcp name '%" SVf "' on operation '%" SVf "' is already a " |
| 323 |
|
| "tool", PMCP_WHO, SVfARG(xname), SVfARG(op_id)); |
| 324 |
13 |
| (void)hv_store_ent(used, xname, newSViv(1), 0); |
| 325 |
13 |
| name = xname; |
| 326 |
|
| } |
| 327 |
|
| else { |
| 328 |
|
| /* the name before any _N: a DSL tool by that name is a |
| 329 |
|
| * clash to fix, not one to number round */ |
| 330 |
114 |
| HV *probe = (HV *)sv_2mortal((SV *)newHV()); |
| 331 |
114 |
| SV *base = pmcp_oa_name(aTHX_ op_id, probe); |
| 332 |
114 |
| HE *he = hv_fetch_ent(used, base, 0, 0); |
| 333 |
114 |
▶ | if (he && SvIV(HeVAL(he)) == 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (he && SvIV(HeVAL(he)) == 2) |
14 |
100 |
114 |
| if (he && SvIV(HeVAL(he)) == 2) |
1 |
13 |
14 |
|
| 334 |
1 |
| croak("%s: operation '%" SVf "' becomes the tool '%" SVf "', which the " |
| 335 |
|
| "mcp keyword already declares. Rename one, or give the operation " |
| 336 |
|
| "x-mcp: { name: ... }", PMCP_WHO, SVfARG(op_id), SVfARG(base)); |
| 337 |
113 |
| name = pmcp_oa_name(aTHX_ op_id, used); |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
126 |
▶ | Renew(ts->t, ts->n + 1, pmcp_tool); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(ts->t, ts->n + 1, pmcp_tool); |
0 |
126 |
126 |
|
| 341 |
126 |
| t = &ts->t[ts->n++]; |
| 342 |
126 |
| Zero(t, 1, pmcp_tool); |
| 343 |
126 |
| t->name = newSVsv(name); |
| 344 |
126 |
| t->input = pmcp_compile_schema(aTHX_ name, pmcp_hget(aTHX_ spec, "input"), "input"); |
| 345 |
126 |
| t->timeout = 0; |
| 346 |
126 |
| t->oa_method = newSVsv(pmcp_hget(aTHX_ op, "method")); |
| 347 |
|
| { |
| 348 |
126 |
| char *u = SvPV_force_nolen(t->oa_method); |
| 349 |
|
| STRLEN k; |
| 350 |
556 |
▶ | for (k = 0; u[k]; k++) u[k] = toUPPER(u[k]); |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = 0; u[k]; k++) u[k] = toUPPER(u[k]); |
430 |
0 |
430 |
| for (k = 0; u[k]; k++) u[k] = toUPPER(u[k]); |
430 |
126 |
556 |
|
| 351 |
|
| } |
| 352 |
126 |
▶ | full = newSVsv(prefix && SvOK(prefix) ? prefix : &PL_sv_no); |
Branch: 2 branches
| Branch | True | False | Total |
| full = newSVsv(prefix && SvOK(prefix) ? prefix : &PL_sv_no); |
126 |
0 |
126 |
| full = newSVsv(prefix && SvOK(prefix) ? prefix : &PL_sv_no); |
126 |
0 |
126 |
|
| 353 |
126 |
| sv_catsv(full, pmcp_hget(aTHX_ op, "path")); |
| 354 |
126 |
| t->oa_path = full; |
| 355 |
126 |
| t->oa_where = pmcp_oa_where(aTHX_ api, op_id); |
| 356 |
|
| { |
| 357 |
126 |
| SV *annotations = pmcp_tool_annotations(aTHX_ name, |
| 358 |
|
| pmcp_hget(aTHX_ spec, "annotations")); |
| 359 |
126 |
▶ | if (annotations) sv_2mortal(annotations); |
Branch: 1 branch
| Branch | True | False | Total |
| if (annotations) sv_2mortal(annotations); |
126 |
0 |
126 |
|
| 360 |
504 |
▶ | for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) |
378 |
126 |
504 |
|
| 361 |
378 |
| t->entry[ver] = pmcp_tool_entry(aTHX_ name, spec, annotations, |
| 362 |
|
| (enum pmcp_ver)ver); |
| 363 |
|
| } |
| 364 |
126 |
| added++; |
| 365 |
|
| } |
| 366 |
|
| } |
| 367 |
17 |
▶ | if (!matched) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!matched) |
2 |
15 |
17 |
|
| 368 |
2 |
| croak("%s: openapi '%" SVf "' names no api mount (a mount object, '*', its " |
| 369 |
|
| "spec or its prefix)", PMCP_WHO, SVfARG(want)); |
| 370 |
|
| } |
| 371 |
15 |
▶ | if (allowed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allowed) { |
2 |
13 |
15 |
|
| 372 |
|
| HE *he; |
| 373 |
2 |
| hv_iterinit(allowed); |
| 374 |
5 |
▶ | while ((he = hv_iternext(allowed))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(allowed))) |
4 |
1 |
5 |
|
| 375 |
4 |
▶ | if (!SvIV(HeVAL(he))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIV(HeVAL(he))) |
1 |
3 |
4 |
|
| 376 |
1 |
| croak("%s: ops names '%" SVf "', which no named mount has", PMCP_WHO, |
| 377 |
|
| SVfARG(hv_iterkeysv(he))); |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
14 |
▶ | if (ts->n > 1) qsort(ts->t, (size_t)ts->n, sizeof(pmcp_tool), pmcp_tool_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts->n > 1) qsort(ts->t, (size_t)ts->n, sizeof(pmcp_tool), pmcp_tool_cmp); |
14 |
0 |
14 |
|
| 381 |
|
| { |
| 382 |
|
| int ver; |
| 383 |
56 |
▶ | for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) { |
42 |
14 |
56 |
|
| 384 |
42 |
▶ | if (ts->list[ver]) Safefree(ts->list[ver]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts->list[ver]) Safefree(ts->list[ver]); |
3 |
39 |
42 |
|
| 385 |
42 |
▶ | Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
42 |
0 |
42 |
| Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
0 |
42 |
42 |
|
| 386 |
402 |
▶ | for (i = 0; i < ts->n; i++) ts->list[ver][i] = ts->t[i].entry[ver]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ts->n; i++) ts->list[ver][i] = ts->t[i].entry[ver]; |
360 |
42 |
402 |
|
| 387 |
|
| } |
| 388 |
|
| } |
| 389 |
|
| PERL_UNUSED_VAR(added); |
| 390 |
14 |
| } |
| 391 |
|
| |
| 392 |
|
| /* ---- the innermost middleware ----------------------------------------------------------- */ |
| 393 |
|
| |
| 394 |
|
| /* Added last at on_compile, so it wraps the raw app: it keeps that app for the |
| 395 |
|
| * calls, derives the tools, and returns the app unchanged. cap = [app, server] */ |
| 396 |
19 |
| XS_INTERNAL(pmcp_capture_cb) |
| 397 |
|
| { |
| 398 |
19 |
| dXSARGS; |
| 399 |
19 |
| SV *app = pmcp_cap(aTHX_ cv, 0); |
| 400 |
19 |
| pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 1)); |
| 401 |
19 |
▶ | SV *inner = items > 0 ? ST(0) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *inner = items > 0 ? ST(0) : &PL_sv_undef; |
19 |
0 |
19 |
|
| 402 |
19 |
▶ | if (ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ps) { |
19 |
0 |
19 |
|
| 403 |
19 |
▶ | if (ps->srv.psgi) SvREFCNT_dec(ps->srv.psgi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ps->srv.psgi) SvREFCNT_dec(ps->srv.psgi); |
0 |
19 |
19 |
|
| 404 |
19 |
| ps->srv.psgi = newSVsv(inner); |
| 405 |
19 |
| pmcp_oa_derive(aTHX_ app, ps); |
| 406 |
14 |
▶ | if (pmcp_hget(aTHX_ ps->opts, "oauth")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_hget(aTHX_ ps->opts, "oauth")) |
3 |
11 |
14 |
|
| 407 |
3 |
| warn("%s: openapi tools carry the caller's token to the mount, whose own " |
| 408 |
|
| "checker decides: it must accept tokens issued for this MCP endpoint\n", |
| 409 |
|
| PMCP_WHO); |
| 410 |
|
| } |
| 411 |
14 |
| ST(0) = inner; |
| 412 |
14 |
| XSRETURN(1); |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* ---- the call ------------------------------------------------------------------------------ */ |
| 416 |
|
| |
| 417 |
|
| /* Run an OpenAPI tool: the synthetic env, the inner app, the answer. Returns |
| 418 |
|
| * the value the app answered with (mortal), or NULL with *why (+1). */ |
| 419 |
17 |
| static SV *pmcp_oa_call(pTHX_ SV *psgi, pmcp_tool *t, SV *c, HV *args, SV **why) |
| 420 |
|
| { |
| 421 |
17 |
| SV *envsv = sv_2mortal(pmcp_call_method(aTHX_ c, "env", NULL, 0, G_SCALAR, NULL)); |
| 422 |
17 |
| HV *env = (HV *)SvRV(envsv), *nenv = newHV(); |
| 423 |
17 |
| SV *path = newSVpvs(""), *query = newSVpvs(""), *body = NULL, *nrv, *res, *err = NULL; |
| 424 |
|
| STRLEN tl, i; |
| 425 |
17 |
| const char *tp = SvPV_const(t->oa_path, tl); |
| 426 |
17 |
| int nq = 0; |
| 427 |
|
| |
| 428 |
17 |
| *why = NULL; |
| 429 |
134 |
▶ | for (i = 0; i < tl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tl; i++) { |
117 |
17 |
134 |
|
| 430 |
117 |
▶ | if (tp[i] == '{') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tp[i] == '{') { |
6 |
111 |
117 |
|
| 431 |
6 |
| STRLEN j = i + 1; |
| 432 |
18 |
▶ | while (j < tl && tp[j] != '}') j++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < tl && tp[j] != '}') j++; |
18 |
0 |
18 |
| while (j < tl && tp[j] != '}') j++; |
12 |
6 |
18 |
|
| 433 |
6 |
▶ | if (j < tl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j < tl) { |
6 |
0 |
6 |
|
| 434 |
6 |
| SV **a = hv_fetch(args, tp + i + 1, (I32)(j - i - 1), 0); |
| 435 |
6 |
▶ | if (a && *a) pmcp_pct_cat(aTHX_ path, *a); |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && *a) pmcp_pct_cat(aTHX_ path, *a); |
6 |
0 |
6 |
| if (a && *a) pmcp_pct_cat(aTHX_ path, *a); |
6 |
0 |
6 |
|
| 436 |
6 |
| i = j; |
| 437 |
6 |
| continue; |
| 438 |
|
| } |
| 439 |
|
| } |
| 440 |
111 |
| sv_catpvn(path, tp + i, 1); |
| 441 |
|
| } |
| 442 |
|
| { |
| 443 |
|
| HE *he; |
| 444 |
17 |
| hv_iterinit(args); |
| 445 |
29 |
▶ | while ((he = hv_iternext(args))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(args))) { |
12 |
17 |
29 |
|
| 446 |
12 |
| SV *k = hv_iterkeysv(he), *v = HeVAL(he); |
| 447 |
|
| HE *we; |
| 448 |
|
| HV *w; |
| 449 |
|
| const char *in; |
| 450 |
|
| SV *nm; |
| 451 |
12 |
▶ | if (strEQ(SvPV_nolen(k), "body")) { body = v; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(SvPV_nolen(k), "body")) { body = v; continue; } |
2 |
10 |
12 |
|
| 452 |
10 |
| we = hv_fetch_ent(t->oa_where, k, 0, 0); |
| 453 |
10 |
▶ | if (!we || !pmcp_is_hash(HeVAL(we))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!we || !pmcp_is_hash(HeVAL(we))) continue; |
10 |
0 |
10 |
| if (!we || !pmcp_is_hash(HeVAL(we))) continue; |
0 |
10 |
10 |
|
| 454 |
10 |
| w = (HV *)SvRV(HeVAL(we)); |
| 455 |
10 |
| in = SvPV_nolen(pmcp_hget(aTHX_ w, "in")); |
| 456 |
10 |
| nm = pmcp_hget(aTHX_ w, "name"); |
| 457 |
10 |
▶ | if (strEQ(in, "query")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(in, "query")) { |
3 |
7 |
10 |
|
| 458 |
3 |
▶ | if (pmcp_is_array(v)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_array(v)) { |
1 |
2 |
3 |
|
| 459 |
1 |
| SSize_t x, n = av_len((AV *)SvRV(v)) + 1; |
| 460 |
3 |
▶ | for (x = 0; x < n; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < n; x++) { |
2 |
1 |
3 |
|
| 461 |
2 |
| SV **el = av_fetch((AV *)SvRV(v), x, 0); |
| 462 |
2 |
▶ | if (nq++) sv_catpvs(query, "&"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nq++) sv_catpvs(query, "&"); |
2 |
0 |
2 |
|
| 463 |
2 |
| pmcp_pct_cat(aTHX_ query, nm); |
| 464 |
2 |
| sv_catpvs(query, "="); |
| 465 |
2 |
▶ | if (el) pmcp_pct_cat(aTHX_ query, *el); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el) pmcp_pct_cat(aTHX_ query, *el); |
2 |
0 |
2 |
|
| 466 |
|
| } |
| 467 |
|
| } |
| 468 |
|
| else { |
| 469 |
2 |
▶ | if (nq++) sv_catpvs(query, "&"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nq++) sv_catpvs(query, "&"); |
1 |
1 |
2 |
|
| 470 |
2 |
| pmcp_pct_cat(aTHX_ query, nm); |
| 471 |
2 |
| sv_catpvs(query, "="); |
| 472 |
2 |
| pmcp_pct_cat(aTHX_ query, v); |
| 473 |
|
| } |
| 474 |
|
| } |
| 475 |
7 |
▶ | else if (strEQ(in, "header")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(in, "header")) { |
1 |
6 |
7 |
|
| 476 |
1 |
| SV *hk = newSVpvs("HTTP_"); |
| 477 |
|
| STRLEN x, nl; |
| 478 |
1 |
| const char *np = SvPV_const(nm, nl); |
| 479 |
8 |
▶ | for (x = 0; x < nl; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < nl; x++) { |
7 |
1 |
8 |
|
| 480 |
7 |
| char ch = np[x]; |
| 481 |
7 |
▶ | ch = ch == '-' ? '_' : toUPPER(ch); |
Branch: 2 branches
| Branch | True | False | Total |
| ch = ch == '-' ? '_' : toUPPER(ch); |
6 |
1 |
7 |
| ch = ch == '-' ? '_' : toUPPER(ch); |
4 |
2 |
6 |
|
| 482 |
7 |
| sv_catpvn(hk, &ch, 1); |
| 483 |
|
| } |
| 484 |
1 |
▶ | (void)hv_store_ent(nenv, hk, SvROK(v) ? newSVpv(SvTRUE(SvRV(v)) ? "true" : "false", 0) |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store_ent(nenv, hk, SvROK(v) ? newSVpv(SvTRUE(SvRV(v)) ? "true" : "false", 0) |
0 |
1 |
1 |
| (void)hv_store_ent(nenv, hk, SvROK(v) ? newSVpv(SvTRUE(SvRV(v)) ? "true" : "false", 0) |
0 |
0 |
0 |
|
| 485 |
|
| : newSVsv(v), 0); |
| 486 |
1 |
| SvREFCNT_dec(hk); |
| 487 |
|
| } |
| 488 |
|
| } |
| 489 |
|
| } |
| 490 |
|
| { |
| 491 |
|
| static const char *const carry[] = { |
| 492 |
|
| "HTTP_HOST", "HTTP_AUTHORIZATION", "REMOTE_ADDR", "REMOTE_PORT", |
| 493 |
|
| "SERVER_NAME", "SERVER_PORT", "SERVER_PROTOCOL", "psgi.url_scheme", |
| 494 |
|
| "psgi.errors", "psgi.version", "psgi.multithread", "psgi.multiprocess", |
| 495 |
|
| "psgi.run_once", "psgi.nonblocking", "psgix.logger", NULL |
| 496 |
|
| }; |
| 497 |
|
| int k; |
| 498 |
272 |
▶ | for (k = 0; carry[k]; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; carry[k]; k++) { |
255 |
17 |
272 |
|
| 499 |
255 |
| SV **s = hv_fetch(env, carry[k], (I32)strlen(carry[k]), 0); |
| 500 |
255 |
▶ | if (s && *s) (void)hv_store(nenv, carry[k], (I32)strlen(carry[k]), newSVsv(*s), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (s && *s) (void)hv_store(nenv, carry[k], (I32)strlen(carry[k]), newSVsv(*s), 0); |
125 |
130 |
255 |
| if (s && *s) (void)hv_store(nenv, carry[k], (I32)strlen(carry[k]), newSVsv(*s), 0); |
125 |
0 |
125 |
|
| 501 |
|
| } |
| 502 |
|
| } |
| 503 |
17 |
| (void)hv_stores(nenv, "REQUEST_METHOD", newSVsv(t->oa_method)); |
| 504 |
17 |
| (void)hv_stores(nenv, "PATH_INFO", path); |
| 505 |
17 |
| (void)hv_stores(nenv, "QUERY_STRING", query); |
| 506 |
17 |
| (void)hv_stores(nenv, "SCRIPT_NAME", newSVpvs("")); |
| 507 |
17 |
| (void)hv_stores(nenv, "pmcp.inner", newSViv(1)); |
| 508 |
17 |
| (void)hv_stores(nenv, "pmcp.tool", newSVsv(t->name)); |
| 509 |
17 |
▶ | if (body) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (body) { |
2 |
15 |
17 |
|
| 510 |
2 |
| SV *bytes = pmcp_json_trapped(aTHX_ body, &err); |
| 511 |
2 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
2 |
2 |
|
| 512 |
0 |
| SvREFCNT_dec((SV *)nenv); |
| 513 |
0 |
| *why = newSVpvs("the body argument is not JSON"); |
| 514 |
0 |
| SvREFCNT_dec(err); |
| 515 |
0 |
| return NULL; |
| 516 |
|
| } |
| 517 |
2 |
| sv_2mortal(bytes); |
| 518 |
2 |
| (void)hv_stores(nenv, "CONTENT_TYPE", newSVpvs("application/json")); |
| 519 |
2 |
| (void)hv_stores(nenv, "CONTENT_LENGTH", newSViv((IV)SvCUR(bytes))); |
| 520 |
2 |
| (void)hv_stores(nenv, "psgi.input", pmcp_input_handle(aTHX_ bytes)); |
| 521 |
|
| } |
| 522 |
|
| else { |
| 523 |
15 |
| (void)hv_stores(nenv, "CONTENT_LENGTH", newSViv(0)); |
| 524 |
15 |
| (void)hv_stores(nenv, "psgi.input", pmcp_input_handle(aTHX_ sv_2mortal(newSVpvs("")))); |
| 525 |
|
| } |
| 526 |
17 |
| nrv = sv_2mortal(newRV_noinc((SV *)nenv)); |
| 527 |
17 |
| res = pmcp_call_sv(aTHX_ psgi, &nrv, 1, G_SCALAR, &err); |
| 528 |
17 |
▶ | if (!res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) { |
0 |
17 |
17 |
|
| 529 |
0 |
| *why = pmcp_die_text(aTHX_ err); |
| 530 |
0 |
| SvREFCNT_dec(err); |
| 531 |
0 |
| return NULL; |
| 532 |
|
| } |
| 533 |
17 |
| sv_2mortal(res); |
| 534 |
17 |
| return pmcp_settle(aTHX_ c, res, t->timeout, why); |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
|
| #endif /* PMCP_OA_H */ |