include/pmcp/pmcp_stdio.h

filter: Statementstmt 97.3% 216 / 222 Branchbran 63.4% 104 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.9% 320 / 386 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/mcp.xs ↑ Summary include/pmcp/pmcp_oa.h »
1 /* pmcp_stdio.h - `punk mcp`: one application served over stdin and stdout.
2 *
3 * Needs: pmcp_auth.h, pmcp_bus.h, pmcp_sse.h.
4 *
5 * Newline-delimited JSON-RPC, one message a line each way, and nothing else on
6 * stdout. The process is the session: it holds the negotiated version, the
7 * log level and the subscribed URIs.
8 *
9 * A line is dispatched directly, not as a synthetic POST through the HTTP
10 * route: Origin, Accept, the version header, sessions and SSE framing are all
11 * HTTP's, and none of them means anything on a pipe. What does carry over is
12 * who may call: each line gets a context of its own, the route's guard list
13 * runs on it (with --token as its Authorization), and so does `principal`.
14 * The application's request hooks do not run, and the POD says so.
15 *
16 * One call at a time (P7): `$c->await` blocks off a Hyperman loop, so a Future
17 * resolves, and a line that arrives during a call waits in the pipe.
18 */
19  
20 #ifndef PMCP_STDIO_H
21 #define PMCP_STDIO_H
22  
23 #include <unistd.h>
24  
25 #define PMCP_STDIO_REFUSED (-32001)
26  
27 typedef struct {
28 PerlIO *out; /* the real stdout, kept for the protocol */
29 HV *subs; /* subscribed URIs */
30 int active;
31 } pmcp_stdio_t;
32  
33 static pmcp_stdio_t PMCP_STDIO = { NULL, NULL, 0 };
34  
35 /* One message, one line. Every protocol byte goes through here, so a
36 * notification written during a call can never split an answer. */
37 20 static void pmcp_stdio_write(pTHX_ SV *json)
38 {
39 STRLEN l;
40 const char *p;
41 20 ▶ if (!PMCP_STDIO.out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_STDIO.out) return; 0 20 20
42 20 p = SvPV_const(json, l);
43 20 PerlIO_write(PMCP_STDIO.out, p, l);
44 20 PerlIO_write(PMCP_STDIO.out, "\n", 1);
45 20 PerlIO_flush(PMCP_STDIO.out);
46 }
47  
48 /* resources/updated, if this process subscribes to it. */
49 13 static void pmcp_stdio_updated(pTHX_ SV *uri)
50 {
51 HV *m, *p;
52 13 SV *rv, *bytes, *err = NULL;
53 13 ▶ if (!PMCP_STDIO.active || !PMCP_STDIO.subs || !uri || !hv_exists_ent(PMCP_STDIO.subs, uri, 0))
Branch: 4 branches
BranchTrueFalseTotal
if (!PMCP_STDIO.active || !PMCP_STDIO.subs || !uri || !hv_exists_ent(PMCP_STDIO.subs, uri, 0)) 2 11 13
if (!PMCP_STDIO.active || !PMCP_STDIO.subs || !uri || !hv_exists_ent(PMCP_STDIO.subs, uri, 0)) 2 0 2
if (!PMCP_STDIO.active || !PMCP_STDIO.subs || !uri || !hv_exists_ent(PMCP_STDIO.subs, uri, 0)) 2 0 2
if (!PMCP_STDIO.active || !PMCP_STDIO.subs || !uri || !hv_exists_ent(PMCP_STDIO.subs, uri, 0)) 1 1 2
54 12 return;
55 1 m = newHV();
56 1 p = newHV();
57 1 (void)hv_stores(p, "uri", newSVsv(uri));
58 1 (void)hv_stores(m, "jsonrpc", newSVpvs("2.0"));
59 1 (void)hv_stores(m, "method", newSVpvs("notifications/resources/updated"));
60 1 (void)hv_stores(m, "params", newRV_noinc((SV *)p));
61 1 rv = sv_2mortal(newRV_noinc((SV *)m));
62 1 bytes = pmcp_json_trapped(aTHX_ rv, &err);
63 1 ▶ if (bytes) { pmcp_stdio_write(aTHX_ bytes); SvREFCNT_dec(bytes); }
Branch: 1 branch
BranchTrueFalseTotal
if (bytes) { pmcp_stdio_write(aTHX_ bytes); SvREFCNT_dec(bytes); } 1 0 1
64 0 else SvREFCNT_dec(err);
65 }
66  
67 2 static int pmcp_stdio_active(void) { return PMCP_STDIO.active; }
68  
69 1 static void pmcp_stdio_subscribe(pTHX_ SV *uri, int on)
70 {
71 1 ▶ if (!PMCP_STDIO.subs) PMCP_STDIO.subs = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_STDIO.subs) PMCP_STDIO.subs = newHV(); 1 0 1
72 1 ▶ if (on) (void)hv_store_ent(PMCP_STDIO.subs, uri, newSViv(1), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (on) (void)hv_store_ent(PMCP_STDIO.subs, uri, newSViv(1), 0); 1 0 1
73 0 else (void)hv_delete_ent(PMCP_STDIO.subs, uri, G_DISCARD, 0);
74 1 }
75  
76 /* ---- one line ----------------------------------------------------------------------- */
77  
78 /* A context for one line, as a request would have: the token as Authorization,
79 * a loopback address, the MCP path. +1 */
80 16 static SV *pmcp_stdio_context(pTHX_ SV *app, pmcp_server *ps, SV *token, SV *ctx_class)
81 {
82 16 HV *env = newHV(), *match = newHV();
83 16 AV *ver = newAV();
84 SV *argv[3];
85 16 av_push(ver, newSViv(1));
86 16 av_push(ver, newSViv(1));
87 16 (void)hv_stores(env, "REQUEST_METHOD", newSVpvs("POST"));
88 16 (void)hv_stores(env, "PATH_INFO", newSVsv(pmcp_hget(aTHX_ ps->opts, "path")));
89 16 (void)hv_stores(env, "SCRIPT_NAME", newSVpvs(""));
90 16 (void)hv_stores(env, "QUERY_STRING", newSVpvs(""));
91 16 (void)hv_stores(env, "SERVER_NAME", newSVpvs("localhost"));
92 16 (void)hv_stores(env, "SERVER_PORT", newSVpvs("0"));
93 16 (void)hv_stores(env, "SERVER_PROTOCOL", newSVpvs("HTTP/1.1"));
94 16 (void)hv_stores(env, "REMOTE_ADDR", newSVpvs("127.0.0.1"));
95 16 (void)hv_stores(env, "CONTENT_TYPE", newSVpvs("application/json"));
96 16 (void)hv_stores(env, "CONTENT_LENGTH", newSVpvs("0"));
97 16 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("http"));
98 16 (void)hv_stores(env, "psgi.version", newRV_noinc((SV *)ver));
99 16 (void)hv_stores(env, "psgi.errors", newRV_inc((SV *)gv_fetchpvs("STDERR", GV_ADD, SVt_PVIO)));
100 16 (void)hv_stores(env, "pmcp.stdio", newSViv(1));
101 16 ▶ if (token && SvOK(token) && SvCUR(token))
Branch: 3 branches
BranchTrueFalseTotal
if (token && SvOK(token) && SvCUR(token)) 14 2 16
if (token && SvOK(token) && SvCUR(token)) 14 0 14
if (token && SvOK(token) && SvCUR(token)) 14 0 14
102 14 (void)hv_stores(env, "HTTP_AUTHORIZATION", newSVpvf("Bearer %" SVf, SVfARG(token)));
103 16 (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV()));
104 16 argv[0] = sv_2mortal(newRV_noinc((SV *)env));
105 16 argv[1] = app;
106 16 argv[2] = sv_2mortal(newRV_noinc((SV *)match));
107 16 return pmcp_call_method(aTHX_ ctx_class, "_build", argv, 3, G_SCALAR, NULL);
108 }
109  
110 /* The guards, unless `stdio_trust`: NULL to carry on, or the refusal's
111 * status (mortal). */
112 16 static SV *pmcp_stdio_guard(pTHX_ pmcp_server *ps, SV *c)
113 {
114 16 SV *trust = pmcp_hget(aTHX_ ps->opts, "stdio_trust");
115 AV *guards;
116 SSize_t i, n;
117 16 ▶ if ((trust && SvTRUE(trust)) || !pmcp_is_array(ps->guards)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if ((trust && SvTRUE(trust)) || !pmcp_is_array(ps->guards)) return NULL; 16 0 16
if ((trust && SvTRUE(trust)) || !pmcp_is_array(ps->guards)) return NULL; 15 1 16
if ((trust && SvTRUE(trust)) || !pmcp_is_array(ps->guards)) return NULL; 0 15 15
118 15 guards = (AV *)SvRV(ps->guards);
119 15 n = av_len(guards) + 1;
120 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 14 29
121 15 SV *err = NULL;
122 15 SV *r = pmcp_call_sv(aTHX_ *av_fetch(guards, i, 0), &c, 1, G_SCALAR, &err);
123 16 ▶ if (!r) return sv_2mortal(pmcp_die_text(aTHX_ sv_2mortal(err)));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return sv_2mortal(pmcp_die_text(aTHX_ sv_2mortal(err))); 0 15 15
124 15 ▶ if (SvROK(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(r)) { 1 14 15
125 1 SV *t = pmcp_refusal_text(aTHX_ r);
126 1 SvREFCNT_dec(r);
127 1 return sv_2mortal(t);
128 }
129 14 SvREFCNT_dec(r);
130 }
131 14 return NULL;
132 }
133  
134 /* ---- the loop --------------------------------------------------------------------------- */
135  
136 /* Serve stdin until EOF. Returns the exit status. */
137 4 static int pmcp_stdio_run(pTHX_ SV *app, pmcp_server *ps, SV *token, SV *class)
138 {
139 /* the application's own context class, which carries its helpers: a
140 * request is built in it, and so is a line */
141 4 SV *ctx_class = sv_2mortal(newSVpvf("%" SVf "::_Context", SVfARG(class)));
142 4 ▶ if (!gv_stashsv(ctx_class, 0)) ctx_class = sv_2mortal(newSVpvs("Punk::Context"));
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_stashsv(ctx_class, 0)) ctx_class = sv_2mortal(newSVpvs("Punk::Context")); 0 4 4
143 4 PerlIO *in = PerlIO_stdin();
144 4 SV *line = sv_2mortal(newSV(256));
145 int proto;
146 4 enum pmcp_ver v = PMCP_V_NONE;
147 4 int initialized = 0;
148 4 HV *st = (HV *)sv_2mortal((SV *)newHV());
149 4 SV *strv = sv_2mortal(newRV_inc((SV *)st));
150  
151 /* stdout carries protocol lines and nothing else. The real descriptor is
152 * kept for the protocol, and fd 1 becomes stderr, so a stray print in a
153 * controller lands there instead of corrupting the stream. */
154 4 PerlIO_flush(PerlIO_stdout());
155 4 proto = dup(1);
156 4 ▶ if (proto < 0 || dup2(2, 1) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (proto < 0 || dup2(2, 1) < 0) 4 0 4
if (proto < 0 || dup2(2, 1) < 0) 0 4 4
157 0 croak("%s: punk mcp could not set stdout aside: %s", PMCP_WHO, Strerror(errno));
158 4 PMCP_STDIO.out = PerlIO_fdopen(proto, "w");
159 4 ▶ if (!PMCP_STDIO.out) croak("%s: punk mcp could not open the protocol stream", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_STDIO.out) croak("%s: punk mcp could not open the protocol stream", PMCP_WHO); 0 4 4
160 4 PerlIO_binmode(aTHX_ PMCP_STDIO.out, '>', O_BINARY, NULL);
161 4 PMCP_STDIO.active = 1;
162  
163 4 (void)hv_stores(st, "stdio", newSViv(1));
164 4 (void)hv_stores(st, "n", newSViv(0));
165 4 (void)hv_stores(st, "logging", newSViv(ps->srv.logging));
166 4 (void)hv_stores(st, "level", newSViv(ps->srv.log_level));
167  
168 21 ▶ while (sv_gets(line, in, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
while (sv_gets(line, in, 0)) { 17 4 21
169 STRLEN l;
170 17 char *p = SvPV(line, l);
171 SV *doc, *c, *reply, *refused;
172 pmcp_msg m;
173 pmcp_req r;
174 17 int status = 0;
175  
176 34 ▶ while (l && (p[l - 1] == '\n' || p[l - 1] == '\r')) l--;
Branch: 3 branches
BranchTrueFalseTotal
while (l && (p[l - 1] == '\n' || p[l - 1] == '\r')) l--; 34 0 34
while (l && (p[l - 1] == '\n' || p[l - 1] == '\r')) l--; 17 17 34
while (l && (p[l - 1] == '\n' || p[l - 1] == '\r')) l--; 0 17 17
177 19 ▶ if (!l) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) continue; 0 17 17
178 17 SvCUR_set(line, l);
179  
180 17 ENTER; SAVETMPS;
181 17 doc = pmcp_rpc_decode(aTHX_ line);
182 17 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 1 16 17
183 1 reply = pmcp_rpc_error(aTHX_ NULL, PMCP_PARSE, "parse error", NULL);
184 1 pmcp_stdio_write(aTHX_ sv_2mortal(reply));
185 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
186 1 continue;
187 }
188 16 sv_2mortal(doc);
189 16 pmcp_rpc_classify(aTHX_ doc, &m);
190 16 c = sv_2mortal(pmcp_stdio_context(aTHX_ app, ps, token, ctx_class));
191 16 (void)hv_stores(st, "c", newSVsv(c));
192  
193 16 ▶ if (m.kind != PMCP_K_INVALID && (refused = pmcp_stdio_guard(aTHX_ ps, c))) {
Branch: 2 branches
BranchTrueFalseTotal
if (m.kind != PMCP_K_INVALID && (refused = pmcp_stdio_guard(aTHX_ ps, c))) { 16 0 16
if (m.kind != PMCP_K_INVALID && (refused = pmcp_stdio_guard(aTHX_ ps, c))) { 1 15 16
194 1 ▶ if (m.kind == PMCP_K_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if (m.kind == PMCP_K_REQUEST) { 1 0 1
195 1 SV *data = newRV_noinc((SV *)newHV());
196 1 (void)hv_stores((HV *)SvRV(data), "reason", newSVsv(refused));
197 1 pmcp_stdio_write(aTHX_ sv_2mortal(
198 pmcp_rpc_error(aTHX_ m.id, PMCP_STDIO_REFUSED, "not authorized", data)));
199 }
200 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
201 1 continue;
202 }
203 15 ▶ if (pmcp_is_code(ps->principal)) pmcp_principal_run(aTHX_ c, ps->principal);
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_code(ps->principal)) pmcp_principal_run(aTHX_ c, ps->principal); 15 0 15
204  
205 15 pmcp_req_init(&r, v, initialized);
206 15 r.c = c;
207 15 r.stdio = 1;
208 15 r.stream = strv;
209 15 reply = pmcp_dispatch_doc(aTHX_ &ps->srv, &r, doc, &status);
210 15 ▶ if (m.id) pmcp_live_del(aTHX_ NULL, m.id);
Branch: 1 branch
BranchTrueFalseTotal
if (m.id) pmcp_live_del(aTHX_ NULL, m.id); 14 1 15
211 15 ▶ if (r.negotiated) { v = r.negotiated; initialized = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (r.negotiated) { v = r.negotiated; initialized = 1; } 1 14 15
212 15 ▶ if (r.level_set >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.level_set >= 0) { 1 14 15
213 1 ps->srv.log_level = r.level_set;
214 1 (void)hv_stores(st, "level", newSViv(r.level_set));
215 }
216 15 pmcp_req_free(aTHX_ &r);
217 15 ▶ if (reply) pmcp_stdio_write(aTHX_ sv_2mortal(reply));
Branch: 1 branch
BranchTrueFalseTotal
if (reply) pmcp_stdio_write(aTHX_ sv_2mortal(reply)); 14 1 15
218 15 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 15 0 15
219 }
220 4 PerlIO_flush(PMCP_STDIO.out);
221 4 PMCP_STDIO.active = 0;
222 4 return 0;
223 }
224  
225 /* ---- `punk mcp` ---------------------------------------------------------------------------- */
226  
227 /* Perl's STDERR as it stands, so a caller that has redirected the handle
228 * (a test, a wrapper) sees what is said, which fd 2 alone would not show. */
229 2 static PerlIO *pmcp_stderr(pTHX)
230 {
231 2 ▶ IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL;
Branch: 5 branches
BranchTrueFalseTotal
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; 2 0 2
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; 2 0 2
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; 0 2 2
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; 0 0 0
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; 2 0 2
232 2 ▶ return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr();
Branch: 2 branches
BranchTrueFalseTotal
return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr(); 2 0 2
return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr(); 2 0 2
233 }
234  
235 /* The command's body: ($opt, @args). Loads the application, checks it uses
236 * the plugin, and serves it. */
237 6 XS_INTERNAL(pmcp_command_cb)
238 {
239 6 dXSARGS;
240 6 ▶ SV *opt = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *opt = items > 0 ? ST(0) : &PL_sv_undef; 6 0 6
241 6 ▶ HV *o = pmcp_is_hash(opt) ? (HV *)SvRV(opt) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *o = pmcp_is_hash(opt) ? (HV *)SvRV(opt) : NULL; 6 0 6
242 6 ▶ SV *dir = o ? pmcp_hget(aTHX_ o, "dir") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *dir = o ? pmcp_hget(aTHX_ o, "dir") : NULL; 6 0 6
243 6 ▶ SV *token = o ? pmcp_hget(aTHX_ o, "token") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *token = o ? pmcp_hget(aTHX_ o, "token") : NULL; 6 0 6
244 6 ▶ SV *envname = o ? pmcp_hget(aTHX_ o, "env") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *envname = o ? pmcp_hget(aTHX_ o, "env") : NULL; 6 0 6
245 6 SV *argv[4], *info, *err = NULL, *class, *app;
246 HV *st;
247 pmcp_server *ps;
248 6 int code = 0;
249  
250 6 ▶ if (!token) {
Branch: 1 branch
BranchTrueFalseTotal
if (!token) { 5 1 6
251 5 HV *envh = GvHV(gv_fetchpvs("ENV", GV_ADD, SVt_PVHV));
252 5 SV **e = hv_fetchs(envh, "PUNK_MCP_TOKEN", 0);
253 5 ▶ if (e && SvOK(*e)) token = *e;
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) token = *e; 1 4 5
if (e && SvOK(*e)) token = *e; 1 0 1
254 }
255 6 ▶ if (envname) {
Branch: 1 branch
BranchTrueFalseTotal
if (envname) { 0 6 6
256 0 HV *envh = GvHV(gv_fetchpvs("ENV", GV_ADD, SVt_PVHV));
257 0 (void)hv_stores(envh, "PUNK_ENV", newSVsv(envname));
258 }
259  
260 6 argv[0] = sv_2mortal(newSVpvs("dir"));
261 6 ▶ argv[1] = dir ? dir : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = dir ? dir : &PL_sv_undef; 6 0 6
262 6 argv[2] = sv_2mortal(newSVpvs("chdir"));
263 6 argv[3] = sv_2mortal(newSViv(1));
264 6 info = pmcp_call_method(aTHX_ sv_2mortal(newSVpvs("Punk::Command")), "load_app",
265 argv, 4, G_SCALAR, &err);
266 6 ▶ if (!info) {
Branch: 1 branch
BranchTrueFalseTotal
if (!info) { 1 5 6
267 1 PerlIO_printf(pmcp_stderr(aTHX), "punk mcp: %" SVf, SVfARG(sv_2mortal(err)));
268 1 code = 1;
269 1 goto done;
270 }
271 5 sv_2mortal(info);
272 5 class = pmcp_hget(aTHX_ (HV *)SvRV(info), "class");
273 5 app = pmcp_hget(aTHX_ (HV *)SvRV(info), "registrar");
274 5 ▶ st = (app && pmcp_is_hash(app) && pmcp_hget(aTHX_ (HV *)SvRV(app), "mcp"))
Branch: 2 branches
BranchTrueFalseTotal
st = (app && pmcp_is_hash(app) && pmcp_hget(aTHX_ (HV *)SvRV(app), "mcp")) 5 0 5
st = (app && pmcp_is_hash(app) && pmcp_hget(aTHX_ (HV *)SvRV(app), "mcp")) 4 1 5
275 10 ▶ ? pmcp_state(aTHX_ app) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? pmcp_state(aTHX_ app) : NULL; 5 0 5
276 5 ▶ ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL; 4 1 5
277 5 ▶ if (!ps || !pmcp_flag(aTHX_ st, "registered")) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps || !pmcp_flag(aTHX_ st, "registered")) { 4 1 5
if (!ps || !pmcp_flag(aTHX_ st, "registered")) { 0 4 4
278 1 ▶ PerlIO_printf(pmcp_stderr(aTHX), "punk mcp: %" SVf " does not use plugin 'MCP' "
Branch: 1 branch
BranchTrueFalseTotal
PerlIO_printf(pmcp_stderr(aTHX), "punk mcp: %" SVf " does not use plugin 'MCP' " 0 1 1
279 "(add plugin 'MCP' => {...} to it)\n",
280 0 SVfARG(class ? class : sv_2mortal(newSVpvs("the application"))));
281 1 code = 2;
282 1 goto done;
283 }
284 4 code = pmcp_stdio_run(aTHX_ app, ps, token, class);
285  
286 6 done:
287 6 SP = PL_stack_base + ax - 1;
288 6 ST(0) = sv_2mortal(newSViv(code));
289 6 XSRETURN(1);
290 }
291  
292 /* The doctor row: one line per application in this process using the plugin. */
293 2 XS_INTERNAL(pmcp_doctor_cb)
294 {
295 2 dXSARGS;
296 2 SV *detail = sv_2mortal(newSVpvs(""));
297 2 ▶ SSize_t i, n = PMCP_SERVERS ? av_len(PMCP_SERVERS) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = PMCP_SERVERS ? av_len(PMCP_SERVERS) + 1 : 0; 1 1 2
298 PERL_UNUSED_VAR(items);
299 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 2 3
300 1 pmcp_server *ps = pmcp_server_of(aTHX_ *av_fetch(PMCP_SERVERS, i, 0));
301 pmcp_tools *ts;
302 SV *sess, *oauth;
303 1 ▶ if (!ps) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) continue; 0 1 1
304 1 ts = (pmcp_tools *)ps->srv.tools;
305 1 sess = pmcp_hget(aTHX_ ps->opts, "session");
306 1 oauth = pmcp_hget(aTHX_ ps->opts, "oauth");
307 1 ▶ if (SvCUR(detail)) sv_catpvs(detail, "; ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(detail)) sv_catpvs(detail, "; "); 0 1 1
308 1 ▶ sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf
Branch: 8 branches
BranchTrueFalseTotal
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 0 1 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 1 0 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 1 0 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 1 0 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 0 1 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 0 1 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 1 0 1
sv_catpvf(detail, "%" SVf ": %" IVdf " tools, %" IVdf " resources, %" IVdf 1 0 1
309 " prompts, %s%" SVf ", oauth %s",
310 SVfARG(pmcp_hget(aTHX_ ps->opts, "path")),
311 ts ? ts->n : 0,
312 ps->srv.resources ? ((pmcp_resources *)ps->srv.resources)->n : 0,
313 ps->srv.prompts ? ((pmcp_prompts *)ps->srv.prompts)->n : 0,
314 sess && SvTRUE(sess) ? "sessions in " : "stateless",
315 SVfARG(sess && SvTRUE(sess) ? sess : sv_2mortal(newSVpvs(""))),
316 oauth ? "on" : "off");
317 }
318 2 SP = PL_stack_base + ax - 1;
319 2 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2 2
320 2 ST(0) = sv_2mortal(newSViv(1));
321 2 ▶ ST(1) = SvCUR(detail) ? detail : sv_2mortal(newSVpvs("no application here uses it"));
Branch: 1 branch
BranchTrueFalseTotal
ST(1) = SvCUR(detail) ? detail : sv_2mortal(newSVpvs("no application here uses it")); 1 1 2
322 2 XSRETURN(2);
323 }
324  
325 /* `use Punk::Plugin::MCP ':command'`, from Punk/Command/Mcp.pm: register the
326 * command and the doctor row, once. */
327 6 static void pmcp_command_register(pTHX)
328 {
329 static int done = 0;
330 6 SV *class = sv_2mortal(newSVpvs("Punk::Command"));
331 HV *spec;
332 AV *opts;
333 SV *argv[3], *r;
334 6 ▶ if (done || !pmcp_can(aTHX_ class, "register")) return;
Branch: 2 branches
BranchTrueFalseTotal
if (done || !pmcp_can(aTHX_ class, "register")) return; 6 0 6
if (done || !pmcp_can(aTHX_ class, "register")) return; 0 6 6
335 6 done = 1;
336  
337 6 opts = newAV();
338 {
339 6 HV *t = newHV();
340 6 (void)hv_stores(t, "spec", newSVpvs("token=s"));
341 6 (void)hv_stores(t, "arg", newSVpvs("TOKEN"));
342 6 (void)hv_stores(t, "doc", newSVpvs("a bearer token the guards see (or PUNK_MCP_TOKEN)"));
343 6 av_push(opts, newRV_noinc((SV *)t));
344 }
345 {
346 6 HV *e = newHV();
347 6 (void)hv_stores(e, "spec", newSVpvs("env=s"));
348 6 (void)hv_stores(e, "arg", newSVpvs("NAME"));
349 6 (void)hv_stores(e, "doc", newSVpvs("the configuration environment (sets PUNK_ENV)"));
350 6 av_push(opts, newRV_noinc((SV *)e));
351 }
352 6 spec = newHV();
353 6 (void)hv_stores(spec, "abstract", newSVpvs("serve the application's MCP endpoint over stdio"));
354 6 (void)hv_stores(spec, "usage", newSVpvs("[options]"));
355 6 (void)hv_stores(spec, "desc", newSVpvs(
356 "Newline-delimited JSON-RPC on stdin and stdout, for an MCP client that\n"
357 "launches its servers, such as Claude Code: claude mcp add NAME -- punk mcp"));
358 6 (void)hv_stores(spec, "options", newRV_noinc((SV *)opts));
359 6 (void)hv_stores(spec, "code", pmcp_closure(aTHX_ pmcp_command_cb, newAV()));
360 6 argv[0] = sv_2mortal(newSVpvs("mcp"));
361 6 argv[1] = sv_2mortal(newRV_noinc((SV *)spec));
362 6 argv[2] = sv_2mortal(newSVpvs("Punk::Command::Mcp"));
363 6 r = pmcp_call_method(aTHX_ class, "register", argv, 3, G_SCALAR, NULL);
364 6 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 6 0 6
365  
366 6 ▶ if (pmcp_can(aTHX_ class, "register_doctor")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_can(aTHX_ class, "register_doctor")) { 6 0 6
367 6 argv[0] = sv_2mortal(newSVpvs("Punk::MCP"));
368 6 argv[1] = sv_2mortal(pmcp_closure(aTHX_ pmcp_doctor_cb, newAV()));
369 6 argv[2] = sv_2mortal(newSVpvs("Punk::Command::Mcp"));
370 6 r = pmcp_call_method(aTHX_ class, "register_doctor", argv, 3, G_SCALAR, NULL);
371 6 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 6 0 6
372 }
373 }
374  
375 #endif /* PMCP_STDIO_H */
« xs/mcp.xs ↑ Summary include/pmcp/pmcp_oa.h »