include/pmcp/pmcp_proto.h

filter: Statementstmt 100% 148 / 148 Branchbran 92.7% 115 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.6% 263 / 272 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_abi.h ↑ Summary MCP.xs »
1 /* pmcp_proto.h - the protocol core: versions, initialize, ping, notifications,
2 * and the dispatcher every transport shares.
3 *
4 * Needs: pmcp_rpc.h.
5 * Gives: pmcp_srv (what a server says about itself), pmcp_req (one message's
6 * state), pmcp_dispatch (bytes in, bytes out).
7 *
8 * pmcp_dispatch is the whole of the protocol below the transport. HTTP (07),
9 * the SSE answer (08) and stdio (13) all hand it the body and send back what it
10 * gives, so none of them knows a method name.
11 */
12  
13 #ifndef PMCP_PROTO_H
14 #define PMCP_PROTO_H
15  
16 /* ---- versions ------------------------------------------------------------ */
17  
18 /* In order, so a later version compares greater. */
19 enum pmcp_ver {
20 PMCP_V_NONE = 0,
21 PMCP_V_2025_03_26,
22 PMCP_V_2025_06_18,
23 PMCP_V_2025_11_25
24 };
25 #define PMCP_V_LATEST PMCP_V_2025_11_25
26  
27 static const char *const PMCP_VER_NAME[] = {
28 NULL, "2025-03-26", "2025-06-18", "2025-11-25"
29 };
30  
31 /* The version a string names, or PMCP_V_NONE. */
32 429 static enum pmcp_ver pmcp_version_of(const char *s, STRLEN l)
33 {
34 int i;
35 429 ▶ if (l != 10) return PMCP_V_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if (l != 10) return PMCP_V_NONE; 2 427 429
36 1249 ▶ for (i = PMCP_V_2025_03_26; i <= PMCP_V_LATEST; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = PMCP_V_2025_03_26; i <= PMCP_V_LATEST; i++) 1244 5 1249
37 1244 ▶ if (memEQ(s, PMCP_VER_NAME[i], 10)) return (enum pmcp_ver)i;
Branch: 1 branch
BranchTrueFalseTotal
if (memEQ(s, PMCP_VER_NAME[i], 10)) return (enum pmcp_ver)i; 422 822 1244
38 5 return PMCP_V_NONE;
39 }
40  
41 /* ---- what a server says about itself ------------------------------------- */
42  
43 /* Borrowed SVs, held by whoever owns the server (the frozen app state, 04).
44 * Any of them may be NULL. */
45 typedef struct {
46 SV *name;
47 SV *title;
48 SV *version;
49 SV *description;
50 SV *website;
51 SV *icons; /* an array ref, checked by 04 */
52 SV *instructions;
53 unsigned has_tools : 1;
54 unsigned has_resources : 1;
55 unsigned has_prompts : 1;
56 unsigned logging : 1;
57 /* The transport can carry a notification the client did not ask for:
58 * sessions with a GET stream on HTTP, or stdio. Without it subscribe and
59 * logging would be promises nothing can keep, so neither is advertised. */
60 unsigned can_notify : 1;
61 /* the frozen tables (pmcp_decl.h, pmcp_res.h, pmcp_prompt.h), each NULL
62 * when nothing of that kind is declared */
63 void *tools;
64 void *resources;
65 void *prompts;
66 /* the innermost app, for an OpenAPI tool to re-enter (12), owned */
67 SV *psgi;
68 /* The minimum log level logging/setLevel asked for, a PMCP_LOG_* index.
69 * Per server until sessions (09) keep one per session, and per process on
70 * stdio (13). */
71 int log_level;
72 } pmcp_srv;
73  
74 /* RFC 5424 severities, in order, as MCP names them. */
75 static const char *const PMCP_LOG_NAME[] = {
76 "debug", "info", "notice", "warning", "error", "critical", "alert",
77 "emergency", NULL
78 };
79 #define PMCP_LOG_DEFAULT 1 /* info */
80  
81 54 static int pmcp_log_level_of(const char *p, STRLEN l)
82 {
83 int i;
84 237 ▶ for (i = 0; PMCP_LOG_NAME[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PMCP_LOG_NAME[i]; i++) 234 3 237
85 234 ▶ if (strlen(PMCP_LOG_NAME[i]) == l && memEQ(PMCP_LOG_NAME[i], p, l)) return i;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(PMCP_LOG_NAME[i]) == l && memEQ(PMCP_LOG_NAME[i], p, l)) return i; 69 165 234
if (strlen(PMCP_LOG_NAME[i]) == l && memEQ(PMCP_LOG_NAME[i], p, l)) return i; 51 18 69
86 3 return -1;
87 }
88  
89 /* ---- one message ---------------------------------------------------------- */
90  
91 typedef struct {
92 /* in: from the transport */
93 enum pmcp_ver version; /* negotiated, or the header's, for this call */
94 unsigned in_session : 1; /* the request carried a live session id */
95 unsigned stdio : 1; /* over `punk mcp`: the process is the session */
96 SV *c; /* the request's context, borrowed, or NULL */
97 SV *stream; /* the SSE stream's state hash, borrowed (08) */
98 SV *session; /* the session id, borrowed, or NULL (09) */
99  
100 /* out: set by a handler */
101 enum pmcp_ver negotiated; /* initialize: what was agreed */
102 SV *cancelled; /* notifications/cancelled: the requestId, +1 */
103 SV *raw; /* a result already encoded, +1 (the tools) */
104 SV *id; /* the request's id, borrowed, while it runs */
105 int level_set; /* logging/setLevel: the level asked, or -1 */
106 unsigned tool_error : 1; /* tools/call: the result has isError */
107  
108 /* a handler that fails sets these and returns NULL */
109 int ecode;
110 const char *emsg;
111 SV *edata; /* +1, or NULL */
112 } pmcp_req;
113  
114 483 static void pmcp_req_init(pmcp_req *r, enum pmcp_ver v, int in_session)
115 {
116 483 Zero(r, 1, pmcp_req);
117 483 r->version = v;
118 483 r->in_session = in_session ? 1 : 0;
119 483 r->level_set = -1;
120 483 }
121  
122 483 static void pmcp_req_free(pTHX_ pmcp_req *r)
123 {
124 483 ▶ if (r->cancelled) SvREFCNT_dec(r->cancelled);
Branch: 1 branch
BranchTrueFalseTotal
if (r->cancelled) SvREFCNT_dec(r->cancelled); 11 472 483
125 483 ▶ if (r->edata) SvREFCNT_dec(r->edata);
Branch: 1 branch
BranchTrueFalseTotal
if (r->edata) SvREFCNT_dec(r->edata); 0 483 483
126 483 ▶ if (r->raw) SvREFCNT_dec(r->raw);
Branch: 1 branch
BranchTrueFalseTotal
if (r->raw) SvREFCNT_dec(r->raw); 195 288 483
127 483 r->cancelled = r->edata = r->raw = NULL;
128 483 }
129  
130 69 static SV *pmcp_fail(pmcp_req *r, int code, const char *msg)
131 {
132 69 r->ecode = code;
133 69 r->emsg = msg;
134 69 return NULL;
135 }
136  
137 /* A handler answers a request with a result (+1, encoded by the dispatcher) or
138 * NULL after pmcp_fail. A notification handler's return is discarded. */
139 typedef SV *(*pmcp_handler)(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
140  
141 /* ---- initialize ----------------------------------------------------------- */
142  
143 100 static SV *pmcp_initialize(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
144 {
145 HV *res, *caps, *info;
146 100 enum pmcp_ver v = PMCP_V_LATEST;
147 SV **pv;
148  
149 /* A second initialize on a live session is a client that lost track. It is
150 * refused rather than renegotiated under the other requests' feet. */
151 100 ▶ if (r->in_session)
Branch: 1 branch
BranchTrueFalseTotal
if (r->in_session) 3 97 100
152 3 return pmcp_fail(r, PMCP_REQUEST, "session already initialized");
153 97 res = newHV();
154 97 caps = newHV();
155 97 info = newHV();
156  
157 /* The client's version when this server speaks it, otherwise the latest,
158 * and the client decides whether it can live with that. */
159 97 ▶ pv = params ? hv_fetchs(params, "protocolVersion", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pv = params ? hv_fetchs(params, "protocolVersion", 0) : NULL; 96 1 97
160 97 ▶ if (pv && pmcp_is_str(*pv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pv && pmcp_is_str(*pv)) { 95 2 97
if (pv && pmcp_is_str(*pv)) { 94 1 95
161 STRLEN l;
162 94 const char *p = SvPV_const(*pv, l);
163 94 enum pmcp_ver asked = pmcp_version_of(p, l);
164 94 ▶ if (asked != PMCP_V_NONE) v = asked;
Branch: 1 branch
BranchTrueFalseTotal
if (asked != PMCP_V_NONE) v = asked; 90 4 94
165 }
166 97 r->negotiated = v;
167  
168 /* Only what was declared, and only what the transport can deliver. No
169 * listChanged anywhere: the lists are frozen at to_app (P6). */
170 97 ▶ if (s->has_tools)
Branch: 1 branch
BranchTrueFalseTotal
if (s->has_tools) 75 22 97
171 75 (void)hv_stores(caps, "tools", newRV_noinc((SV *)newHV()));
172 97 ▶ if (s->has_resources) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->has_resources) { 72 25 97
173 72 HV *rc = newHV();
174 72 ▶ if (s->can_notify || r->stdio) (void)hv_stores(rc, "subscribe", pmcp_bool(aTHX_ 1));
Branch: 2 branches
BranchTrueFalseTotal
if (s->can_notify || r->stdio) (void)hv_stores(rc, "subscribe", pmcp_bool(aTHX_ 1)); 5 67 72
if (s->can_notify || r->stdio) (void)hv_stores(rc, "subscribe", pmcp_bool(aTHX_ 1)); 1 4 5
175 72 (void)hv_stores(caps, "resources", newRV_noinc((SV *)rc));
176 }
177 97 ▶ if (s->has_prompts)
Branch: 1 branch
BranchTrueFalseTotal
if (s->has_prompts) 9 88 97
178 9 (void)hv_stores(caps, "prompts", newRV_noinc((SV *)newHV()));
179 97 ▶ if (s->logging && (s->can_notify || r->stdio))
Branch: 3 branches
BranchTrueFalseTotal
if (s->logging && (s->can_notify || r->stdio)) 73 24 97
if (s->logging && (s->can_notify || r->stdio)) 5 68 73
if (s->logging && (s->can_notify || r->stdio)) 1 4 5
180 69 (void)hv_stores(caps, "logging", newRV_noinc((SV *)newHV()));
181  
182 /* serverInfo grows with the version: title in 2025-06-18, description,
183 * websiteUrl and icons in 2025-11-25. An older client gets none of them. */
184 97 ▶ (void)hv_stores(info, "name",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(info, "name", 77 20 97
185 s->name ? pmcp_str(aTHX_ s->name) : newSVpvs("punk"));
186 97 ▶ (void)hv_stores(info, "version",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(info, "version", 77 20 97
187 s->version ? pmcp_str(aTHX_ s->version) : newSVpvs("0"));
188 97 ▶ if (v >= PMCP_V_2025_06_18 && s->title)
Branch: 2 branches
BranchTrueFalseTotal
if (v >= PMCP_V_2025_06_18 && s->title) 95 2 97
if (v >= PMCP_V_2025_06_18 && s->title) 4 91 95
189 4 (void)hv_stores(info, "title", pmcp_str(aTHX_ s->title));
190 97 ▶ if (v >= PMCP_V_2025_11_25) {
Branch: 1 branch
BranchTrueFalseTotal
if (v >= PMCP_V_2025_11_25) { 91 6 97
191 91 ▶ if (s->description)
Branch: 1 branch
BranchTrueFalseTotal
if (s->description) 1 90 91
192 1 (void)hv_stores(info, "description", pmcp_str(aTHX_ s->description));
193 91 ▶ if (s->website)
Branch: 1 branch
BranchTrueFalseTotal
if (s->website) 1 90 91
194 1 (void)hv_stores(info, "websiteUrl", pmcp_str(aTHX_ s->website));
195 91 ▶ if (s->icons) (void)hv_stores(info, "icons", newSVsv(s->icons));
Branch: 1 branch
BranchTrueFalseTotal
if (s->icons) (void)hv_stores(info, "icons", newSVsv(s->icons)); 1 90 91
196 }
197  
198 97 (void)hv_stores(res, "protocolVersion", newSVpv(PMCP_VER_NAME[v], 0));
199 97 (void)hv_stores(res, "capabilities", newRV_noinc((SV *)caps));
200 97 (void)hv_stores(res, "serverInfo", newRV_noinc((SV *)info));
201 97 ▶ if (s->instructions)
Branch: 1 branch
BranchTrueFalseTotal
if (s->instructions) 2 95 97
202 2 (void)hv_stores(res, "instructions", pmcp_str(aTHX_ s->instructions));
203 97 return newRV_noinc((SV *)res);
204 }
205  
206 43 static SV *pmcp_ping(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
207 {
208 PERL_UNUSED_ARG(s);
209 PERL_UNUSED_ARG(r);
210 PERL_UNUSED_ARG(params);
211 43 return newRV_noinc((SV *)newHV());
212 }
213  
214 8 static SV *pmcp_on_initialized(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
215 {
216 PERL_UNUSED_CONTEXT;
217 PERL_UNUSED_ARG(s);
218 PERL_UNUSED_ARG(r);
219 PERL_UNUSED_ARG(params);
220 8 return NULL;
221 }
222  
223 /* Defined in pmcp_sse.h: flag a call streaming on this worker as cancelled. */
224 static void pmcp_live_cancel(pTHX_ SV *sid, SV *id);
225  
226 /* Recorded for the transport, and honoured when the call is streaming on this
227 * worker. Anywhere else it is accepted and ignored (16). */
228 13 static SV *pmcp_on_cancelled(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
229 {
230 13 ▶ SV **e = params ? hv_fetchs(params, "requestId", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = params ? hv_fetchs(params, "requestId", 0) : NULL; 12 1 13
231 PERL_UNUSED_ARG(s);
232 13 ▶ if (e && pmcp_id_ok(*e) && !r->cancelled) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && pmcp_id_ok(*e) && !r->cancelled) { 12 1 13
if (e && pmcp_id_ok(*e) && !r->cancelled) { 11 1 12
if (e && pmcp_id_ok(*e) && !r->cancelled) { 11 0 11
233 11 r->cancelled = newSVsv(*e);
234 11 pmcp_live_cancel(aTHX_ r->session, *e);
235 }
236 13 return NULL;
237 }
238  
239 /* logging/setLevel. Only where a log message could reach the client: with
240 * logging on and a transport that can notify. Elsewhere the method does not
241 * exist, as the capability was never advertised. */
242 11 static SV *pmcp_set_level(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
243 {
244 11 ▶ SV **lv = params ? hv_fetchs(params, "level", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **lv = params ? hv_fetchs(params, "level", 0) : NULL; 11 0 11
245 int level;
246 STRLEN l;
247 const char *p;
248 11 ▶ if (!(s->logging && (s->can_notify || r->stdio)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(s->logging && (s->can_notify || r->stdio))) 10 1 11
if (!(s->logging && (s->can_notify || r->stdio))) 2 8 10
if (!(s->logging && (s->can_notify || r->stdio))) 1 1 2
249 2 return pmcp_fail(r, PMCP_METHOD, "method not found");
250 9 ▶ if (!(lv && pmcp_is_str(*lv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(lv && pmcp_is_str(*lv))) 8 1 9
if (!(lv && pmcp_is_str(*lv))) 1 7 8
251 2 return pmcp_fail(r, PMCP_PARAMS, "a level is required");
252 7 p = SvPV_const(*lv, l);
253 7 level = pmcp_log_level_of(p, l);
254 7 ▶ if (level < 0) return pmcp_fail(r, PMCP_PARAMS, "unknown log level");
Branch: 1 branch
BranchTrueFalseTotal
if (level < 0) return pmcp_fail(r, PMCP_PARAMS, "unknown log level"); 2 5 7
255 5 r->level_set = level;
256 5 return newRV_noinc((SV *)newHV());
257 }
258  
259 /* ---- the table ------------------------------------------------------------ */
260  
261 /* Defined in pmcp_call.h, pmcp_res.h and pmcp_prompt.h, which need the
262 * tables this header precedes. */
263 static SV *pmcp_tools_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
264 static SV *pmcp_tools_call(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
265 static SV *pmcp_resources_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
266 static SV *pmcp_resources_templates(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
267 static SV *pmcp_resources_read(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
268 static SV *pmcp_resources_subscribe(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
269 static SV *pmcp_resources_unsubscribe(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
270 static SV *pmcp_prompts_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
271 static SV *pmcp_prompts_get(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params);
272  
273 #define PMCP_REQ 1 /* answers a request */
274 #define PMCP_NOTE 2 /* handles a notification */
275  
276 typedef struct {
277 const char *name;
278 STRLEN len;
279 int kind;
280 pmcp_handler fn;
281 } pmcp_method;
282  
283 /* Sorted by length, then by bytes. Later phases add rows in place, keeping the
284 * order, and t/12 checks it. */
285 #define PMCP_M(s, k, f) { s, sizeof(s) - 1, k, f }
286 static const pmcp_method PMCP_METHODS[] = {
287 PMCP_M("ping", PMCP_REQ, pmcp_ping),
288 PMCP_M("initialize", PMCP_REQ, pmcp_initialize),
289 PMCP_M("tools/call", PMCP_REQ, pmcp_tools_call),
290 PMCP_M("tools/list", PMCP_REQ, pmcp_tools_list),
291 PMCP_M("prompts/get", PMCP_REQ, pmcp_prompts_get),
292 PMCP_M("prompts/list", PMCP_REQ, pmcp_prompts_list),
293 PMCP_M("resources/list", PMCP_REQ, pmcp_resources_list),
294 PMCP_M("resources/read", PMCP_REQ, pmcp_resources_read),
295 PMCP_M("logging/setLevel", PMCP_REQ, pmcp_set_level),
296 PMCP_M("resources/subscribe", PMCP_REQ, pmcp_resources_subscribe),
297 PMCP_M("resources/unsubscribe", PMCP_REQ, pmcp_resources_unsubscribe),
298 PMCP_M("notifications/cancelled", PMCP_NOTE, pmcp_on_cancelled),
299 PMCP_M("resources/templates/list", PMCP_REQ, pmcp_resources_templates),
300 PMCP_M("notifications/initialized", PMCP_NOTE, pmcp_on_initialized),
301 };
302 #undef PMCP_M
303 #define PMCP_N_METHODS (sizeof PMCP_METHODS / sizeof PMCP_METHODS[0])
304  
305 1490 static int pmcp_method_cmp(const char *a, STRLEN al, const pmcp_method *m)
306 {
307 1490 ▶ if (al != m->len) return al < m->len ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (al != m->len) return al < m->len ? -1 : 1; 647 843 1490
if (al != m->len) return al < m->len ? -1 : 1; 523 124 647
308 843 return memcmp(a, m->name, al);
309 }
310  
311 460 static const pmcp_method *pmcp_method_find(const char *name, STRLEN len)
312 {
313 460 size_t lo = 0, hi = PMCP_N_METHODS;
314 1497 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 1490 7 1497
315 1490 size_t mid = lo + (hi - lo) / 2;
316 1490 int c = pmcp_method_cmp(name, len, &PMCP_METHODS[mid]);
317 1490 ▶ if (c == 0) return &PMCP_METHODS[mid];
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) return &PMCP_METHODS[mid]; 453 1037 1490
318 1037 ▶ if (c < 0) hi = mid; else lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) hi = mid; else lo = mid + 1; 775 262 1037
319 }
320 7 return NULL;
321 }
322  
323 /* ---- the dispatcher ------------------------------------------------------- */
324  
325 #define PMCP_HTTP_OK 200
326 #define PMCP_HTTP_ACCEPTED 202
327 #define PMCP_HTTP_BAD_REQUEST 400
328  
329 /* One body in, the answer out. Returns the reply bytes (+1), or NULL when
330 * nothing is owed. *status is what HTTP should send: 200 with a reply, 202 for
331 * a notification or response, 400 for a body that is not JSON-RPC. stdio
332 * ignores it. */
333 static SV *pmcp_dispatch_doc(pTHX_ const pmcp_srv *s, pmcp_req *r, SV *doc,
334 int *status);
335 static SV *pmcp_otel_start(pTHX_ pmcp_req *r, const char *method, STRLEN mlen,
336 HV *params, SV **tracer);
337 static void pmcp_otel_end(pTHX_ pmcp_req *r, SV *span, SV *tracer, int ecode);
338  
339 231 static SV *pmcp_dispatch(pTHX_ const pmcp_srv *s, pmcp_req *r, SV *bytes,
340 int *status)
341 {
342 231 SV *doc = pmcp_rpc_decode(aTHX_ bytes), *reply;
343 231 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 9 222 231
344 9 *status = PMCP_HTTP_BAD_REQUEST;
345 9 return pmcp_rpc_error(aTHX_ NULL, PMCP_PARSE, "parse error", NULL);
346 }
347 222 reply = pmcp_dispatch_doc(aTHX_ s, r, doc, status);
348 222 SvREFCNT_dec(doc);
349 222 return reply;
350 }
351  
352 /* The same, for a document already decoded (the HTTP transport decodes once
353 * to decide whether to stream). `doc` is borrowed. */
354 474 static SV *pmcp_dispatch_doc(pTHX_ const pmcp_srv *s, pmcp_req *r, SV *doc,
355 int *status)
356 {
357 474 SV *reply = NULL;
358 pmcp_msg m;
359 const pmcp_method *meth;
360  
361 474 ENTER; SAVETMPS;
362 474 pmcp_rpc_classify(aTHX_ doc, &m);
363 474 r->id = m.id;
364  
365 474 switch (m.kind) {
366 27 case PMCP_K_INVALID:
367 27 *status = PMCP_HTTP_BAD_REQUEST;
368 27 reply = pmcp_rpc_error(aTHX_ m.id, m.err_code, m.err_msg, NULL);
369 27 break;
370  
371 4 case PMCP_K_RESPONSE:
372 4 *status = PMCP_HTTP_ACCEPTED;
373 4 break;
374  
375 26 case PMCP_K_NOTIFY:
376 /* An unknown notification, or a request method sent with no id, is
377 * dropped: a notification is never answered, not even with an error. */
378 26 *status = PMCP_HTTP_ACCEPTED;
379 26 meth = pmcp_method_find(m.method, m.mlen);
380 26 ▶ if (meth && meth->kind == PMCP_NOTE) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && meth->kind == PMCP_NOTE) { 24 2 26
if (meth && meth->kind == PMCP_NOTE) { 21 3 24
381 21 SV *ignored = meth->fn(aTHX_ s, r, m.params);
382 21 ▶ if (ignored) SvREFCNT_dec(ignored);
Branch: 1 branch
BranchTrueFalseTotal
if (ignored) SvREFCNT_dec(ignored); 0 21 21
383 }
384 26 break;
385  
386 417 case PMCP_K_REQUEST:
387 417 *status = PMCP_HTTP_OK;
388 417 meth = pmcp_method_find(m.method, m.mlen);
389 417 ▶ if (!meth || meth->kind != PMCP_REQ) {
Branch: 2 branches
BranchTrueFalseTotal
if (!meth || meth->kind != PMCP_REQ) { 415 2 417
if (!meth || meth->kind != PMCP_REQ) { 0 415 415
390 2 reply = pmcp_rpc_error(aTHX_ m.id, PMCP_METHOD, "method not found",
391 NULL);
392 2 break;
393 }
394 {
395 415 SV *tracer, *span = pmcp_otel_start(aTHX_ r, m.method, m.mlen, m.params, &tracer);
396 415 SV *result = meth->fn(aTHX_ s, r, m.params);
397 415 ▶ if (span)
Branch: 1 branch
BranchTrueFalseTotal
if (span) 6 409 415
398 7 ▶ pmcp_otel_end(aTHX_ r, span, tracer, (result || r->raw) ? 0
Branch: 2 branches
BranchTrueFalseTotal
pmcp_otel_end(aTHX_ r, span, tracer, (result || r->raw) ? 0 6 0 6
pmcp_otel_end(aTHX_ r, span, tracer, (result || r->raw) ? 0 1 5 6
399 1 ▶ : (r->ecode ? r->ecode : PMCP_INTERNAL));
Branch: 1 branch
BranchTrueFalseTotal
: (r->ecode ? r->ecode : PMCP_INTERNAL)); 1 0 1
400 415 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 151 264 415
401 151 reply = pmcp_rpc_result(aTHX_ m.id, result);
402 }
403 264 ▶ else if (r->raw) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->raw) { 195 69 264
404 195 reply = pmcp_rpc_result_bytes(aTHX_ m.id, r->raw);
405 }
406 else {
407 69 SV *data = r->edata;
408 69 r->edata = NULL;
409 138 reply = pmcp_rpc_error(aTHX_ m.id,
410 69 ▶ r->ecode ? r->ecode : PMCP_INTERNAL,
Branch: 1 branch
BranchTrueFalseTotal
r->ecode ? r->ecode : PMCP_INTERNAL, 69 0 69
411 69 ▶ r->emsg ? r->emsg : "internal error",
Branch: 1 branch
BranchTrueFalseTotal
r->emsg ? r->emsg : "internal error", 69 0 69
412 data);
413 }
414 }
415 415 break;
416 }
417  
418 474 r->id = NULL; /* borrowed from doc, which the caller owns */
419 474 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 404 70 474
420 474 return reply;
421 }
422  
423 #endif /* PMCP_PROTO_H */
« include/pmcp/pmcp_abi.h ↑ Summary MCP.xs »