xs/scope.xs

filter: Statementstmt 93.0% 67 / 72 Branchbran 59.2% 45 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.6% 112 / 148 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_views.h ↑ Summary include/punk/punk_cachefile.h »
1 MODULE = Punk PACKAGE = Punk::Router::Scope
2  
3 PROTOTYPES: DISABLE
4  
5 # The `under` handle, in C (punk_scope.h). A scope is a blessed hash of
6 # app/prefix/guards that records into the application exactly as the
7 # top-level DSL keywords do; Punk::Router::Scope.pm is the documentation.
8  
9 SV *
10 new(class, ...)
11 SV *class
12 CODE:
13 {
14 60 HV *h = newHV();
15 60 SV *app = NULL, *prefix = NULL, *guards = NULL;
16 int i;
17 240 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 180 60 240
18 180 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
19 180 ▶ if (kl == 3 && memEQ(k, "app", 3)) app = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 3 && memEQ(k, "app", 3)) app = ST(i + 1); 60 120 180
if (kl == 3 && memEQ(k, "app", 3)) app = ST(i + 1); 60 0 60
20 120 ▶ else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1); 120 0 120
else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1); 60 60 120
21 60 ▶ else if (kl == 6 && memEQ(k, "guards", 6)) guards = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 6 && memEQ(k, "guards", 6)) guards = ST(i + 1); 60 0 60
else if (kl == 6 && memEQ(k, "guards", 6)) guards = ST(i + 1); 60 0 60
22 }
23 60 ▶ (void)hv_stores(h, "app", app ? newSVsv(app) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "app", app ? newSVsv(app) : newSV(0)); 60 0 60
24 60 ▶ (void)hv_stores(h, "prefix",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(h, "prefix", 60 0 60
(void)hv_stores(h, "prefix", 60 0 60
25 (prefix && SvOK(prefix)) ? newSVsv(prefix) : newSVpvs(""));
26 60 ▶ (void)hv_stores(h, "guards",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "guards", 60 0 60
(void)hv_stores(h, "guards", 60 0 60
(void)hv_stores(h, "guards", 60 0 60
27 (guards && SvROK(guards) && SvTYPE(SvRV(guards)) == SVt_PVAV)
28 ? newSVsv(guards) : newRV_noinc((SV *)newAV()));
29 60 RETVAL = sv_bless(newRV_noinc((SV *)h), gv_stashsv(class, GV_ADD));
30 }
31 OUTPUT:
32 RETVAL
33  
34 # under($prefix, $guard?) -> a nested scope: prefixes concatenate, the guard
35 # joins the end of the chain, and both are frozen into the new object.
36 SV *
37 under(self, prefix, guard = &PL_sv_undef)
38 SV *self
39 SV *prefix
40 SV *guard
41 CODE:
42 {
43 5 HV *h = scope_hv(aTHX_ self);
44 5 SV *pfx = scope_get(aTHX_ h, "prefix");
45 5 ▶ SV *full = sv_2mortal(newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no));
Branch: 2 branches
BranchTrueFalseTotal
SV *full = sv_2mortal(newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no)); 5 0 5
SV *full = sv_2mortal(newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no)); 5 0 5
46 5 ▶ if (!(pfx && SvOK(pfx))) sv_setpvs(full, "");
Branch: 2 branches
BranchTrueFalseTotal
if (!(pfx && SvOK(pfx))) sv_setpvs(full, ""); 5 0 5
if (!(pfx && SvOK(pfx))) sv_setpvs(full, ""); 0 5 5
47 5 sv_catsv(full, prefix);
48 5 RETVAL = scope_new_like(aTHX_ self, scope_get(aTHX_ h, "app"), full,
49 sv_2mortal(scope_guards_copy(aTHX_ h, guard)));
50 }
51 OUTPUT:
52 RETVAL
53  
54 # _route($method, $path, $target), and the verbs that are it with the method
55 # already supplied. Chains.
56 #
57 # Declared variadic because the aliases differ in arity - get($path, $target)
58 # against _route($method, $path, $target) - and xsubpp generates one usage
59 # check from the declared parameter list for the whole ALIAS group.
60 SV *
61 _route(self, ...)
62 SV *self
63 ALIAS:
64 get = 1
65 post = 2
66 put = 3
67 patch = 4
68 del = 5
69 any = 6
70 CODE:
71 {
72 static const char *VERB[] =
73 { NULL, "GET", "POST", "PUT", "PATCH", "DELETE", "ANY" };
74 static const char *NAME[] =
75 { "_route", "get", "post", "put", "patch", "del", "any" };
76 51 HV *h = scope_hv(aTHX_ self);
77 51 SV *method, *path, *target, *opts = NULL, *argv[5];
78  
79 51 ▶ if (ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix) { 49 2 51
80 49 ▶ if (items != 3 && items != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (items != 3 && items != 4) 9 40 49
if (items != 3 && items != 4) 1 8 9
81 1 croak("Usage: Punk::Router::Scope::%s(self, path, target, "
82 "\\%%opts?)", NAME[ix]);
83 48 method = sv_2mortal(newSVpv(VERB[ix], 0));
84 48 path = ST(1);
85 48 target = ST(2);
86 48 ▶ if (items == 4) opts = ST(3);
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) opts = ST(3); 8 40 48
87 }
88 else {
89 2 ▶ if (items != 4 && items != 5)
Branch: 2 branches
BranchTrueFalseTotal
if (items != 4 && items != 5) 1 1 2
if (items != 4 && items != 5) 1 0 1
90 1 croak("Usage: Punk::Router::Scope::_route(self, method, "
91 "path, target, \\%%opts?)");
92 1 method = ST(1);
93 1 path = ST(2);
94 1 target = ST(3);
95 1 ▶ if (items == 5) opts = ST(4);
Branch: 1 branch
BranchTrueFalseTotal
if (items == 5) opts = ST(4); 0 1 1
96 }
97  
98 49 argv[0] = method;
99 49 argv[1] = scope_full_path(aTHX_ h, path);
100 49 argv[2] = target;
101 49 argv[3] = sv_2mortal(scope_guards_copy(aTHX_ h, NULL));
102 49 ▶ if (opts) argv[4] = opts;
Branch: 1 branch
BranchTrueFalseTotal
if (opts) argv[4] = opts; 8 41 49
103 49 ▶ scope_app_call(aTHX_ h, "route", argv, opts ? 5 : 4);
Branch: 1 branch
BranchTrueFalseTotal
scope_app_call(aTHX_ h, "route", argv, opts ? 5 : 4); 8 41 49
104 49 RETVAL = newSVsv(self);
105 }
106 OUTPUT:
107 RETVAL
108  
109 # api($spec, $opts?) -> the Punk::Mount::OpenAPI, which takes this scope's
110 # prefix and guards. Returns the mount, not the scope - `docs` wants it.
111 SV *
112 api(self, spec, opts = &PL_sv_undef)
113 SV *self
114 SV *spec
115 SV *opts
116 CODE:
117 {
118 18 HV *h = scope_hv(aTHX_ self);
119 18 SV *app = scope_get(aTHX_ h, "app");
120 SV *argv[3], *mount;
121 18 argv[0] = spec;
122 18 argv[1] = opts;
123 18 argv[2] = self;
124 18 ▶ mount = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "api", argv, 3, 1);
Branch: 1 branch
BranchTrueFalseTotal
mount = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "api", argv, 3, 1); 18 0 18
125 18 ▶ RETVAL = mount ? mount : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = mount ? mount : newSV(0); 0 18 18
126 }
127 OUTPUT:
128 RETVAL
129  
130 # websocket($path, $target, $opts?): the prefix and the guard chain apply
131 # exactly as they do to an ordinary route, so a guard can refuse the
132 # connection with a normal HTTP response before the upgrade. Chains.
133 SV *
134 websocket(self, path, target, opts = &PL_sv_undef)
135 SV *self
136 SV *path
137 SV *target
138 SV *opts
139 CODE:
140 {
141 3 HV *h = scope_hv(aTHX_ self);
142 SV *argv[4];
143 3 argv[0] = scope_full_path(aTHX_ h, path);
144 3 argv[1] = target;
145 3 argv[2] = opts;
146 3 argv[3] = sv_2mortal(scope_guards_copy(aTHX_ h, NULL));
147 3 scope_app_call(aTHX_ h, "websocket", argv, 4);
148 3 RETVAL = newSVsv(self);
149 }
150 OUTPUT:
151 RETVAL
152  
153 # headers(%pairs) / headers(\%pairs): this scope's response-header policy -
154 # the same pairs the app-wide `headers` keyword takes, applied only to
155 # requests under this prefix (including the 404s under it) and ahead of the
156 # application-wide policy, so a scope can tighten, add, or - with an undef
157 # value - drop a header for its subtree. Chains.
158 SV *
159 headers(self, ...)
160 SV *self
161 CODE:
162 {
163 3 HV *h = scope_hv(aTHX_ self);
164 3 HV *cfg = newHV();
165 3 SV *pfx = scope_get(aTHX_ h, "prefix");
166 SV *argv[2];
167 int i;
168 3 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 3 3
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 0 0
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 0 0
169 0 HE *e; HV *given = (HV *)SvRV(ST(1));
170 0 hv_iterinit(given);
171 0 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 0 0 0
172 0 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 0 0
173 0 (void)hv_store(cfg, k, (I32)kl,
174 newSVsv(hv_iterval(given, e)), 0);
175 }
176 }
177 8 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 5 3 8
178 5 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
179 5 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
180 }
181 3 ▶ argv[0] = (pfx && SvOK(pfx)) ? pfx : &PL_sv_no;
Branch: 2 branches
BranchTrueFalseTotal
argv[0] = (pfx && SvOK(pfx)) ? pfx : &PL_sv_no; 3 0 3
argv[0] = (pfx && SvOK(pfx)) ? pfx : &PL_sv_no; 3 0 3
182 3 argv[1] = sv_2mortal(newRV_noinc((SV *)cfg));
183 3 scope_app_call(aTHX_ h, "headers_scoped", argv, 2);
184 3 RETVAL = newSVsv(self);
185 }
186 OUTPUT:
187 RETVAL
188  
189 SV *
190 prefix(self)
191 SV *self
192 CODE:
193 {
194 19 SV *p = scope_get(aTHX_ scope_hv(aTHX_ self), "prefix");
195 19 ▶ RETVAL = (p && SvOK(p)) ? newSVsv(p) : newSVpvs("");
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (p && SvOK(p)) ? newSVsv(p) : newSVpvs(""); 19 0 19
RETVAL = (p && SvOK(p)) ? newSVsv(p) : newSVpvs(""); 19 0 19
196 }
197 OUTPUT:
198 RETVAL
199  
200 # A copy, so a caller cannot reach in and change what this scope's already
201 # registered routes were given.
202 SV *
203 guards(self)
204 SV *self
205 CODE:
206 23 RETVAL = scope_guards_copy(aTHX_ scope_hv(aTHX_ self), NULL);
207 OUTPUT:
208 RETVAL
« include/punk/punk_views.h ↑ Summary include/punk/punk_cachefile.h »