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
« include/punk/punk_metrics.h ↑ Summary xs/views.xs »
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 2274 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 61315 pr_router *rt = punk_router_of(aTHX_ self);
28 61315 HV *rec = newHV();
29 61315 SV *path = NULL;
30 int i;
31 61315 ▶ if (rt->compiled) croak("Punk::Router: cannot add after compile");
Branch: 1 branch
BranchTrueFalseTotal
if (rt->compiled) croak("Punk::Router: cannot add after compile"); 0 61315 61315
32 306567 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 245252 61315 306567
33 STRLEN kl;
34 245252 const char *k = SvPV_const(ST(i), kl);
35 245252 (void)hv_store(rec, k, (I32)kl, newSVsv(ST(i + 1)), 0);
36 245252 ▶ if (kl == 4 && memEQ(k, "path", 4)) path = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "path", 4)) path = ST(i + 1); 61315 183937 245252
if (kl == 4 && memEQ(k, "path", 4)) path = ST(i + 1); 61315 0 61315
37 }
38 {
39 STRLEN pl;
40 122630 ▶ const char *p = (path && SvOK(path)) ? SvPV_const(path, pl) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *p = (path && SvOK(path)) ? SvPV_const(path, pl) : NULL; 61315 0 61315
const char *p = (path && SvOK(path)) ? SvPV_const(path, pl) : NULL; 61315 0 61315
41 61315 ▶ if (!p || !pl || p[0] != '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !pl || p[0] != '/') { 61315 0 61315
if (!p || !pl || p[0] != '/') { 61315 0 61315
if (!p || !pl || p[0] != '/') { 1 61314 61315
42 1 SvREFCNT_dec((SV *)rec);
43 1 croak("Punk::Router: a route needs a path starting with '/'");
44 }
45 }
46 61314 av_push(rt->raw, newRV_noinc((SV *)rec));
47 61314 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 61314 61314
48 }
49  
50 # compile($resolve, \@extra?): resolve every target and guard through the
51 # app's $resolve->($target, $desc), then build the exact table, dynamic
52 # records, Allow lists and per-route record hashrefs in C. $extra is an
53 # arrayref of the same { method, path, target, guards } records (the docs UI
54 # routes). Returns self - the compiled handle.
55 void
56 compile(self, resolve, ...)
57 SV *self
58 SV *resolve
59 PPCODE:
60 {
61 660 pr_router *rt = punk_router_of(aTHX_ self);
62 660 AV *routes = (AV *)sv_2mortal((SV *)newAV());
63 SSize_t i, n;
64 660 ▶ if (rt->compiled) croak("Punk::Router: already compiled");
Branch: 1 branch
BranchTrueFalseTotal
if (rt->compiled) croak("Punk::Router: already compiled"); 1 659 660
65 659 n = av_len(rt->raw) + 1;
66 61952 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61298 654 61952
67 61298 SV **e = av_fetch(rt->raw, i, 0);
68 61298 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 61298 0 61298
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 61298 0 61298
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 61298 0 61298
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 61298 0 61298
69 61298 pr_add_route(aTHX_ routes, resolve, (HV *)SvRV(*e));
70 }
71 654 ▶ if (items > 2 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items > 2 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { 653 1 654
if (items > 2 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { 653 0 653
if (items > 2 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVAV) { 653 0 653
72 653 AV *extra = (AV *)SvRV(ST(2));
73 653 SSize_t en = av_len(extra) + 1;
74 698 ▶ for (i = 0; i < en; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < en; i++) { 45 653 698
75 45 SV **e = av_fetch(extra, i, 0);
76 45 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 45 0 45
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 45 0 45
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 45 0 45
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 45 0 45
77 45 pr_add_route(aTHX_ routes, resolve, (HV *)SvRV(*e));
78 }
79 }
80 654 pr_build(aTHX_ rt, routes);
81 652 SP = PL_stack_base + ax - 1; /* $resolve ran Perl */
82 652 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 652 652
83 }
84  
85 # The compiled record hashrefs (code + guards + method + path), indexed by the
86 # record index match_static / match return.
87 SV *
88 records(self)
89 SV *self
90 CODE:
91 674 RETVAL = newRV_inc((SV *)punk_router_of(aTHX_ self)->records);
92 OUTPUT:
93 RETVAL
94  
95 # The record index -> dynamic position map, as an arrayref of IVs (-1 for a
96 # static route). Internal, and here so the map is falsifiable: named routes
97 # (punk_url.h) resolve a name to a RECORD index, and reaching that record's
98 # parsed segments means crossing from `records` (indexed by record index) to
99 # `recs` (indexed by dynamic position). Nothing else can see that it agrees.
100 SV *
101 _dyn_of(self)
102 SV *self
103 CODE:
104 {
105 1 pr_router *rt = punk_router_of(aTHX_ self);
106 1 AV *out = newAV();
107 int i;
108 7 ▶ for (i = 0; i < rt->nrecs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rt->nrecs; i++) 6 1 7
109 6 av_push(out, newSViv(rt->dyn_of[i]));
110 1 RETVAL = newRV_noinc((SV *)out);
111 }
112 OUTPUT:
113 RETVAL
114  
115 # The record index for an exact static match (HEAD falls back to GET), or the
116 # empty list. Consulted before mounts so a mounted app is never shadowed.
117 void
118 match_static(self, method, path)
119 SV *self
120 SV *method
121 SV *path
122 PPCODE:
123 {
124 10 pr_router *rt = punk_router_of(aTHX_ self);
125 STRLEN ml, pl;
126 10 ▶ const char *m = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV_const(method, ml); 10 0 10
127 10 const char *p = SvPV_const(path, pl);
128 10 IV idx = pr_static_match(aTHX_ rt, m, ml, p, pl);
129 10 ▶ if (idx >= 0) mXPUSHi(idx);
Branch: 2 branches
BranchTrueFalseTotal
if (idx >= 0) mXPUSHi(idx); 7 3 10
if (idx >= 0) mXPUSHi(idx); 0 7 7
130 }
131  
132 # Dynamic dispatch and the 405 cold path in one call:
133 # (idx, \%captures) a dynamic hit
134 # (undef, \@allow) the path exists under other methods (405)
135 # () no such path (404)
136 void
137 match(self, method, path)
138 SV *self
139 SV *method
140 SV *path
141 PPCODE:
142 {
143 21 pr_router *rt = punk_router_of(aTHX_ self);
144 STRLEN ml, pl;
145 21 ▶ const char *m = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV_const(method, ml); 21 0 21
146 21 const char *p = SvPV_const(path, pl);
147 21 HV *caps = (HV *)sv_2mortal((SV *)newHV());
148 21 IV idx = pr_match(aTHX_ rt, m, ml, p, pl, caps);
149 21 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 14 7 21
150 14 ▶ mXPUSHi(idx);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi(idx); 0 14 14
151 14 ▶ mXPUSHs(newRV_inc((SV *)caps));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)caps)); 0 14 14
152 }
153 else {
154 7 AV *allow = (AV *)sv_2mortal((SV *)newAV());
155 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
156 3 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 3 3
157 3 ▶ mXPUSHs(newRV_inc((SV *)allow));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)allow)); 0 3 3
158 }
159 /* else: empty list = 404 */
160 }
161 }
162  
163 void
164 DESTROY(self)
165 SV *self
166 CODE:
167 {
168 1137 ▶ if (SvROK(self) && SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvIOK(SvRV(self))) 1137 0 1137
if (SvROK(self) && SvIOK(SvRV(self))) 1137 0 1137
169 1137 pr_free(aTHX_ punk_router_of(aTHX_ self));
170 }
171  
172 MODULE = Punk PACKAGE = Punk::Proxy
173  
174 PROTOTYPES: DISABLE
175  
176 # Punk::Proxy is the compiled `proxy` trust policy: a blessed IV-ref to a
177 # pp_policy (punk_proxy.h), built once at to_app and read by pp_resolve at
178 # the top of every request. There is no other method - the object exists so
179 # the struct is freed with the compiled application.
180  
181 void
182 DESTROY(self)
183 SV *self
184 CODE:
185 {
186 10 ▶ if (SvROK(self) && SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvIOK(SvRV(self))) 10 0 10
if (SvROK(self) && SvIOK(SvRV(self))) 10 0 10
187 10 pp_free(aTHX_ (pp_policy *)INT2PTR(void *, SvIV(SvRV(self))));
188 }
189  
190 # _client($xff, $peer, $trust): the hop walk alone, for t/1210-proxy-parse.t.
191 # $trust is a hop count, an arrayref of CIDRs, or 'all' - the same shapes the
192 # keyword takes, compiled fresh per call. Author-facing, not documented.
193 SV *
194 _client(xff, peer, trust)
195 SV *xff
196 SV *peer
197 SV *trust
198 CODE:
199 {
200 36 HV *cfg = (HV *)sv_2mortal((SV *)newHV());
201 pp_policy *p;
202 36 STRLEN xl = 0, pl = 0, cl = 0;
203 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
204 72 ▶ const char *r = SvOK(peer) ? SvPV_const(peer, pl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *r = SvOK(peer) ? SvPV_const(peer, pl) : ""; 36 0 36
205 const char *c;
206 36 (void)hv_stores(cfg, "trust", newSVsv(trust));
207 36 p = pp_compile(aTHX_ cfg, "development");
208 36 c = pp_xff_client(x, xl, p, r, pl, &cl);
209 36 RETVAL = newSVpvn(c, cl);
210 36 pp_free(aTHX_ p);
211 }
212 OUTPUT:
213 RETVAL
« include/punk/punk_metrics.h ↑ Summary xs/views.xs »