| 1 |
|
| /* punk_app.h - the Punk::App registrar, in C. |
| 2 |
|
| * |
| 3 |
|
| * The application object is a blessed hash (the DSL keywords and plugins push |
| 4 |
|
| * into its slots at boot); new and the registrar methods - route, under, api, |
| 5 |
|
| * docs, static, mount, websocket, views, database, model_class, hook, |
| 6 |
|
| * middleware, on_error, helper, plugin - are XSUBs (xs/app.xs). The boot-time |
| 7 |
|
| * orchestration that drives the other XS subsystems (compile, the config |
| 8 |
|
| * loader, target/context resolution) stays Perl in Punk::App, since it is a |
| 9 |
|
| * sequence of require / subsystem ->compile calls. |
| 10 |
|
| * |
| 11 |
|
| * Must be included after punk_context.h (for pcx_call_meth). |
| 12 |
|
| */ |
| 13 |
|
| |
| 14 |
|
| #ifndef PUNK_APP_H |
| 15 |
|
| #define PUNK_APP_H |
| 16 |
|
| |
| 17 |
131311 |
| static HV *app_hv(pTHX_ SV *self) { |
| 18 |
131311 |
▶ | if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
131311 |
0 |
131311 |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
0 |
131311 |
131311 |
|
| 19 |
0 |
| croak("Punk::App: not an application object"); |
| 20 |
131311 |
| return (HV *)SvRV(self); |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
294316 |
| static SV *app_get(pTHX_ HV *h, const char *k) { |
| 24 |
294316 |
| SV **e = hv_fetch(h, k, (I32)strlen(k), 0); |
| 25 |
294316 |
▶ | return (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; |
289745 |
4571 |
294316 |
| return (e && *e) ? *e : NULL; |
289745 |
0 |
289745 |
|
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
|
| /* the AV in slot k, creating an empty one if absent */ |
| 29 |
3914 |
| static AV *app_av(pTHX_ HV *h, const char *k) { |
| 30 |
3914 |
| SV *v = app_get(aTHX_ h, k); |
| 31 |
3914 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); |
3014 |
900 |
3914 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); |
3014 |
0 |
3014 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); |
3014 |
0 |
3014 |
|
| 32 |
|
| { |
| 33 |
900 |
| AV *a = newAV(); |
| 34 |
900 |
| (void)hv_store(h, k, (I32)strlen(k), newRV_noinc((SV *)a), 0); |
| 35 |
900 |
| return a; |
| 36 |
|
| } |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| /* the HV in slot k, creating an empty one if absent */ |
| 40 |
1743 |
| static HV *app_hash(pTHX_ HV *h, const char *k) { |
| 41 |
1743 |
| SV *v = app_get(aTHX_ h, k); |
| 42 |
1743 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); |
1648 |
95 |
1743 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); |
1648 |
0 |
1648 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); |
1648 |
0 |
1648 |
|
| 43 |
|
| { |
| 44 |
95 |
| HV *n = newHV(); |
| 45 |
95 |
| (void)hv_store(h, k, (I32)strlen(k), newRV_noinc((SV *)n), 0); |
| 46 |
95 |
| return n; |
| 47 |
|
| } |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* A deep copy of plain data: unblessed hash and array references are copied |
| 51 |
|
| * through, everything else (scalars, blessed objects, coderefs) by newSVsv. |
| 52 |
|
| * For the read-back accessors - a plugin must not be able to edit the |
| 53 |
|
| * application's configuration through a nested hash the shallow copy shared. */ |
| 54 |
76 |
| static SV *app_deep_copy(pTHX_ SV *sv) { |
| 55 |
76 |
▶ | if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
76 |
0 |
76 |
| if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
29 |
47 |
76 |
| if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
29 |
0 |
29 |
|
| 56 |
29 |
| SV *t = SvRV(sv); |
| 57 |
29 |
▶ | if (SvTYPE(t) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(t) == SVt_PVHV) { |
23 |
6 |
29 |
|
| 58 |
23 |
| HV *src = (HV *)t, *dst = newHV(); |
| 59 |
|
| HE *e; |
| 60 |
23 |
| hv_iterinit(src); |
| 61 |
81 |
▶ | while ((e = hv_iternext(src))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = hv_iternext(src))) |
58 |
23 |
81 |
|
| 62 |
58 |
| (void)hv_store_ent(dst, hv_iterkeysv(e), |
| 63 |
|
| app_deep_copy(aTHX_ hv_iterval(src, e)), 0); |
| 64 |
23 |
| return newRV_noinc((SV *)dst); |
| 65 |
|
| } |
| 66 |
6 |
▶ | if (SvTYPE(t) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(t) == SVt_PVAV) { |
3 |
3 |
6 |
|
| 67 |
3 |
| AV *src = (AV *)t, *dst = newAV(); |
| 68 |
3 |
| SSize_t i, n = av_len(src) + 1; |
| 69 |
12 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
9 |
3 |
12 |
|
| 70 |
9 |
| SV **x = av_fetch(src, i, 0); |
| 71 |
9 |
▶ | av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef)); |
9 |
0 |
9 |
| av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef)); |
9 |
0 |
9 |
|
| 72 |
|
| } |
| 73 |
3 |
| return newRV_noinc((SV *)dst); |
| 74 |
|
| } |
| 75 |
|
| } |
| 76 |
50 |
▶ | return newSVsv(sv ? sv : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| return newSVsv(sv ? sv : &PL_sv_undef); |
50 |
0 |
50 |
|
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* strip a single trailing '/' from a fresh copy of an SV's string */ |
| 80 |
161 |
| static SV *app_strip_slash(pTHX_ SV *sv) { |
| 81 |
|
| STRLEN l; |
| 82 |
322 |
▶ | const char *p = SvOK(sv) ? SvPV_const(sv, l) : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = SvOK(sv) ? SvPV_const(sv, l) : ""; |
161 |
0 |
161 |
|
| 83 |
161 |
▶ | if (!SvOK(sv)) l = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv)) l = 0; |
0 |
161 |
161 |
|
| 84 |
161 |
▶ | if (l && p[l - 1] == '/') l--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l && p[l - 1] == '/') l--; |
161 |
0 |
161 |
| if (l && p[l - 1] == '/') l--; |
15 |
146 |
161 |
|
| 85 |
161 |
| return newSVpvn(p, l); |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /* ---- config-shape helpers (for _apply_config) ------------------------------ |
| 89 |
|
| * These mirror Punk::App's _pairs / _list: turning the loose YAML shapes a |
| 90 |
|
| * config block may take into a uniform list to drive the registrar with. */ |
| 91 |
|
| |
| 92 |
|
| /* a mapping (name => opts) or a list of names / [name,opts] / {name=>opts} |
| 93 |
|
| * into a mortal AV of [name, opts] pair-refs (opts defaulting to {}). */ |
| 94 |
20 |
| static AV *app_pairs(pTHX_ SV *node, const char *what) { |
| 95 |
20 |
| AV *out = (AV *)sv_2mortal((SV *)newAV()); |
| 96 |
40 |
▶ | if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
20 |
0 |
20 |
| if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
20 |
0 |
20 |
|
| 97 |
20 |
| HV *h = (HV *)SvRV(node); HE *e; |
| 98 |
20 |
| hv_iterinit(h); |
| 99 |
40 |
▶ | while ((e = hv_iternext(h))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = hv_iternext(h))) { |
20 |
20 |
40 |
|
| 100 |
20 |
| SV *v = hv_iterval(h, e); |
| 101 |
20 |
| AV *pair = newAV(); |
| 102 |
20 |
| av_push(pair, newSVsv(hv_iterkeysv(e))); |
| 103 |
20 |
▶ | av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
20 |
0 |
20 |
| av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
20 |
0 |
20 |
|
| 104 |
|
| : newRV_noinc((SV *)newHV())); |
| 105 |
20 |
| av_push(out, newRV_noinc((SV *)pair)); |
| 106 |
|
| } |
| 107 |
|
| } |
| 108 |
0 |
▶ | else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
0 |
0 |
0 |
| else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 109 |
0 |
| AV *a = (AV *)SvRV(node); SSize_t i, n = av_len(a) + 1; |
| 110 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 111 |
0 |
| SV **ep = av_fetch(a, i, 0); |
| 112 |
0 |
▶ | SV *el = (ep && *ep) ? *ep : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *el = (ep && *ep) ? *ep : &PL_sv_undef; |
0 |
0 |
0 |
| SV *el = (ep && *ep) ? *ep : &PL_sv_undef; |
0 |
0 |
0 |
|
| 113 |
0 |
| AV *pair = newAV(); |
| 114 |
0 |
▶ | if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) { |
0 |
0 |
0 |
| if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 115 |
0 |
| AV *ea = (AV *)SvRV(el); |
| 116 |
0 |
| SV **k = av_fetch(ea, 0, 0), **v = av_fetch(ea, 1, 0); |
| 117 |
0 |
▶ | av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef)); |
0 |
0 |
0 |
| av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef)); |
0 |
0 |
0 |
|
| 118 |
0 |
▶ | av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) |
Branch: 3 branches
| Branch | True | False | Total |
| av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) |
0 |
0 |
0 |
| av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) |
0 |
0 |
0 |
| av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) |
0 |
0 |
0 |
|
| 119 |
|
| : newRV_noinc((SV *)newHV())); |
| 120 |
|
| } |
| 121 |
0 |
▶ | else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { |
0 |
0 |
0 |
| else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 122 |
0 |
| HV *eh = (HV *)SvRV(el); HE *he; |
| 123 |
0 |
| hv_iterinit(eh); |
| 124 |
0 |
▶ | if ((he = hv_iternext(eh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((he = hv_iternext(eh))) { |
0 |
0 |
0 |
|
| 125 |
0 |
| SV *v = hv_iterval(eh, he); |
| 126 |
0 |
| av_push(pair, newSVsv(hv_iterkeysv(he))); |
| 127 |
0 |
▶ | av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
0 |
0 |
0 |
| av_push(pair, (v && SvOK(v)) ? newSVsv(v) |
0 |
0 |
0 |
|
| 128 |
|
| : newRV_noinc((SV *)newHV())); |
| 129 |
|
| } |
| 130 |
|
| } |
| 131 |
|
| else { |
| 132 |
0 |
| av_push(pair, newSVsv(el)); |
| 133 |
0 |
| av_push(pair, newRV_noinc((SV *)newHV())); |
| 134 |
|
| } |
| 135 |
0 |
| av_push(out, newRV_noinc((SV *)pair)); |
| 136 |
|
| } |
| 137 |
|
| } |
| 138 |
|
| else { |
| 139 |
0 |
| croak("Punk: the config %s block must be a mapping or a list", what); |
| 140 |
|
| } |
| 141 |
20 |
| return out; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| /* a scalar / arrayref / hashref (keys) into a mortal AV of names */ |
| 145 |
0 |
| static AV *app_list(pTHX_ SV *node, const char *what) { |
| 146 |
0 |
| AV *out = (AV *)sv_2mortal((SV *)newAV()); |
| 147 |
0 |
▶ | if (!SvROK(node)) { av_push(out, newSVsv(node)); return out; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(node)) { av_push(out, newSVsv(node)); return out; } |
0 |
0 |
0 |
|
| 148 |
0 |
▶ | if (SvTYPE(SvRV(node)) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(node)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 149 |
0 |
| AV *a = (AV *)SvRV(node); SSize_t i, n = av_len(a) + 1; |
| 150 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 151 |
0 |
| SV **e = av_fetch(a, i, 0); |
| 152 |
0 |
▶ | av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); |
0 |
0 |
0 |
| av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); |
0 |
0 |
0 |
|
| 153 |
|
| } |
| 154 |
0 |
| return out; |
| 155 |
|
| } |
| 156 |
0 |
▶ | if (SvTYPE(SvRV(node)) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(node)) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 157 |
0 |
| HV *h = (HV *)SvRV(node); HE *e; |
| 158 |
0 |
| hv_iterinit(h); |
| 159 |
0 |
▶ | while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); |
0 |
0 |
0 |
|
| 160 |
0 |
| return out; |
| 161 |
|
| } |
| 162 |
0 |
| croak("Punk: the config %s block must be a list", what); |
| 163 |
|
| return out; |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| /* the sorted (string) keys of an HV as a mortal AV of key SVs */ |
| 167 |
670 |
| static AV *app_sorted_keys(pTHX_ HV *h) { |
| 168 |
670 |
| AV *out = (AV *)sv_2mortal((SV *)newAV()); |
| 169 |
|
| HE *e; |
| 170 |
670 |
| hv_iterinit(h); |
| 171 |
868 |
▶ | while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); |
198 |
670 |
868 |
|
| 172 |
670 |
| sortsv(AvARRAY(out), (size_t)(av_len(out) + 1), Perl_sv_cmp); |
| 173 |
670 |
| return out; |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| /* call $self->$meth(@$args), discarding the (chainable) return */ |
| 177 |
|
| /* Is a config value the bare "on" form? A YAML `true` does not arrive as |
| 178 |
|
| * the scalar 1: Punk::Config loads booleans as JSON::PP::Boolean objects, |
| 179 |
|
| * a blessed scalar ref whose truth is an overload. So a plain truthy |
| 180 |
|
| * scalar is on, a boolean object is whatever it says, and any other |
| 181 |
|
| * reference - a mapping, a list - is not this form at all. */ |
| 182 |
6 |
| static int app_cfg_on(pTHX_ SV *sv) { |
| 183 |
6 |
▶ | if (!sv || !SvOK(sv)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return 0; |
6 |
0 |
6 |
| if (!sv || !SvOK(sv)) return 0; |
0 |
6 |
6 |
|
| 184 |
6 |
▶ | if (!SvROK(sv)) return SvTRUE(sv) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(sv)) return SvTRUE(sv) ? 1 : 0; |
0 |
6 |
6 |
|
| 185 |
6 |
▶ | if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV) |
6 |
0 |
6 |
| if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV) |
6 |
0 |
6 |
|
| 186 |
6 |
| return SvTRUE(sv) ? 1 : 0; |
| 187 |
0 |
| return 0; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
52 |
| static void app_call_list(pTHX_ SV *self, const char *meth, AV *args) { |
| 191 |
52 |
| SSize_t n = av_len(args) + 1, i; |
| 192 |
|
| SV **argv, *r; |
| 193 |
52 |
▶ | Newx(argv, n > 0 ? n : 1, SV *); |
Branch: 3 branches
| Branch | True | False | Total |
| Newx(argv, n > 0 ? n : 1, SV *); |
48 |
4 |
52 |
| Newx(argv, n > 0 ? n : 1, SV *); |
0 |
48 |
48 |
| Newx(argv, n > 0 ? n : 1, SV *); |
48 |
4 |
52 |
|
| 194 |
152 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
100 |
52 |
152 |
|
| 195 |
100 |
| SV **e = av_fetch(args, i, 0); |
| 196 |
100 |
▶ | argv[i] = (e && *e) ? *e : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| argv[i] = (e && *e) ? *e : &PL_sv_undef; |
100 |
0 |
100 |
| argv[i] = (e && *e) ? *e : &PL_sv_undef; |
100 |
0 |
100 |
|
| 197 |
|
| } |
| 198 |
52 |
| r = pcx_call_meth(aTHX_ self, meth, argv, (int)n, 0); |
| 199 |
52 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
52 |
52 |
|
| 200 |
52 |
| Safefree(argv); |
| 201 |
52 |
| } |
| 202 |
|
| |
| 203 |
|
| /* ---- the one-hashref route form ------------------------------------------- |
| 204 |
|
| * |
| 205 |
|
| * post '/upload' => { cb => 'Web::File#create', max_body => 50_000_000 }; |
| 206 |
|
| * |
| 207 |
|
| * instead of the positional |
| 208 |
|
| * |
| 209 |
|
| * post '/upload' => 'Web::File#create', { max_body => 50_000_000 }; |
| 210 |
|
| * |
| 211 |
|
| * Both spellings are supported; this splits the first into the second at the |
| 212 |
|
| * front door, so the router, the compiled records and every consumer |
| 213 |
|
| * downstream see exactly what they have always seen. |
| 214 |
|
| * |
| 215 |
|
| * Purely additive: a hashref in the target slot was a boot croak before this |
| 216 |
|
| * existed ("must be a coderef or 'Controller#method'"), so no application can |
| 217 |
|
| * have been relying on the behaviour being replaced. |
| 218 |
|
| * |
| 219 |
|
| * `what` names the declaration for the croaks ("route POST", "websocket", |
| 220 |
|
| * "sse"); `path` is only ever read for those messages. Both out-params are |
| 221 |
|
| * left alone when `*target` is not a hashref. |
| 222 |
|
| */ |
| 223 |
61388 |
| static void pk_spec_split(pTHX_ const char *what, SV *path, |
| 224 |
|
| SV **target, SV **opts) { |
| 225 |
|
| HV *spec, *o; |
| 226 |
|
| SV **cb; |
| 227 |
|
| HE *he; |
| 228 |
|
| |
| 229 |
61388 |
▶ | if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) |
61388 |
0 |
61388 |
| if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) |
61356 |
32 |
61388 |
| if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) |
61331 |
25 |
61356 |
|
| 230 |
61363 |
| return; |
| 231 |
25 |
| spec = (HV *)SvRV(*target); |
| 232 |
|
| |
| 233 |
25 |
▶ | if (opts && *opts && SvOK(*opts)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && *opts && SvOK(*opts)) |
25 |
0 |
25 |
| if (opts && *opts && SvOK(*opts)) |
25 |
0 |
25 |
| if (opts && *opts && SvOK(*opts)) |
1 |
24 |
25 |
|
| 234 |
2 |
▶ | croak("Punk: %s %s takes its options either inside the hashref or " |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk: %s %s takes its options either inside the hashref or " |
1 |
0 |
1 |
|
| 235 |
|
| "after the target, not both", |
| 236 |
|
| what, SvOK(path) ? SvPV_nolen(path) : "(no path)"); |
| 237 |
|
| |
| 238 |
24 |
| cb = hv_fetchs(spec, "cb", 0); |
| 239 |
24 |
▶ | if (!(cb && *cb && SvOK(*cb))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cb && *cb && SvOK(*cb))) |
22 |
2 |
24 |
| if (!(cb && *cb && SvOK(*cb))) |
22 |
0 |
22 |
| if (!(cb && *cb && SvOK(*cb))) |
1 |
21 |
22 |
|
| 240 |
6 |
▶ | croak("Punk: %s %s has options but no `cb` - a route needs a handler " |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk: %s %s has options but no `cb` - a route needs a handler " |
3 |
0 |
3 |
|
| 241 |
|
| "(a coderef or 'Controller#method')", |
| 242 |
|
| what, SvOK(path) ? SvPV_nolen(path) : "(no path)"); |
| 243 |
|
| |
| 244 |
|
| /* Copy the remaining keys rather than deleting `cb` from the caller's |
| 245 |
|
| * hashref. A spec built once and declared twice |
| 246 |
|
| * |
| 247 |
|
| * my %spec = (cb => \&h, max_body => 10); |
| 248 |
|
| * get '/a' => \%spec; post '/b' => \%spec; |
| 249 |
|
| * |
| 250 |
|
| * must give two working routes; mutating it would leave the second |
| 251 |
|
| * without a handler, and would pass every test but that one. */ |
| 252 |
21 |
| o = newHV(); |
| 253 |
21 |
| hv_iterinit(spec); |
| 254 |
54 |
▶ | while ((he = hv_iternext(spec))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(spec))) { |
33 |
21 |
54 |
|
| 255 |
|
| STRLEN kl; |
| 256 |
33 |
▶ | const char *k = HePV(he, kl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, kl); |
0 |
33 |
33 |
|
| 257 |
33 |
▶ | if (kl == 2 && memEQ(k, "cb", 2)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 2 && memEQ(k, "cb", 2)) continue; |
21 |
12 |
33 |
| if (kl == 2 && memEQ(k, "cb", 2)) continue; |
21 |
0 |
21 |
|
| 258 |
12 |
| (void)hv_store(o, k, (I32)kl, newSVsv(hv_iterval(spec, he)), 0); |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* the target LAST: it aliases into the spec we have just finished |
| 262 |
|
| * reading, and reassigning it first would be reading a freed hashref if |
| 263 |
|
| * the caller held no other reference */ |
| 264 |
21 |
| *target = sv_2mortal(newSVsv(*cb)); |
| 265 |
21 |
▶ | if (opts) *opts = sv_2mortal(newRV_noinc((SV *)o)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) *opts = sv_2mortal(newRV_noinc((SV *)o)); |
21 |
0 |
21 |
|
| 266 |
0 |
| else SvREFCNT_dec((SV *)o); |
| 267 |
|
| } |
| 268 |
|
| |
| 269 |
|
| /* "METHOD PATH" -> the compiled record, as a mortal HV. |
| 270 |
|
| * |
| 271 |
|
| * A route option is recorded at declaration as { method, path, ... } and |
| 272 |
|
| * compiled at to_app onto the record the router built, which is the only |
| 273 |
|
| * place the guards AV lives. Every option that becomes a guard needs the |
| 274 |
|
| * same lookup, so it is written once; `what` names the option in the two |
| 275 |
|
| * croaks, which are both "the application declared this against a route |
| 276 |
|
| * that does not exist". */ |
| 277 |
8 |
| static HV *pk_route_index(pTHX_ SV *self, const char *what) { |
| 278 |
8 |
| HV *h = (HV *)SvRV(self); |
| 279 |
8 |
| SV **rop = hv_fetchs(h, K_ROUTER, 0); |
| 280 |
|
| SV *recs_rv; |
| 281 |
|
| AV *recs; |
| 282 |
|
| HV *by; |
| 283 |
|
| SSize_t ri, rn; |
| 284 |
8 |
▶ | if (!(rop && *rop && SvOK(*rop))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(rop && *rop && SvOK(*rop))) |
8 |
0 |
8 |
| if (!(rop && *rop && SvOK(*rop))) |
8 |
0 |
8 |
| if (!(rop && *rop && SvOK(*rop))) |
0 |
8 |
8 |
|
| 285 |
0 |
| croak("Punk: %s routes with no router", what); |
| 286 |
8 |
| recs_rv = pcx_call_meth(aTHX_ *rop, "records", NULL, 0, 1); |
| 287 |
8 |
▶ | if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { |
8 |
0 |
8 |
| if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { |
8 |
0 |
8 |
| if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { |
0 |
8 |
8 |
|
| 288 |
0 |
▶ | if (recs_rv) SvREFCNT_dec(recs_rv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (recs_rv) SvREFCNT_dec(recs_rv); |
0 |
0 |
0 |
|
| 289 |
0 |
| croak("Punk: %s routes but no compiled records", what); |
| 290 |
|
| } |
| 291 |
8 |
| sv_2mortal(recs_rv); |
| 292 |
8 |
| recs = (AV *)SvRV(recs_rv); |
| 293 |
8 |
| by = (HV *)sv_2mortal((SV *)newHV()); |
| 294 |
8 |
| rn = av_len(recs) + 1; |
| 295 |
42 |
▶ | for (ri = 0; ri < rn; ri++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ri = 0; ri < rn; ri++) { |
34 |
8 |
42 |
|
| 296 |
34 |
| SV **rp = av_fetch(recs, ri, 0); |
| 297 |
|
| HV *rec; |
| 298 |
|
| SV **m, **p, *key; |
| 299 |
34 |
▶ | if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) |
34 |
0 |
34 |
| if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) |
34 |
0 |
34 |
| if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) |
34 |
0 |
34 |
| if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) |
0 |
34 |
34 |
|
| 300 |
0 |
| continue; |
| 301 |
34 |
| rec = (HV *)SvRV(*rp); |
| 302 |
34 |
| m = hv_fetchs(rec, K_METHOD, 0); |
| 303 |
34 |
| p = hv_fetchs(rec, K_PATH, 0); |
| 304 |
34 |
▶ | if (!(m && *m && p && *p)) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(m && *m && p && *p)) continue; |
34 |
0 |
34 |
| if (!(m && *m && p && *p)) continue; |
34 |
0 |
34 |
| if (!(m && *m && p && *p)) continue; |
34 |
0 |
34 |
| if (!(m && *m && p && *p)) continue; |
0 |
34 |
34 |
|
| 305 |
34 |
| key = sv_2mortal(newSVsv(*m)); |
| 306 |
34 |
| sv_catpvs(key, " "); |
| 307 |
34 |
| sv_catsv(key, *p); |
| 308 |
34 |
| (void)hv_store_ent(by, key, newSVsv(*rp), 0); |
| 309 |
|
| } |
| 310 |
8 |
| return by; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| /* Append a guard closure to a compiled record's guards, vivifying the AV. |
| 314 |
|
| * Takes the +1 the caller holds. */ |
| 315 |
14 |
| static void pk_route_guard_push(pTHX_ HV *rec, SV *guard) { |
| 316 |
14 |
| SV **gp = hv_fetchs(rec, K_GUARDS, 0); |
| 317 |
14 |
▶ | if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { |
14 |
0 |
14 |
| if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { |
14 |
0 |
14 |
| if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { |
14 |
0 |
14 |
| if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { |
0 |
14 |
14 |
|
| 318 |
0 |
| (void)hv_stores(rec, K_GUARDS, newRV_noinc((SV *)newAV())); |
| 319 |
0 |
| gp = hv_fetchs(rec, K_GUARDS, 0); |
| 320 |
|
| } |
| 321 |
14 |
| av_push((AV *)SvRV(*gp), guard); |
| 322 |
14 |
| } |
| 323 |
|
| |
| 324 |
|
| #endif /* PUNK_APP_H */ |