include/punk/punk_state.h

filter: Statementstmt 100% 83 / 83 Branchbran 65.3% 81 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 164 / 207 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/txn.xs ↑ Summary include/punk/punk_oamount.h »
1 /* punk_state.h - the compiled application as a C struct.
2 *
3 * to_app freezes the application into a hash of named entries (router,
4 * records, mounts, hooks, policies) that the request path used to read by
5 * name: about twenty hv_fetch per request, each a strlen and a hash of a
6 * constant key, plus six more on the matched route's record and a stash
7 * lookup by class name for the context. All of it is known at compile time.
8 *
9 * So the hash is read ONCE, here, into a struct the app closure carries
10 * beside it. Every pointer is borrowed from the hash, which the same closure
11 * capture owns and which outlives the struct; the struct owns only the
12 * record mirror it allocates. Nothing outside this translation unit reads
13 * the hash by key (the plugin dists attach through on_compile, the observers
14 * and the context slots), so the hash could go; it stays because it is the
15 * shape `punk routes` and the tests know, and it costs nothing per request.
16 *
17 * The record mirror holds the fields the dispatcher reads from a matched
18 * route's record on every request, indexed by the record index the router
19 * returns. A record hash is never written after to_app (on_compile and
20 * compile_extras run before the state is built), which is what makes a
21 * mirror frozen here true for the life of the app.
22 */
23  
24 #ifndef PUNK_STATE_H
25 #define PUNK_STATE_H
26  
27 typedef struct {
28 SV *code; /* the handler, borrowed; may be undef */
29 AV *guards; /* NULL when the route has none */
30 IV max_body; /* valid when has_max_body */
31 unsigned char has_max_body, etag, idempotent, ws, sse, nocompress;
32 } pk_rec_c;
33  
34 typedef struct {
35 HV *hv; /* the state hash, for anything rare */
36 SV *router_sv;
37 pr_router *rt;
38 AV *recs;
39 AV *apims; /* api mounts, possibly empty */
40 AV *mounts; /* NULL when there are none */
41 SV *ctxcls;
42 HV *ctxstash; /* gv_stashsv(ctxcls), done once */
43 SV *app;
44 SV *upload_dir; /* NULL when unset */
45 AV *before, *after;
46 AV *before_req; /* NULL when the chain is empty */
47 SV *on_err; /* may be undef */
48 SV *on_nf; /* NULL when unset */
49 const pp_policy *proxy; /* NULL without a `proxy` policy */
50 IV max_body;
51 int has_max_body;
52 SV *cget; /* NULL unless Punk::Plugin::ConditionalGet */
53 SV *idem; /* NULL unless Punk::Plugin::Idempotency */
54 HV *cors; /* NULL without the keyword */
55 AV *headers; /* the frozen pair list, or NULL */
56 AV *headers_scoped; /* NULL when no scope has a policy */
57 HV *csp; /* NULL unless Punk::Plugin::CSP */
58 pk_rec_c *rec;
59 int nrec;
60 } pk_state;
61  
62 11960 static SV *pks_get(pTHX_ HV *h, const char *k) {
63 11960 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
64 11960 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 6121 5839 11960
return (e && *e) ? *e : NULL; 6121 0 6121
65 }
66 2392 static SV *pks_def(pTHX_ HV *h, const char *k) {
67 2392 SV *v = pks_get(aTHX_ h, k);
68 2392 ▶ return (v && SvOK(v)) ? v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvOK(v)) ? v : NULL; 613 1779 2392
return (v && SvOK(v)) ? v : NULL; 20 593 613
69 }
70 4784 static AV *pks_av(pTHX_ HV *h, const char *k) {
71 4784 SV *v = pks_get(aTHX_ h, k);
72 4784 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 3012 1772 4784
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 3012 0 3012
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 3012 0 3012
73 }
74 1196 static HV *pks_hv(pTHX_ HV *h, const char *k) {
75 1196 SV *v = pks_get(aTHX_ h, k);
76 1196 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 75 1121 1196
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 75 0 75
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 75 0 75
77 }
78  
79 475 static int pk_state_free(pTHX_ SV *sv, MAGIC *mg) {
80 475 pk_state *S = (pk_state *)mg->mg_ptr;
81 PERL_UNUSED_ARG(sv);
82 475 ▶ if (S) { if (S->rec) Safefree(S->rec); Safefree(S); }
Branch: 2 branches
BranchTrueFalseTotal
if (S) { if (S->rec) Safefree(S->rec); Safefree(S); } 475 0 475
if (S) { if (S->rec) Safefree(S->rec); Safefree(S); } 475 0 475
83 475 return 0;
84 }
85 static MGVTBL pk_state_vtbl = { NULL, NULL, NULL, NULL, pk_state_free,
86 NULL, NULL, NULL };
87  
88 /* One record's hot fields. A missing key reads as the dispatcher always
89 * read it: no guards, no ceiling, not a socket, compression allowed. */
90 61310 static void pk_rec_mirror(pTHX_ pk_rec_c *R, SV *recsv) {
91 HV *rh;
92 SV **x;
93 61310 Zero(R, 1, pk_rec_c);
94 61310 ▶ if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 61310 0 61310
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 61310 0 61310
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 0 61310 61310
95 61310 rh = (HV *)SvRV(recsv);
96 61310 x = hv_fetchs(rh, "code", 0);
97 61310 ▶ R->code = (x && *x) ? *x : NULL;
Branch: 2 branches
BranchTrueFalseTotal
R->code = (x && *x) ? *x : NULL; 61310 0 61310
R->code = (x && *x) ? *x : NULL; 61310 0 61310
98 61310 x = hv_fetchs(rh, "guards", 0);
99 61310 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61310 0 61310
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61310 0 61310
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61310 0 61310
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61310 0 61310
100 61310 ▶ && av_len((AV *)SvRV(*x)) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
&& av_len((AV *)SvRV(*x)) >= 0) 48 61262 61310
101 48 R->guards = (AV *)SvRV(*x);
102 61310 x = hv_fetch(rh, K_MAX_BODY, (I32)strlen(K_MAX_BODY), 0);
103 61310 ▶ if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; } 33 61277 61310
if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; } 33 0 33
if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; } 33 0 33
104 61310 x = hv_fetch(rh, K_ETAG, (I32)strlen(K_ETAG), 0);
105 61310 ▶ R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0;
Branch: 5 branches
BranchTrueFalseTotal
R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0; 43 61267 61310
R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0; 43 0 43
R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0; 43 0 43
R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0; 20 23 43
R->etag = (x && *x && SvOK(*x) && !SvROK(*x) && SvTRUE(*x)) ? 1 : 0; 20 0 20
106 61310 x = hv_fetch(rh, K_IDEMPOTENT, (I32)strlen(K_IDEMPOTENT), 0);
107 61310 ▶ R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 22 61288 61310
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 22 0 22
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 22 0 22
108 61310 x = hv_fetch(rh, K_WS, (I32)strlen(K_WS), 0);
109 61310 ▶ R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 30 61280 61310
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 30 0 30
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 30 0 30
110 61310 x = hv_fetchs(rh, "sse", 0);
111 61310 ▶ R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 10 61300 61310
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 10 0 10
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 10 0 10
112 61310 x = hv_fetch(rh, K_COMPRESS, (I32)strlen(K_COMPRESS), 0);
113 61310 ▶ R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 61304 61310
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 0 6
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 0 6
114 }
115  
116 /* Read the frozen hash into a struct. Returns the SV that carries it (+1);
117 * the struct lives as long as that SV. */
118 598 static SV *pk_state_build(pTHX_ HV *state) {
119 598 SV *holder = newSV(0);
120 pk_state *S;
121 598 Newxz(S, 1, pk_state);
122 598 S->hv = state;
123 598 S->router_sv = pks_get(aTHX_ state, K_ROUTER);
124 598 ▶ S->rt = (S->router_sv && SvROK(S->router_sv) && SvIOK(SvRV(S->router_sv)))
Branch: 2 branches
BranchTrueFalseTotal
S->rt = (S->router_sv && SvROK(S->router_sv) && SvIOK(SvRV(S->router_sv))) 598 0 598
S->rt = (S->router_sv && SvROK(S->router_sv) && SvIOK(SvRV(S->router_sv))) 598 0 598
125 1196 ▶ ? (pr_router *)INT2PTR(void *, SvIV(SvRV(S->router_sv))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (pr_router *)INT2PTR(void *, SvIV(SvRV(S->router_sv))) : NULL; 598 0 598
126 598 S->recs = pks_av(aTHX_ state, K_RECS);
127 598 S->apims = pks_av(aTHX_ state, K_API_MOUNTS);
128 598 S->mounts = pks_av(aTHX_ state, K_MOUNTS);
129 598 S->ctxcls = pks_get(aTHX_ state, K_CTX);
130 598 ▶ S->ctxstash = S->ctxcls ? gv_stashsv(S->ctxcls, GV_ADD) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
S->ctxstash = S->ctxcls ? gv_stashsv(S->ctxcls, GV_ADD) : NULL; 598 0 598
131 598 S->app = pks_get(aTHX_ state, K_APP);
132 598 S->upload_dir = pks_def(aTHX_ state, "upload_dir");
133 598 S->before = pks_av(aTHX_ state, K_BEFORE);
134 598 S->after = pks_av(aTHX_ state, K_AFTER);
135 598 S->before_req = pks_av(aTHX_ state, K_BEFORE_REQ);
136 598 ▶ if (S->before_req && av_len(S->before_req) < 0) S->before_req = NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (S->before_req && av_len(S->before_req) < 0) S->before_req = NULL; 14 584 598
if (S->before_req && av_len(S->before_req) < 0) S->before_req = NULL; 0 14 14
137 598 S->on_err = pks_get(aTHX_ state, K_ON_ERROR);
138 598 S->on_nf = pks_def(aTHX_ state, K_ON_NOT_FOUND);
139 {
140 598 SV *px = pks_get(aTHX_ state, K_PROXY);
141 10 ▶ S->proxy = (px && SvROK(px) && SvIOK(SvRV(px)))
Branch: 2 branches
BranchTrueFalseTotal
S->proxy = (px && SvROK(px) && SvIOK(SvRV(px))) 10 0 10
S->proxy = (px && SvROK(px) && SvIOK(SvRV(px))) 10 0 10
142 608 ▶ ? (const pp_policy *)INT2PTR(void *, SvIV(SvRV(px))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (const pp_policy *)INT2PTR(void *, SvIV(SvRV(px))) : NULL; 10 588 598
143 }
144 {
145 598 SV *mb = pks_def(aTHX_ state, K_MAX_BODY);
146 598 ▶ if (mb) { S->max_body = SvIV(mb); S->has_max_body = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (mb) { S->max_body = SvIV(mb); S->has_max_body = 1; } 2 596 598
147 }
148 {
149 598 SV *cg = pks_get(aTHX_ state, K_CGET);
150 598 ▶ S->cget = (cg && SvTRUE(cg)) ? cg : NULL;
Branch: 2 branches
BranchTrueFalseTotal
S->cget = (cg && SvTRUE(cg)) ? cg : NULL; 19 579 598
S->cget = (cg && SvTRUE(cg)) ? cg : NULL; 19 0 19
151 }
152 598 S->idem = pks_def(aTHX_ state, K_IDEM);
153 598 S->cors = pks_hv(aTHX_ state, K_CORS);
154 598 S->headers = pks_av(aTHX_ state, K_HEADERS);
155 598 S->headers_scoped = pks_av(aTHX_ state, K_HEADERS_SCOPED);
156 598 S->csp = pks_hv(aTHX_ state, K_CSP);
157 598 ▶ if (S->recs) {
Branch: 1 branch
BranchTrueFalseTotal
if (S->recs) { 598 0 598
158 598 SSize_t i, n = av_len(S->recs) + 1;
159 598 ▶ Newxz(S->rec, n > 0 ? n : 1, pk_rec_c);
Branch: 2 branches
BranchTrueFalseTotal
Newxz(S->rec, n > 0 ? n : 1, pk_rec_c); 518 80 598
Newxz(S->rec, n > 0 ? n : 1, pk_rec_c); 0 518 518
160 598 S->nrec = (int)n;
161 61908 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61310 598 61908
162 61310 SV **rp = av_fetch(S->recs, i, 0);
163 61310 ▶ pk_rec_mirror(aTHX_ &S->rec[i], (rp && *rp) ? *rp : NULL);
Branch: 2 branches
BranchTrueFalseTotal
pk_rec_mirror(aTHX_ &S->rec[i], (rp && *rp) ? *rp : NULL); 61310 0 61310
pk_rec_mirror(aTHX_ &S->rec[i], (rp && *rp) ? *rp : NULL); 61310 0 61310
164 }
165 }
166 598 sv_magicext(holder, NULL, PERL_MAGIC_ext, &pk_state_vtbl, (char *)S, 0);
167 598 return holder;
168 }
169  
170 /* the struct a carrier SV holds, or NULL */
171 219533 static pk_state *pk_state_of(pTHX_ SV *holder) {
172 219533 MAGIC *mg = holder ? mg_findext(holder, PERL_MAGIC_ext, &pk_state_vtbl)
173 219533 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 219533 0 219533
174 219533 ▶ return mg ? (pk_state *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (pk_state *)mg->mg_ptr : NULL; 219533 0 219533
175 }
176  
177 #endif /* PUNK_STATE_H */
« xs/txn.xs ↑ Summary include/punk/punk_oamount.h »