| 1 |
|
| /* pmcp_res.h - resources: declarations, the two lists, and resources/read. |
| 2 |
|
| * |
| 3 |
|
| * Needs: pmcp_uri.h, pmcp_call.h. |
| 4 |
|
| * |
| 5 |
|
| * A declared URI with a {var} is a template and is listed by |
| 6 |
|
| * resources/templates/list. Without one it is a resource and is listed by |
| 7 |
|
| * resources/list. A read tries the exact URIs first, then the templates in |
| 8 |
|
| * declaration order, and the template's variables reach the action as params. |
| 9 |
|
| * |
| 10 |
|
| * A resource has no isError. A read that fails is a JSON-RPC error: -32002 |
| 11 |
|
| * when there is nothing at the URI (or the caller may not see it), -32603 when |
| 12 |
|
| * the action failed. |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #ifndef PMCP_RES_H |
| 16 |
|
| #define PMCP_RES_H |
| 17 |
|
| |
| 18 |
|
| #define PMCP_NOT_FOUND (-32002) |
| 19 |
|
| |
| 20 |
|
| typedef struct { |
| 21 |
|
| SV *uri; /* the URI or the template, owned */ |
| 22 |
|
| pmcp_tpl *tpl; /* NULL for an exact URI */ |
| 23 |
|
| SV *target; |
| 24 |
|
| AV *guards; |
| 25 |
|
| NV timeout; |
| 26 |
|
| SV *mime; /* or NULL */ |
| 27 |
|
| SV *entry[PMCP_V_LATEST + 1]; |
| 28 |
|
| } pmcp_resource; |
| 29 |
|
| |
| 30 |
|
| typedef struct { |
| 31 |
|
| pmcp_resource *r; /* exact URIs sorted, then templates */ |
| 32 |
|
| IV n, n_exact; |
| 33 |
|
| IV page_size; |
| 34 |
|
| int any_guard; |
| 35 |
|
| SV **list[PMCP_V_LATEST + 1]; /* exact entries, borrowed */ |
| 36 |
|
| SV **tlist[PMCP_V_LATEST + 1]; /* template entries, borrowed */ |
| 37 |
|
| } pmcp_resources; |
| 38 |
|
| |
| 39 |
|
| static const char *const PMCP_RES_KEYS[] = { |
| 40 |
|
| "name", "title", "description", "mime", "size", "annotations", "guard", |
| 41 |
|
| "icons", "timeout", NULL |
| 42 |
|
| }; |
| 43 |
|
| |
| 44 |
7 |
| static void pmcp_res_croak(pTHX_ SV *uri, const char *what) |
| 45 |
|
| { |
| 46 |
7 |
| croak("%s: mcp resource '%" SVf "' %s", PMCP_WHO, SVfARG(uri), what); |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| /* audience, priority, lastModified. +1 HV ref, or NULL. */ |
| 50 |
99 |
| static SV *pmcp_res_annotations(pTHX_ SV *uri, SV *a) |
| 51 |
|
| { |
| 52 |
|
| static const char *const keys[] = { "audience", "priority", "lastModified", NULL }; |
| 53 |
|
| HV *in, *out; |
| 54 |
|
| SV *v; |
| 55 |
99 |
▶ | if (!a) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) return NULL; |
95 |
4 |
99 |
|
| 56 |
4 |
▶ | if (!pmcp_is_hash(a)) pmcp_res_croak(aTHX_ uri, "annotations must be a hash"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_hash(a)) pmcp_res_croak(aTHX_ uri, "annotations must be a hash"); |
0 |
4 |
4 |
|
| 57 |
4 |
| in = (HV *)SvRV(a); |
| 58 |
4 |
| pmcp_check_keys(aTHX_ "resource annotation", in, keys); |
| 59 |
4 |
| out = (HV *)sv_2mortal((SV *)newHV()); |
| 60 |
4 |
▶ | if ((v = pmcp_hget(aTHX_ in, "audience"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ in, "audience"))) { |
2 |
2 |
4 |
|
| 61 |
|
| AV *src, *dst; |
| 62 |
|
| SSize_t i, n; |
| 63 |
2 |
▶ | if (!pmcp_is_array(v)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_array(v)) |
0 |
2 |
2 |
|
| 64 |
0 |
| pmcp_res_croak(aTHX_ uri, "annotation audience must be a list of user and assistant"); |
| 65 |
2 |
| src = (AV *)SvRV(v); |
| 66 |
2 |
| dst = newAV(); |
| 67 |
2 |
| (void)hv_stores(out, "audience", newRV_noinc((SV *)dst)); |
| 68 |
2 |
| n = av_len(src) + 1; |
| 69 |
4 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
3 |
1 |
4 |
|
| 70 |
3 |
| SV **e = av_fetch(src, i, 0); |
| 71 |
6 |
▶ | const char *s = (e && pmcp_is_text(*e)) ? SvPV_nolen(*e) : ""; |
Branch: 2 branches
| Branch | True | False | Total |
| const char *s = (e && pmcp_is_text(*e)) ? SvPV_nolen(*e) : ""; |
3 |
0 |
3 |
| const char *s = (e && pmcp_is_text(*e)) ? SvPV_nolen(*e) : ""; |
3 |
0 |
3 |
|
| 72 |
3 |
▶ | if (!(strEQ(s, "user") || strEQ(s, "assistant"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(strEQ(s, "user") || strEQ(s, "assistant"))) |
2 |
1 |
3 |
| if (!(strEQ(s, "user") || strEQ(s, "assistant"))) |
1 |
1 |
2 |
|
| 73 |
1 |
| pmcp_res_croak(aTHX_ uri, "annotation audience must be a list of user and assistant"); |
| 74 |
2 |
| av_push(dst, newSVpv(s, 0)); |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
3 |
▶ | if ((v = pmcp_hget(aTHX_ in, "priority"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ in, "priority"))) { |
2 |
1 |
3 |
|
| 78 |
2 |
▶ | if (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) > 1) |
Branch: 4 branches
| Branch | True | False | Total |
| if (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) > 1) |
2 |
0 |
2 |
| if (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) > 1) |
2 |
0 |
2 |
| if (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) > 1) |
2 |
0 |
2 |
| if (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) > 1) |
1 |
1 |
2 |
|
| 79 |
1 |
| pmcp_res_croak(aTHX_ uri, "annotation priority must be a number from 0 to 1"); |
| 80 |
1 |
| (void)hv_stores(out, "priority", newSVnv(SvNV(v))); |
| 81 |
|
| } |
| 82 |
2 |
▶ | if ((v = pmcp_hget(aTHX_ in, "lastModified"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ in, "lastModified"))) { |
2 |
0 |
2 |
|
| 83 |
|
| STRLEN l; |
| 84 |
4 |
▶ | const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = pmcp_is_text(v) ? SvPV_const(v, l) : NULL; |
2 |
0 |
2 |
|
| 85 |
|
| /* an ISO 8601 date-time: the date and a T are enough to tell */ |
| 86 |
2 |
▶ | if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
Branch: 5 branches
| Branch | True | False | Total |
| if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
2 |
0 |
2 |
| if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
1 |
1 |
2 |
| if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
1 |
0 |
1 |
| if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
1 |
0 |
1 |
| if (!p || l < 11 || p[4] != '-' || p[7] != '-' || p[10] != 'T') |
0 |
1 |
1 |
|
| 87 |
1 |
| pmcp_res_croak(aTHX_ uri, "annotation lastModified must be an ISO 8601 date-time"); |
| 88 |
1 |
| (void)hv_stores(out, "lastModified", newSVsv(v)); |
| 89 |
|
| } |
| 90 |
1 |
| return newRV_inc((SV *)out); |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
288 |
| static SV *pmcp_res_entry(pTHX_ pmcp_resource *r, HV *spec, SV *annotations, |
| 94 |
|
| enum pmcp_ver v) |
| 95 |
|
| { |
| 96 |
288 |
| HV *e = newHV(); |
| 97 |
288 |
| SV *x, *bytes, *err = NULL; |
| 98 |
288 |
▶ | (void)hv_store(e, r->tpl ? "uriTemplate" : "uri", r->tpl ? 11 : 3, |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store(e, r->tpl ? "uriTemplate" : "uri", r->tpl ? 11 : 3, |
141 |
147 |
288 |
| (void)hv_store(e, r->tpl ? "uriTemplate" : "uri", r->tpl ? 11 : 3, |
141 |
147 |
288 |
|
| 99 |
|
| newSVsv(r->uri), 0); |
| 100 |
288 |
| x = pmcp_hget(aTHX_ spec, "name"); |
| 101 |
288 |
▶ | (void)hv_stores(e, "name", x ? pmcp_str(aTHX_ x) : newSVsv(r->uri)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(e, "name", x ? pmcp_str(aTHX_ x) : newSVsv(r->uri)); |
12 |
276 |
288 |
|
| 102 |
288 |
▶ | if ((x = pmcp_hget(aTHX_ spec, "description"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((x = pmcp_hget(aTHX_ spec, "description"))) |
9 |
279 |
288 |
|
| 103 |
9 |
| (void)hv_stores(e, "description", pmcp_str(aTHX_ x)); |
| 104 |
288 |
▶ | if (r->mime) (void)hv_stores(e, "mimeType", newSVsv(r->mime)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->mime) (void)hv_stores(e, "mimeType", newSVsv(r->mime)); |
18 |
270 |
288 |
|
| 105 |
288 |
▶ | if (annotations) (void)hv_stores(e, "annotations", newSVsv(annotations)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (annotations) (void)hv_stores(e, "annotations", newSVsv(annotations)); |
3 |
285 |
288 |
|
| 106 |
288 |
▶ | if (!r->tpl && (x = pmcp_hget(aTHX_ spec, "size"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r->tpl && (x = pmcp_hget(aTHX_ spec, "size"))) |
147 |
141 |
288 |
| if (!r->tpl && (x = pmcp_hget(aTHX_ spec, "size"))) |
3 |
144 |
147 |
|
| 107 |
3 |
| (void)hv_stores(e, "size", newSViv(SvIV(x))); |
| 108 |
288 |
▶ | if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title"))) |
192 |
96 |
288 |
| if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title"))) |
2 |
190 |
192 |
|
| 109 |
2 |
| (void)hv_stores(e, "title", pmcp_str(aTHX_ x)); |
| 110 |
288 |
▶ | if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
96 |
192 |
288 |
| if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
1 |
95 |
96 |
|
| 111 |
1 |
| (void)hv_stores(e, "icons", newSVsv(x)); |
| 112 |
288 |
| bytes = pmcp_json_trapped(aTHX_ sv_2mortal(newRV_noinc((SV *)e)), &err); |
| 113 |
288 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
288 |
288 |
|
| 114 |
0 |
| sv_2mortal(err); |
| 115 |
0 |
| croak("%s: mcp resource '%" SVf "' cannot be listed: %" SVf, PMCP_WHO, |
| 116 |
|
| SVfARG(r->uri), SVfARG(err)); |
| 117 |
|
| } |
| 118 |
288 |
| return bytes; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
34 |
| static void pmcp_resources_free(pTHX_ pmcp_resources *rs) |
| 122 |
|
| { |
| 123 |
|
| IV i; |
| 124 |
|
| int v; |
| 125 |
34 |
▶ | if (!rs) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rs) return; |
0 |
34 |
34 |
|
| 126 |
122 |
▶ | for (i = 0; i < rs->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < rs->n; i++) { |
88 |
34 |
122 |
|
| 127 |
88 |
| pmcp_resource *r = &rs->r[i]; |
| 128 |
88 |
▶ | if (r->uri) SvREFCNT_dec(r->uri); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->uri) SvREFCNT_dec(r->uri); |
88 |
0 |
88 |
|
| 129 |
88 |
▶ | if (r->tpl) pmcp_tpl_free(r->tpl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->tpl) pmcp_tpl_free(r->tpl); |
39 |
49 |
88 |
|
| 130 |
88 |
▶ | if (r->target) SvREFCNT_dec(r->target); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->target) SvREFCNT_dec(r->target); |
88 |
0 |
88 |
|
| 131 |
88 |
▶ | if (r->guards) SvREFCNT_dec((SV *)r->guards); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->guards) SvREFCNT_dec((SV *)r->guards); |
16 |
72 |
88 |
|
| 132 |
88 |
▶ | if (r->mime) SvREFCNT_dec(r->mime); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->mime) SvREFCNT_dec(r->mime); |
6 |
82 |
88 |
|
| 133 |
440 |
▶ | for (v = 0; v <= PMCP_V_LATEST; v++) if (r->entry[v]) SvREFCNT_dec(r->entry[v]); |
Branch: 2 branches
| Branch | True | False | Total |
| for (v = 0; v <= PMCP_V_LATEST; v++) if (r->entry[v]) SvREFCNT_dec(r->entry[v]); |
264 |
88 |
352 |
| for (v = 0; v <= PMCP_V_LATEST; v++) if (r->entry[v]) SvREFCNT_dec(r->entry[v]); |
352 |
88 |
440 |
|
| 134 |
|
| } |
| 135 |
170 |
▶ | for (v = 0; v <= PMCP_V_LATEST; v++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (v = 0; v <= PMCP_V_LATEST; v++) { |
136 |
34 |
170 |
|
| 136 |
136 |
▶ | if (rs->list[v]) Safefree(rs->list[v]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rs->list[v]) Safefree(rs->list[v]); |
102 |
34 |
136 |
|
| 137 |
136 |
▶ | if (rs->tlist[v]) Safefree(rs->tlist[v]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rs->tlist[v]) Safefree(rs->tlist[v]); |
102 |
34 |
136 |
|
| 138 |
|
| } |
| 139 |
34 |
| Safefree(rs->r); |
| 140 |
34 |
| Safefree(rs); |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
50 |
| static int pmcp_res_cmp(const void *a, const void *b) |
| 144 |
|
| { |
| 145 |
|
| dTHX; |
| 146 |
50 |
| return sv_cmp(((const pmcp_resource *)a)->uri, ((const pmcp_resource *)b)->uri); |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
56 |
| static pmcp_resources *pmcp_resources_freeze(pTHX_ SV *app, AV *decls, HV *opts) |
| 150 |
|
| { |
| 151 |
|
| pmcp_resources *rs; |
| 152 |
56 |
| SSize_t i, n = av_len(decls) + 1; |
| 153 |
56 |
| IV k, nt = 0; |
| 154 |
|
| pmcp_resource *tmp; |
| 155 |
|
| SV *v; |
| 156 |
56 |
▶ | NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0; |
0 |
56 |
56 |
|
| 157 |
|
| int pass; |
| 158 |
|
| |
| 159 |
56 |
| Newxz(rs, 1, pmcp_resources); |
| 160 |
56 |
▶ | Newxz(rs->r, n > 0 ? n : 1, pmcp_resource); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(rs->r, n > 0 ? n : 1, pmcp_resource); |
56 |
0 |
56 |
| Newxz(rs->r, n > 0 ? n : 1, pmcp_resource); |
0 |
56 |
56 |
|
| 161 |
56 |
▶ | Newxz(tmp, n > 0 ? n : 1, pmcp_resource); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(tmp, n > 0 ? n : 1, pmcp_resource); |
56 |
0 |
56 |
| Newxz(tmp, n > 0 ? n : 1, pmcp_resource); |
0 |
56 |
56 |
|
| 162 |
56 |
▶ | rs->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50; |
Branch: 1 branch
| Branch | True | False | Total |
| rs->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50; |
56 |
0 |
56 |
|
| 163 |
|
| |
| 164 |
246 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
208 |
38 |
246 |
|
| 165 |
208 |
| AV *d = (AV *)SvRV(*av_fetch(decls, i, 0)); |
| 166 |
208 |
| SV *uri = *av_fetch(d, PMCP_D_KEY, 0); |
| 167 |
208 |
| HV *spec = (HV *)SvRV(*av_fetch(d, PMCP_D_SPEC, 0)); |
| 168 |
|
| pmcp_resource *r; |
| 169 |
|
| SV *annotations; |
| 170 |
|
| STRLEN ul; |
| 171 |
|
| const char *up; |
| 172 |
|
| int ver; |
| 173 |
|
| |
| 174 |
416 |
▶ | if (!strEQ(SvPV_nolen(*av_fetch(d, PMCP_D_KIND, 0)), "resource")) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strEQ(SvPV_nolen(*av_fetch(d, PMCP_D_KIND, 0)), "resource")) continue; |
94 |
114 |
208 |
|
| 175 |
|
| { |
| 176 |
114 |
| SV *what = sv_2mortal(newSVpvf("mcp resource '%" SVf "' key", SVfARG(uri))); |
| 177 |
114 |
| pmcp_check_keys(aTHX_ SvPV_nolen(what), spec, PMCP_RES_KEYS); |
| 178 |
|
| } |
| 179 |
113 |
| up = SvPV_const(uri, ul); |
| 180 |
113 |
▶ | if (!memchr(up, ':', ul)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!memchr(up, ':', ul)) |
1 |
112 |
113 |
|
| 181 |
1 |
| pmcp_res_croak(aTHX_ uri, "is not a URI: it has no scheme"); |
| 182 |
|
| { |
| 183 |
|
| static const char *const text[] = { "name", "title", "description", "mime", NULL }; |
| 184 |
|
| int ti; |
| 185 |
559 |
▶ | for (ti = 0; text[ti]; ti++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ti = 0; text[ti]; ti++) |
448 |
111 |
559 |
|
| 186 |
448 |
▶ | if ((v = pmcp_hget(aTHX_ spec, text[ti])) && !pmcp_is_text(v)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, text[ti])) && !pmcp_is_text(v)) { |
15 |
433 |
448 |
| if ((v = pmcp_hget(aTHX_ spec, text[ti])) && !pmcp_is_text(v)) { |
1 |
14 |
15 |
|
| 187 |
1 |
| SV *m = sv_2mortal(newSVpvf("%s must be a string", text[ti])); |
| 188 |
1 |
| pmcp_res_croak(aTHX_ uri, SvPV_nolen(m)); |
| 189 |
|
| } |
| 190 |
|
| } |
| 191 |
111 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "size")) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "size")) |
3 |
108 |
111 |
|
| 192 |
3 |
▶ | && (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) != (NV)SvIV(v))) |
Branch: 4 branches
| Branch | True | False | Total |
| && (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) != (NV)SvIV(v))) |
3 |
0 |
3 |
| && (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) != (NV)SvIV(v))) |
3 |
0 |
3 |
| && (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) != (NV)SvIV(v))) |
2 |
1 |
3 |
| && (SvROK(v) || !looks_like_number(v) || SvNV(v) < 0 || SvNV(v) != (NV)SvIV(v))) |
0 |
2 |
2 |
|
| 193 |
1 |
| pmcp_res_croak(aTHX_ uri, "size must be a whole number of bytes"); |
| 194 |
110 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v); |
1 |
109 |
110 |
|
| 195 |
110 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "timeout")) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "timeout")) |
0 |
110 |
110 |
|
| 196 |
0 |
▶ | && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
Branch: 3 branches
| Branch | True | False | Total |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
0 |
0 |
0 |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
0 |
0 |
0 |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
0 |
0 |
0 |
|
| 197 |
0 |
| pmcp_res_croak(aTHX_ uri, "timeout must be a number of seconds above 0"); |
| 198 |
|
| |
| 199 |
110 |
| r = &tmp[nt++]; |
| 200 |
110 |
| r->uri = newSVsv(uri); |
| 201 |
110 |
▶ | if (pmcp_is_template(up, ul)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_template(up, ul)) { |
58 |
52 |
110 |
|
| 202 |
58 |
| r->tpl = pmcp_tpl_parse(aTHX_ uri); |
| 203 |
48 |
▶ | if (pmcp_hget(aTHX_ spec, "size")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_hget(aTHX_ spec, "size")) |
1 |
47 |
48 |
|
| 204 |
1 |
| pmcp_res_croak(aTHX_ uri, "is a template, and a template has no size"); |
| 205 |
|
| } |
| 206 |
99 |
| r->target = newSVsv(*av_fetch(d, PMCP_D_TARGET, 0)); |
| 207 |
115 |
▶ | r->guards = pmcp_resolve_guards(aTHX_ app, "resource", uri, |
Branch: 1 branch
| Branch | True | False | Total |
| r->guards = pmcp_resolve_guards(aTHX_ app, "resource", uri, |
16 |
83 |
99 |
|
| 208 |
115 |
| hv_exists(spec, "guard", 5) ? *hv_fetchs(spec, "guard", 0) : NULL); |
| 209 |
99 |
▶ | if (r->guards) rs->any_guard = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->guards) rs->any_guard = 1; |
16 |
83 |
99 |
|
| 210 |
99 |
▶ | r->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout; |
Branch: 1 branch
| Branch | True | False | Total |
| r->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout; |
0 |
99 |
99 |
|
| 211 |
99 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "mime"))) r->mime = newSVsv(v); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "mime"))) r->mime = newSVsv(v); |
6 |
93 |
99 |
|
| 212 |
99 |
| annotations = pmcp_res_annotations(aTHX_ uri, pmcp_hget(aTHX_ spec, "annotations")); |
| 213 |
96 |
▶ | if (annotations) sv_2mortal(annotations); |
Branch: 1 branch
| Branch | True | False | Total |
| if (annotations) sv_2mortal(annotations); |
1 |
95 |
96 |
|
| 214 |
384 |
▶ | 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++) |
288 |
96 |
384 |
|
| 215 |
288 |
| r->entry[ver] = pmcp_res_entry(aTHX_ r, spec, annotations, (enum pmcp_ver)ver); |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| /* exact URIs first, sorted for the list and the search; then templates in |
| 219 |
|
| * declaration order, which is the order a read tries them */ |
| 220 |
114 |
▶ | for (pass = 0; pass < 2; pass++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (pass = 0; pass < 2; pass++) |
76 |
38 |
114 |
|
| 221 |
268 |
▶ | for (k = 0; k < nt; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nt; k++) |
192 |
76 |
268 |
|
| 222 |
192 |
▶ | if ((pass == 0) == (tmp[k].tpl == NULL)) rs->r[rs->n++] = tmp[k]; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((pass == 0) == (tmp[k].tpl == NULL)) rs->r[rs->n++] = tmp[k]; |
96 |
96 |
192 |
|
| 223 |
38 |
| Safefree(tmp); |
| 224 |
134 |
▶ | for (k = 0; k < rs->n; k++) if (!rs->r[k].tpl) rs->n_exact++; |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = 0; k < rs->n; k++) if (!rs->r[k].tpl) rs->n_exact++; |
49 |
47 |
96 |
| for (k = 0; k < rs->n; k++) if (!rs->r[k].tpl) rs->n_exact++; |
96 |
38 |
134 |
|
| 225 |
38 |
▶ | if (rs->n_exact > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rs->n_exact > 1) |
16 |
22 |
38 |
|
| 226 |
16 |
| qsort(rs->r, (size_t)rs->n_exact, sizeof(pmcp_resource), pmcp_res_cmp); |
| 227 |
|
| |
| 228 |
|
| /* two templates one URI could match is a read whose answer depends on |
| 229 |
|
| * declaration order, which nobody reading the code would guess */ |
| 230 |
|
| { |
| 231 |
|
| IV a, b; |
| 232 |
77 |
▶ | for (a = rs->n_exact; a < rs->n; a++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (a = rs->n_exact; a < rs->n; a++) |
43 |
34 |
77 |
|
| 233 |
53 |
▶ | for (b = a + 1; b < rs->n; b++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (b = a + 1; b < rs->n; b++) |
14 |
39 |
53 |
|
| 234 |
14 |
▶ | if (pmcp_tpl_overlap(rs->r[a].tpl, rs->r[b].tpl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_tpl_overlap(rs->r[a].tpl, rs->r[b].tpl)) |
4 |
10 |
14 |
|
| 235 |
4 |
| croak("%s: mcp resource '%" SVf "' and '%" SVf "' can match " |
| 236 |
|
| "the same URI", PMCP_WHO, SVfARG(rs->r[a].uri), |
| 237 |
|
| SVfARG(rs->r[b].uri)); |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| { |
| 241 |
|
| int ver; |
| 242 |
136 |
▶ | 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++) { |
102 |
34 |
136 |
|
| 243 |
102 |
▶ | Newxz(rs->list[ver], rs->n_exact > 0 ? rs->n_exact : 1, SV *); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(rs->list[ver], rs->n_exact > 0 ? rs->n_exact : 1, SV *); |
60 |
42 |
102 |
| Newxz(rs->list[ver], rs->n_exact > 0 ? rs->n_exact : 1, SV *); |
0 |
60 |
60 |
|
| 244 |
102 |
▶ | Newxz(rs->tlist[ver], rs->n - rs->n_exact > 0 ? rs->n - rs->n_exact : 1, SV *); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(rs->tlist[ver], rs->n - rs->n_exact > 0 ? rs->n - rs->n_exact : 1, SV *); |
96 |
6 |
102 |
| Newxz(rs->tlist[ver], rs->n - rs->n_exact > 0 ? rs->n - rs->n_exact : 1, SV *); |
0 |
96 |
96 |
|
| 245 |
366 |
▶ | for (k = 0; k < rs->n; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < rs->n; k++) { |
264 |
102 |
366 |
|
| 246 |
264 |
▶ | if (k < rs->n_exact) rs->list[ver][k] = rs->r[k].entry[ver]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < rs->n_exact) rs->list[ver][k] = rs->r[k].entry[ver]; |
147 |
117 |
264 |
|
| 247 |
117 |
| else rs->tlist[ver][k - rs->n_exact] = rs->r[k].entry[ver]; |
| 248 |
|
| } |
| 249 |
|
| } |
| 250 |
|
| } |
| 251 |
34 |
| return rs; |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
|
| /* ---- the lists ---------------------------------------------------------------- */ |
| 255 |
|
| |
| 256 |
|
| typedef struct { pmcp_resources *rs; IV offset; SV *c; } pmcp_res_list_ctx; |
| 257 |
|
| |
| 258 |
38 |
| static int pmcp_res_visible(pTHX_ void *vctx, IV i) |
| 259 |
|
| { |
| 260 |
38 |
| pmcp_res_list_ctx *x = (pmcp_res_list_ctx *)vctx; |
| 261 |
38 |
| pmcp_resource *r = &x->rs->r[x->offset + i]; |
| 262 |
|
| SV *why; |
| 263 |
38 |
▶ | if (!r->guards) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->guards) return 1; |
36 |
2 |
38 |
|
| 264 |
2 |
▶ | if (!x->c) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x->c) return 0; |
0 |
2 |
2 |
|
| 265 |
2 |
| why = pmcp_run_guards(aTHX_ r->guards, x->c); |
| 266 |
2 |
▶ | if (!why) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!why) return 1; |
0 |
2 |
2 |
|
| 267 |
2 |
| SvREFCNT_dec(why); |
| 268 |
2 |
| return 0; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
13 |
| static SV *pmcp_res_list_common(pTHX_ const pmcp_srv *s, pmcp_req *r, |
| 272 |
|
| HV *params, int templates) |
| 273 |
|
| { |
| 274 |
13 |
| pmcp_resources *rs = (pmcp_resources *)s->resources; |
| 275 |
13 |
▶ | enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; |
Branch: 1 branch
| Branch | True | False | Total |
| enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; |
13 |
0 |
13 |
|
| 276 |
13 |
▶ | SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL; |
13 |
0 |
13 |
|
| 277 |
13 |
| IV count, start = 0; |
| 278 |
|
| pmcp_res_list_ctx x; |
| 279 |
|
| |
| 280 |
13 |
▶ | if (!rs) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rs) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
0 |
13 |
13 |
|
| 281 |
13 |
▶ | count = templates ? rs->n - rs->n_exact : rs->n_exact; |
Branch: 1 branch
| Branch | True | False | Total |
| count = templates ? rs->n - rs->n_exact : rs->n_exact; |
2 |
11 |
13 |
|
| 282 |
13 |
▶ | if (cur && SvOK(*cur)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cur && SvOK(*cur)) { |
8 |
5 |
13 |
| if (cur && SvOK(*cur)) { |
8 |
0 |
8 |
|
| 283 |
8 |
| start = pmcp_cursor_read(aTHX_ *cur, count); |
| 284 |
8 |
▶ | if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor"); |
0 |
8 |
8 |
|
| 285 |
|
| } |
| 286 |
13 |
| x.rs = rs; |
| 287 |
13 |
▶ | x.offset = templates ? rs->n_exact : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| x.offset = templates ? rs->n_exact : 0; |
2 |
11 |
13 |
|
| 288 |
13 |
| x.c = r->c; |
| 289 |
13 |
▶ | r->raw = pmcp_page_bytes(aTHX_ templates ? "resourceTemplates" : "resources", |
Branch: 2 branches
| Branch | True | False | Total |
| r->raw = pmcp_page_bytes(aTHX_ templates ? "resourceTemplates" : "resources", |
2 |
11 |
13 |
| r->raw = pmcp_page_bytes(aTHX_ templates ? "resourceTemplates" : "resources", |
2 |
11 |
13 |
|
| 290 |
|
| templates ? rs->tlist[v] : rs->list[v], count, start, |
| 291 |
13 |
▶ | rs->page_size, rs->any_guard ? pmcp_res_visible : NULL, &x); |
Branch: 1 branch
| Branch | True | False | Total |
| rs->page_size, rs->any_guard ? pmcp_res_visible : NULL, &x); |
11 |
2 |
13 |
|
| 292 |
13 |
| return NULL; |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
11 |
| static SV *pmcp_resources_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params) |
| 296 |
|
| { |
| 297 |
11 |
| return pmcp_res_list_common(aTHX_ s, r, params, 0); |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
2 |
| static SV *pmcp_resources_templates(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params) |
| 301 |
|
| { |
| 302 |
2 |
| return pmcp_res_list_common(aTHX_ s, r, params, 1); |
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
|
| /* ---- resources/read --------------------------------------------------------------- */ |
| 306 |
|
| |
| 307 |
|
| /* Is this media type text? Any text/ type, and the structured text types. */ |
| 308 |
19 |
| static int pmcp_mime_textual(pTHX_ SV *mime) |
| 309 |
|
| { |
| 310 |
|
| STRLEN l; |
| 311 |
|
| const char *p; |
| 312 |
19 |
▶ | if (!mime) return -1; /* undeclared: look at the bytes */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mime) return -1; /* undeclared: look at the bytes */ |
7 |
12 |
19 |
|
| 313 |
12 |
| p = SvPV_nolen(mime); |
| 314 |
12 |
| l = strlen(p); |
| 315 |
12 |
▶ | if (l >= 5 && memEQ(p, "text/", 5)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l >= 5 && memEQ(p, "text/", 5)) return 1; |
12 |
0 |
12 |
| if (l >= 5 && memEQ(p, "text/", 5)) return 1; |
2 |
10 |
12 |
|
| 316 |
1 |
▶ | return instr(p, "json") || instr(p, "xml") || instr(p, "yaml") |
Branch: 2 branches
| Branch | True | False | Total |
| return instr(p, "json") || instr(p, "xml") || instr(p, "yaml") |
1 |
0 |
1 |
| return instr(p, "json") || instr(p, "xml") || instr(p, "yaml") |
1 |
0 |
1 |
|
| 317 |
11 |
▶ | || instr(p, "javascript") || instr(p, "ecmascript"); |
Branch: 3 branches
| Branch | True | False | Total |
| || instr(p, "javascript") || instr(p, "ecmascript"); |
1 |
9 |
10 |
| || instr(p, "javascript") || instr(p, "ecmascript"); |
1 |
0 |
1 |
| || instr(p, "javascript") || instr(p, "ecmascript"); |
0 |
1 |
1 |
|
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
|
| /* One contents entry for a string. +1 HV ref. */ |
| 321 |
19 |
| static SV *pmcp_res_content(pTHX_ SV *uri, SV *mime, SV *val) |
| 322 |
|
| { |
| 323 |
19 |
| HV *e = newHV(); |
| 324 |
19 |
| int textual = pmcp_mime_textual(aTHX_ mime); |
| 325 |
|
| STRLEN l; |
| 326 |
|
| const char *p; |
| 327 |
19 |
| (void)hv_stores(e, "uri", newSVsv(uri)); |
| 328 |
19 |
▶ | if (mime) (void)hv_stores(e, "mimeType", newSVsv(mime)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (mime) (void)hv_stores(e, "mimeType", newSVsv(mime)); |
12 |
7 |
19 |
|
| 329 |
19 |
▶ | if (SvUTF8(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(val)) { |
3 |
16 |
19 |
|
| 330 |
3 |
▶ | if (textual != 0) { (void)hv_stores(e, "text", newSVsv(val)); goto done; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (textual != 0) { (void)hv_stores(e, "text", newSVsv(val)); goto done; } |
3 |
0 |
3 |
|
| 331 |
0 |
| p = SvPVutf8(val, l); |
| 332 |
|
| } |
| 333 |
16 |
| else p = SvPV(val, l); |
| 334 |
30 |
▶ | if (textual == 1 || (textual == -1 && is_utf8_string((U8 *)p, l))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (textual == 1 || (textual == -1 && is_utf8_string((U8 *)p, l))) { |
6 |
10 |
16 |
| if (textual == 1 || (textual == -1 && is_utf8_string((U8 *)p, l))) { |
5 |
1 |
6 |
| if (textual == 1 || (textual == -1 && is_utf8_string((U8 *)p, l))) { |
4 |
1 |
5 |
|
| 335 |
|
| /* bytes that are UTF-8 go out as the characters they spell */ |
| 336 |
14 |
| SV *t = newSVpvn(p, l); |
| 337 |
14 |
▶ | if (is_utf8_string((U8 *)p, l)) SvUTF8_on(t); |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_utf8_string((U8 *)p, l)) SvUTF8_on(t); |
14 |
0 |
14 |
|
| 338 |
14 |
| (void)hv_stores(e, "text", t); |
| 339 |
|
| } |
| 340 |
2 |
| else (void)hv_stores(e, "blob", pmcp_b64(aTHX_ (const unsigned char *)p, l)); |
| 341 |
19 |
| done: |
| 342 |
19 |
| return newRV_noinc((SV *)e); |
| 343 |
|
| } |
| 344 |
|
| |
| 345 |
|
| /* The exact resource, or the first template that matches (with *caps +1). */ |
| 346 |
43 |
| static pmcp_resource *pmcp_res_find(pTHX_ pmcp_resources *rs, SV *uri, HV **caps) |
| 347 |
|
| { |
| 348 |
43 |
| IV lo = 0, hi = rs->n_exact, k; |
| 349 |
43 |
| *caps = NULL; |
| 350 |
106 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
81 |
25 |
106 |
|
| 351 |
81 |
| IV mid = lo + (hi - lo) / 2; |
| 352 |
81 |
| int c = sv_cmp(uri, rs->r[mid].uri); |
| 353 |
81 |
▶ | if (c == 0) return &rs->r[mid]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0) return &rs->r[mid]; |
18 |
63 |
81 |
|
| 354 |
63 |
▶ | if (c < 0) hi = mid; else lo = mid + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0) hi = mid; else lo = mid + 1; |
36 |
27 |
63 |
|
| 355 |
|
| } |
| 356 |
61 |
▶ | for (k = rs->n_exact; k < rs->n; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = rs->n_exact; k < rs->n; k++) |
51 |
10 |
61 |
|
| 357 |
51 |
▶ | if ((*caps = pmcp_tpl_match(aTHX_ rs->r[k].tpl, uri))) return &rs->r[k]; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*caps = pmcp_tpl_match(aTHX_ rs->r[k].tpl, uri))) return &rs->r[k]; |
15 |
36 |
51 |
|
| 358 |
10 |
| return NULL; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
15 |
| static SV *pmcp_res_not_found(pTHX_ pmcp_req *r, SV *uri) |
| 362 |
|
| { |
| 363 |
15 |
| r->edata = newRV_noinc((SV *)newHV()); |
| 364 |
15 |
| (void)hv_stores((HV *)SvRV(r->edata), "uri", newSVsv(uri)); |
| 365 |
15 |
| return pmcp_fail(r, PMCP_NOT_FOUND, "Resource not found"); |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
3 |
| static SV *pmcp_res_failed(pTHX_ pmcp_req *r, SV *why, IV status) |
| 369 |
|
| { |
| 370 |
3 |
| r->edata = newRV_noinc((SV *)newHV()); |
| 371 |
3 |
▶ | if (why) (void)hv_stores((HV *)SvRV(r->edata), "reason", why); |
Branch: 1 branch
| Branch | True | False | Total |
| if (why) (void)hv_stores((HV *)SvRV(r->edata), "reason", why); |
2 |
1 |
3 |
|
| 372 |
3 |
▶ | if (status) (void)hv_stores((HV *)SvRV(r->edata), "status", newSViv(status)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (status) (void)hv_stores((HV *)SvRV(r->edata), "status", newSViv(status)); |
1 |
2 |
3 |
|
| 373 |
3 |
| return pmcp_fail(r, PMCP_INTERNAL, "the resource could not be read"); |
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
38 |
| static SV *pmcp_resources_read(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params) |
| 377 |
|
| { |
| 378 |
38 |
| pmcp_resources *rs = (pmcp_resources *)s->resources; |
| 379 |
38 |
▶ | enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; |
Branch: 1 branch
| Branch | True | False | Total |
| enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; |
38 |
0 |
38 |
|
| 380 |
38 |
▶ | SV **uv = params ? hv_fetchs(params, "uri", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **uv = params ? hv_fetchs(params, "uri", 0) : NULL; |
38 |
0 |
38 |
|
| 381 |
38 |
| SV *uri, *args, *call, *child, *why = NULL, *val, *mime, *out, *err = NULL; |
| 382 |
|
| HV *caps; |
| 383 |
|
| pmcp_resource *res; |
| 384 |
|
| AV *contents; |
| 385 |
|
| |
| 386 |
38 |
▶ | if (!rs) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rs) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
0 |
38 |
38 |
|
| 387 |
38 |
▶ | if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required"); |
37 |
1 |
38 |
| if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required"); |
1 |
36 |
37 |
|
| 388 |
36 |
| uri = *uv; |
| 389 |
36 |
| res = pmcp_res_find(aTHX_ rs, uri, &caps); |
| 390 |
36 |
▶ | if (!res) return pmcp_res_not_found(aTHX_ r, uri); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) return pmcp_res_not_found(aTHX_ r, uri); |
9 |
27 |
36 |
|
| 391 |
27 |
▶ | if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to read the resource in"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to read the resource in"); |
0 |
27 |
27 |
|
| 392 |
|
| |
| 393 |
27 |
▶ | args = sv_2mortal(newRV_noinc((SV *)(caps ? caps : newHV()))); |
Branch: 1 branch
| Branch | True | False | Total |
| args = sv_2mortal(newRV_noinc((SV *)(caps ? caps : newHV()))); |
17 |
10 |
27 |
|
| 394 |
27 |
| call = sv_2mortal(pmcp_call_object(aTHX_ "resources/read", uri, args, v, params)); |
| 395 |
27 |
| pmcp_call_attach(aTHX_ r, call); |
| 396 |
27 |
| child = sv_2mortal(pmcp_child_context(aTHX_ r->c, "resource", uri, args, call)); |
| 397 |
|
| |
| 398 |
|
| /* a resource the caller may not see is one that is not there */ |
| 399 |
27 |
▶ | if ((why = pmcp_run_guards(aTHX_ res->guards, child))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((why = pmcp_run_guards(aTHX_ res->guards, child))) { |
1 |
26 |
27 |
|
| 400 |
1 |
| SvREFCNT_dec(why); |
| 401 |
1 |
| return pmcp_res_not_found(aTHX_ r, uri); |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
26 |
| val = pmcp_run_action(aTHX_ res->target, child, res->timeout, &why); |
| 405 |
26 |
▶ | if (!val) return pmcp_res_failed(aTHX_ r, why, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val) return pmcp_res_failed(aTHX_ r, why, 0); |
1 |
25 |
26 |
|
| 406 |
25 |
| mime = res->mime; |
| 407 |
|
| |
| 408 |
25 |
▶ | if (!SvOK(val)) return pmcp_res_not_found(aTHX_ r, uri); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(val)) return pmcp_res_not_found(aTHX_ r, uri); |
1 |
24 |
25 |
|
| 409 |
24 |
▶ | if (pmcp_is_hash(val) && hv_exists((HV *)SvRV(val), "contents", 8)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pmcp_is_hash(val) && hv_exists((HV *)SvRV(val), "contents", 8)) { |
7 |
17 |
24 |
| if (pmcp_is_hash(val) && hv_exists((HV *)SvRV(val), "contents", 8)) { |
1 |
6 |
7 |
|
| 410 |
1 |
| out = newSVsv(val); |
| 411 |
1 |
| goto answer; |
| 412 |
|
| } |
| 413 |
23 |
| contents = newAV(); |
| 414 |
23 |
| out = newRV_noinc((SV *)newHV()); |
| 415 |
23 |
| (void)hv_stores((HV *)SvRV(out), "contents", newRV_noinc((SV *)contents)); |
| 416 |
23 |
▶ | if (pmcp_is_triplet(aTHX_ val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_triplet(aTHX_ val)) { |
5 |
18 |
23 |
|
| 417 |
5 |
| AV *tr = (AV *)SvRV(val); |
| 418 |
5 |
| IV status = SvIV(*av_fetch(tr, 0, 0)); |
| 419 |
|
| int is_json; |
| 420 |
5 |
| SV *body = pmcp_triplet_body(aTHX_ tr, &is_json); |
| 421 |
5 |
| SvREFCNT_dec(out); |
| 422 |
6 |
▶ | if (status == 404) return pmcp_res_not_found(aTHX_ r, uri); |
Branch: 1 branch
| Branch | True | False | Total |
| if (status == 404) return pmcp_res_not_found(aTHX_ r, uri); |
2 |
3 |
5 |
|
| 423 |
3 |
▶ | if (status >= 400) return pmcp_res_failed(aTHX_ r, NULL, status); |
Branch: 1 branch
| Branch | True | False | Total |
| if (status >= 400) return pmcp_res_failed(aTHX_ r, NULL, status); |
1 |
2 |
3 |
|
| 424 |
2 |
▶ | if (!body) return pmcp_res_failed(aTHX_ r, newSVpvs("a streamed body"), status); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!body) return pmcp_res_failed(aTHX_ r, newSVpvs("a streamed body"), status); |
0 |
2 |
2 |
|
| 425 |
2 |
▶ | if (!mime && is_json) mime = sv_2mortal(newSVpvs("application/json")); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!mime && is_json) mime = sv_2mortal(newSVpvs("application/json")); |
2 |
0 |
2 |
| if (!mime && is_json) mime = sv_2mortal(newSVpvs("application/json")); |
2 |
0 |
2 |
|
| 426 |
2 |
| contents = newAV(); |
| 427 |
2 |
| out = newRV_noinc((SV *)newHV()); |
| 428 |
2 |
| (void)hv_stores((HV *)SvRV(out), "contents", newRV_noinc((SV *)contents)); |
| 429 |
2 |
| av_push(contents, pmcp_res_content(aTHX_ uri, mime, body)); |
| 430 |
|
| } |
| 431 |
18 |
▶ | else if (!SvROK(val) || SvAMAGIC(val)) { |
Branch: 4 branches
| Branch | True | False | Total |
| else if (!SvROK(val) || SvAMAGIC(val)) { |
8 |
10 |
18 |
| else if (!SvROK(val) || SvAMAGIC(val)) { |
8 |
0 |
8 |
| else if (!SvROK(val) || SvAMAGIC(val)) { |
0 |
8 |
8 |
| else if (!SvROK(val) || SvAMAGIC(val)) { |
0 |
0 |
0 |
|
| 432 |
10 |
| av_push(contents, pmcp_res_content(aTHX_ uri, mime, val)); |
| 433 |
|
| } |
| 434 |
15 |
▶ | else if (pmcp_is_hash(val) || pmcp_is_array(val)) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (pmcp_is_hash(val) || pmcp_is_array(val)) { |
2 |
6 |
8 |
| else if (pmcp_is_hash(val) || pmcp_is_array(val)) { |
1 |
1 |
2 |
|
| 435 |
7 |
| SV *json = pmcp_json_trapped(aTHX_ val, &err); |
| 436 |
7 |
▶ | if (!json) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!json) { |
0 |
7 |
7 |
|
| 437 |
0 |
| SvREFCNT_dec(out); |
| 438 |
0 |
| SvREFCNT_dec(err); |
| 439 |
0 |
| return pmcp_fail(r, PMCP_INTERNAL, "the resource is not JSON"); |
| 440 |
|
| } |
| 441 |
7 |
| sv_2mortal(json); |
| 442 |
7 |
▶ | av_push(contents, pmcp_res_content(aTHX_ uri, |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(contents, pmcp_res_content(aTHX_ uri, |
2 |
5 |
7 |
|
| 443 |
|
| mime ? mime : sv_2mortal(newSVpvs("application/json")), json)); |
| 444 |
|
| } |
| 445 |
|
| else { |
| 446 |
1 |
| SvREFCNT_dec(out); |
| 447 |
1 |
| return pmcp_res_failed(aTHX_ r, |
| 448 |
1 |
| newSVpvf("the action returned a %s reference", sv_reftype(SvRV(val), 0)), 0); |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
20 |
| answer: |
| 452 |
|
| { |
| 453 |
20 |
| SV *bytes = pmcp_json_trapped(aTHX_ out, &err); |
| 454 |
20 |
| SvREFCNT_dec(out); |
| 455 |
20 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
20 |
20 |
|
| 456 |
0 |
| SvREFCNT_dec(err); |
| 457 |
0 |
| return pmcp_fail(r, PMCP_INTERNAL, "the resource is not JSON"); |
| 458 |
|
| } |
| 459 |
20 |
| r->raw = bytes; |
| 460 |
|
| } |
| 461 |
20 |
| return NULL; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
|
| #endif /* PMCP_RES_H */ |