include/punk/punk_state.h

filter: Statementstmt 100% 83 / 83 Branchbran 65.3% 81 / 124 (4 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 164 / 207 (4 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_context.h ↑ Summary include/punk/punk_idem.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 15840 static SV *pks_get(pTHX_ HV *h, const char *k) {
63 15840 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
64 15840 ▶ return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 8085 7755 15840
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 8085 0 8085
65 }
66 3168 static SV *pks_def(pTHX_ HV *h, const char *k) {
67 3168 SV *v = pks_get(aTHX_ h, k);
68 3168 ▶ return (v && SvOK(v)) ? v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvOK(v)) ? v : NULL; 828 2340 3168
return (v && SvOK(v)) ? v : NULL; 41 787 828
69 }
70 6336 static AV *pks_av(pTHX_ HV *h, const char *k) {
71 6336 SV *v = pks_get(aTHX_ h, k);
72 /*
73 # uncoverable condition right count:2 class:ignore_covered_err
74 # uncoverable condition right count:3 class:ignore_covered_err */
75 6336 ▶ 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; 3982 2354 6336
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 3982 0 3982
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 3982 0 3982
76 }
77 1584 static HV *pks_hv(pTHX_ HV *h, const char *k) {
78 1584 SV *v = pks_get(aTHX_ h, k);
79 /*
80 # uncoverable condition right count:2 class:ignore_covered_err
81 # uncoverable condition right count:3 class:ignore_covered_err */
82 1584 ▶ 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; 78 1506 1584
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 78 0 78
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) ? (HV *)SvRV(v) : NULL; 78 0 78
83 }
84  
85 609 static int pk_state_free(pTHX_ SV *sv, MAGIC *mg) {
86 609 pk_state *S = (pk_state *)mg->mg_ptr;
87 PERL_UNUSED_ARG(sv);
88 /*
89 # uncoverable branch false
90 # uncoverable condition right count:1 class:ignore_covered_err
91 # uncoverable condition right count:2 class:ignore_covered_err */
92 609 ▶ if (S) { if (S->rec) Safefree(S->rec); Safefree(S); }
Branch: 2 branches
BranchTrueFalseTotal
if (S) { if (S->rec) Safefree(S->rec); Safefree(S); } 609 0 609
if (S) { if (S->rec) Safefree(S->rec); Safefree(S); } 609 0 609
93 609 return 0;
94 }
95 static MGVTBL pk_state_vtbl = { NULL, NULL, NULL, NULL, pk_state_free,
96 NULL, NULL, NULL };
97  
98 /* One record's hot fields. A missing key reads as the dispatcher always
99 * read it: no guards, no ceiling, not a socket, compression allowed. */
100 61788 static void pk_rec_mirror(pTHX_ pk_rec_c *R, SV *recsv) {
101 HV *rh;
102 SV **x;
103 61788 Zero(R, 1, pk_rec_c);
104 /*
105 # uncoverable branch true
106 # uncoverable condition left count:1 class:ignore_covered_err
107 # uncoverable condition left count:2 class:ignore_covered_err
108 # uncoverable condition left count:3 class:ignore_covered_err */
109 61788 ▶ if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 61788 0 61788
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 61788 0 61788
if (!(recsv && SvROK(recsv) && SvTYPE(SvRV(recsv)) == SVt_PVHV)) return; 0 61788 61788
110 61788 rh = (HV *)SvRV(recsv);
111 61788 x = hv_fetchs(rh, "code", 0);
112 /*
113 # uncoverable condition right count:1 class:ignore_covered_err
114 # uncoverable condition right count:2 class:ignore_covered_err */
115 61788 ▶ R->code = (x && *x) ? *x : NULL;
Branch: 2 branches
BranchTrueFalseTotal
R->code = (x && *x) ? *x : NULL; 61788 0 61788
R->code = (x && *x) ? *x : NULL; 61788 0 61788
116 61788 x = hv_fetchs(rh, "guards", 0);
117 /*
118 # uncoverable condition right count:1 class:ignore_covered_err
119 # uncoverable condition right count:2 class:ignore_covered_err
120 # uncoverable condition right count:3 class:ignore_covered_err
121 # uncoverable condition right count:4 class:ignore_covered_err */
122 61788 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61788 0 61788
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61788 0 61788
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61788 0 61788
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV 61788 0 61788
123 61788 ▶ && av_len((AV *)SvRV(*x)) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
&& av_len((AV *)SvRV(*x)) >= 0) 63 61725 61788
124 63 R->guards = (AV *)SvRV(*x);
125 61788 x = hv_fetch(rh, K_MAX_BODY, (I32)strlen(K_MAX_BODY), 0);
126 /*
127 # uncoverable condition right count:2 class:ignore_covered_err
128 # uncoverable condition right count:3 class:ignore_covered_err */
129 61788 ▶ 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; } 37 61751 61788
if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; } 37 0 37
if (x && *x && SvOK(*x)) { R->max_body = SvIV(*x); R->has_max_body = 1; } 37 0 37
130 61788 x = hv_fetch(rh, K_ETAG, (I32)strlen(K_ETAG), 0);
131 /*
132 # uncoverable condition right count:2 class:ignore_covered_err
133 # uncoverable condition right count:3 class:ignore_covered_err
134 # uncoverable condition right count:5 class:ignore_covered_err */
135 61788 ▶ 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 61745 61788
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
136 61788 x = hv_fetch(rh, K_IDEMPOTENT, (I32)strlen(K_IDEMPOTENT), 0);
137 /*
138 # uncoverable condition right count:2 class:ignore_covered_err
139 # uncoverable condition right count:3 class:ignore_covered_err */
140 61788 ▶ R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 40 61748 61788
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 40 0 40
R->idempotent = (x && *x && SvTRUE(*x)) ? 1 : 0; 40 0 40
141 61788 x = hv_fetch(rh, K_WS, (I32)strlen(K_WS), 0);
142 /*
143 # uncoverable condition right count:2 class:ignore_covered_err
144 # uncoverable condition right count:3 class:ignore_covered_err */
145 61788 ▶ R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 94 61694 61788
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 94 0 94
R->ws = (x && *x && SvTRUE(*x)) ? 1 : 0; 94 0 94
146 61788 x = hv_fetchs(rh, "sse", 0);
147 /*
148 # uncoverable condition right count:2 class:ignore_covered_err
149 # uncoverable condition right count:3 class:ignore_covered_err */
150 61788 ▶ R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 35 61753 61788
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 35 0 35
R->sse = (x && *x && SvTRUE(*x)) ? 1 : 0; 35 0 35
151 61788 x = hv_fetch(rh, K_COMPRESS, (I32)strlen(K_COMPRESS), 0);
152 /*
153 # uncoverable condition right count:2 class:ignore_covered_err
154 # uncoverable condition right count:3 class:ignore_covered_err */
155 61788 ▶ R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 61782 61788
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 0 6
R->nocompress = (x && *x && !SvTRUE(*x)) ? 1 : 0; 6 0 6
156 }
157  
158 /* Read the frozen hash into a struct. Returns the SV that carries it (+1);
159 * the struct lives as long as that SV. */
160 792 static SV *pk_state_build(pTHX_ HV *state) {
161 792 SV *holder = newSV(0);
162 pk_state *S;
163 792 Newxz(S, 1, pk_state);
164 792 S->hv = state;
165 792 S->router_sv = pks_get(aTHX_ state, K_ROUTER);
166 792 ▶ 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))) 792 0 792
S->rt = (S->router_sv && SvROK(S->router_sv) && SvIOK(SvRV(S->router_sv))) 792 0 792
167 1584 ▶ ? (pr_router *)INT2PTR(void *, SvIV(SvRV(S->router_sv))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (pr_router *)INT2PTR(void *, SvIV(SvRV(S->router_sv))) : NULL; 792 0 792
168 792 S->recs = pks_av(aTHX_ state, K_RECS);
169 792 S->apims = pks_av(aTHX_ state, K_API_MOUNTS);
170 792 S->mounts = pks_av(aTHX_ state, K_MOUNTS);
171 792 S->ctxcls = pks_get(aTHX_ state, K_CTX);
172 792 ▶ S->ctxstash = S->ctxcls ? gv_stashsv(S->ctxcls, GV_ADD) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
S->ctxstash = S->ctxcls ? gv_stashsv(S->ctxcls, GV_ADD) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 792 0 792
173 792 S->app = pks_get(aTHX_ state, K_APP);
174 792 S->upload_dir = pks_def(aTHX_ state, "upload_dir");
175 792 S->before = pks_av(aTHX_ state, K_BEFORE);
176 792 S->after = pks_av(aTHX_ state, K_AFTER);
177 792 S->before_req = pks_av(aTHX_ state, K_BEFORE_REQ);
178 /*
179 # uncoverable branch true
180 # uncoverable condition left count:1 class:ignore_covered_err
181 # uncoverable condition left count:2 class:ignore_covered_err */
182 792 ▶ 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 778 792
if (S->before_req && av_len(S->before_req) < 0) S->before_req = NULL; 0 14 14
183 792 S->on_err = pks_get(aTHX_ state, K_ON_ERROR);
184 792 S->on_nf = pks_def(aTHX_ state, K_ON_NOT_FOUND);
185 {
186 792 SV *px = pks_get(aTHX_ state, K_PROXY);
187 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
188 802 ▶ ? (const pp_policy *)INT2PTR(void *, SvIV(SvRV(px))) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (const pp_policy *)INT2PTR(void *, SvIV(SvRV(px))) : NULL; 10 782 792
189 }
190 {
191 792 SV *mb = pks_def(aTHX_ state, K_MAX_BODY);
192 792 ▶ 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 790 792
193 }
194 {
195 792 SV *cg = pks_get(aTHX_ state, K_CGET);
196 792 ▶ S->cget = (cg && SvTRUE(cg)) ? cg : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
S->cget = (cg && SvTRUE(cg)) ? cg : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 19 773 792
S->cget = (cg && SvTRUE(cg)) ? cg : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 19 0 19
197 }
198 792 S->idem = pks_def(aTHX_ state, K_IDEM);
199 792 S->cors = pks_hv(aTHX_ state, K_CORS);
200 792 S->headers = pks_av(aTHX_ state, K_HEADERS);
201 792 S->headers_scoped = pks_av(aTHX_ state, K_HEADERS_SCOPED);
202 792 S->csp = pks_hv(aTHX_ state, K_CSP);
203 /*
204 # uncoverable branch false
205 # uncoverable condition right count:1 class:ignore_covered_err */
206 792 ▶ if (S->recs) {
Branch: 1 branch
BranchTrueFalseTotal
if (S->recs) { 792 0 792
207 792 SSize_t i, n = av_len(S->recs) + 1;
208 792 ▶ Newxz(S->rec, n > 0 ? n : 1, pk_rec_c);
Branch: 2 branches
BranchTrueFalseTotal
Newxz(S->rec, n > 0 ? n : 1, pk_rec_c); 701 91 792
Newxz(S->rec, n > 0 ? n : 1, pk_rec_c); 0 701 701
209 792 S->nrec = (int)n;
210 62580 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61788 792 62580
211 61788 SV **rp = av_fetch(S->recs, i, 0);
212 /*
213 # uncoverable condition right count:1 class:ignore_covered_err
214 # uncoverable condition right count:2 class:ignore_covered_err */
215 61788 ▶ 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); 61788 0 61788
pk_rec_mirror(aTHX_ &S->rec[i], (rp && *rp) ? *rp : NULL); 61788 0 61788
216 }
217 }
218 792 sv_magicext(holder, NULL, PERL_MAGIC_ext, &pk_state_vtbl, (char *)S, 0);
219 792 return holder;
220 }
221  
222 /* the struct a carrier SV holds, or NULL */
223 221550 static pk_state *pk_state_of(pTHX_ SV *holder) {
224 221550 MAGIC *mg = holder ? mg_findext(holder, PERL_MAGIC_ext, &pk_state_vtbl)
225 221550 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 221550 0 221550
226 221550 ▶ return mg ? (pk_state *)mg->mg_ptr : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (pk_state *)mg->mg_ptr : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 221550 0 221550
227 }
228  
229 #endif /* PUNK_STATE_H */
« include/punk/punk_context.h ↑ Summary include/punk/punk_idem.h »