include/pmcp/pmcp_sse.h

filter: Statementstmt 87.6% 241 / 275 Branchbran 66.8% 167 / 250 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.7% 408 / 525 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« example/lib/Bookshop/Controller/Books.pm ↑ Summary include/pmcp/pmcp_call.h »
1 /* pmcp_sse.h - SSE on POST: a request answered with an event stream that
2 * carries progress and log notifications and ends with the response.
3 *
4 * Needs: pmcp_call.h, pmcp_http.h.
5 *
6 * A stream's state is one hash, shared by the transport and the call object
7 * so $c->mcp_progress and $c->mcp_log can write to it:
8 *
9 * w the Punk::Stream writer
10 * c the request's context, to await the writer's drain on
11 * n the last event id written
12 * level the minimum log level, a PMCP_LOG_* index
13 * logging 1 when log messages may be sent at all
14 * done 1 once the response is written, which stops the heartbeat
15 *
16 * Punk::Stream sends X-Accel-Buffering: no on every stream itself.
17 *
18 * JSON stays the default. On HTTP/1.1 a streamed answer ends the connection,
19 * so a client pays a reconnect for every streamed call (POD, 08).
20 */
21  
22 #ifndef PMCP_SSE_H
23 #define PMCP_SSE_H
24  
25 #define PMCP_HEARTBEAT_SECS 15
26  
27 /* Defined in pmcp_stdio.h: one line on the protocol stream (13). */
28 static void pmcp_stdio_write(pTHX_ SV *json);
29  
30 /* ---- calls streaming on this worker, for notifications/cancelled ------------- */
31  
32 static HV *PMCP_LIVE = NULL;
33  
34 /* The session (or "-" stateless), then "s:" or "i:" and the id, so the
35 * string "1" and the number 1 differ, and one session cannot reach into
36 * another's calls. Stateless, the id alone is the key, so any client could
37 * flag another's call: it only sets a flag the tool chooses to read. Mortal. */
38 68 static SV *pmcp_live_key(pTHX_ SV *sid, SV *id)
39 {
40 68 ▶ return sv_2mortal(newSVpvf("%" SVf "/%c:%" SVf,
Branch: 2 branches
BranchTrueFalseTotal
return sv_2mortal(newSVpvf("%" SVf "/%c:%" SVf, 8 60 68
return sv_2mortal(newSVpvf("%" SVf "/%c:%" SVf, 47 21 68
41 SVfARG(sid ? sid : sv_2mortal(newSVpvs("-"))),
42 SvPOK(id) ? 's' : 'i', SVfARG(id)));
43 }
44  
45 26 static void pmcp_live_add(pTHX_ SV *sid, SV *id, SV *call)
46 {
47 26 ▶ if (!PMCP_LIVE) PMCP_LIVE = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_LIVE) PMCP_LIVE = newHV(); 5 21 26
48 26 (void)hv_store_ent(PMCP_LIVE, pmcp_live_key(aTHX_ sid, id), newSVsv(call), 0);
49 26 }
50  
51 37 static void pmcp_live_del(pTHX_ SV *sid, SV *id)
52 {
53 37 ▶ if (PMCP_LIVE) (void)hv_delete_ent(PMCP_LIVE, pmcp_live_key(aTHX_ sid, id), G_DISCARD, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (PMCP_LIVE) (void)hv_delete_ent(PMCP_LIVE, pmcp_live_key(aTHX_ sid, id), G_DISCARD, 0); 33 4 37
54 37 }
55  
56 11 static void pmcp_live_cancel(pTHX_ SV *sid, SV *id)
57 {
58 11 ▶ HE *he = PMCP_LIVE ? hv_fetch_ent(PMCP_LIVE, pmcp_live_key(aTHX_ sid, id), 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = PMCP_LIVE ? hv_fetch_ent(PMCP_LIVE, pmcp_live_key(aTHX_ sid, id), 0, 0) : NULL; 9 2 11
59 11 ▶ if (he && pmcp_is_hash(HeVAL(he)))
Branch: 2 branches
BranchTrueFalseTotal
if (he && pmcp_is_hash(HeVAL(he))) 3 8 11
if (he && pmcp_is_hash(HeVAL(he))) 3 0 3
60 3 (void)hv_stores((HV *)SvRV(HeVAL(he)), "cancelled", newSViv(1));
61 11 }
62  
63 /* A call object made while a stream is open learns where to write, and is
64 * found by notifications/cancelled while it runs. */
65 174 static void pmcp_call_attach(pTHX_ pmcp_req *r, SV *call)
66 {
67 174 ▶ if (!pmcp_is_hash(call)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(call)) return; 0 174 174
68 174 ▶ if (r->session)
Branch: 1 branch
BranchTrueFalseTotal
if (r->session) 24 150 174
69 24 (void)hv_stores((HV *)SvRV(call), "session", newSVsv(r->session));
70 174 ▶ if (!r->stream) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r->stream) return; 148 26 174
71 26 (void)hv_stores((HV *)SvRV(call), "_stream", newSVsv(r->stream));
72 26 ▶ if (r->id) pmcp_live_add(aTHX_ r->session, r->id, call);
Branch: 1 branch
BranchTrueFalseTotal
if (r->id) pmcp_live_add(aTHX_ r->session, r->id, call); 26 0 26
73 }
74  
75 /* ---- writing an event ------------------------------------------------------------ */
76  
77 /* One event: `event: message`, an id, the JSON on one data line, a blank line.
78 * Then the writer's drain is awaited, so bytes never pile up behind a slow
79 * client. Returns whether the stream is still open. */
80 45 static int pmcp_sse_send(pTHX_ HV *st, SV *json)
81 {
82 45 SV *w = pmcp_hget(aTHX_ st, "w"), *c = pmcp_hget(aTHX_ st, "c");
83 45 SV *nsv = pmcp_hget(aTHX_ st, "n");
84 45 ▶ IV n = (nsv ? SvIV(nsv) : 0) + 1;
Branch: 1 branch
BranchTrueFalseTotal
IV n = (nsv ? SvIV(nsv) : 0) + 1; 45 0 45
85 45 SV *frame, *err = NULL, *r, *drain;
86 STRLEN l;
87 const char *p;
88  
89 /* over stdio a message is a line, not an event */
90 45 ▶ if (pmcp_hget(aTHX_ st, "stdio")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_hget(aTHX_ st, "stdio")) { 3 42 45
91 3 pmcp_stdio_write(aTHX_ json);
92 3 return 1;
93 }
94 42 ▶ if (!w || !c) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!w || !c) return 0; 42 0 42
if (!w || !c) return 0; 0 42 42
95 42 (void)hv_stores(st, "n", newSViv(n));
96 42 p = SvPV_const(json, l);
97 42 frame = sv_2mortal(newSVpvf("event: message\nid: %" IVdf "\ndata: ", n));
98 42 sv_catpvn(frame, p, l);
99 42 sv_catpvs(frame, "\n\n");
100 42 r = pmcp_call_method(aTHX_ w, "write", &frame, 1, G_SCALAR, &err);
101 42 ▶ if (!r) { SvREFCNT_dec(err); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { SvREFCNT_dec(err); return 0; } 0 42 42
102 42 SvREFCNT_dec(r);
103 42 drain = pmcp_call_method(aTHX_ w, "drain", NULL, 0, G_SCALAR, &err);
104 42 ▶ if (!drain) { SvREFCNT_dec(err); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!drain) { SvREFCNT_dec(err); return 0; } 0 42 42
105 42 sv_2mortal(drain);
106 42 r = pmcp_call_method(aTHX_ c, "await", &drain, 1, G_SCALAR, &err);
107 42 ▶ if (!r) { SvREFCNT_dec(err); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { SvREFCNT_dec(err); return 0; } 0 42 42
108 {
109 42 int ok = SvTRUE(r);
110 42 SvREFCNT_dec(r);
111 42 return ok;
112 }
113 }
114  
115 /* A notification, {"jsonrpc":"2.0","method":M,"params":P}, as an event. Takes
116 * ownership of params. Returns 1 when written. */
117 22 static int pmcp_sse_notify(pTHX_ HV *st, const char *method, SV *params)
118 {
119 22 HV *m = newHV();
120 22 SV *rv, *bytes, *err = NULL;
121 int ok;
122 22 (void)hv_stores(m, "jsonrpc", newSVpvs("2.0"));
123 22 (void)hv_stores(m, "method", newSVpv(method, 0));
124 22 (void)hv_stores(m, "params", params);
125 22 rv = sv_2mortal(newRV_noinc((SV *)m));
126 22 bytes = pmcp_json_trapped(aTHX_ rv, &err);
127 22 ▶ if (!bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { 0 22 22
128 0 sv_2mortal(err);
129 0 warn("%s: a %s notification is not JSON, not sent: %" SVf, PMCP_WHO,
130 method, SVfARG(err));
131 0 return 0;
132 }
133 22 ok = pmcp_sse_send(aTHX_ st, sv_2mortal(bytes));
134 22 return ok;
135 }
136  
137 /* ---- the heartbeat ------------------------------------------------------------------ */
138  
139 /* A comment line every PMCP_HEARTBEAT_SECS while the call runs, so a quiet
140 * call does not meet an idle timeout in a client or a proxy. Only on a live
141 * Hyperman loop (psgix.loop): off one, $c->timer sleeps where it is made and
142 * would hold the call up instead. $Punk::Plugin::MCP::HEARTBEAT overrides the
143 * interval, for the live tests. cap = [state] */
144 XS_INTERNAL(pmcp_heartbeat_cb);
145 0 static void pmcp_heartbeat_arm(pTHX_ SV *strv)
146 {
147 0 HV *st = (HV *)SvRV(strv);
148 0 SV *c = pmcp_hget(aTHX_ st, "c"), *secs, *timer, *err = NULL, *cb, *r;
149 0 SV *knob = get_sv("Punk::Plugin::MCP::HEARTBEAT", 0);
150 AV *cap;
151 0 ▶ if (pmcp_hget(aTHX_ st, "done") || !c) return;
Branch: 2 branches
BranchTrueFalseTotal
if (pmcp_hget(aTHX_ st, "done") || !c) return; 0 0 0
if (pmcp_hget(aTHX_ st, "done") || !c) return; 0 0 0
152 0 ▶ secs = sv_2mortal(newSVnv(knob && SvOK(knob) && SvNV(knob) > 0
Branch: 3 branches
BranchTrueFalseTotal
secs = sv_2mortal(newSVnv(knob && SvOK(knob) && SvNV(knob) > 0 0 0 0
secs = sv_2mortal(newSVnv(knob && SvOK(knob) && SvNV(knob) > 0 0 0 0
secs = sv_2mortal(newSVnv(knob && SvOK(knob) && SvNV(knob) > 0 0 0 0
153 ? SvNV(knob) : PMCP_HEARTBEAT_SECS));
154 0 timer = pmcp_call_method(aTHX_ c, "timer", &secs, 1, G_SCALAR, &err);
155 0 ▶ if (!timer) { SvREFCNT_dec(err); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!timer) { SvREFCNT_dec(err); return; } 0 0 0
156 0 sv_2mortal(timer);
157 /* A timer settled already is one that slept where it was made: there is no
158 * loop behind this request after all, and re-arming from on_done would
159 * never return. */
160 {
161 0 SV *ready = pmcp_call_method(aTHX_ timer, "is_ready", NULL, 0, G_SCALAR, NULL);
162 0 ▶ int now = ready && SvTRUE(ready);
Branch: 2 branches
BranchTrueFalseTotal
int now = ready && SvTRUE(ready); 0 0 0
int now = ready && SvTRUE(ready); 0 0 0
163 0 ▶ if (ready) SvREFCNT_dec(ready);
Branch: 1 branch
BranchTrueFalseTotal
if (ready) SvREFCNT_dec(ready); 0 0 0
164 0 ▶ if (now) return;
Branch: 1 branch
BranchTrueFalseTotal
if (now) return; 0 0 0
165 }
166 0 cap = newAV();
167 0 av_push(cap, newSVsv(strv));
168 0 cb = sv_2mortal(pmcp_closure(aTHX_ pmcp_heartbeat_cb, cap));
169 0 r = pmcp_call_method(aTHX_ timer, "on_done", &cb, 1, G_SCALAR, &err);
170 0 ▶ if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err); 0 0 0
171 }
172  
173 0 XS_INTERNAL(pmcp_heartbeat_cb)
174 {
175 0 dXSARGS;
176 0 SV *strv = pmcp_cap(aTHX_ cv, 0);
177 PERL_UNUSED_VAR(items);
178 0 ▶ if (strv && pmcp_is_hash(strv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (strv && pmcp_is_hash(strv)) { 0 0 0
if (strv && pmcp_is_hash(strv)) { 0 0 0
179 0 HV *st = (HV *)SvRV(strv);
180 0 SV *w = pmcp_hget(aTHX_ st, "w");
181 0 ▶ if (!pmcp_hget(aTHX_ st, "done") && w) {
Branch: 2 branches
BranchTrueFalseTotal
if (!pmcp_hget(aTHX_ st, "done") && w) { 0 0 0
if (!pmcp_hget(aTHX_ st, "done") && w) { 0 0 0
182 0 SV *ping = sv_2mortal(newSVpvs(": ping\n\n")), *err = NULL;
183 0 SV *r = pmcp_call_method(aTHX_ w, "write", &ping, 1, G_SCALAR, &err);
184 0 ▶ if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err); 0 0 0
185 0 pmcp_heartbeat_arm(aTHX_ strv);
186 }
187 }
188 0 XSRETURN_EMPTY;
189 }
190  
191 /* ---- the stream ------------------------------------------------------------------------- */
192  
193 /* The dispatch, run under G_EVAL from the stream callback, so a croak becomes
194 * an error event and a clean close rather than a body cut short.
195 * cap = [server, doc, version, state] */
196 23 XS_INTERNAL(pmcp_stream_work)
197 {
198 23 dXSARGS;
199 23 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 23 0 23
200 23 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 0));
201 23 SV *doc = pmcp_cap(aTHX_ cv, 1);
202 23 enum pmcp_ver v = (enum pmcp_ver)SvIV(pmcp_cap(aTHX_ cv, 2));
203 23 SV *strv = pmcp_cap(aTHX_ cv, 3);
204 23 SV *sid = pmcp_cap(aTHX_ cv, 4);
205 pmcp_req r;
206 23 int status = 0;
207 SV *reply;
208 23 SV *id = NULL;
209  
210 23 ▶ if (sid && !SvOK(sid)) sid = NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (sid && !SvOK(sid)) sid = NULL; 23 0 23
if (sid && !SvOK(sid)) sid = NULL; 13 10 23
211  
212 /* t/32 proves a croak in here becomes an error event and a clean close */
213 {
214 23 SV *k = get_sv("Punk::Plugin::MCP::_TEST_CROAK", 0);
215 23 ▶ if (k && SvTRUE(k)) croak("%s: a croak inside the stream, for t/32", PMCP_WHO);
Branch: 2 branches
BranchTrueFalseTotal
if (k && SvTRUE(k)) croak("%s: a croak inside the stream, for t/32", PMCP_WHO); 8 15 23
if (k && SvTRUE(k)) croak("%s: a croak inside the stream, for t/32", PMCP_WHO); 1 7 8
216 }
217 22 pmcp_req_init(&r, v, 0);
218 22 r.c = c;
219 22 r.stream = strv;
220 22 r.session = sid;
221 22 r.in_session = sid ? 1 : 0;
222 {
223 pmcp_msg m;
224 22 pmcp_rpc_classify(aTHX_ doc, &m);
225 22 ▶ if (m.id) id = sv_2mortal(newSVsv(m.id));
Branch: 1 branch
BranchTrueFalseTotal
if (m.id) id = sv_2mortal(newSVsv(m.id)); 22 0 22
226 }
227 22 reply = pmcp_dispatch_doc(aTHX_ &ps->srv, &r, doc, &status);
228 22 ▶ if (id) pmcp_live_del(aTHX_ sid, id);
Branch: 1 branch
BranchTrueFalseTotal
if (id) pmcp_live_del(aTHX_ sid, id); 22 0 22
229 22 ▶ if (r.level_set >= 0) pmcp_level_set(aTHX_ c, ps, sid, r.level_set);
Branch: 1 branch
BranchTrueFalseTotal
if (r.level_set >= 0) pmcp_level_set(aTHX_ c, ps, sid, r.level_set); 1 21 22
230 22 pmcp_req_free(aTHX_ &r);
231 22 ▶ if (reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply) { 22 0 22
232 22 sv_2mortal(reply);
233 22 (void)pmcp_sse_send(aTHX_ (HV *)SvRV(strv), reply);
234 }
235 22 XSRETURN_EMPTY;
236 }
237  
238 /* Punk::Stream's callback: ($c, $w). cap = [server, doc, version, sid, level] */
239 23 XS_INTERNAL(pmcp_stream_cb)
240 {
241 23 dXSARGS;
242 23 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 23 0 23
243 23 ▶ SV *w = items > 1 ? ST(1) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *w = items > 1 ? ST(1) : &PL_sv_undef; 23 0 23
244 23 SV *server = pmcp_cap(aTHX_ cv, 0), *doc = pmcp_cap(aTHX_ cv, 1);
245 23 SV *ver = pmcp_cap(aTHX_ cv, 2);
246 23 SV *sid = pmcp_cap(aTHX_ cv, 3), *lvl = pmcp_cap(aTHX_ cv, 4);
247 23 pmcp_server *ps = pmcp_server_of(aTHX_ server);
248 23 HV *st = newHV();
249 23 SV *strv = sv_2mortal(newRV_noinc((SV *)st));
250 AV *cap;
251 23 SV *work, *err = NULL, *r;
252  
253 23 (void)hv_stores(st, "w", newSVsv(w));
254 23 (void)hv_stores(st, "c", newSVsv(c));
255 23 (void)hv_stores(st, "n", newSViv(0));
256 23 ▶ (void)hv_stores(st, "level", newSViv(lvl && SvOK(lvl) ? SvIV(lvl)
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(st, "level", newSViv(lvl && SvOK(lvl) ? SvIV(lvl) 23 0 23
(void)hv_stores(st, "level", newSViv(lvl && SvOK(lvl) ? SvIV(lvl) 10 13 23
(void)hv_stores(st, "level", newSViv(lvl && SvOK(lvl) ? SvIV(lvl) 13 0 13
257 : ps ? ps->srv.log_level : PMCP_LOG_DEFAULT));
258 23 ▶ (void)hv_stores(st, "logging", newSViv(ps && ps->srv.logging && ps->srv.can_notify));
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(st, "logging", newSViv(ps && ps->srv.logging && ps->srv.can_notify)); 23 0 23
(void)hv_stores(st, "logging", newSViv(ps && ps->srv.logging && ps->srv.can_notify)); 11 12 23
(void)hv_stores(st, "logging", newSViv(ps && ps->srv.logging && ps->srv.can_notify)); 10 1 11
259 {
260 23 SV *envsv = sv_2mortal(pmcp_call_method(aTHX_ c, "env", NULL, 0, G_SCALAR, NULL));
261 23 ▶ if (pmcp_is_hash(envsv) && pmcp_hget(aTHX_ (HV *)SvRV(envsv), "psgix.loop"))
Branch: 2 branches
BranchTrueFalseTotal
if (pmcp_is_hash(envsv) && pmcp_hget(aTHX_ (HV *)SvRV(envsv), "psgix.loop")) 23 0 23
if (pmcp_is_hash(envsv) && pmcp_hget(aTHX_ (HV *)SvRV(envsv), "psgix.loop")) 0 23 23
262 0 pmcp_heartbeat_arm(aTHX_ strv);
263 }
264  
265 23 cap = newAV();
266 23 av_push(cap, newSVsv(server));
267 23 av_push(cap, newSVsv(doc));
268 23 av_push(cap, newSVsv(ver));
269 23 av_push(cap, newSVsv(strv));
270 23 ▶ av_push(cap, sid ? newSVsv(sid) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, sid ? newSVsv(sid) : newSV(0)); 23 0 23
271 23 work = sv_2mortal(pmcp_closure(aTHX_ pmcp_stream_work, cap));
272 23 r = pmcp_call_sv(aTHX_ work, &c, 1, G_SCALAR, &err);
273 23 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 22 1 23
274 else {
275 /* the dispatch itself croaked: say so as the answer, then end cleanly */
276 pmcp_msg m;
277 SV *e;
278 1 pmcp_rpc_classify(aTHX_ doc, &m);
279 1 ▶ if (m.id) pmcp_live_del(aTHX_ (sid && SvOK(sid)) ? sid : NULL, m.id);
Branch: 3 branches
BranchTrueFalseTotal
if (m.id) pmcp_live_del(aTHX_ (sid && SvOK(sid)) ? sid : NULL, m.id); 1 0 1
if (m.id) pmcp_live_del(aTHX_ (sid && SvOK(sid)) ? sid : NULL, m.id); 1 0 1
if (m.id) pmcp_live_del(aTHX_ (sid && SvOK(sid)) ? sid : NULL, m.id); 0 1 1
280 1 e = sv_2mortal(pmcp_rpc_error(aTHX_ m.id, PMCP_INTERNAL, "internal error", NULL));
281 1 (void)pmcp_sse_send(aTHX_ st, e);
282 1 SvREFCNT_dec(err);
283 }
284 23 (void)hv_stores(st, "done", newSViv(1));
285 23 XSRETURN_EMPTY;
286 }
287  
288 /* Does the client accept an event stream? Only if it says so: no Accept header
289 * is a client that never asked for one. */
290 160 static int pmcp_wants_sse(pTHX_ HV *env)
291 {
292 160 SV *a = pmcp_env_get(aTHX_ env, "HTTP_ACCEPT");
293 STRLEN l;
294 const char *p;
295 160 ▶ if (!a) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return 0; 50 110 160
296 110 p = SvPV_const(a, l);
297 {
298 110 SV *lc = sv_2mortal(newSVpvn(p, l));
299 110 char *s = SvPVX(lc);
300 STRLEN i;
301 3379 ▶ for (i = 0; i < l; i++) s[i] = toLOWER(s[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l; i++) s[i] = toLOWER(s[i]); 15 3254 3269
for (i = 0; i < l; i++) s[i] = toLOWER(s[i]); 3269 110 3379
302 110 return instr(s, "text/event-stream") != NULL;
303 }
304 }
305  
306 /* Stream this request? After the body is parsed and before anything runs:
307 * the tool has to run inside the stream's callback, so the choice comes first. */
308 238 static int pmcp_should_stream(pTHX_ pmcp_server *ps, HV *env, pmcp_msg *m)
309 {
310 238 SV *mode = pmcp_hget(aTHX_ ps->opts, "stream");
311 476 ▶ const char *mp = mode ? SvPV_nolen(mode) : "auto";
Branch: 1 branch
BranchTrueFalseTotal
const char *mp = mode ? SvPV_nolen(mode) : "auto"; 238 0 238
312 238 ▶ if (m->kind != PMCP_K_REQUEST || strEQ(mp, "json")) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (m->kind != PMCP_K_REQUEST || strEQ(mp, "json")) return 0; 225 13 238
if (m->kind != PMCP_K_REQUEST || strEQ(mp, "json")) return 0; 1 224 225
313 /* initialize answers with the session id in a header: never a stream */
314 224 ▶ if (m->mlen == 10 && memEQ(m->method, "initialize", 10)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (m->mlen == 10 && memEQ(m->method, "initialize", 10)) return 0; 169 55 224
if (m->mlen == 10 && memEQ(m->method, "initialize", 10)) return 0; 72 97 169
315 152 ▶ if (!pmcp_wants_sse(aTHX_ env)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_wants_sse(aTHX_ env)) return 0; 73 79 152
316 79 ▶ if (strEQ(mp, "always")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(mp, "always")) return 1; 1 78 79
317 78 ▶ if (m->params) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->params) { 64 14 78
318 64 SV **meta = hv_fetchs(m->params, "_meta", 0);
319 64 ▶ if (meta && pmcp_is_hash(*meta)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meta && pmcp_is_hash(*meta)) { 5 59 64
if (meta && pmcp_is_hash(*meta)) { 5 0 5
320 5 SV **pt = hv_fetchs((HV *)SvRV(*meta), "progressToken", 0);
321 5 ▶ if (pt && pmcp_id_ok(*pt)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (pt && pmcp_id_ok(*pt)) return 1; 5 0 5
if (pt && pmcp_id_ok(*pt)) return 1; 5 0 5
322 }
323 }
324 73 ▶ if (m->mlen == 10 && memEQ(m->method, "tools/call", 10) && ps->srv.tools && m->params) {
Branch: 4 branches
BranchTrueFalseTotal
if (m->mlen == 10 && memEQ(m->method, "tools/call", 10) && ps->srv.tools && m->params) { 45 28 73
if (m->mlen == 10 && memEQ(m->method, "tools/call", 10) && ps->srv.tools && m->params) { 39 6 45
if (m->mlen == 10 && memEQ(m->method, "tools/call", 10) && ps->srv.tools && m->params) { 39 0 39
if (m->mlen == 10 && memEQ(m->method, "tools/call", 10) && ps->srv.tools && m->params) { 39 0 39
325 39 SV **nv = hv_fetchs(m->params, "name", 0);
326 39 ▶ pmcp_tool *t = (nv && pmcp_is_str(*nv))
Branch: 1 branch
BranchTrueFalseTotal
pmcp_tool *t = (nv && pmcp_is_str(*nv)) 39 0 39
327 78 ▶ ? pmcp_tool_find(aTHX_ (pmcp_tools *)ps->srv.tools, *nv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? pmcp_tool_find(aTHX_ (pmcp_tools *)ps->srv.tools, *nv) : NULL; 39 0 39
328 39 ▶ if (t && t->stream) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (t && t->stream) return 1; 38 1 39
if (t && t->stream) return 1; 17 21 38
329 }
330 56 return 0;
331 }
332  
333 /* The streamed answer: what $c->stream returns, for the handler to return. +1 */
334 23 static SV *pmcp_stream_answer(pTHX_ SV *c, SV *server, SV *doc, enum pmcp_ver v,
335 int cors, SV *sid, int level)
336 {
337 23 AV *cap = newAV(), *hdr = newAV();
338 23 HV *opts = newHV();
339 SV *argv[3], *res;
340 23 av_push(cap, newSVsv(server));
341 23 av_push(cap, newSVsv(doc));
342 23 av_push(cap, newSViv((IV)v));
343 23 ▶ av_push(cap, sid ? newSVsv(sid) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, sid ? newSVsv(sid) : newSV(0)); 10 13 23
344 23 ▶ av_push(cap, level >= 0 ? newSViv(level) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, level >= 0 ? newSViv(level) : newSV(0)); 10 13 23
345 23 av_push(hdr, newSVpvs("Cache-Control"));
346 23 av_push(hdr, newSVpvs("no-store"));
347 23 ▶ if (cors) {
Branch: 1 branch
BranchTrueFalseTotal
if (cors) { 1 22 23
348 1 av_push(hdr, newSVpvs("Access-Control-Expose-Headers"));
349 1 av_push(hdr, newSVpvs(PMCP_EXPOSE));
350 }
351 23 (void)hv_stores(opts, "headers", newRV_noinc((SV *)hdr));
352 23 argv[0] = sv_2mortal(newSVpvs("text/event-stream"));
353 23 argv[1] = sv_2mortal(newRV_noinc((SV *)opts));
354 23 argv[2] = sv_2mortal(pmcp_closure(aTHX_ pmcp_stream_cb, cap));
355 23 res = pmcp_call_method(aTHX_ c, "stream", argv, 3, G_SCALAR, NULL);
356 23 return res;
357 }
358  
359 /* ---- $c->mcp_progress and $c->mcp_log ---------------------------------------------------- */
360  
361 /* The call object and its stream state, or NULL. Both borrowed. */
362 71 static HV *pmcp_call_stream(pTHX_ SV *c, HV **call)
363 {
364 71 SV *callsv = pmcp_stash_get(aTHX_ c, PMCP_STASH_CALL);
365 SV *st;
366 71 *call = NULL;
367 71 ▶ if (!callsv || !pmcp_is_hash(callsv)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!callsv || !pmcp_is_hash(callsv)) return NULL; 65 6 71
if (!callsv || !pmcp_is_hash(callsv)) return NULL; 0 65 65
368 65 *call = (HV *)SvRV(callsv);
369 65 st = pmcp_hget(aTHX_ *call, "_stream");
370 65 ▶ return pmcp_is_hash(st) ? (HV *)SvRV(st) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return pmcp_is_hash(st) ? (HV *)SvRV(st) : NULL; 51 14 65
371 }
372  
373 /* A number as JSON writes it: an integer when it is one. +1 */
374 15 static SV *pmcp_num(pTHX_ SV *v)
375 {
376 15 NV n = SvNV(v);
377 15 ▶ if (n == (NV)(IV)n) return newSViv((IV)n);
Branch: 1 branch
BranchTrueFalseTotal
if (n == (NV)(IV)n) return newSViv((IV)n); 14 1 15
378 1 return newSVnv(n);
379 }
380  
381 /* $c->mcp_progress($done, $total?, $message?): 1 when sent, 0 when there is
382 * no stream or no progress token to send it against. */
383 26 XS_INTERNAL(pmcp_helper_progress)
384 {
385 26 dXSARGS;
386 HV *call, *st, *p;
387 SV *token, *last, *done;
388 26 int ok = 0;
389  
390 26 ▶ if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)) || !looks_like_number(ST(1)))
Branch: 4 branches
BranchTrueFalseTotal
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)) || !looks_like_number(ST(1))) 26 0 26
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)) || !looks_like_number(ST(1))) 26 0 26
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)) || !looks_like_number(ST(1))) 26 0 26
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)) || !looks_like_number(ST(1))) 1 25 26
391 1 croak("%s: mcp_progress takes a number, how far along", PMCP_WHO);
392 25 done = ST(1);
393 25 st = pmcp_call_stream(aTHX_ ST(0), &call);
394 25 ▶ token = call ? pmcp_hget(aTHX_ call, "progress_token") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
token = call ? pmcp_hget(aTHX_ call, "progress_token") : NULL; 25 0 25
395 25 ▶ if (st && token) {
Branch: 2 branches
BranchTrueFalseTotal
if (st && token) { 17 8 25
if (st && token) { 10 7 17
396 10 last = pmcp_hget(aTHX_ call, "_progress");
397 10 ▶ if (last && SvNV(done) <= SvNV(last)) {
Branch: 2 branches
BranchTrueFalseTotal
if (last && SvNV(done) <= SvNV(last)) { 5 5 10
if (last && SvNV(done) <= SvNV(last)) { 2 3 5
398 2 warn("%s: progress %" NVgf " is not above %" NVgf ", not sent\n",
399 PMCP_WHO, SvNV(done), SvNV(last));
400 }
401 else {
402 8 (void)hv_stores(call, "_progress", newSVnv(SvNV(done)));
403 8 p = newHV();
404 8 (void)hv_stores(p, "progressToken", newSVsv(token));
405 8 (void)hv_stores(p, "progress", pmcp_num(aTHX_ done));
406 8 ▶ if (items > 2 && SvOK(ST(2)) && looks_like_number(ST(2)))
Branch: 3 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2)) && looks_like_number(ST(2))) 7 1 8
if (items > 2 && SvOK(ST(2)) && looks_like_number(ST(2))) 7 0 7
if (items > 2 && SvOK(ST(2)) && looks_like_number(ST(2))) 7 0 7
407 7 (void)hv_stores(p, "total", pmcp_num(aTHX_ ST(2)));
408 8 ▶ if (items > 3 && SvOK(ST(3)))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 3 && SvOK(ST(3))) 3 5 8
if (items > 3 && SvOK(ST(3))) 3 0 3
409 3 (void)hv_stores(p, "message", pmcp_str(aTHX_ ST(3)));
410 8 ok = pmcp_sse_notify(aTHX_ st, "notifications/progress",
411 newRV_noinc((SV *)p));
412 }
413 }
414 25 SP = PL_stack_base + ax - 1;
415 25 ST(0) = sv_2mortal(newSViv(ok));
416 25 XSRETURN(1);
417 }
418  
419 /* Punk::Logger's level for each of MCP's eight, in PMCP_LOG_NAME order. */
420 static const char *const PMCP_LOG_PUNK[] = {
421 "debug", "info", "info", "warn", "error", "fatal", "fatal", "fatal"
422 };
423  
424 /* The record mcp_log hands the request logger: a plain hash's keys as fields,
425 * a plain array under data, anything else as the message, and the logger name
426 * as a field unless the hash has one. The caller's hash is copied, not
427 * touched. +1 */
428 46 static SV *pmcp_log_record(pTHX_ SV *data, SV *logger)
429 {
430 46 HV *r = newHV();
431  
432 84 ▶ if (SvROK(data) && !sv_isobject(data) && SvTYPE(SvRV(data)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(data) && !sv_isobject(data) && SvTYPE(SvRV(data)) == SVt_PVHV) { 39 7 46
if (SvROK(data) && !sv_isobject(data) && SvTYPE(SvRV(data)) == SVt_PVHV) { 39 0 39
if (SvROK(data) && !sv_isobject(data) && SvTYPE(SvRV(data)) == SVt_PVHV) { 38 1 39
433 38 HV *from = (HV *)SvRV(data);
434 HE *he;
435 38 hv_iterinit(from);
436 79 ▶ while ((he = hv_iternext(from)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(from))) 41 38 79
437 41 (void)hv_store_ent(r, hv_iterkeysv(he), newSVsv(hv_iterval(from, he)), 0);
438 }
439 8 ▶ else if (SvROK(data) && !sv_isobject(data))
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(data) && !sv_isobject(data)) 1 7 8
else if (SvROK(data) && !sv_isobject(data)) 1 0 1
440 1 (void)hv_stores(r, "data", newSVsv(data));
441 else
442 7 (void)hv_stores(r, "message", newSVsv(data));
443 46 ▶ if (logger && SvOK(logger) && !hv_exists(r, "logger", 6))
Branch: 3 branches
BranchTrueFalseTotal
if (logger && SvOK(logger) && !hv_exists(r, "logger", 6)) 37 9 46
if (logger && SvOK(logger) && !hv_exists(r, "logger", 6)) 37 0 37
if (logger && SvOK(logger) && !hv_exists(r, "logger", 6)) 36 1 37
444 36 (void)hv_stores(r, "logger", newSVsv(logger));
445 46 return newRV_noinc((SV *)r);
446 }
447  
448 /* $c->log->$level($record), whatever the client asked for and whether or not
449 * there is a client to send to. */
450 46 static void pmcp_log_app(pTHX_ SV *c, int level, SV *data, SV *logger)
451 {
452 46 SV *log = sv_2mortal(pmcp_call_method(aTHX_ c, "log", NULL, 0, G_SCALAR, NULL));
453 SV *rec;
454  
455 46 ▶ if (!SvROK(log)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(log)) return; 0 46 46
456 46 rec = sv_2mortal(pmcp_log_record(aTHX_ data, logger));
457 46 SvREFCNT_dec(pmcp_call_method(aTHX_ log, PMCP_LOG_PUNK[level], &rec, 1, G_SCALAR, NULL));
458 }
459  
460 /* $c->mcp_log($level, $data, $logger?): writes the request logger, then sends
461 * to the client. 1 when sent, 0 below the client's level or with nowhere to
462 * send it. An unknown level croaks: it is a bug in the caller. */
463 47 XS_INTERNAL(pmcp_helper_log)
464 {
465 47 dXSARGS;
466 HV *call, *st, *p;
467 SV *logging;
468 47 int level, ok = 0;
469 STRLEN l;
470 const char *lp;
471  
472 47 ▶ if (items < 3) croak("%s: mcp_log takes a level and the data", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) croak("%s: mcp_log takes a level and the data", PMCP_WHO); 0 47 47
473 94 ▶ lp = SvOK(ST(1)) ? SvPV_const(ST(1), l) : "";
Branch: 1 branch
BranchTrueFalseTotal
lp = SvOK(ST(1)) ? SvPV_const(ST(1), l) : ""; 47 0 47
474 47 ▶ if (!SvOK(ST(1))) l = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(1))) l = 0; 0 47 47
475 47 level = pmcp_log_level_of(lp, l);
476 47 ▶ if (level < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (level < 0) 1 46 47
477 1 croak("%s: mcp_log level '%.*s' is not one of debug, info, notice, "
478 "warning, error, critical, alert, emergency", PMCP_WHO, (int)l, lp);
479 46 ▶ pmcp_log_app(aTHX_ ST(0), level, ST(2), items > 3 ? ST(3) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
pmcp_log_app(aTHX_ ST(0), level, ST(2), items > 3 ? ST(3) : NULL); 37 9 46
480 46 st = pmcp_call_stream(aTHX_ ST(0), &call);
481 /* In a variable: before 5.16 SvTRUE does not parenthesise its argument,
482 * so a ?: passed to it parsed as (!a) ? a : no and was false whenever a
483 * was set. */
484 46 ▶ logging = st ? pmcp_hget(aTHX_ st, "logging") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
logging = st ? pmcp_hget(aTHX_ st, "logging") : NULL; 34 12 46
485 46 ▶ if (logging && SvTRUE(logging)) {
Branch: 2 branches
BranchTrueFalseTotal
if (logging && SvTRUE(logging)) { 34 12 46
if (logging && SvTRUE(logging)) { 24 10 34
486 24 SV *min = pmcp_hget(aTHX_ st, "level");
487 24 ▶ if (level >= (min ? SvIV(min) : PMCP_LOG_DEFAULT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (level >= (min ? SvIV(min) : PMCP_LOG_DEFAULT)) { 24 0 24
if (level >= (min ? SvIV(min) : PMCP_LOG_DEFAULT)) { 14 10 24
488 14 p = newHV();
489 14 (void)hv_stores(p, "level", newSVpv(PMCP_LOG_NAME[level], 0));
490 14 (void)hv_stores(p, "data", newSVsv(ST(2)));
491 14 ▶ if (items > 3 && SvOK(ST(3)))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 3 && SvOK(ST(3))) 9 5 14
if (items > 3 && SvOK(ST(3))) 9 0 9
492 9 (void)hv_stores(p, "logger", pmcp_str(aTHX_ ST(3)));
493 14 ok = pmcp_sse_notify(aTHX_ st, "notifications/message", newRV_noinc((SV *)p));
494 }
495 }
496 46 SP = PL_stack_base + ax - 1;
497 46 ST(0) = sv_2mortal(newSViv(ok));
498 46 XSRETURN(1);
499 }
500  
501 #endif /* PMCP_SSE_H */
« example/lib/Bookshop/Controller/Books.pm ↑ Summary include/pmcp/pmcp_call.h »