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