include/pmcp/pmcp_http.h

filter: Statementstmt 100% 244 / 244 Branchbran 82.3% 168 / 204 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.9% 412 / 448 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_decl.h ↑ Summary include/pmcp/pmcp_uri.h »
1 /* pmcp_http.h - Streamable HTTP, the JSON half: one POST in, one answer out.
2 *
3 * Needs: pmcp_proto.h, pmcp_reg.h.
4 *
5 * The checks run in Maat's order (Maat/include/mt_mcp.h:826), cheapest and
6 * least trusting first: Origin, Accept, Content-Type, the protocol version
7 * header, the guards, the body size, and only then the JSON. A refusal before
8 * the dispatcher is still a JSON-RPC error with a null id, so a client shows
9 * why rather than a bare status.
10 *
11 * GET and DELETE need nothing here: Punk answers a method the path does not
12 * serve with 405 and an Allow header from the router. Sessions (09) and the
13 * GET stream (10) add those routes.
14 */
15  
16 #ifndef PMCP_HTTP_H
17 #define PMCP_HTTP_H
18  
19 /* Defined in pmcp_sse.h, which needs this header's env reader. */
20 static int pmcp_should_stream(pTHX_ pmcp_server *ps, HV *env, pmcp_msg *m);
21 static SV *pmcp_stream_answer(pTHX_ SV *c, SV *server, SV *doc, enum pmcp_ver v,
22 int cors, SV *sid, int level);
23  
24 /* What sessions (pmcp_sess.h) carry from before the dispatch to after it. */
25 typedef struct {
26 SV *store; /* mortal, or NULL with sessions off */
27 SV *sid; /* the request's session, mortal, or NULL */
28 HV *rec; /* its record, mortal */
29 int creating; /* an initialize that will make one if it succeeds */
30 int in_session; /* a second initialize carrying a live session id */
31 int level; /* the session's log level, or -1 */
32 enum pmcp_ver v; /* the session's version */
33 } pmcp_sess_t;
34 static SV *pmcp_sess_begin(pTHX_ SV *c, pmcp_server *ps, HV *env, pmcp_msg *m,
35 pmcp_sess_t *s, int cors);
36 static SV *pmcp_sess_end(pTHX_ pmcp_server *ps, HV *env, pmcp_sess_t *s,
37 pmcp_req *r, pmcp_msg *m);
38 static void pmcp_level_set(pTHX_ SV *c, pmcp_server *ps, SV *sid, int level);
39 XS_INTERNAL(pmcp_delete_cb);
40 static void pmcp_sess_boot_check(pTHX_ SV *app, HV *opts);
41 static int pmcp_sessions_on(pTHX_ HV *opts);
42 static void pmcp_bus_install(pTHX_ SV *app, SV *server, SV *guards);
43 static SV *pmcp_auth_guards(pTHX_ SV *app, HV *opts);
44 static void pmcp_prm_install(pTHX_ SV *app, HV *opts);
45 static void pmcp_expose_refusal(pTHX_ SV *out, int cors);
46 static void pmcp_principal_run(pTHX_ SV *c, SV *principal);
47  
48 /* What a browser may read off an answer. The plugin sets it itself, so a
49 * browser client works without the application listing these in `cors`:
50 * Punk's CORS layer leaves a handler's own value alone. */
51 #define PMCP_EXPOSE "Mcp-Session-Id, MCP-Protocol-Version, WWW-Authenticate"
52  
53 /* One header of a PSGI env, borrowed, or NULL. */
54 2331 static SV *pmcp_env_get(pTHX_ HV *env, const char *k)
55 {
56 2331 SV **e = hv_fetch(env, k, (I32)strlen(k), 0);
57 2331 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 1227 1104 2331
return (e && *e && SvOK(*e)) ? *e : NULL; 1227 0 1227
return (e && *e && SvOK(*e)) ? *e : NULL; 1226 1 1227
58 }
59  
60 /* [status, [headers], [body]] +1. `body` (+1) may be NULL for none. */
61 271 static SV *pmcp_http_answer(pTHX_ int status, SV *body, int cors)
62 {
63 271 AV *t = newAV(), *h = newAV(), *b = newAV();
64 271 ▶ if (body) {
Branch: 1 branch
BranchTrueFalseTotal
if (body) { 256 15 271
65 256 av_push(h, newSVpvs("Content-Type"));
66 256 av_push(h, newSVpvs("application/json"));
67 }
68 271 av_push(h, newSVpvs("Cache-Control"));
69 271 av_push(h, newSVpvs("no-store"));
70 271 ▶ if (cors) {
Branch: 1 branch
BranchTrueFalseTotal
if (cors) { 16 255 271
71 16 av_push(h, newSVpvs("Access-Control-Expose-Headers"));
72 16 av_push(h, newSVpvs(PMCP_EXPOSE));
73 }
74 271 ▶ if (body) av_push(b, body);
Branch: 1 branch
BranchTrueFalseTotal
if (body) av_push(b, body); 256 15 271
75 271 av_push(t, newSViv(status));
76 271 av_push(t, newRV_noinc((SV *)h));
77 271 av_push(t, newRV_noinc((SV *)b));
78 271 return newRV_noinc((SV *)t);
79 }
80  
81 /* A refusal before the dispatcher: the status, with a JSON-RPC error saying
82 * why. */
83 49 static SV *pmcp_http_refuse(pTHX_ int status, const char *why, int cors)
84 {
85 49 return pmcp_http_answer(aTHX_ status,
86 pmcp_rpc_error(aTHX_ NULL, PMCP_REQUEST, why, NULL), cors);
87 }
88  
89 /* ---- Origin ------------------------------------------------------------------ */
90  
91 /* scheme://host[:port], lowercased, with the scheme's default port dropped,
92 * into a mortal SV, so https://A.test:443 and https://a.test compare equal. */
93 40 static SV *pmcp_origin_norm(pTHX_ const char *p, STRLEN l)
94 {
95 40 SV *o = sv_2mortal(newSVpvn(p, l));
96 40 char *s = SvPVX(o);
97 40 STRLEN i, n = SvCUR(o);
98 752 ▶ for (i = 0; i < n; i++) s[i] = toLOWER(s[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) s[i] = toLOWER(s[i]); 9 703 712
for (i = 0; i < n; i++) s[i] = toLOWER(s[i]); 712 40 752
99 41 ▶ while (n && s[n - 1] == '/') n--;
Branch: 2 branches
BranchTrueFalseTotal
while (n && s[n - 1] == '/') n--; 41 0 41
while (n && s[n - 1] == '/') n--; 1 40 41
100 40 ▶ if (n > 12 && memEQ(s, "https://", 8) && memEQ(s + n - 4, ":443", 4)) n -= 4;
Branch: 3 branches
BranchTrueFalseTotal
if (n > 12 && memEQ(s, "https://", 8) && memEQ(s + n - 4, ":443", 4)) n -= 4; 38 2 40
if (n > 12 && memEQ(s, "https://", 8) && memEQ(s + n - 4, ":443", 4)) n -= 4; 13 25 38
if (n > 12 && memEQ(s, "https://", 8) && memEQ(s + n - 4, ":443", 4)) n -= 4; 1 12 13
101 39 ▶ else if (n > 11 && memEQ(s, "http://", 7) && memEQ(s + n - 3, ":80", 3)) n -= 3;
Branch: 3 branches
BranchTrueFalseTotal
else if (n > 11 && memEQ(s, "http://", 7) && memEQ(s + n - 3, ":80", 3)) n -= 3; 37 2 39
else if (n > 11 && memEQ(s, "http://", 7) && memEQ(s + n - 3, ":80", 3)) n -= 3; 25 12 37
else if (n > 11 && memEQ(s, "http://", 7) && memEQ(s + n - 3, ":80", 3)) n -= 3; 1 24 25
102 40 SvCUR_set(o, n);
103 40 return o;
104 }
105  
106 /* Is the request's Origin acceptable? No Origin is (a non-browser client).
107 * With `origins`, it must be listed. Without, it must be the server's own,
108 * which is the request's scheme and Host. `Origin: null` never matches. */
109 317 static int pmcp_origin_ok(pTHX_ HV *env, HV *opts)
110 {
111 317 SV *o = pmcp_env_get(aTHX_ env, "HTTP_ORIGIN");
112 317 SV *list = pmcp_hget(aTHX_ opts, "origins");
113 SV *have;
114 STRLEN l;
115 const char *p;
116  
117 317 ▶ if (!o) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return 1; 299 18 317
118 18 p = SvPV_const(o, l);
119 18 have = pmcp_origin_norm(aTHX_ p, l);
120 18 ▶ if (list) {
Branch: 1 branch
BranchTrueFalseTotal
if (list) { 7 11 18
121 7 AV *av = (AV *)SvRV(list);
122 7 SSize_t i, n = av_len(av) + 1;
123 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 3 14
124 11 SV **e = av_fetch(av, i, 0);
125 STRLEN el;
126 11 const char *ep = SvPV_const(*e, el);
127 11 ▶ if (sv_eq(have, pmcp_origin_norm(aTHX_ ep, el))) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(have, pmcp_origin_norm(aTHX_ ep, el))) return 1; 4 7 11
128 }
129 3 return 0;
130 }
131 {
132 11 SV *scheme = pmcp_env_get(aTHX_ env, "psgi.url_scheme");
133 11 SV *host = pmcp_env_get(aTHX_ env, "HTTP_HOST");
134 SV *own;
135 11 ▶ if (!host) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!host) return 0; 0 11 11
136 22 ▶ own = sv_2mortal(newSVpvf("%s://%" SVf, scheme ? SvPV_nolen(scheme) : "http",
Branch: 1 branch
BranchTrueFalseTotal
own = sv_2mortal(newSVpvf("%s://%" SVf, scheme ? SvPV_nolen(scheme) : "http", 11 0 11
137 SVfARG(host)));
138 11 p = SvPV_const(own, l);
139 11 return sv_eq(have, pmcp_origin_norm(aTHX_ p, l));
140 }
141 }
142  
143 /* ---- media types -------------------------------------------------------------- */
144  
145 /* Does an Accept header allow `type` (application/json, text/event-stream)?
146 * No header allows everything, as HTTP has it. */
147 288 static int pmcp_accepts(pTHX_ HV *env, const char *type)
148 {
149 288 SV *a = pmcp_env_get(aTHX_ env, "HTTP_ACCEPT");
150 288 const char *p, *e, *slash = strchr(type, '/');
151 288 STRLEN l, tl = strlen(type), major = (STRLEN)(slash - type);
152 288 ▶ if (!a) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return 1; 68 220 288
153 220 p = SvPV_const(a, l);
154 220 e = p + l;
155 224 ▶ while (p < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { 221 3 224
156 221 const char *s = p, *end;
157 STRLEN n;
158 3734 ▶ while (p < e && *p != ',') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < e && *p != ',') p++; 3704 30 3734
while (p < e && *p != ',') p++; 3513 191 3704
159 221 end = p;
160 221 ▶ if (p < e) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < e) p++; 191 30 221
161 222 ▶ while (s < end && isSPACE(*s)) s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isSPACE(*s)) s++; 222 0 222
while (s < end && isSPACE(*s)) s++; 1 221 222
162 {
163 221 const char *semi = s;
164 3727 ▶ while (semi < end && *semi != ';') semi++;
Branch: 2 branches
BranchTrueFalseTotal
while (semi < end && *semi != ';') semi++; 3507 220 3727
while (semi < end && *semi != ';') semi++; 3506 1 3507
165 221 end = semi;
166 }
167 221 ▶ while (end > s && isSPACE(end[-1])) end--;
Branch: 2 branches
BranchTrueFalseTotal
while (end > s && isSPACE(end[-1])) end--; 221 0 221
while (end > s && isSPACE(end[-1])) end--; 0 221 221
168 221 n = (STRLEN)(end - s);
169 221 ▶ if ((n == 3 && memEQ(s, "*/*", 3))
Branch: 2 branches
BranchTrueFalseTotal
if ((n == 3 && memEQ(s, "*/*", 3)) 1 220 221
if ((n == 3 && memEQ(s, "*/*", 3)) 0 1 1
170 220 ▶ || (n == tl && pmcp_foldEQ(s, type, (I32)tl))
Branch: 2 branches
BranchTrueFalseTotal
|| (n == tl && pmcp_foldEQ(s, type, (I32)tl)) 215 5 220
|| (n == tl && pmcp_foldEQ(s, type, (I32)tl)) 0 215 215
171 5 ▶ || (n == major + 2 && pmcp_foldEQ(s, type, (I32)major) && memEQ(s + major, "/*", 2)))
Branch: 3 branches
BranchTrueFalseTotal
|| (n == major + 2 && pmcp_foldEQ(s, type, (I32)major) && memEQ(s + major, "/*", 2))) 1 4 5
|| (n == major + 2 && pmcp_foldEQ(s, type, (I32)major) && memEQ(s + major, "/*", 2))) 1 0 1
|| (n == major + 2 && pmcp_foldEQ(s, type, (I32)major) && memEQ(s + major, "/*", 2))) 1 0 1
172 217 return 1;
173 }
174 3 return 0;
175 }
176  
177 285 static int pmcp_content_type_ok(pTHX_ HV *env)
178 {
179 285 SV *ct = pmcp_env_get(aTHX_ env, "CONTENT_TYPE");
180 285 STRLEN l, n = 0;
181 const char *p;
182 285 ▶ if (!ct) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return 0; 0 285 285
183 285 p = SvPV_const(ct, l);
184 4857 ▶ while (n < l && p[n] != ';' && !isSPACE(p[n])) n++;
Branch: 3 branches
BranchTrueFalseTotal
while (n < l && p[n] != ';' && !isSPACE(p[n])) n++; 4573 284 4857
while (n < l && p[n] != ';' && !isSPACE(p[n])) n++; 4572 1 4573
while (n < l && p[n] != ';' && !isSPACE(p[n])) n++; 4572 0 4572
185 285 ▶ return n == 16 && pmcp_foldEQ(p, "application/json", 16);
Branch: 2 branches
BranchTrueFalseTotal
return n == 16 && pmcp_foldEQ(p, "application/json", 16); 282 3 285
return n == 16 && pmcp_foldEQ(p, "application/json", 16); 282 0 282
186 }
187  
188 /* ---- the handler ------------------------------------------------------------------ */
189  
190 /* POST on the MCP path. cap = [server object, guards AV ref (the hook 11 fills)]. */
191 296 XS_INTERNAL(pmcp_http_cb)
192 {
193 296 dXSARGS;
194 296 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 296 0 296
195 296 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 0));
196 296 SV *gslot = pmcp_cap(aTHX_ cv, 1);
197 296 ▶ AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL; 296 0 296
AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL; 296 0 296
198 296 SV *envsv, *out = NULL;
199 HV *env, *opts;
200 int cors;
201 296 enum pmcp_ver v = PMCP_V_2025_03_26;
202  
203 296 ▶ if (!ps) croak("%s: the MCP route lost its server", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) croak("%s: the MCP route lost its server", PMCP_WHO); 0 296 296
204 296 opts = ps->opts;
205 296 envsv = sv_2mortal(pmcp_call_method(aTHX_ c, "env", NULL, 0, G_SCALAR, NULL));
206 296 env = (HV *)SvRV(envsv);
207 296 cors = pmcp_env_get(aTHX_ env, "HTTP_ORIGIN") != NULL;
208  
209 /* 1. Origin: a page on another site driving a local server is the attack */
210 296 ▶ if (!pmcp_origin_ok(aTHX_ env, opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_origin_ok(aTHX_ env, opts)) { 8 288 296
211 8 out = pmcp_http_refuse(aTHX_ 403, "origin not allowed", cors);
212 8 goto answer;
213 }
214 /* 2. Accept */
215 288 ▶ if (!pmcp_accepts(aTHX_ env, "application/json")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_accepts(aTHX_ env, "application/json")) { 3 285 288
216 3 out = pmcp_http_refuse(aTHX_ 406, "Accept must allow application/json", cors);
217 3 goto answer;
218 }
219 /* 3. Content-Type */
220 285 ▶ if (!pmcp_content_type_ok(aTHX_ env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_content_type_ok(aTHX_ env)) { 3 282 285
221 3 out = pmcp_http_refuse(aTHX_ 415, "Content-Type must be application/json", cors);
222 3 goto answer;
223 }
224 /* 4. the protocol version: absent is read as 2025-03-26, as the spec says */
225 {
226 282 SV *pv = pmcp_env_get(aTHX_ env, "HTTP_MCP_PROTOCOL_VERSION");
227 282 ▶ if (pv) {
Branch: 1 branch
BranchTrueFalseTotal
if (pv) { 147 135 282
228 STRLEN l;
229 147 const char *p = SvPV_const(pv, l);
230 147 v = pmcp_version_of(p, l);
231 147 ▶ if (v == PMCP_V_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == PMCP_V_NONE) { 3 144 147
232 3 out = pmcp_http_refuse(aTHX_ 400, "unsupported MCP-Protocol-Version", cors);
233 3 goto answer;
234 }
235 }
236 }
237 /* 5. the guards, before any JSON is read. A refusal goes out as the guard
238 * made it, since a 401 carries the challenge a client acts on. */
239 279 ▶ if (guards) {
Branch: 1 branch
BranchTrueFalseTotal
if (guards) { 279 0 279
240 279 SSize_t i, n = av_len(guards) + 1;
241 295 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 35 260 295
242 35 SV *err = NULL;
243 35 SV *r = pmcp_call_sv(aTHX_ *av_fetch(guards, i, 0), &c, 1, G_SCALAR, &err);
244 35 ▶ if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 35 35
245 35 ▶ if (SvROK(r)) { out = r; pmcp_expose_refusal(aTHX_ out, cors); goto answer; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(r)) { out = r; pmcp_expose_refusal(aTHX_ out, cors); goto answer; } 19 16 35
246 16 SvREFCNT_dec(r);
247 }
248 }
249 /* principal, now the request is let in: what it returns is $c->mcp_user */
250 {
251 260 SV *p = pmcp_cap(aTHX_ cv, 2);
252 260 ▶ if (p && pmcp_is_code(p)) pmcp_principal_run(aTHX_ c, p);
Branch: 2 branches
BranchTrueFalseTotal
if (p && pmcp_is_code(p)) pmcp_principal_run(aTHX_ c, p); 260 0 260
if (p && pmcp_is_code(p)) pmcp_principal_run(aTHX_ c, p); 2 258 260
253 }
254 /* 6. the body: refused on its declared length before it is read, and on
255 * what arrived when there was no length */
256 {
257 260 SV *cl = pmcp_env_get(aTHX_ env, "CONTENT_LENGTH");
258 260 SV *maxsv = pmcp_hget(aTHX_ opts, "max_body");
259 260 ▶ IV max = maxsv ? SvIV(maxsv) : 1048576;
Branch: 1 branch
BranchTrueFalseTotal
IV max = maxsv ? SvIV(maxsv) : 1048576; 260 0 260
260 SV *req, *body;
261 260 ▶ if (cl && SvIV(cl) > max) {
Branch: 2 branches
BranchTrueFalseTotal
if (cl && SvIV(cl) > max) { 259 1 260
if (cl && SvIV(cl) > max) { 2 257 259
262 2 out = pmcp_http_refuse(aTHX_ 413, "the body is too large", cors);
263 2 goto answer;
264 }
265 258 req = sv_2mortal(pmcp_call_method(aTHX_ c, "req", NULL, 0, G_SCALAR, NULL));
266 258 body = sv_2mortal(pmcp_call_method(aTHX_ req, "body", NULL, 0, G_SCALAR, NULL));
267 258 ▶ if (SvOK(body) && (IV)SvCUR(body) > max) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(body) && (IV)SvCUR(body) > max) { 258 0 258
if (SvOK(body) && (IV)SvCUR(body) > max) { 1 257 258
268 1 out = pmcp_http_refuse(aTHX_ 413, "the body is too large", cors);
269 1 goto answer;
270 }
271 /* 7. the protocol. Decoded once here, because whether to answer with
272 * an event stream is decided before anything runs (08). */
273 {
274 pmcp_req r;
275 257 int status = 0;
276 SV *reply;
277 257 ▶ SV *doc = pmcp_rpc_decode(aTHX_ SvOK(body) ? body : sv_2mortal(newSVpvs("")));
Branch: 1 branch
BranchTrueFalseTotal
SV *doc = pmcp_rpc_decode(aTHX_ SvOK(body) ? body : sv_2mortal(newSVpvs(""))); 0 257 257
278 257 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 1 256 257
279 1 out = pmcp_http_answer(aTHX_ 400,
280 pmcp_rpc_error(aTHX_ NULL, PMCP_PARSE, "parse error", NULL), cors);
281 42 goto answer;
282 }
283 256 sv_2mortal(doc);
284 {
285 pmcp_msg m;
286 pmcp_sess_t s;
287 SV *sid_out;
288 256 Zero(&s, 1, pmcp_sess_t);
289 256 s.level = -1;
290 256 s.v = v;
291 256 pmcp_rpc_classify(aTHX_ doc, &m);
292 /* 8. the session: required after initialize, and checked */
293 256 ▶ if ((out = pmcp_sess_begin(aTHX_ c, ps, env, &m, &s, cors)))
Branch: 1 branch
BranchTrueFalseTotal
if ((out = pmcp_sess_begin(aTHX_ c, ps, env, &m, &s, cors))) 18 238 256
294 41 goto answer;
295 238 v = s.v;
296 238 ▶ if (pmcp_should_stream(aTHX_ ps, env, &m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_should_stream(aTHX_ ps, env, &m)) { 23 215 238
297 23 (void)pmcp_sess_end(aTHX_ ps, env, &s, NULL, &m);
298 23 out = pmcp_stream_answer(aTHX_ c, pmcp_cap(aTHX_ cv, 0), doc, v,
299 cors, s.sid, s.level);
300 23 goto answer;
301 }
302 215 ▶ pmcp_req_init(&r, v, s.in_session || s.sid);
Branch: 2 branches
BranchTrueFalseTotal
pmcp_req_init(&r, v, s.in_session || s.sid); 214 1 215
pmcp_req_init(&r, v, s.in_session || s.sid); 39 175 214
303 215 r.c = c;
304 215 r.session = s.sid;
305 215 reply = pmcp_dispatch_doc(aTHX_ &ps->srv, &r, doc, &status);
306 215 sid_out = pmcp_sess_end(aTHX_ ps, env, &s, &r, &m);
307 215 ▶ if (r.level_set >= 0 && !s.store) ps->srv.log_level = r.level_set;
Branch: 2 branches
BranchTrueFalseTotal
if (r.level_set >= 0 && !s.store) ps->srv.log_level = r.level_set; 3 212 215
if (r.level_set >= 0 && !s.store) ps->srv.log_level = r.level_set; 0 3 3
308 215 pmcp_req_free(aTHX_ &r);
309 215 out = pmcp_http_answer(aTHX_ status, reply, cors);
310 215 ▶ if (sid_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (sid_out) { 66 149 215
311 66 AV *h = (AV *)SvRV(*av_fetch((AV *)SvRV(out), 1, 0));
312 66 av_push(h, newSVpvs("Mcp-Session-Id"));
313 66 av_push(h, newSVsv(sid_out));
314 }
315 }
316 }
317 }
318  
319 296 answer:
320 296 SP = PL_stack_base + ax - 1;
321 296 ST(0) = sv_2mortal(out);
322 296 XSRETURN(1);
323 }
324  
325 /* ---- installing ----------------------------------------------------------------------- */
326  
327 /* The POST route, at on_compile. */
328 112 static void pmcp_http_route(pTHX_ SV *app, SV *server, HV *opts)
329 {
330 112 AV *cap = newAV();
331 SV *argv[5], *r;
332 112 av_push(cap, newSVsv(server));
333 112 av_push(cap, pmcp_auth_guards(aTHX_ app, opts)); /* auth, then oauth (11) */
334 {
335 /* principal, resolved like any target, or undef */
336 107 SV *p = pmcp_hget(aTHX_ opts, "principal");
337 108 ▶ if (p && !pmcp_is_code(p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p && !pmcp_is_code(p)) { 7 100 107
if (p && !pmcp_is_code(p)) { 1 6 7
338 1 SV *pargv[2], *perr = NULL, *code;
339 1 pargv[0] = p;
340 1 pargv[1] = sv_2mortal(newSVpvs("the MCP principal"));
341 1 code = pmcp_call_method(aTHX_ app, "_resolve_target", pargv, 2, G_SCALAR, &perr);
342 1 ▶ if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(perr));
Branch: 1 branch
BranchTrueFalseTotal
if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(perr)); 0 1 1
343 1 av_push(cap, code);
344 }
345 106 ▶ else av_push(cap, p ? newSVsv(p) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
else av_push(cap, p ? newSVsv(p) : newSV(0)); 6 100 106
346 }
347 {
348 /* the same guards and principal, kept for stdio (13) */
349 107 pmcp_server *srvp = pmcp_server_of(aTHX_ server);
350 107 SV **pp = av_fetch(cap, 2, 0);
351 107 srvp->guards = newSVsv(*av_fetch(cap, 1, 0));
352 107 ▶ srvp->principal = (pp && pmcp_is_code(*pp)) ? newSVsv(*pp) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
srvp->principal = (pp && pmcp_is_code(*pp)) ? newSVsv(*pp) : NULL; 107 0 107
srvp->principal = (pp && pmcp_is_code(*pp)) ? newSVsv(*pp) : NULL; 7 100 107
353 }
354 107 pmcp_prm_install(aTHX_ app, opts);
355 107 argv[0] = sv_2mortal(newSVpvs("POST"));
356 107 argv[1] = pmcp_hget(aTHX_ opts, "path");
357 107 argv[2] = sv_2mortal(pmcp_closure(aTHX_ pmcp_http_cb, cap));
358 107 argv[3] = sv_2mortal(newRV_noinc((SV *)newAV()));
359 107 argv[4] = sv_2mortal(newRV_noinc((SV *)newHV()));
360 107 r = pmcp_call_method(aTHX_ app, "route", argv, 5, G_SCALAR, NULL);
361 107 SvREFCNT_dec(r);
362  
363 /* DELETE ends a session. Without sessions the path answers DELETE with
364 * Punk's 405, as it does any method it does not serve. The guard hook is
365 * the POST route's own, so 11 fills both at once. */
366 107 ▶ if (pmcp_sessions_on(aTHX_ opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_sessions_on(aTHX_ opts)) { 15 92 107
367 15 AV *dcap = newAV();
368 15 pmcp_bus_install(aTHX_ app, server, *av_fetch(cap, 1, 0));
369 15 av_push(dcap, newSVsv(server));
370 15 av_push(dcap, newSVsv(*av_fetch(cap, 1, 0)));
371 15 argv[0] = sv_2mortal(newSVpvs("DELETE"));
372 15 argv[2] = sv_2mortal(pmcp_closure(aTHX_ pmcp_delete_cb, dcap));
373 15 argv[3] = sv_2mortal(newRV_noinc((SV *)newAV()));
374 15 argv[4] = sv_2mortal(newRV_noinc((SV *)newHV()));
375 15 r = pmcp_call_method(aTHX_ app, "route", argv, 5, G_SCALAR, NULL);
376 15 SvREFCNT_dec(r);
377 }
378 107 }
379  
380 /* At to_app, with the inner app: if the plugin lists `origins`, send the
381 * first a preflight and warn when the application's `cors` would refuse the
382 * headers an MCP client sends. Only an explicit cors `headers` list can: with
383 * none, Punk echoes what was asked. */
384 96 static void pmcp_http_cors_check(pTHX_ SV *inner, HV *opts)
385 {
386 96 SV *origins = pmcp_hget(aTHX_ opts, "origins");
387 96 SV *path = pmcp_hget(aTHX_ opts, "path");
388 HV *env;
389 96 SV *res, *err = NULL, *envrv;
390 static const char *const need[] = { "authorization", "content-type",
391 "mcp-session-id", "mcp-protocol-version", "last-event-id", NULL };
392  
393 98 ▶ if (!origins || !pmcp_is_code(inner) || av_len((AV *)SvRV(origins)) < 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!origins || !pmcp_is_code(inner) || av_len((AV *)SvRV(origins)) < 0) return; 5 91 96
if (!origins || !pmcp_is_code(inner) || av_len((AV *)SvRV(origins)) < 0) return; 5 0 5
if (!origins || !pmcp_is_code(inner) || av_len((AV *)SvRV(origins)) < 0) return; 0 5 5
394 5 env = newHV();
395 5 envrv = sv_2mortal(newRV_noinc((SV *)env));
396 5 (void)hv_stores(env, "REQUEST_METHOD", newSVpvs("OPTIONS"));
397 5 (void)hv_stores(env, "PATH_INFO", newSVsv(path));
398 5 (void)hv_stores(env, "SCRIPT_NAME", newSVpvs(""));
399 5 (void)hv_stores(env, "QUERY_STRING", newSVpvs(""));
400 5 (void)hv_stores(env, "SERVER_NAME", newSVpvs("localhost"));
401 5 (void)hv_stores(env, "SERVER_PORT", newSVpvs("80"));
402 5 (void)hv_stores(env, "SERVER_PROTOCOL", newSVpvs("HTTP/1.1"));
403 5 (void)hv_stores(env, "REMOTE_ADDR", newSVpvs("127.0.0.1"));
404 5 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("http"));
405 5 (void)hv_stores(env, "psgi.version", newRV_noinc((SV *)newAV()));
406 5 (void)hv_stores(env, "HTTP_ORIGIN", newSVsv(*av_fetch((AV *)SvRV(origins), 0, 0)));
407 5 (void)hv_stores(env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD", newSVpvs("POST"));
408 5 (void)hv_stores(env, "HTTP_ACCESS_CONTROL_REQUEST_HEADERS",
409 newSVpvs("authorization, content-type, mcp-session-id, mcp-protocol-version, last-event-id"));
410 5 res = pmcp_call_sv(aTHX_ inner, &envrv, 1, G_SCALAR, &err);
411 5 ▶ if (!res) { SvREFCNT_dec(err); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!res) { SvREFCNT_dec(err); return; } 0 5 5
412 5 sv_2mortal(res);
413 5 ▶ if (pmcp_is_array(res) && av_len((AV *)SvRV(res)) >= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (pmcp_is_array(res) && av_len((AV *)SvRV(res)) >= 1) { 5 0 5
if (pmcp_is_array(res) && av_len((AV *)SvRV(res)) >= 1) { 5 0 5
414 5 SV **hp = av_fetch((AV *)SvRV(res), 1, 0);
415 5 ▶ AV *h = (hp && pmcp_is_array(*hp)) ? (AV *)SvRV(*hp) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *h = (hp && pmcp_is_array(*hp)) ? (AV *)SvRV(*hp) : NULL; 5 0 5
AV *h = (hp && pmcp_is_array(*hp)) ? (AV *)SvRV(*hp) : NULL; 5 0 5
416 5 SV *allow = NULL, *missing = sv_2mortal(newSVpvs(""));
417 5 int has_acao = 0, i;
418 5 ▶ SSize_t k, n = h ? av_len(h) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t k, n = h ? av_len(h) + 1 : 0; 5 0 5
419 31 ▶ for (k = 0; k + 1 < n; k += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k + 1 < n; k += 2) { 26 5 31
420 26 const char *name = SvPV_nolen(*av_fetch(h, k, 0));
421 26 ▶ if (strcasecmp(name, "Access-Control-Allow-Origin") == 0) has_acao = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(name, "Access-Control-Allow-Origin") == 0) has_acao = 1; 3 23 26
422 26 ▶ if (strcasecmp(name, "Access-Control-Allow-Headers") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(name, "Access-Control-Allow-Headers") == 0) 3 23 26
423 3 allow = *av_fetch(h, k + 1, 0);
424 }
425 5 ▶ if (!has_acao) return; /* cors does not cover this path */
Branch: 1 branch
BranchTrueFalseTotal
if (!has_acao) return; /* cors does not cover this path */ 2 3 5
426 18 ▶ for (i = 0; need[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; need[i]; i++) { 15 3 18
427 15 ▶ SV *lc = sv_2mortal(newSVsv(allow ? allow : &PL_sv_no));
Branch: 1 branch
BranchTrueFalseTotal
SV *lc = sv_2mortal(newSVsv(allow ? allow : &PL_sv_no)); 15 0 15
428 15 char *s = SvPV_nolen(lc);
429 STRLEN j;
430 875 ▶ for (j = 0; s[j]; j++) s[j] = toLOWER(s[j]);
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; s[j]; j++) s[j] = toLOWER(s[j]); 85 775 860
for (j = 0; s[j]; j++) s[j] = toLOWER(s[j]); 860 15 875
431 15 ▶ if (!instr(s, need[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!instr(s, need[i])) { 4 11 15
432 4 ▶ if (SvCUR(missing)) sv_catpvs(missing, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(missing)) sv_catpvs(missing, ", "); 3 1 4
433 4 sv_catpv(missing, need[i]);
434 }
435 }
436 3 ▶ if (SvCUR(missing))
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(missing)) 1 2 3
437 1 warn("%s: the application's cors `headers` does not allow %" SVf
438 " on %" SVf ", so a browser client cannot send them\n",
439 PMCP_WHO, SVfARG(missing), SVfARG(path));
440 }
441 }
442  
443 #endif /* PMCP_HTTP_H */
« include/pmcp/pmcp_decl.h ↑ Summary include/pmcp/pmcp_uri.h »