xs/router.xs

filter: Statementstmt 100% 69 / 69 Branchbran 64.2% 54 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.3% 123 / 153 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Punk.xs ↑ Summary include/punk/punk_sitemap.h »
1 MODULE = Punk PACKAGE = Punk::Router
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Router is entirely XS: the object is a blessed IV-ref to a C router
6 # (punk_route.h). new() opens an accumulation phase, add() records raw routes,
7 # compile() resolves their targets to coderefs (the one Perl call) and builds
8 # the whole table in C, and match_static/match run the request path in C.
9 # lib/Punk/Router.pm is documentation only.
10  
11 SV *
12 new(class)
13 SV *class
14 CODE:
15 2858 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class),
16 PTR2IV(pr_new(aTHX)));
17 OUTPUT:
18 RETVAL
19  
20 # add(method => $m, path => $p, target => $t, guards => \@g): record one raw
21 # route (unresolved). The path must start with '/'. Chains.
22 void
23 add(self, ...)
24 SV *self
25 PPCODE:
26 {
27 61824 pr_router *rt = punk_router_of(aTHX_ self);
28 61824 HV *rec = newHV();
29 61824 SV *path = NULL;
30 int i;
31 61824 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 61824 61824
32 309112 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 247288 61824 309112
33 if (rt->compiled) croak("Punk::Router: cannot add after compile");
34 247288 for (i = 1; i + 1 < items; i += 2) {
35 247288 STRLEN kl;
36 247288 ▶ const char *k = SvPV_const(ST(i), kl);
Branch: 2 branches
BranchTrueFalseTotal
const char *k = SvPV_const(ST(i), kl); 61824 185464 247288
const char *k = SvPV_const(ST(i), kl); 61824 0 61824
37 (void)hv_store(rec, k, (I32)kl, newSVsv(ST(i + 1)), 0);
38 if (kl == 4 && memEQ(k, "path", 4)) path = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
39 }
40 123648 ▶ {
Branch: 2 branches
BranchTrueFalseTotal
{ 61824 0 61824
{ 61824 0 61824
41 61824 ▶ STRLEN pl;
Branch: 3 branches
BranchTrueFalseTotal
STRLEN pl; 61824 0 61824
STRLEN pl; 61824 0 61824
STRLEN pl; 1 61823 61824
42 1 # uncoverable condition right count:1 class:ignore_covered_err
43 1 # uncoverable condition right count:2 class:ignore_covered_err
44 const char *p = (path && SvOK(path)) ? SvPV_const(path, pl) : NULL;
45 # uncoverable condition left count:1 class:ignore_covered_err
46 61823 # uncoverable condition left count:2 class:ignore_covered_err
47 61823 ▶ if (!p || !pl || p[0] != '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (!p || !pl || p[0] != '/') { 0 61823 61823
48 SvREFCNT_dec((SV *)rec);
49 croak("Punk::Router: a route needs a path starting with '/'");
50 }
51 }
52 av_push(rt->raw, newRV_noinc((SV *)rec));
53 XPUSHs(self);
54 }
55  
56 # compile($resolve, \@extra?): resolve every target and guard through the
57 # app's $resolve->($target, $desc), then build the exact table, dynamic
58 # records, Allow lists and per-route record hashrefs in C. $extra is an
59 # arrayref of the same { method, path, target, guards } records (the docs UI
60 # routes). Returns self - the compiled handle.
61 void
62 compile(self, resolve, ...)
63 SV *self
64 SV *resolve
65 PPCODE:
66 {
67 876 pr_router *rt = punk_router_of(aTHX_ self);
68 876 AV *routes = (AV *)sv_2mortal((SV *)newAV());
69 SSize_t i, n;
70 876 ▶ if (rt->compiled) croak("Punk::Router: already compiled");
Branch: 1 branch
BranchTrueFalseTotal
if (rt->compiled) croak("Punk::Router: already compiled"); 1 875 876
71 875 n = av_len(rt->raw) + 1;
72 62646 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61785 861 62646
73 61785 SV **e = av_fetch(rt->raw, i, 0);
74 61785 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 61785 0 61785
# uncoverable branch false class:ignore_covered_err 61785 0 61785
# uncoverable branch false class:ignore_covered_err 61785 0 61785
# uncoverable branch false class:ignore_covered_err 61785 0 61785
75 61785 # uncoverable condition right count:1 class:ignore_covered_err
76 # uncoverable condition right count:2 class:ignore_covered_err
77 861 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 860 1 861
# uncoverable condition right count:3 class:ignore_covered_err 860 0 860
# uncoverable condition right count:3 class:ignore_covered_err 860 0 860
78 860 # uncoverable condition right count:4 class:ignore_covered_err
79 860 if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
80 910 ▶ pr_add_route(aTHX_ routes, resolve, (HV *)SvRV(*e));
Branch: 1 branch
BranchTrueFalseTotal
pr_add_route(aTHX_ routes, resolve, (HV *)SvRV(*e)); 50 860 910
81 50 }
82 50 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 50 0 50
# uncoverable condition right count:2 class:ignore_covered_err 50 0 50
# uncoverable condition right count:2 class:ignore_covered_err 50 0 50
# uncoverable condition right count:2 class:ignore_covered_err 50 0 50
83 50 # uncoverable condition right count:3 class:ignore_covered_err
84 if (items > 2 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) {
85 AV *extra = (AV *)SvRV(ST(2));
86 861 SSize_t en = av_len(extra) + 1;
87 859 for (i = 0; i < en; i++) {
88 859 ▶ SV **e = av_fetch(extra, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av_fetch(extra, i, 0); 0 859 859
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 # uncoverable condition right count:3 class:ignore_covered_err
93 # uncoverable condition right count:4 class:ignore_covered_err
94 if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
95 pr_add_route(aTHX_ routes, resolve, (HV *)SvRV(*e));
96 }
97 }
98 pr_build(aTHX_ rt, routes);
99 SP = PL_stack_base + ax - 1; /* $resolve ran Perl */
100 XPUSHs(self);
101 }
102  
103 # The compiled record hashrefs (code + guards + method + path), indexed by the
104 # record index match_static / match return.
105 SV *
106 records(self)
107 SV *self
108 CODE:
109 898 RETVAL = newRV_inc((SV *)punk_router_of(aTHX_ self)->records);
110 OUTPUT:
111 RETVAL
112  
113 # The record index -> dynamic position map, as an arrayref of IVs (-1 for a
114 # static route). Internal, and here so the map is falsifiable: named routes
115 # (punk_url.h) resolve a name to a RECORD index, and reaching that record's
116 # parsed segments means crossing from `records` (indexed by record index) to
117 # `recs` (indexed by dynamic position). Nothing else can see that it agrees.
118 SV *
119 _dyn_of(self)
120 SV *self
121 CODE:
122 {
123 1 pr_router *rt = punk_router_of(aTHX_ self);
124 1 AV *out = newAV();
125 int i;
126 7 ▶ for (i = 0; i < rt->nrecs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rt->nrecs; i++) 6 1 7
127 6 av_push(out, newSViv(rt->dyn_of[i]));
128 1 RETVAL = newRV_noinc((SV *)out);
129 }
130 OUTPUT:
131 RETVAL
132  
133 # The record index for an exact static match (HEAD falls back to GET), or the
134 # empty list. Consulted before mounts so a mounted app is never shadowed.
135 void
136 match_static(self, method, path)
137 SV *self
138 SV *method
139 SV *path
140 PPCODE:
141 {
142 10 pr_router *rt = punk_router_of(aTHX_ self);
143 STRLEN ml, pl;
144 10 ▶ const char *m = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV_const(method, ml); 10 0 10
145 10 const char *p = SvPV_const(path, pl);
146 10 IV idx = pr_static_match(aTHX_ rt, m, ml, p, pl);
147 10 ▶ if (idx >= 0) mXPUSHi(idx); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (idx >= 0) mXPUSHi(idx); /* # uncoverable condition left count:2 class:ignore_covered_err */ 7 3 10
if (idx >= 0) mXPUSHi(idx); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 7 7
148 }
149  
150 # Dynamic dispatch and the 405 cold path in one call:
151 # (idx, \%captures) a dynamic hit
152 # (undef, \@allow) the path exists under other methods (405)
153 # () no such path (404)
154 void
155 match(self, method, path)
156 SV *self
157 SV *method
158 SV *path
159 PPCODE:
160 {
161 21 pr_router *rt = punk_router_of(aTHX_ self);
162 STRLEN ml, pl;
163 21 ▶ const char *m = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV_const(method, ml); 21 0 21
164 21 const char *p = SvPV_const(path, pl);
165 21 HV *caps = (HV *)sv_2mortal((SV *)newHV());
166 21 IV idx = pr_match(aTHX_ rt, m, ml, p, pl, caps);
167 21 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 14 7 21
168 14 ▶ mXPUSHi(idx);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(idx); 0 14 14
169 14 ▶ mXPUSHs(newRV_inc((SV *)caps));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)caps)); 0 14 14
170 }
171 else {
172 7 AV *allow = (AV *)sv_2mortal((SV *)newAV());
173 7 ▶ if (pr_allow(aTHX_ rt, m, ml, p, pl, allow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pr_allow(aTHX_ rt, m, ml, p, pl, allow)) { 3 4 7
174 3 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 3 3
175 3 ▶ mXPUSHs(newRV_inc((SV *)allow));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)allow)); 0 3 3
176 }
177 /* else: empty list = 404 */
178 }
179 }
180  
181 void
182 DESTROY(self)
183 SV *self
184 CODE:
185 {
186 1429 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 1429 0 1429
# uncoverable branch false 1429 0 1429
187 1429 # uncoverable condition right count:1 class:ignore_covered_err
188 # uncoverable condition right count:2 class:ignore_covered_err
189 if (SvROK(self) && SvIOK(SvRV(self)))
190 pr_free(aTHX_ punk_router_of(aTHX_ self));
191 }
192  
193 MODULE = Punk PACKAGE = Punk::Proxy
194  
195 PROTOTYPES: DISABLE
196  
197 # Punk::Proxy is the compiled `proxy` trust policy: a blessed IV-ref to a
198 # pp_policy (punk_proxy.h), built once at to_app and read by pp_resolve at
199 # the top of every request. There is no other method - the object exists so
200 # the struct is freed with the compiled application.
201  
202 void
203 DESTROY(self)
204 SV *self
205 CODE:
206 {
207 10 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
208 10 # uncoverable condition right count:1 class:ignore_covered_err
209 # uncoverable condition right count:2 class:ignore_covered_err
210 if (SvROK(self) && SvIOK(SvRV(self)))
211 pp_free(aTHX_ (pp_policy *)INT2PTR(void *, SvIV(SvRV(self))));
212 }
213  
214 # _client($xff, $peer, $trust): the hop walk alone, for t/1210-proxy-parse.t.
215 # $trust is a hop count, an arrayref of CIDRs, or 'all' - the same shapes the
216 # keyword takes, compiled fresh per call. Author-facing, not documented.
217 SV *
218 _client(xff, peer, trust)
219 SV *xff
220 SV *peer
221 SV *trust
222 CODE:
223 {
224 36 HV *cfg = (HV *)sv_2mortal((SV *)newHV());
225 pp_policy *p;
226 36 STRLEN xl = 0, pl = 0, cl = 0;
227 71 ▶ const char *x = SvOK(xff) ? SvPV_const(xff, xl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *x = SvOK(xff) ? SvPV_const(xff, xl) : NULL; 35 1 36
228 72 ▶ const char *r = SvOK(peer) ? SvPV_const(peer, pl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *r = SvOK(peer) ? SvPV_const(peer, pl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 36 0 36
229 const char *c;
230 36 (void)hv_stores(cfg, "trust", newSVsv(trust));
231 36 p = pp_compile(aTHX_ cfg, "development");
232 36 c = pp_xff_client(x, xl, p, r, pl, &cl);
233 36 RETVAL = newSVpvn(c, cl);
234 36 pp_free(aTHX_ p);
235 }
236 OUTPUT:
237 RETVAL
« Punk.xs ↑ Summary include/punk/punk_sitemap.h »