include/pmcp/pmcp_sess.h

filter: Statementstmt 98.0% 247 / 252 Branchbran 76.0% 143 / 188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.6% 390 / 440 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_reg.h ↑ Summary include/pmcp/pmcp_state.h »
1 /* pmcp_sess.h - Mcp-Session-Id sessions, kept in a shared Punk::Cache store.
2 *
3 * Needs: pmcp_http.h, pmcp_rand.h.
4 *
5 * Off by default (`session => 0`, the owner's ruling of 23 Oct). With
6 * `session => NAME` the store NAME must be declared with `cache` and shared
7 * across the worker pool, or boot croaks naming it: a session written on one
8 * worker and missing on the next is a 404 at random.
9 *
10 * A record is JSON under pmcp:s:<id>:
11 *
12 * v the negotiated protocol version
13 * client clientInfo, as initialize sent it
14 * caps the client's capabilities
15 * level the minimum log level, a PMCP_LOG_* index
16 * subs subscribed resource URIs (10)
17 * principal a digest of the credential that made it
18 * created, seen, exp epoch seconds
19 *
20 * Reads go to the store's backend, past any per-worker memory tier, as
21 * Punk's own session store and Punk::Plugin::Idempotency do: a tier that has
22 * not seen a write answers "no session", which is a 404 for a live one. Writes
23 * go through the store, which updates the backend and invalidates the tiers.
24 *
25 * The record's own `exp` is the expiry that counts. The store's TTL is set to
26 * match and only backs it up.
27 */
28  
29 #ifndef PMCP_SESS_H
30 #define PMCP_SESS_H
31  
32 #define PMCP_SLIDE_SECS 60 /* a read moves expiry forward at most this often */
33 #define PMCP_ADDR_PER_MIN 30 /* initialize calls per address per minute */
34  
35 /* Now, in seconds. $Punk::Plugin::MCP::_NOW stands in for the clock in t/34. */
36 285 static NV pmcp_now(pTHX)
37 {
38 285 SV *k = get_sv("Punk::Plugin::MCP::_NOW", 0);
39 285 ▶ return (k && SvOK(k)) ? SvNV(k) : (NV)time(NULL);
Branch: 2 branches
BranchTrueFalseTotal
return (k && SvOK(k)) ? SvNV(k) : (NV)time(NULL); 144 141 285
return (k && SvOK(k)) ? SvNV(k) : (NV)time(NULL); 97 47 144
40 }
41  
42 502 static int pmcp_sessions_on(pTHX_ HV *opts)
43 {
44 502 SV *s = pmcp_hget(aTHX_ opts, "session");
45 502 ▶ return s && SvTRUE(s);
Branch: 2 branches
BranchTrueFalseTotal
return s && SvTRUE(s); 502 0 502
return s && SvTRUE(s); 207 295 502
46 }
47  
48 /* The store, through a context, or NULL with sessions off. Mortal. */
49 287 static SV *pmcp_sess_store(pTHX_ SV *c, HV *opts)
50 {
51 SV *name;
52 SV *s;
53 287 ▶ if (!pmcp_sessions_on(aTHX_ opts)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_sessions_on(aTHX_ opts)) return NULL; 120 167 287
54 167 name = pmcp_hget(aTHX_ opts, "session");
55 167 s = pmcp_call_method(aTHX_ c, "cache", &name, 1, G_SCALAR, NULL);
56 167 ▶ return s ? sv_2mortal(s) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return s ? sv_2mortal(s) : NULL; 167 0 167
57 }
58  
59 /* At to_app: the named store exists and is shared, or croak. Asked through a
60 * context built for the purpose, since the getter lives on the context and
61 * the `cache` keyword on the app declares rather than fetches. */
62 96 static void pmcp_sess_boot_check(pTHX_ SV *app, HV *opts)
63 {
64 96 SV *name = pmcp_hget(aTHX_ opts, "session");
65 96 SV *argv[3], *c, *err = NULL, *store, *backend, *shared;
66 HV *match;
67 96 int ok = 0;
68  
69 96 ▶ if (!pmcp_sessions_on(aTHX_ opts)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_sessions_on(aTHX_ opts)) return; 81 15 96
70 15 match = newHV();
71 15 (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV()));
72 15 argv[0] = sv_2mortal(newRV_noinc((SV *)newHV()));
73 15 argv[1] = app;
74 15 argv[2] = sv_2mortal(newRV_noinc((SV *)match));
75 15 c = sv_2mortal(pmcp_call_method(aTHX_ sv_2mortal(newSVpvs("Punk::Context")),
76 "_build", argv, 3, G_SCALAR, NULL));
77 15 store = pmcp_call_method(aTHX_ c, "cache", &name, 1, G_SCALAR, &err);
78 15 ▶ if (!store) {
Branch: 1 branch
BranchTrueFalseTotal
if (!store) { 1 14 15
79 1 SvREFCNT_dec(err);
80 1 croak("%s: `session => '%" SVf "'` names a store that is not declared "
81 "(add cache %" SVf " => { backend => 'file', ... })", PMCP_WHO,
82 SVfARG(name), SVfARG(name));
83 }
84 14 sv_2mortal(store);
85 14 backend = pmcp_call_method(aTHX_ store, "backend", NULL, 0, G_SCALAR, NULL);
86 14 ▶ if (backend) {
Branch: 1 branch
BranchTrueFalseTotal
if (backend) { 14 0 14
87 14 sv_2mortal(backend);
88 14 ▶ if (SvOK(backend) && pmcp_can(aTHX_ backend, "is_shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(backend) && pmcp_can(aTHX_ backend, "is_shared")) { 14 0 14
if (SvOK(backend) && pmcp_can(aTHX_ backend, "is_shared")) { 14 0 14
89 14 shared = pmcp_call_method(aTHX_ backend, "is_shared", NULL, 0, G_SCALAR, NULL);
90 14 ▶ ok = shared && SvTRUE(shared);
Branch: 2 branches
BranchTrueFalseTotal
ok = shared && SvTRUE(shared); 14 0 14
ok = shared && SvTRUE(shared); 13 1 14
91 14 ▶ if (shared) SvREFCNT_dec(shared);
Branch: 1 branch
BranchTrueFalseTotal
if (shared) SvREFCNT_dec(shared); 14 0 14
92 }
93 }
94 14 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 1 13 14
95 1 croak("%s: the session store '%" SVf "' is not shared across workers, so "
96 "a session made on one would be missing on the next. Use a shared "
97 "backend (the file one is)", PMCP_WHO, SVfARG(name));
98 }
99  
100 /* ---- the record ----------------------------------------------------------------- */
101  
102 171 static SV *pmcp_sess_key(pTHX_ SV *sid)
103 {
104 171 return sv_2mortal(newSVpvf("pmcp:s:%" SVf, SVfARG(sid)));
105 }
106  
107 /* A header value that could be one of our ids: 22 base64url characters. Anything
108 * else is not looked up at all. */
109 73 static int pmcp_sid_ok(pTHX_ SV *sid)
110 {
111 STRLEN l, i;
112 const char *p;
113 73 ▶ if (!sid || !SvOK(sid) || SvROK(sid)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!sid || !SvOK(sid) || SvROK(sid)) return 0; 73 0 73
if (!sid || !SvOK(sid) || SvROK(sid)) return 0; 73 0 73
if (!sid || !SvOK(sid) || SvROK(sid)) return 0; 0 73 73
114 73 p = SvPV_nomg_const(sid, l);
115 73 ▶ if (l != PMCP_ID_CHARS) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (l != PMCP_ID_CHARS) return 0; 2 71 73
116 1632 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 1562 70 1632
117 1562 ▶ if (pmcp_b64url_val(p[i]) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_b64url_val(p[i]) < 0) return 0; 1 1561 1562
118 70 return 1;
119 }
120  
121 /* The record, or NULL when absent or expired. Mortal HV. */
122 88 static HV *pmcp_sess_load(pTHX_ SV *store, SV *sid, NV now)
123 {
124 SV *backend, *bytes, *doc, *exp;
125 88 SV *key = pmcp_sess_key(aTHX_ sid);
126 88 backend = pmcp_call_method(aTHX_ store, "backend", NULL, 0, G_SCALAR, NULL);
127 88 ▶ if (!backend) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!backend) return NULL; 0 88 88
128 88 sv_2mortal(backend);
129 88 bytes = pmcp_call_method(aTHX_ backend, "get", &key, 1, G_SCALAR, NULL);
130 88 ▶ if (!bytes) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) return NULL; 0 88 88
131 88 sv_2mortal(bytes);
132 88 ▶ if (!SvOK(bytes)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(bytes)) return NULL; 6 82 88
133 82 doc = pmcp_rpc_decode(aTHX_ bytes);
134 82 ▶ if (!doc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return NULL; 0 82 82
135 82 sv_2mortal(doc);
136 82 ▶ if (!pmcp_is_hash(doc)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(doc)) return NULL; 0 82 82
137 82 exp = pmcp_hget(aTHX_ (HV *)SvRV(doc), "exp");
138 82 ▶ if (!exp || SvNV(exp) <= now) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!exp || SvNV(exp) <= now) return NULL; 82 0 82
if (!exp || SvNV(exp) <= now) return NULL; 2 80 82
139 80 return (HV *)SvRV(doc);
140 }
141  
142 77 static void pmcp_sess_save(pTHX_ SV *store, SV *sid, HV *rec, IV ttl)
143 {
144 77 SV *rv = sv_2mortal(newRV_inc((SV *)rec));
145 77 SV *err = NULL;
146 77 SV *bytes = pmcp_json_trapped(aTHX_ rv, &err);
147 SV *argv[3], *r;
148 77 ▶ if (!bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { 0 77 77
149 0 sv_2mortal(err);
150 0 croak("%s: a session record is not JSON: %" SVf, PMCP_WHO, SVfARG(err));
151 }
152 77 argv[0] = pmcp_sess_key(aTHX_ sid);
153 77 argv[1] = sv_2mortal(bytes);
154 77 argv[2] = sv_2mortal(newSViv(ttl));
155 77 r = pmcp_call_method(aTHX_ store, "set", argv, 3, G_SCALAR, NULL);
156 77 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 77 0 77
157 77 }
158  
159 6 static void pmcp_sess_drop(pTHX_ SV *store, SV *sid)
160 {
161 6 SV *key = pmcp_sess_key(aTHX_ sid);
162 6 SV *r = pmcp_call_method(aTHX_ store, "delete", &key, 1, G_SCALAR, NULL);
163 6 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 6 0 6
164 6 }
165  
166 /* Who the request is: a digest of its credential, so a session id taken from
167 * one caller is no use to another. 11 refines it with `principal`. Mortal. */
168 128 static SV *pmcp_principal(pTHX_ HV *env)
169 {
170 128 SV *auth = pmcp_env_get(aTHX_ env, "HTTP_AUTHORIZATION");
171 128 ▶ SV *arg = auth ? auth : sv_2mortal(newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
SV *arg = auth ? auth : sv_2mortal(newSVpvs("")); 117 11 128
172 128 SV *fn = sv_2mortal(newSVpvs("Digest::SHA::sha256_hex"));
173 SV *r;
174 static int loaded = 0;
175 128 ▶ if (!loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!loaded) { 9 119 128
176 9 SV *err = NULL;
177 9 ▶ if (!pmcp_require(aTHX_ "Digest::SHA", &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_require(aTHX_ "Digest::SHA", &err)) { 0 9 9
178 0 sv_2mortal(err);
179 0 croak("%s: sessions need Digest::SHA: %" SVf, PMCP_WHO, SVfARG(err));
180 }
181 9 loaded = 1;
182 }
183 128 r = pmcp_call_sv(aTHX_ fn, &arg, 1, G_SCALAR, NULL);
184 128 return sv_2mortal(r);
185 }
186  
187 /* A new session for an initialize that succeeded. Returns the id, mortal. */
188 66 static SV *pmcp_sess_create(pTHX_ SV *store, HV *opts, HV *env, HV *params,
189 enum pmcp_ver v, NV now)
190 {
191 66 SV *sid = sv_2mortal(pmcp_rand_id(aTHX));
192 66 IV ttl = SvIV(pmcp_hget(aTHX_ opts, "session_ttl"));
193 66 HV *rec = (HV *)sv_2mortal((SV *)newHV());
194 SV **x;
195 66 (void)hv_stores(rec, "v", newSVpv(PMCP_VER_NAME[v], 0));
196 66 ▶ if (params && (x = hv_fetchs(params, "clientInfo", 0)) && pmcp_is_hash(*x))
Branch: 3 branches
BranchTrueFalseTotal
if (params && (x = hv_fetchs(params, "clientInfo", 0)) && pmcp_is_hash(*x)) 66 0 66
if (params && (x = hv_fetchs(params, "clientInfo", 0)) && pmcp_is_hash(*x)) 64 2 66
if (params && (x = hv_fetchs(params, "clientInfo", 0)) && pmcp_is_hash(*x)) 64 0 64
197 64 (void)hv_stores(rec, "client", newSVsv(*x));
198 66 ▶ if (params && (x = hv_fetchs(params, "capabilities", 0)) && pmcp_is_hash(*x))
Branch: 3 branches
BranchTrueFalseTotal
if (params && (x = hv_fetchs(params, "capabilities", 0)) && pmcp_is_hash(*x)) 66 0 66
if (params && (x = hv_fetchs(params, "capabilities", 0)) && pmcp_is_hash(*x)) 64 2 66
if (params && (x = hv_fetchs(params, "capabilities", 0)) && pmcp_is_hash(*x)) 64 0 64
199 64 (void)hv_stores(rec, "caps", newSVsv(*x));
200 66 (void)hv_stores(rec, "level", newSViv(PMCP_LOG_DEFAULT));
201 66 (void)hv_stores(rec, "subs", newRV_noinc((SV *)newAV()));
202 66 (void)hv_stores(rec, "principal", newSVsv(pmcp_principal(aTHX_ env)));
203 66 (void)hv_stores(rec, "created", newSViv((IV)now));
204 66 (void)hv_stores(rec, "seen", newSViv((IV)now));
205 66 (void)hv_stores(rec, "exp", newSViv((IV)now + ttl));
206 66 pmcp_sess_save(aTHX_ store, sid, rec, ttl);
207 66 return sid;
208 }
209  
210 /* After a request in a session: a level the client set, and the slide. The
211 * slide writes at most once a PMCP_SLIDE_SECS, so most requests only read. */
212 50 static void pmcp_sess_after(pTHX_ SV *store, HV *opts, SV *sid, HV *rec,
213 int level_set, NV now)
214 {
215 50 IV ttl = SvIV(pmcp_hget(aTHX_ opts, "session_ttl"));
216 50 SV *seen = pmcp_hget(aTHX_ rec, "seen");
217 50 int write = 0;
218 50 ▶ if (level_set >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level_set >= 0) { 4 46 50
219 4 (void)hv_stores(rec, "level", newSViv(level_set));
220 4 write = 1;
221 }
222 50 ▶ if (!seen || now - SvNV(seen) >= PMCP_SLIDE_SECS) {
Branch: 2 branches
BranchTrueFalseTotal
if (!seen || now - SvNV(seen) >= PMCP_SLIDE_SECS) { 50 0 50
if (!seen || now - SvNV(seen) >= PMCP_SLIDE_SECS) { 2 48 50
223 2 (void)hv_stores(rec, "seen", newSViv((IV)now));
224 2 (void)hv_stores(rec, "exp", newSViv((IV)now + ttl));
225 2 write = 1;
226 }
227 50 ▶ if (write) pmcp_sess_save(aTHX_ store, sid, rec, ttl);
Branch: 1 branch
BranchTrueFalseTotal
if (write) pmcp_sess_save(aTHX_ store, sid, rec, ttl); 6 44 50
228 50 }
229  
230 /* ---- bounding creation ------------------------------------------------------------- */
231  
232 210 static IV pmcp_counter(pTHX_ SV *store, SV *key)
233 {
234 210 SV *backend = pmcp_call_method(aTHX_ store, "backend", NULL, 0, G_SCALAR, NULL);
235 SV *v;
236 210 IV n = 0;
237 210 ▶ if (!backend) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!backend) return 0; 0 210 210
238 210 sv_2mortal(backend);
239 210 v = pmcp_call_method(aTHX_ backend, "get", &key, 1, G_SCALAR, NULL);
240 210 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 210 0 210
241 210 ▶ if (SvOK(v) && looks_like_number(v)) n = SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && looks_like_number(v)) n = SvIV(v); 109 101 210
if (SvOK(v) && looks_like_number(v)) n = SvIV(v); 109 0 109
242 210 SvREFCNT_dec(v);
243 }
244 210 return n;
245 }
246  
247 132 static void pmcp_counter_set(pTHX_ SV *store, SV *key, IV n, IV ttl)
248 {
249 SV *argv[3], *r;
250 132 argv[0] = key;
251 132 argv[1] = sv_2mortal(newSViv(n));
252 132 argv[2] = sv_2mortal(newSViv(ttl));
253 132 r = pmcp_call_method(aTHX_ store, "set", argv, 3, G_SCALAR, NULL);
254 132 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 132 0 132
255 132 }
256  
257 /* May this initialize make a session? 0 when it may, else the HTTP status
258 * (503 at the cap, 429 for an address over its rate) with *retry set.
259 *
260 * The cap counts sessions made in the last session_ttl, from two window
261 * counters, the previous one weighted by how much of it still overlaps. Expiry
262 * is lazy, so live records cannot be counted without a scan, and a DELETE does
263 * not hand a slot back: both err towards refusing. The counters are a read and
264 * a write, not atomic, so a burst across workers can overshoot by a few. */
265 70 static int pmcp_sess_limits(pTHX_ SV *store, HV *opts, HV *env, NV now, IV *retry)
266 {
267 70 IV ttl = SvIV(pmcp_hget(aTHX_ opts, "session_ttl"));
268 70 IV max = SvIV(pmcp_hget(aTHX_ opts, "max_sessions"));
269 70 IV w = (IV)(now / (NV)ttl);
270 70 NV into = (now - (NV)w * (NV)ttl) / (NV)ttl;
271 70 SV *kcur = sv_2mortal(newSVpvf("pmcp:n:%" IVdf, w));
272 70 SV *kprev = sv_2mortal(newSVpvf("pmcp:n:%" IVdf, w - 1));
273 70 IV cur = pmcp_counter(aTHX_ store, kcur), prev = pmcp_counter(aTHX_ store, kprev);
274 70 SV *addr = pmcp_env_get(aTHX_ env, "REMOTE_ADDR");
275 70 IV minute = (IV)(now / 60.0);
276 104 ▶ SV *ka = sv_2mortal(newSVpvf("pmcp:a:%s:%" IVdf, addr ? SvPV_nolen(addr) : "-", minute));
Branch: 1 branch
BranchTrueFalseTotal
SV *ka = sv_2mortal(newSVpvf("pmcp:a:%s:%" IVdf, addr ? SvPV_nolen(addr) : "-", minute)); 34 36 70
277 70 IV a = pmcp_counter(aTHX_ store, ka);
278  
279 70 ▶ if ((NV)cur + (NV)prev * (1.0 - into) >= (NV)max) {
Branch: 1 branch
BranchTrueFalseTotal
if ((NV)cur + (NV)prev * (1.0 - into) >= (NV)max) { 3 67 70
280 3 *retry = (IV)((1.0 - into) * (NV)ttl) + 1;
281 3 return 503;
282 }
283 67 ▶ if (a >= PMCP_ADDR_PER_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (a >= PMCP_ADDR_PER_MIN) { 1 66 67
284 1 *retry = 60 - (IV)(now - (NV)minute * 60.0);
285 1 return 429;
286 }
287 66 pmcp_counter_set(aTHX_ store, kcur, cur + 1, ttl * 2);
288 66 pmcp_counter_set(aTHX_ store, ka, a + 1, 120);
289 66 return 0;
290 }
291  
292 /* ---- around the dispatch ------------------------------------------------------------- */
293  
294 /* A refusal with Retry-After. +1 */
295 4 static SV *pmcp_refuse_retry(pTHX_ int status, const char *why, IV retry, int cors)
296 {
297 4 SV *out = pmcp_http_refuse(aTHX_ status, why, cors);
298 4 AV *h = (AV *)SvRV(*av_fetch((AV *)SvRV(out), 1, 0));
299 4 av_push(h, newSVpvs("Retry-After"));
300 4 av_push(h, newSViv(retry > 0 ? retry : 1));
301 4 return out;
302 }
303  
304 /* Before the dispatch. NULL to carry on (with *s filled), or the refusal (+1):
305 *
306 * initialize with no session id may make one, within the limits
307 * initialize with one dispatched, and refused there (-32600)
308 * anything else needs a live session of this caller's:
309 * none 400, unknown or someone else's 404
310 *
311 * A message that is not JSON-RPC at all is left to the dispatcher's 400. */
312 256 static SV *pmcp_sess_begin(pTHX_ SV *c, pmcp_server *ps, HV *env, pmcp_msg *m,
313 pmcp_sess_t *s, int cors)
314 {
315 256 SV *store = pmcp_sess_store(aTHX_ c, ps->opts), *sid, *pv;
316 NV now;
317 HV *rec;
318 int is_init;
319  
320 256 ▶ if (!store || m->kind == PMCP_K_INVALID) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!store || m->kind == PMCP_K_INVALID) return NULL; 136 120 256
if (!store || m->kind == PMCP_K_INVALID) return NULL; 2 134 136
321 134 s->store = store;
322 134 now = pmcp_now(aTHX);
323 134 sid = pmcp_env_get(aTHX_ env, "HTTP_MCP_SESSION_ID");
324 130 ▶ is_init = m->kind == PMCP_K_REQUEST && m->mlen == 10
Branch: 1 branch
BranchTrueFalseTotal
is_init = m->kind == PMCP_K_REQUEST && m->mlen == 10 106 24 130
325 264 ▶ && memEQ(m->method, "initialize", 10);
Branch: 2 branches
BranchTrueFalseTotal
&& memEQ(m->method, "initialize", 10); 130 4 134
&& memEQ(m->method, "initialize", 10); 71 35 106
326  
327 134 ▶ if (is_init) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_init) { 71 63 134
328 71 IV retry = 0;
329 int status;
330 71 ▶ if (sid) { s->in_session = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (sid) { s->in_session = 1; return NULL; } 1 70 71
331 70 status = pmcp_sess_limits(aTHX_ store, ps->opts, env, now, &retry);
332 70 ▶ if (status == 503)
Branch: 1 branch
BranchTrueFalseTotal
if (status == 503) 3 67 70
333 3 return pmcp_refuse_retry(aTHX_ 503, "too many sessions, try again later",
334 retry, cors);
335 67 ▶ if (status == 429)
Branch: 1 branch
BranchTrueFalseTotal
if (status == 429) 1 66 67
336 1 return pmcp_refuse_retry(aTHX_ 429, "too many new sessions from this address",
337 retry, cors);
338 66 s->creating = 1;
339 66 return NULL;
340 }
341  
342 63 ▶ if (!sid) return pmcp_http_refuse(aTHX_ 400, "a session id is required", cors);
Branch: 1 branch
BranchTrueFalseTotal
if (!sid) return pmcp_http_refuse(aTHX_ 400, "a session id is required", cors); 3 60 63
343 60 ▶ rec = pmcp_sid_ok(aTHX_ sid) ? pmcp_sess_load(aTHX_ store, sid, now) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
rec = pmcp_sid_ok(aTHX_ sid) ? pmcp_sess_load(aTHX_ store, sid, now) : NULL; 57 3 60
344 60 ▶ if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env)))
Branch: 2 branches
BranchTrueFalseTotal
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) 52 8 60
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) 2 50 52
345 10 return pmcp_http_refuse(aTHX_ 404, "unknown session", cors);
346 {
347 50 SV *rv = pmcp_hget(aTHX_ rec, "v");
348 STRLEN l;
349 100 ▶ const char *p = rv ? SvPV_const(rv, l) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *p = rv ? SvPV_const(rv, l) : ""; 50 0 50
350 50 ▶ enum pmcp_ver stored = rv ? pmcp_version_of(p, l) : PMCP_V_NONE;
Branch: 1 branch
BranchTrueFalseTotal
enum pmcp_ver stored = rv ? pmcp_version_of(p, l) : PMCP_V_NONE; 50 0 50
351 50 pv = pmcp_env_get(aTHX_ env, "HTTP_MCP_PROTOCOL_VERSION");
352 50 ▶ if (pv && !sv_eq(pv, rv))
Branch: 2 branches
BranchTrueFalseTotal
if (pv && !sv_eq(pv, rv)) 28 22 50
if (pv && !sv_eq(pv, rv)) 1 27 28
353 1 return pmcp_http_refuse(aTHX_ 400,
354 "MCP-Protocol-Version does not match the session's", cors);
355 49 ▶ if (stored != PMCP_V_NONE) s->v = stored;
Branch: 1 branch
BranchTrueFalseTotal
if (stored != PMCP_V_NONE) s->v = stored; 49 0 49
356 }
357 49 s->sid = sv_2mortal(newSVsv(sid));
358 49 s->rec = rec;
359 {
360 49 SV *lv = pmcp_hget(aTHX_ rec, "level");
361 49 ▶ s->level = lv ? (int)SvIV(lv) : PMCP_LOG_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
s->level = lv ? (int)SvIV(lv) : PMCP_LOG_DEFAULT; 49 0 49
362 }
363 49 return NULL;
364 }
365  
366 /* After the dispatch, or before a stream (r NULL). Returns the id of a session
367 * just made, for the Mcp-Session-Id header (mortal), or NULL. */
368 238 static SV *pmcp_sess_end(pTHX_ pmcp_server *ps, HV *env, pmcp_sess_t *s,
369 pmcp_req *r, pmcp_msg *m)
370 {
371 NV now;
372 238 ▶ if (!s->store) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->store) return NULL; 122 116 238
373 116 now = pmcp_now(aTHX);
374 116 ▶ if (s->creating) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->creating) { 66 50 116
375 /* only an initialize that negotiated: an error made no session */
376 66 ▶ if (r && r->negotiated)
Branch: 2 branches
BranchTrueFalseTotal
if (r && r->negotiated) 66 0 66
if (r && r->negotiated) 66 0 66
377 66 return pmcp_sess_create(aTHX_ s->store, ps->opts, env, m->params,
378 r->negotiated, now);
379 0 return NULL;
380 }
381 50 ▶ if (s->sid && s->rec)
Branch: 2 branches
BranchTrueFalseTotal
if (s->sid && s->rec) 49 1 50
if (s->sid && s->rec) 49 0 49
382 49 ▶ pmcp_sess_after(aTHX_ s->store, ps->opts, s->sid, s->rec,
Branch: 1 branch
BranchTrueFalseTotal
pmcp_sess_after(aTHX_ s->store, ps->opts, s->sid, s->rec, 39 10 49
383 r ? r->level_set : -1, now);
384 50 return NULL;
385 }
386  
387 /* A level set by logging/setLevel in a stream, which ends after the handler has
388 * returned: the session record is read again and written, or with no session
389 * the server's own level is set (stdio, 13). */
390 1 static void pmcp_level_set(pTHX_ SV *c, pmcp_server *ps, SV *sid, int level)
391 {
392 SV *store;
393 HV *rec;
394 1 ▶ if (!sid) { ps->srv.log_level = level; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!sid) { ps->srv.log_level = level; return; } 0 1 1
395 1 store = pmcp_sess_store(aTHX_ c, ps->opts);
396 1 ▶ rec = store ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
rec = store ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL; 1 0 1
397 1 ▶ if (rec) pmcp_sess_after(aTHX_ store, ps->opts, sid, rec, level, pmcp_now(aTHX));
Branch: 1 branch
BranchTrueFalseTotal
if (rec) pmcp_sess_after(aTHX_ store, ps->opts, sid, rec, level, pmcp_now(aTHX)); 1 0 1
398 }
399  
400 /* ---- DELETE ---------------------------------------------------------------------- */
401  
402 /* A session ended by DELETE: its GET stream is closed, here or over the bus.
403 * Defined in pmcp_bus.h. */
404 static void pmcp_session_closed(pTHX_ pmcp_server *ps, SV *c, SV *sid);
405  
406 /* DELETE on the MCP path. cap = [server, guards] */
407 12 XS_INTERNAL(pmcp_delete_cb)
408 {
409 12 dXSARGS;
410 12 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 12 0 12
411 12 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 0));
412 12 SV *gslot = pmcp_cap(aTHX_ cv, 1);
413 12 ▶ 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; 12 0 12
AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL; 12 0 12
414 12 SV *envsv = sv_2mortal(pmcp_call_method(aTHX_ c, "env", NULL, 0, G_SCALAR, NULL));
415 12 HV *env = (HV *)SvRV(envsv);
416 12 int cors = pmcp_env_get(aTHX_ env, "HTTP_ORIGIN") != NULL;
417 12 SV *out = NULL, *sid, *store;
418 HV *rec;
419 12 NV now = pmcp_now(aTHX);
420  
421 12 ▶ if (!pmcp_origin_ok(aTHX_ env, ps->opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_origin_ok(aTHX_ env, ps->opts)) { 1 11 12
422 1 out = pmcp_http_refuse(aTHX_ 403, "origin not allowed", cors);
423 1 goto answer;
424 }
425 11 ▶ if (guards) {
Branch: 1 branch
BranchTrueFalseTotal
if (guards) { 11 0 11
426 11 SSize_t i, n = av_len(guards) + 1;
427 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 10 12
428 2 SV *err = NULL;
429 2 SV *r = pmcp_call_sv(aTHX_ *av_fetch(guards, i, 0), &c, 1, G_SCALAR, &err);
430 2 ▶ if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 2 2
431 2 ▶ 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; } 1 1 2
432 1 SvREFCNT_dec(r);
433 }
434 }
435 10 sid = pmcp_env_get(aTHX_ env, "HTTP_MCP_SESSION_ID");
436 10 ▶ if (!sid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sid) { 1 9 10
437 1 out = pmcp_http_refuse(aTHX_ 400, "a session id is required", cors);
438 1 goto answer;
439 }
440 9 store = pmcp_sess_store(aTHX_ c, ps->opts);
441 9 ▶ rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, now) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, now) : NULL; 9 0 9
rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, now) : NULL; 9 0 9
442 9 ▶ if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) { 7 2 9
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) { 1 6 7
443 3 out = pmcp_http_refuse(aTHX_ 404, "unknown session", cors);
444 3 goto answer;
445 }
446 6 pmcp_sess_drop(aTHX_ store, sid);
447 6 pmcp_session_closed(aTHX_ ps, c, sid);
448 6 out = pmcp_http_answer(aTHX_ 204, NULL, cors);
449  
450 12 answer:
451 12 SP = PL_stack_base + ax - 1;
452 12 ST(0) = sv_2mortal(out);
453 12 XSRETURN(1);
454 }
455  
456 #endif /* PMCP_SESS_H */
« include/pmcp/pmcp_reg.h ↑ Summary include/pmcp/pmcp_state.h »