| 1 |
|
| /* punk_url.h - named routes: what a name may be, and the URL a name builds. |
| 2 |
|
| * |
| 3 |
|
| * A route carries a `name` option, the keyword records it, to_app stamps it |
| 4 |
|
| * onto the compiled record and resolves the whole set into one name -> |
| 5 |
|
| * record index hash. $c->url_for reads that hash and walks the pr_seg array |
| 6 |
|
| * punk_route.h already parsed at to_app. |
| 7 |
|
| * |
| 8 |
|
| * The validator lives here rather than in xs/app.xs because three callers |
| 9 |
|
| * want one answer: the route option, the websocket/sse keywords, and the |
| 10 |
|
| * documentation that has to say what a name is. |
| 11 |
|
| */ |
| 12 |
|
| |
| 13 |
|
| #ifndef PUNK_URL_H |
| 14 |
|
| #define PUNK_URL_H |
| 15 |
|
| |
| 16 |
|
| /* Reserved argument words in url_for. A route named for one of these would |
| 17 |
|
| * be callable and un-passable at once: url_for('query', query => {...}) has |
| 18 |
|
| * no reading that names the route AND its query string. */ |
| 19 |
|
| static const char *const PK_URL_RESERVED[] = { "absolute", "query" }; |
| 20 |
|
| #define PK_URL_N_RESERVED 2 |
| 21 |
|
| |
| 22 |
|
| /* Is this a usable route name? A name is an IDENTIFIER, not a string: it is |
| 23 |
|
| * written in url_for('book'), in {% url.book %}, in `punk routes --name |
| 24 |
|
| * book` and in a Punk::Test arrayref, and only an identifier works in all |
| 25 |
|
| * four. Croaks with the reason, at the keyword, so a bad name fails on the |
| 26 |
|
| * line that wrote it rather than at to_app or at a render. |
| 27 |
|
| * |
| 28 |
|
| * `what` names the caller for the message ("route", "websocket", "sse"). |
| 29 |
|
| */ |
| 30 |
103 |
| static void pk_url_check_name(pTHX_ SV *name, const char *what, |
| 31 |
|
| SV *method, SV *path) { |
| 32 |
|
| const char *n; |
| 33 |
|
| STRLEN nl, i; |
| 34 |
|
| int k; |
| 35 |
|
| |
| 36 |
103 |
▶ | if (SvROK(name)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(name)) |
1 |
102 |
103 |
|
| 37 |
1 |
| croak("Punk: %s name must be a string, not a %s - a route has one " |
| 38 |
|
| "name, and aliases are not a thing", what, |
| 39 |
|
| sv_reftype(SvRV(name), 0)); |
| 40 |
102 |
▶ | if (!SvOK(name) || !SvCUR(name)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(name) || !SvCUR(name)) |
101 |
1 |
102 |
| if (!SvOK(name) || !SvCUR(name)) |
1 |
100 |
101 |
|
| 41 |
6 |
▶ | croak("Punk: %s name on %s %s is empty - a name is what url_for " |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk: %s name on %s %s is empty - a name is what url_for " |
2 |
0 |
2 |
| croak("Punk: %s name on %s %s is empty - a name is what url_for " |
2 |
0 |
2 |
|
| 42 |
|
| "asks for", what, |
| 43 |
|
| method ? SvPV_nolen(method) : "?", path ? SvPV_nolen(path) : "?"); |
| 44 |
|
| |
| 45 |
100 |
| n = SvPV_const(name, nl); |
| 46 |
489 |
▶ | for (i = 0; i < nl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nl; i++) { |
393 |
96 |
489 |
|
| 47 |
393 |
| const unsigned char ch = (unsigned char)n[i]; |
| 48 |
|
| /* |
| 49 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 50 |
|
| # uncoverable condition right count:4 class:ignore_covered_err |
| 51 |
|
| # uncoverable condition right count:6 class:ignore_covered_err */ |
| 52 |
393 |
▶ | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
388 |
5 |
393 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
386 |
2 |
388 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
373 |
18 |
391 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
0 |
373 |
373 |
|
| 53 |
18 |
▶ | || (ch >= '0' && ch <= '9') || ch == '_') |
Branch: 3 branches
| Branch | True | False | Total |
| || (ch >= '0' && ch <= '9') || ch == '_') |
14 |
4 |
18 |
| || (ch >= '0' && ch <= '9') || ch == '_') |
13 |
1 |
14 |
| || (ch >= '0' && ch <= '9') || ch == '_') |
13 |
4 |
17 |
|
| 54 |
389 |
| continue; |
| 55 |
|
| /* A dot is the one wrong character worth its own message: it is the |
| 56 |
|
| * natural way to namespace a plugin's routes, it reads fine, and it |
| 57 |
|
| * produces a name a handler can build and a template cannot reach - |
| 58 |
|
| * Template::Stencil resolves {% url.queue.jobs %} as |
| 59 |
|
| * url->{queue}{jobs}, so the name would be invisible at exactly the |
| 60 |
|
| * seam it was written for. */ |
| 61 |
4 |
▶ | if (ch == '.') |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '.') |
2 |
2 |
4 |
|
| 62 |
2 |
| croak("Punk: %s name '%.*s' may not contain '.' - a template " |
| 63 |
|
| "reads {%% url.%.*s %%} as a path through nested hashes, " |
| 64 |
|
| "so a dotted name cannot be reached from one. Namespace " |
| 65 |
|
| "with an underscore: '%.*s' -> 'queue_jobs'", |
| 66 |
|
| what, (int)nl, n, (int)nl, n, (int)nl, n); |
| 67 |
2 |
| croak("Punk: %s name '%.*s' is not an identifier - names are " |
| 68 |
|
| "[A-Za-z0-9_]+, because one name is written in url_for, in a " |
| 69 |
|
| "template, in `punk routes --name` and in a test", |
| 70 |
|
| what, (int)nl, n); |
| 71 |
|
| } |
| 72 |
284 |
▶ | for (k = 0; k < PK_URL_N_RESERVED; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < PK_URL_N_RESERVED; k++) |
191 |
93 |
284 |
|
| 73 |
191 |
▶ | if (strEQ(n, PK_URL_RESERVED[k])) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(n, PK_URL_RESERVED[k])) |
3 |
188 |
191 |
|
| 74 |
3 |
| croak("Punk: '%s' is reserved and cannot be a %s name - url_for " |
| 75 |
|
| "takes it as an option, so the route could be named and " |
| 76 |
|
| "never called", PK_URL_RESERVED[k], what); |
| 77 |
93 |
| } |
| 78 |
|
| |
| 79 |
|
| |
| 80 |
|
| /* ---- the encoder -------------------------------------------------------- |
| 81 |
|
| * |
| 82 |
|
| * RFC 3986: unreserved (A-Za-z0-9-._~) passes, everything else becomes %XX |
| 83 |
|
| * with uppercase hex. `keep_slash` passes '/' through as well, which is the |
| 84 |
|
| * difference between a *splat (a path tail, slashes and all) and a :param |
| 85 |
|
| * (one segment, where a slash would change the shape of the URL). |
| 86 |
|
| * |
| 87 |
|
| * Space is %20 and never '+': '+' is a form-body convention, and a path has |
| 88 |
|
| * no forms. Using one encoder on both sides of the '?' means a query value |
| 89 |
|
| * built here decodes identically and does not have to know which side it is |
| 90 |
|
| * on. |
| 91 |
|
| */ |
| 92 |
105 |
| static void pk_pct_cat(pTHX_ SV *out, const char *s, STRLEN len, int keep_slash) { |
| 93 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 94 |
105 |
| STRLEN i, run = 0; |
| 95 |
436 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
331 |
105 |
436 |
|
| 96 |
331 |
| const unsigned char ch = (unsigned char)s[i]; |
| 97 |
|
| /* |
| 98 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 99 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 100 |
331 |
▶ | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
223 |
108 |
331 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
223 |
0 |
223 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
222 |
109 |
331 |
| if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
11 |
211 |
222 |
|
| 101 |
120 |
▶ | || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' |
Branch: 4 branches
| Branch | True | False | Total |
| || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' |
82 |
38 |
120 |
| || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' |
17 |
65 |
82 |
| || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' |
54 |
1 |
55 |
| || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' |
53 |
1 |
54 |
|
| 102 |
53 |
▶ | || ch == '.' || ch == '~' || (keep_slash && ch == '/')) { |
Branch: 4 branches
| Branch | True | False | Total |
| || ch == '.' || ch == '~' || (keep_slash && ch == '/')) { |
50 |
3 |
53 |
| || ch == '.' || ch == '~' || (keep_slash && ch == '/')) { |
49 |
1 |
50 |
| || ch == '.' || ch == '~' || (keep_slash && ch == '/')) { |
22 |
27 |
49 |
| || ch == '.' || ch == '~' || (keep_slash && ch == '/')) { |
19 |
3 |
22 |
|
| 103 |
301 |
| run++; /* copy passing bytes in one go */ |
| 104 |
301 |
| continue; |
| 105 |
|
| } |
| 106 |
30 |
▶ | if (run) { sv_catpvn(out, s + i - run, run); run = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) { sv_catpvn(out, s + i - run, run); run = 0; } |
25 |
5 |
30 |
|
| 107 |
|
| { |
| 108 |
|
| char e[3]; |
| 109 |
30 |
| e[0] = '%'; e[1] = hex[ch >> 4]; e[2] = hex[ch & 15]; |
| 110 |
30 |
| sv_catpvn(out, e, 3); |
| 111 |
|
| } |
| 112 |
|
| } |
| 113 |
105 |
▶ | if (run) sv_catpvn(out, s + len - run, run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) sv_catpvn(out, s + len - run, run); |
99 |
6 |
105 |
|
| 114 |
105 |
| } |
| 115 |
|
| |
| 116 |
6 |
| static SV *pk_pct_encode(pTHX_ const char *s, STRLEN len, int keep_slash) { |
| 117 |
6 |
| SV *out = newSVpvs(""); |
| 118 |
6 |
| pk_pct_cat(aTHX_ out, s, len, keep_slash); |
| 119 |
6 |
| return out; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* The UTF-8 BYTES of a value, for encoding. |
| 123 |
|
| * |
| 124 |
|
| * A URL carries bytes, and the bytes a browser will send back for a |
| 125 |
|
| * non-ASCII capture are UTF-8. An SV with the flag on already holds those; |
| 126 |
|
| * one with the flag off holds Latin-1 semantics, where a 0xE9 means U+00E9 |
| 127 |
|
| * and has to become %C3%A9 rather than %E9. Upgrading a MORTAL COPY rather |
| 128 |
|
| * than the caller's SV matters: the value is usually a field of a model row |
| 129 |
|
| * the handler still holds, and a read-only SV would croak. |
| 130 |
|
| * |
| 131 |
|
| * Pure ASCII - almost every id - takes neither the copy nor the scan twice. |
| 132 |
|
| */ |
| 133 |
107 |
| static const char *pk_url_bytes(pTHX_ SV *sv, STRLEN *lenp) { |
| 134 |
|
| STRLEN i, l; |
| 135 |
107 |
| const char *p = SvPV_const(sv, l); |
| 136 |
107 |
▶ | if (SvUTF8(sv)) { *lenp = l; return p; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(sv)) { *lenp = l; return p; } |
1 |
106 |
107 |
|
| 137 |
396 |
▶ | for (i = 0; i < l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) |
294 |
102 |
396 |
|
| 138 |
294 |
▶ | if ((unsigned char)p[i] & 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((unsigned char)p[i] & 0x80) { |
4 |
290 |
294 |
|
| 139 |
4 |
| SV *cp = sv_mortalcopy(sv); |
| 140 |
4 |
| sv_utf8_upgrade(cp); |
| 141 |
4 |
| return SvPV_const(cp, *lenp); |
| 142 |
|
| } |
| 143 |
102 |
| *lenp = l; |
| 144 |
102 |
| return p; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| /* ---- the query string --------------------------------------------------- |
| 148 |
|
| * |
| 149 |
|
| * Keys sorted bytewise, because a URL that reorders itself between runs is |
| 150 |
|
| * one nobody can test or cache and a hash walk in C has no order worth |
| 151 |
|
| * relying on. An undef value is the bare key (?flag); an arrayref repeats |
| 152 |
|
| * the key in the array's order, which is what Punk::Request reads back for |
| 153 |
|
| * a repeated key. |
| 154 |
|
| */ |
| 155 |
80 |
| static void pk_url_query_cat(pTHX_ SV *out, HV *q) { |
| 156 |
80 |
| AV *keys = (AV *)sv_2mortal((SV *)newAV()); |
| 157 |
80 |
| SSize_t i, n = 0; |
| 158 |
|
| HE *he; |
| 159 |
80 |
| int first = 1; |
| 160 |
|
| |
| 161 |
80 |
| hv_iterinit(q); |
| 162 |
101 |
▶ | while ((he = hv_iternext(q))) { av_push(keys, newSVsv(hv_iterkeysv(he))); n++; } |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(q))) { av_push(keys, newSVsv(hv_iterkeysv(he))); n++; } |
21 |
80 |
101 |
|
| 163 |
80 |
▶ | if (!n) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return; |
66 |
14 |
80 |
|
| 164 |
14 |
▶ | if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp); |
5 |
9 |
14 |
|
| 165 |
|
| |
| 166 |
35 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
21 |
14 |
35 |
|
| 167 |
21 |
| SV *k = *av_fetch(keys, i, 0); |
| 168 |
21 |
| HE *e = hv_fetch_ent(q, k, 0, 0); |
| 169 |
21 |
▶ | SV *v = e ? HeVAL(e) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *v = e ? HeVAL(e) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
21 |
0 |
21 |
|
| 170 |
|
| STRLEN kl; |
| 171 |
21 |
| const char *kp = pk_url_bytes(aTHX_ k, &kl); |
| 172 |
21 |
▶ | AV *multi = (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| AV *multi = (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) |
1 |
20 |
21 |
| AV *multi = (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) |
1 |
0 |
1 |
|
| 173 |
42 |
▶ | ? (AV *)SvRV(v) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(v) : NULL; |
21 |
0 |
21 |
|
| 174 |
21 |
▶ | SSize_t j, jn = multi ? av_len(multi) + 1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t j, jn = multi ? av_len(multi) + 1 : 1; |
1 |
20 |
21 |
|
| 175 |
|
| |
| 176 |
43 |
▶ | for (j = 0; j < jn; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < jn; j++) { |
22 |
21 |
43 |
|
| 177 |
22 |
▶ | SV *one = multi ? *av_fetch(multi, j, 0) : v; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *one = multi ? *av_fetch(multi, j, 0) : v; |
2 |
20 |
22 |
|
| 178 |
22 |
▶ | sv_catpvn(out, first ? "?" : "&", 1); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvn(out, first ? "?" : "&", 1); |
14 |
8 |
22 |
|
| 179 |
22 |
| first = 0; |
| 180 |
22 |
| pk_pct_cat(aTHX_ out, kp, kl, 0); |
| 181 |
22 |
▶ | if (one && SvOK(one)) { /* undef is the bare key */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (one && SvOK(one)) { /* undef is the bare key */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
22 |
0 |
22 |
| if (one && SvOK(one)) { /* undef is the bare key */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
21 |
1 |
22 |
|
| 182 |
|
| STRLEN vl; |
| 183 |
21 |
| const char *vp = pk_url_bytes(aTHX_ one, &vl); |
| 184 |
21 |
| sv_catpvn(out, "=", 1); |
| 185 |
21 |
| pk_pct_cat(aTHX_ out, vp, vl, 0); |
| 186 |
|
| } |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| /* ---- the build ---------------------------------------------------------- |
| 192 |
|
| * |
| 193 |
|
| * One pass over the segments punk_route.h already parsed at to_app, or the |
| 194 |
|
| * declared path for a static route. Free of the request: everything that |
| 195 |
|
| * varies per request - the prefix, the origin - is passed in, so the |
| 196 |
|
| * template filter and the API mount can call this with their own. |
| 197 |
|
| * |
| 198 |
|
| * rt the compiled router |
| 199 |
|
| * idx the record index the name resolved to |
| 200 |
|
| * args the caller's pairs; capture names are consumed from here and |
| 201 |
|
| * whatever is left becomes the query string |
| 202 |
|
| * query an explicit query hash, or NULL. With one, a leftover argument |
| 203 |
|
| * is a mistake rather than a query key |
| 204 |
|
| * prefix SCRIPT_NAME and the path on `host`, or NULL |
| 205 |
|
| * origin scheme://host for an absolute URL, or NULL |
| 206 |
|
| * name the route's name, for the messages |
| 207 |
|
| */ |
| 208 |
|
| /* Fill one hole from the arguments, percent-encoded, and mark the argument |
| 209 |
|
| * used so it does not also become a query pair. |
| 210 |
|
| * |
| 211 |
|
| * `splat` passes slashes through: it is the one place in a URL where a |
| 212 |
|
| * capture may legitimately contain one. Everywhere else a '/' croaks rather |
| 213 |
|
| * than being encoded, because PATH_INFO reaches the router percent-DECODED - |
| 214 |
|
| * the %2F would be a '/' again, the path would have one segment too many, |
| 215 |
|
| * and the request would 404. A URL that cannot work is a bug at the call |
| 216 |
|
| * site, not a value to return. |
| 217 |
|
| */ |
| 218 |
|
| #define PK_CAP_PARAM 0 /* :param - one segment of a Punk route */ |
| 219 |
|
| #define PK_CAP_SPLAT 1 /* *splat - the tail, slashes and all */ |
| 220 |
|
| #define PK_CAP_HOLE 2 /* {hole} - one OpenAPI path-template variable */ |
| 221 |
|
| |
| 222 |
71 |
| static void pk_url_cap_cat(pTHX_ SV *out, HV *args, HV *used, SV *capname, |
| 223 |
|
| int kind, const char *name) { |
| 224 |
71 |
| const int splat = (kind == PK_CAP_SPLAT); |
| 225 |
71 |
| HE *he = hv_fetch_ent(args, capname, 0, 0); |
| 226 |
71 |
▶ | SV *v = he ? HeVAL(he) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *v = he ? HeVAL(he) : NULL; |
67 |
4 |
71 |
|
| 227 |
71 |
| const char *cn = SvPV_nolen(capname); |
| 228 |
|
| STRLEN vl, i; |
| 229 |
|
| const char *vp; |
| 230 |
|
| |
| 231 |
71 |
▶ | if (!v || !SvOK(v)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) { |
67 |
4 |
71 |
| if (!v || !SvOK(v)) { |
1 |
66 |
67 |
|
| 232 |
5 |
| SvREFCNT_dec(out); |
| 233 |
5 |
| croak("Punk: url_for('%s'): no value for :%s - a URL with a hole in " |
| 234 |
|
| "it is a bug, not a value", name, cn); |
| 235 |
|
| } |
| 236 |
66 |
▶ | if (SvROK(v)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(v)) { |
1 |
65 |
66 |
|
| 237 |
1 |
| SvREFCNT_dec(out); |
| 238 |
1 |
| croak("Punk: url_for('%s'): :%s is a %s - a capture is one value", |
| 239 |
|
| name, cn, sv_reftype(SvRV(v), 0)); |
| 240 |
|
| } |
| 241 |
65 |
| vp = pk_url_bytes(aTHX_ v, &vl); |
| 242 |
65 |
▶ | if (!vl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vl) { |
1 |
64 |
65 |
|
| 243 |
1 |
| SvREFCNT_dec(out); |
| 244 |
1 |
| croak("Punk: url_for('%s'): :%s is empty - the route needs one " |
| 245 |
|
| "character there and would not match the URL this built", |
| 246 |
|
| name, cn); |
| 247 |
|
| } |
| 248 |
192 |
▶ | if (!splat) for (i = 0; i < vl; i++) /* # uncoverable branch true count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!splat) for (i = 0; i < vl; i++) /* # uncoverable branch true count:1 class:ignore_covered_err */ |
56 |
8 |
64 |
| if (!splat) for (i = 0; i < vl; i++) /* # uncoverable branch true count:1 class:ignore_covered_err */ |
130 |
54 |
184 |
|
| 249 |
130 |
▶ | if (vp[i] == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (vp[i] == '/') { |
2 |
128 |
130 |
|
| 250 |
2 |
| SvREFCNT_dec(out); |
| 251 |
|
| /* Encoding it would be right on the wire and wrong on arrival: |
| 252 |
|
| * PATH_INFO reaches the router percent-DECODED, so the %2F is a |
| 253 |
|
| * '/' again and the path has one segment too many. The hint |
| 254 |
|
| * differs because OpenAPI has no splat to point at. */ |
| 255 |
2 |
▶ | croak("Punk: url_for('%s'): :%s contains '/' and cannot be " |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk: url_for('%s'): :%s contains '/' and cannot be " |
1 |
1 |
2 |
|
| 256 |
|
| "expressed on this route - PATH_INFO arrives " |
| 257 |
|
| "percent-DECODED, so a %%2F would split into two segments " |
| 258 |
|
| "and 404.%s", name, cn, |
| 259 |
|
| kind == PK_CAP_HOLE |
| 260 |
|
| ? " An OpenAPI path template has no segment that takes" |
| 261 |
|
| " slashes" |
| 262 |
|
| : " A *splat is the segment that takes slashes"); |
| 263 |
|
| } |
| 264 |
|
| /* A splat that starts with '/' would follow the '/' this builder already |
| 265 |
|
| * wrote, and "//host/..." is a URL for another host. A '.' or '..' |
| 266 |
|
| * segment is one the browser rewrites before the router sees it. Both |
| 267 |
|
| * are URLs that cannot mean what was asked: a bug at the call site. */ |
| 268 |
62 |
▶ | if (splat && vp[0] == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (splat && vp[0] == '/') { |
8 |
54 |
62 |
| if (splat && vp[0] == '/') { |
2 |
6 |
8 |
|
| 269 |
2 |
| SvREFCNT_dec(out); |
| 270 |
2 |
| croak("Punk: url_for('%s'): *%s starts with '/' - the route already " |
| 271 |
|
| "supplies that slash, and a value beginning with one builds a " |
| 272 |
|
| "scheme-relative URL for another host", name, cn); |
| 273 |
|
| } |
| 274 |
|
| { |
| 275 |
60 |
| STRLEN s = 0; |
| 276 |
284 |
▶ | for (i = 0; i <= vl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= vl; i++) { |
228 |
56 |
284 |
|
| 277 |
228 |
▶ | if (i == vl || vp[i] == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i == vl || vp[i] == '/') { |
170 |
58 |
228 |
| if (i == vl || vp[i] == '/') { |
11 |
159 |
170 |
|
| 278 |
69 |
| STRLEN sl = i - s; |
| 279 |
69 |
▶ | if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
Branch: 5 branches
| Branch | True | False | Total |
| if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
28 |
41 |
69 |
| if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
26 |
2 |
28 |
| if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
17 |
50 |
67 |
| if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
2 |
15 |
17 |
| if ((sl == 1 && vp[s] == '.') || (sl == 2 && vp[s] == '.' && vp[s + 1] == '.')) { /* # uncoverable condition right count:5 class:ignore_covered_err */ |
2 |
0 |
2 |
|
| 280 |
4 |
| SvREFCNT_dec(out); |
| 281 |
4 |
| croak("Punk: url_for('%s'): :%s has a '%.*s' segment, " |
| 282 |
|
| "which a browser resolves away before the request " |
| 283 |
|
| "is made", name, cn, (int)sl, vp + s); |
| 284 |
|
| } |
| 285 |
65 |
| s = i + 1; |
| 286 |
|
| } |
| 287 |
|
| } |
| 288 |
|
| } |
| 289 |
56 |
| pk_pct_cat(aTHX_ out, vp, vl, splat); |
| 290 |
56 |
| (void)hv_store_ent(used, capname, PUNK_SET_TRUE, 0); |
| 291 |
56 |
| } |
| 292 |
|
| |
| 293 |
|
| /* Whatever named no hole is the query string - or, with an explicit `query` |
| 294 |
|
| * hash, a mistake. The two conventions do not mix in one call. */ |
| 295 |
81 |
| static void pk_url_leftovers(pTHX_ SV *out, HV *args, HV *used, HV *query, |
| 296 |
|
| const char *name) { |
| 297 |
|
| HV *q; |
| 298 |
|
| HE *he; |
| 299 |
81 |
▶ | if (query) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (query) { |
3 |
78 |
81 |
|
| 300 |
3 |
| hv_iterinit(args); |
| 301 |
5 |
▶ | while ((he = hv_iternext(args))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(args))) { |
3 |
2 |
5 |
|
| 302 |
3 |
| SV *k = hv_iterkeysv(he); |
| 303 |
3 |
▶ | if (hv_exists_ent(used, k, 0)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(used, k, 0)) continue; |
2 |
1 |
3 |
|
| 304 |
1 |
| SvREFCNT_dec(out); |
| 305 |
1 |
| croak("Punk: url_for('%s'): '%s' names no capture, and `query` " |
| 306 |
|
| "was given - with an explicit query hash every other " |
| 307 |
|
| "argument must be a capture", name, SvPV_nolen(k)); |
| 308 |
|
| } |
| 309 |
2 |
| q = query; |
| 310 |
|
| } |
| 311 |
|
| else { |
| 312 |
78 |
| q = (HV *)sv_2mortal((SV *)newHV()); |
| 313 |
78 |
| hv_iterinit(args); |
| 314 |
150 |
▶ | while ((he = hv_iternext(args))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(args))) { |
72 |
78 |
150 |
|
| 315 |
72 |
| SV *k = hv_iterkeysv(he); |
| 316 |
72 |
▶ | if (hv_exists_ent(used, k, 0)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(used, k, 0)) continue; |
53 |
19 |
72 |
|
| 317 |
19 |
| (void)hv_store_ent(q, k, newSVsv(HeVAL(he)), 0); |
| 318 |
|
| } |
| 319 |
|
| } |
| 320 |
80 |
| pk_url_query_cat(aTHX_ out, q); |
| 321 |
80 |
| } |
| 322 |
|
| |
| 323 |
83 |
| static SV *pk_url_build(pTHX_ pr_router *rt, IV idx, HV *args, HV *query, |
| 324 |
|
| SV *prefix, SV *origin, const char *name) { |
| 325 |
83 |
| SV *out = newSVpvs(""); |
| 326 |
83 |
| HV *used = (HV *)sv_2mortal((SV *)newHV()); |
| 327 |
|
| IV dyn; |
| 328 |
|
| |
| 329 |
83 |
▶ | if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
7 |
76 |
83 |
| if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
7 |
0 |
7 |
|
| 330 |
83 |
▶ | if (prefix && SvOK(prefix)) sv_catsv(out, prefix); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (prefix && SvOK(prefix)) sv_catsv(out, prefix); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
10 |
73 |
83 |
| if (prefix && SvOK(prefix)) sv_catsv(out, prefix); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
10 |
0 |
10 |
|
| 331 |
|
| |
| 332 |
|
| /* |
| 333 |
|
| # uncoverable branch true |
| 334 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 335 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 336 |
83 |
▶ | if (idx < 0 || idx >= (IV)rt->nrecs) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx < 0 || idx >= (IV)rt->nrecs) { |
83 |
0 |
83 |
| if (idx < 0 || idx >= (IV)rt->nrecs) { |
0 |
83 |
83 |
|
| 337 |
0 |
| SvREFCNT_dec(out); /* # uncoverable statement */ |
| 338 |
0 |
| croak("Punk: url_for('%s'): the route table has no record %" IVdf /* # uncoverable statement */ |
| 339 |
|
| " - the application was compiled from a different table", |
| 340 |
|
| name, idx); /* # uncoverable statement */ |
| 341 |
|
| } |
| 342 |
83 |
| dyn = rt->dyn_of[idx]; |
| 343 |
|
| |
| 344 |
83 |
▶ | if (dyn < 0) { /* static: the path IS the URL */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (dyn < 0) { /* static: the path IS the URL */ |
26 |
57 |
83 |
|
| 345 |
26 |
| SV **rr = av_fetch(rt->records, (SSize_t)idx, 0); |
| 346 |
|
| /* |
| 347 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 348 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 349 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 350 |
26 |
▶ | HV *rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| HV *rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
26 |
0 |
26 |
| HV *rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
26 |
0 |
26 |
| HV *rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
26 |
0 |
26 |
|
| 351 |
26 |
▶ | SV **pp = rec ? hv_fetchs(rec, "path", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **pp = rec ? hv_fetchs(rec, "path", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
26 |
0 |
26 |
|
| 352 |
|
| /* |
| 353 |
|
| # uncoverable branch false |
| 354 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 355 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 356 |
26 |
▶ | if (pp && *pp) sv_catsv(out, *pp); |
Branch: 2 branches
| Branch | True | False | Total |
| if (pp && *pp) sv_catsv(out, *pp); |
26 |
0 |
26 |
| if (pp && *pp) sv_catsv(out, *pp); |
26 |
0 |
26 |
|
| 357 |
|
| } |
| 358 |
|
| else { |
| 359 |
57 |
| pr_rec *r = &rt->recs[dyn]; |
| 360 |
|
| int k; |
| 361 |
164 |
▶ | for (k = 0; k < r->nsegs; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < r->nsegs; k++) { |
120 |
44 |
164 |
|
| 362 |
120 |
| pr_seg *s = &r->segs[k]; |
| 363 |
120 |
| sv_catpvn(out, "/", 1); |
| 364 |
120 |
▶ | if (s->type == PR_LIT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->type == PR_LIT) { |
60 |
60 |
120 |
|
| 365 |
|
| /* |
| 366 |
|
| # uncoverable branch false |
| 367 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 368 |
60 |
▶ | if (s->litlen) sv_catpvn(out, s->lit, s->litlen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->litlen) sv_catpvn(out, s->lit, s->litlen); |
60 |
0 |
60 |
|
| 369 |
60 |
| continue; |
| 370 |
|
| } |
| 371 |
60 |
| pk_url_cap_cat(aTHX_ out, args, used, s->name, |
| 372 |
60 |
| s->type == PR_SPLAT ? PK_CAP_SPLAT : PK_CAP_PARAM, |
| 373 |
|
| name); |
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
70 |
| pk_url_leftovers(aTHX_ out, args, used, query, name); |
| 378 |
69 |
| return out; |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
|
| /* ---- an OpenAPI operation ----------------------------------------------- |
| 382 |
|
| * |
| 383 |
|
| * An operation has an operationId and a path template with {holes}, which is |
| 384 |
|
| * a named route by another spelling - so an application with an `api` |
| 385 |
|
| * keyword is half-named until url_for speaks it. |
| 386 |
|
| * |
| 387 |
|
| * The template does NOT reuse pr_seg. `/files/{name}.json` is legal OpenAPI |
| 388 |
|
| * and the router has no mid-segment holes, so the mount parses its own flat |
| 389 |
|
| * form at to_app: an AV with literals at even indices and hole names at odd |
| 390 |
|
| * ones, always odd length. `/books/{id}` is [ "/books/", "id", "" ]. |
| 391 |
|
| * |
| 392 |
|
| * There is no splat in OpenAPI, so the slash rule applies to every hole. |
| 393 |
|
| */ |
| 394 |
13 |
| static SV *pk_url_build_op(pTHX_ AV *tmpl, SV *mount_prefix, HV *args, |
| 395 |
|
| HV *query, SV *prefix, SV *origin, |
| 396 |
|
| const char *name) { |
| 397 |
13 |
| SV *out = newSVpvs(""); |
| 398 |
13 |
| HV *used = (HV *)sv_2mortal((SV *)newHV()); |
| 399 |
13 |
| SSize_t i, n = av_len(tmpl) + 1; |
| 400 |
|
| |
| 401 |
13 |
▶ | if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1 |
12 |
13 |
| if (origin && SvOK(origin)) sv_catsv(out, origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1 |
0 |
1 |
|
| 402 |
|
| /* |
| 403 |
|
| # uncoverable branch true |
| 404 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 405 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 406 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 407 |
13 |
▶ | if (prefix && SvOK(prefix)) sv_catsv(out, prefix); |
Branch: 2 branches
| Branch | True | False | Total |
| if (prefix && SvOK(prefix)) sv_catsv(out, prefix); |
0 |
13 |
13 |
| if (prefix && SvOK(prefix)) sv_catsv(out, prefix); |
0 |
0 |
0 |
|
| 408 |
|
| /* |
| 409 |
|
| # uncoverable branch false class:ignore_covered_err |
| 410 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 411 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 412 |
13 |
▶ | if (mount_prefix && SvOK(mount_prefix)) sv_catsv(out, mount_prefix); |
Branch: 2 branches
| Branch | True | False | Total |
| if (mount_prefix && SvOK(mount_prefix)) sv_catsv(out, mount_prefix); |
13 |
0 |
13 |
| if (mount_prefix && SvOK(mount_prefix)) sv_catsv(out, mount_prefix); |
13 |
0 |
13 |
|
| 413 |
|
| |
| 414 |
44 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
33 |
11 |
44 |
|
| 415 |
33 |
| SV **e = av_fetch(tmpl, i, 0); |
| 416 |
|
| /* |
| 417 |
|
| # uncoverable branch true |
| 418 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 419 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 420 |
33 |
▶ | if (!(e && *e)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(e && *e)) continue; |
33 |
0 |
33 |
| if (!(e && *e)) continue; |
0 |
33 |
33 |
|
| 421 |
33 |
▶ | if (!(i & 1)) { /* even: a literal */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(i & 1)) { /* even: a literal */ |
22 |
11 |
33 |
|
| 422 |
22 |
▶ | if (SvCUR(*e)) sv_catsv(out, *e); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(*e)) sv_catsv(out, *e); |
16 |
6 |
22 |
|
| 423 |
|
| } |
| 424 |
|
| else /* odd: a hole */ |
| 425 |
11 |
| pk_url_cap_cat(aTHX_ out, args, used, *e, |
| 426 |
|
| PK_CAP_HOLE, name); |
| 427 |
|
| } |
| 428 |
|
| |
| 429 |
11 |
| pk_url_leftovers(aTHX_ out, args, used, query, name); |
| 430 |
11 |
| return out; |
| 431 |
|
| } |
| 432 |
|
| |
| 433 |
|
| |
| 434 |
|
| /* ---- the prefix --------------------------------------------------------- |
| 435 |
|
| * |
| 436 |
|
| * A route matches PATH_INFO, and what the browser saw is whatever sat in |
| 437 |
|
| * front of it. Two things can, and they are LAYERS rather than alternatives: |
| 438 |
|
| * |
| 439 |
|
| * - the path on `host` - a proxy strips /site before Punk sees the |
| 440 |
|
| * request, so SCRIPT_NAME is empty and only configuration knows. Split |
| 441 |
|
| * off `host` once at to_app (K_HOST_PATH_C), because $c->origin |
| 442 |
|
| * deliberately drops it: an origin, to a browser, is scheme://host. |
| 443 |
|
| * - SCRIPT_NAME - a PSGI mount, which the request carries. |
| 444 |
|
| * |
| 445 |
|
| * A proxy strips /a, a builder mounts at /b, the browser sees /a/b/books/42. |
| 446 |
|
| * Returns a mortal SV, or NULL when there is no prefix at all - which is |
| 447 |
|
| * nearly every deployment, and costs two hv_fetchs that find nothing. |
| 448 |
|
| */ |
| 449 |
2171 |
| static SV *pk_url_prefix(pTHX_ HV *app, HV *env) { |
| 450 |
2171 |
▶ | SV **hp = app ? hv_fetchs(app, K_HOST_PATH_C, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **hp = app ? hv_fetchs(app, K_HOST_PATH_C, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2171 |
0 |
2171 |
|
| 451 |
2171 |
▶ | SV **sn = env ? hv_fetchs(env, "SCRIPT_NAME", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **sn = env ? hv_fetchs(env, "SCRIPT_NAME", 0) : NULL; |
2165 |
6 |
2171 |
|
| 452 |
|
| /* |
| 453 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 454 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 455 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 456 |
2171 |
▶ | int have_hp = (hp && *hp && SvOK(*hp) && SvCUR(*hp)); |
Branch: 4 branches
| Branch | True | False | Total |
| int have_hp = (hp && *hp && SvOK(*hp) && SvCUR(*hp)); |
2 |
2169 |
2171 |
| int have_hp = (hp && *hp && SvOK(*hp) && SvCUR(*hp)); |
2 |
0 |
2 |
| int have_hp = (hp && *hp && SvOK(*hp) && SvCUR(*hp)); |
2 |
0 |
2 |
| int have_hp = (hp && *hp && SvOK(*hp) && SvCUR(*hp)); |
2 |
0 |
2 |
|
| 457 |
|
| /* |
| 458 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 459 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 460 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 461 |
2171 |
▶ | int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn)); |
Branch: 4 branches
| Branch | True | False | Total |
| int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn)); |
5 |
2166 |
2171 |
| int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn)); |
5 |
0 |
5 |
| int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn)); |
5 |
0 |
5 |
| int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn)); |
5 |
0 |
5 |
|
| 462 |
|
| SV *out; |
| 463 |
2171 |
▶ | if (!have_hp && !have_sn) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!have_hp && !have_sn) return NULL; |
2169 |
2 |
2171 |
| if (!have_hp && !have_sn) return NULL; |
2165 |
4 |
2169 |
|
| 464 |
6 |
| out = sv_2mortal(newSVpvs("")); |
| 465 |
6 |
▶ | if (have_hp) sv_catsv(out, *hp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (have_hp) sv_catsv(out, *hp); |
2 |
4 |
6 |
|
| 466 |
6 |
▶ | if (have_sn) sv_catsv(out, *sn); |
Branch: 1 branch
| Branch | True | False | Total |
| if (have_sn) sv_catsv(out, *sn); |
5 |
1 |
6 |
|
| 467 |
6 |
| return out; |
| 468 |
|
| } |
| 469 |
|
| |
| 470 |
|
| /* What a name resolves to, or NULL: an IV record index for a route, an |
| 471 |
|
| * arrayref [ mount prefix, parsed template ] for an API operation. */ |
| 472 |
35 |
| static SV *pk_url_target(pTHX_ HV *app, SV *name) { |
| 473 |
35 |
▶ | SV **nm = app ? hv_fetchs(app, K_NAMES_C, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **nm = app ? hv_fetchs(app, K_NAMES_C, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
35 |
0 |
35 |
|
| 474 |
|
| HE *he; |
| 475 |
|
| /* |
| 476 |
|
| # uncoverable branch true |
| 477 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 478 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 479 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 480 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 481 |
35 |
▶ | if (!(nm && *nm && SvROK(*nm) && SvTYPE(SvRV(*nm)) == SVt_PVHV)) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(nm && *nm && SvROK(*nm) && SvTYPE(SvRV(*nm)) == SVt_PVHV)) return NULL; |
35 |
0 |
35 |
| if (!(nm && *nm && SvROK(*nm) && SvTYPE(SvRV(*nm)) == SVt_PVHV)) return NULL; |
35 |
0 |
35 |
| if (!(nm && *nm && SvROK(*nm) && SvTYPE(SvRV(*nm)) == SVt_PVHV)) return NULL; |
35 |
0 |
35 |
| if (!(nm && *nm && SvROK(*nm) && SvTYPE(SvRV(*nm)) == SVt_PVHV)) return NULL; |
0 |
35 |
35 |
|
| 482 |
35 |
| he = hv_fetch_ent((HV *)SvRV(*nm), name, 0, 0); |
| 483 |
35 |
▶ | return he ? HeVAL(he) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return he ? HeVAL(he) : NULL; |
29 |
6 |
35 |
|
| 484 |
|
| } |
| 485 |
|
| |
| 486 |
|
| /* The record index a ROUTE name resolves to, or -1 (an API operation has |
| 487 |
|
| * none, and answers -1 too). */ |
| 488 |
16 |
| static IV pk_url_idx(pTHX_ HV *app, SV *name) { |
| 489 |
16 |
| SV *v = pk_url_target(aTHX_ app, name); |
| 490 |
16 |
▶ | return (v && !SvROK(v)) ? SvIV(v) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| return (v && !SvROK(v)) ? SvIV(v) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
12 |
4 |
16 |
| return (v && !SvROK(v)) ? SvIV(v) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
12 |
0 |
12 |
|
| 491 |
|
| } |
| 492 |
|
| |
| 493 |
|
| /* The compiled router off the application. */ |
| 494 |
30 |
| static pr_router *pk_url_router(pTHX_ HV *app) { |
| 495 |
30 |
▶ | SV **r = app ? hv_fetchs(app, K_ROUTER, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **r = app ? hv_fetchs(app, K_ROUTER, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
30 |
0 |
30 |
|
| 496 |
|
| /* |
| 497 |
|
| # uncoverable branch true |
| 498 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 499 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 500 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 501 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 502 |
30 |
▶ | if (!(r && *r && SvROK(*r) && SvIOK(SvRV(*r)))) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(r && *r && SvROK(*r) && SvIOK(SvRV(*r)))) return NULL; |
30 |
0 |
30 |
| if (!(r && *r && SvROK(*r) && SvIOK(SvRV(*r)))) return NULL; |
30 |
0 |
30 |
| if (!(r && *r && SvROK(*r) && SvIOK(SvRV(*r)))) return NULL; |
30 |
0 |
30 |
| if (!(r && *r && SvROK(*r) && SvIOK(SvRV(*r)))) return NULL; |
0 |
30 |
30 |
|
| 503 |
30 |
| return (pr_router *)INT2PTR(void *, SvIV(SvRV(*r))); |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
|
| /* ---- the template filter ------------------------------------------------ |
| 507 |
|
| * |
| 508 |
|
| * <a href="{% book.id | url_for('book') %}"> a scalar fills one capture |
| 509 |
|
| * <a href="{% book | url_for('book') %}"> a hashref fills by name |
| 510 |
|
| * |
| 511 |
|
| * A Stencil filter receives the value and one literal argument, so the |
| 512 |
|
| * argument names the route and the value carries the captures. A scalar |
| 513 |
|
| * fills a route with exactly one capture; more than one has no way to say |
| 514 |
|
| * which, and croaks rather than guessing. |
| 515 |
|
| * |
| 516 |
|
| * The shape is pa_asset_filter's, and so is the ownership: a magic CV over |
| 517 |
|
| * the application, held WEAKLY, because the app owns the view registry which |
| 518 |
|
| * owns the engine which owns this filter, and a strong reference would close |
| 519 |
|
| * that loop and keep the whole application alive for ever. |
| 520 |
|
| * |
| 521 |
|
| * Slot 1 is the request's prefix, an SV this closure and the application |
| 522 |
|
| * both hold. asset never needed one - a content-addressed URL is application |
| 523 |
|
| * state - but a route URL under a prefix is not, and a filter cannot see $c. |
| 524 |
|
| * The binder below sets it per render. |
| 525 |
|
| */ |
| 526 |
|
| #define PKU_CAP_APP 0 |
| 527 |
|
| #define PKU_CAP_PREFIX 1 |
| 528 |
|
| |
| 529 |
|
| XS_INTERNAL(pk_url_filter_cb); |
| 530 |
11 |
| XS_INTERNAL(pk_url_filter_cb) { |
| 531 |
11 |
| dXSARGS; |
| 532 |
11 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 533 |
|
| SV *appsv, *prefix, *value, *name, *target; |
| 534 |
|
| HV *app, *args; |
| 535 |
|
| pr_router *rt; |
| 536 |
|
| IV idx; |
| 537 |
|
| int is_op; |
| 538 |
|
| |
| 539 |
|
| /* |
| 540 |
|
| # uncoverable branch true |
| 541 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 542 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 543 |
11 |
▶ | if (!cap || items < 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 2) |
11 |
0 |
11 |
| if (!cap || items < 2) |
0 |
11 |
11 |
|
| 544 |
0 |
| croak("Punk: the url_for filter takes a route name: " /* # uncoverable statement */ |
| 545 |
|
| "{%% value | url_for('name') %%}"); |
| 546 |
11 |
| value = ST(0); |
| 547 |
11 |
| name = ST(1); |
| 548 |
11 |
| appsv = *av_fetch(cap, PKU_CAP_APP, 0); |
| 549 |
11 |
| prefix = *av_fetch(cap, PKU_CAP_PREFIX, 0); |
| 550 |
|
| |
| 551 |
|
| /* The weak reference can have gone: the engine may outlive the |
| 552 |
|
| * application if something else kept it. Returning the name would put a |
| 553 |
|
| * link to /book on the page, so this croaks like every other way of |
| 554 |
|
| * failing to build a URL. */ |
| 555 |
|
| /* |
| 556 |
|
| # uncoverable branch true |
| 557 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 558 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 559 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 560 |
11 |
▶ | if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) |
11 |
0 |
11 |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) |
11 |
0 |
11 |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV)) |
0 |
11 |
11 |
|
| 561 |
0 |
▶ | croak("Punk: url_for('%s'): the application is gone", /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk: url_for('%s'): the application is gone", /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 562 |
|
| /* |
| 563 |
|
| # uncoverable statement |
| 564 |
|
| # uncoverable condition left count:1 |
| 565 |
|
| # uncoverable condition right count:1 */ |
| 566 |
|
| SvOK(name) ? SvPV_nolen(name) : "?"); |
| 567 |
11 |
| app = (HV *)SvRV(appsv); |
| 568 |
|
| |
| 569 |
|
| /* |
| 570 |
|
| # uncoverable branch true |
| 571 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 572 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 573 |
11 |
▶ | if (!SvOK(name) || !SvCUR(name)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(name) || !SvCUR(name)) |
11 |
0 |
11 |
| if (!SvOK(name) || !SvCUR(name)) |
0 |
11 |
11 |
|
| 574 |
0 |
| croak("Punk: the url_for filter takes a route name: " /* # uncoverable statement */ |
| 575 |
|
| "{%% value | url_for('name') %%}"); |
| 576 |
11 |
| target = pk_url_target(aTHX_ app, name); |
| 577 |
|
| /* |
| 578 |
|
| # uncoverable branch true |
| 579 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 580 |
11 |
▶ | if (!target) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!target) |
0 |
11 |
11 |
|
| 581 |
0 |
| croak("Punk: url_for('%s'): no route is named that", SvPV_nolen(name)); /* # uncoverable statement */ |
| 582 |
|
| /* |
| 583 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 584 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 585 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 586 |
11 |
▶ | is_op = (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVAV); |
Branch: 2 branches
| Branch | True | False | Total |
| is_op = (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVAV); |
0 |
11 |
11 |
| is_op = (SvROK(target) && SvTYPE(SvRV(target)) == SVt_PVAV); |
0 |
0 |
0 |
|
| 587 |
11 |
▶ | idx = is_op ? -1 : SvIV(target); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| idx = is_op ? -1 : SvIV(target); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
11 |
0 |
11 |
|
| 588 |
11 |
| rt = pk_url_router(aTHX_ app); |
| 589 |
|
| /* |
| 590 |
|
| # uncoverable branch true |
| 591 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 592 |
11 |
▶ | if (!rt) croak("Punk: url_for('%s'): the application is not compiled", |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rt) croak("Punk: url_for('%s'): the application is not compiled", |
0 |
11 |
11 |
|
| 593 |
|
| SvPV_nolen(name)); /* # uncoverable statement */ |
| 594 |
|
| |
| 595 |
11 |
| args = (HV *)sv_2mortal((SV *)newHV()); |
| 596 |
|
| /* |
| 597 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 598 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 599 |
16 |
▶ | if (value && SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (value && SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVHV) { |
11 |
0 |
11 |
| if (value && SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVHV) { |
5 |
6 |
11 |
| if (value && SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVHV) { |
5 |
0 |
5 |
|
| 600 |
5 |
| HV *in = (HV *)SvRV(value); /* a row: captures by name */ |
| 601 |
|
| HE *he; |
| 602 |
5 |
| hv_iterinit(in); |
| 603 |
12 |
▶ | while ((he = hv_iternext(in))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(in))) |
7 |
5 |
12 |
|
| 604 |
7 |
| (void)hv_store_ent(args, hv_iterkeysv(he), newSVsv(HeVAL(he)), 0); |
| 605 |
|
| } |
| 606 |
|
| /* |
| 607 |
|
| # uncoverable branch false |
| 608 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 609 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 610 |
6 |
▶ | else if (value && SvOK(value)) { /* a scalar: the one capture */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (value && SvOK(value)) { /* a scalar: the one capture */ |
6 |
0 |
6 |
| else if (value && SvOK(value)) { /* a scalar: the one capture */ |
6 |
0 |
6 |
|
| 611 |
6 |
| SV *only = NULL; |
| 612 |
6 |
| int n = 0; |
| 613 |
|
| /* |
| 614 |
|
| # uncoverable branch true |
| 615 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 616 |
6 |
▶ | if (is_op) { /* the odd slots are the holes */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_op) { /* the odd slots are the holes */ |
0 |
6 |
6 |
|
| 617 |
0 |
| AV *tmpl = (AV *)SvRV(*av_fetch((AV *)SvRV(target), 1, 0)); /* # uncoverable statement */ |
| 618 |
0 |
| SSize_t j, jn = av_len(tmpl) + 1; /* # uncoverable statement */ |
| 619 |
|
| /* |
| 620 |
|
| # uncoverable statement |
| 621 |
|
| # uncoverable branch true |
| 622 |
|
| # uncoverable branch false |
| 623 |
|
| # uncoverable condition left count:1 |
| 624 |
|
| # uncoverable condition right count:1 */ |
| 625 |
0 |
▶ | for (j = 1; j < jn; j += 2) { only = *av_fetch(tmpl, j, 0); n++; } |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j < jn; j += 2) { only = *av_fetch(tmpl, j, 0); n++; } |
0 |
0 |
0 |
|
| 626 |
|
| } |
| 627 |
|
| /* |
| 628 |
|
| # uncoverable branch false |
| 629 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 630 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 631 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 632 |
6 |
▶ | else if (idx >= 0 && idx < (IV)rt->nrecs && rt->dyn_of[idx] >= 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| else if (idx >= 0 && idx < (IV)rt->nrecs && rt->dyn_of[idx] >= 0) { |
6 |
0 |
6 |
| else if (idx >= 0 && idx < (IV)rt->nrecs && rt->dyn_of[idx] >= 0) { |
6 |
0 |
6 |
| else if (idx >= 0 && idx < (IV)rt->nrecs && rt->dyn_of[idx] >= 0) { |
6 |
0 |
6 |
|
| 633 |
6 |
| pr_rec *r = &rt->recs[rt->dyn_of[idx]]; |
| 634 |
|
| int k; |
| 635 |
20 |
▶ | for (k = 0; k < r->nsegs; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < r->nsegs; k++) |
14 |
6 |
20 |
|
| 636 |
14 |
▶ | if (r->segs[k].type != PR_LIT) { only = r->segs[k].name; n++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->segs[k].type != PR_LIT) { only = r->segs[k].name; n++; } |
7 |
7 |
14 |
|
| 637 |
|
| } |
| 638 |
6 |
▶ | if (n != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != 1) |
1 |
5 |
6 |
|
| 639 |
1 |
| croak("Punk: url_for('%s'): the value is a scalar and this route " |
| 640 |
|
| "has %d captures - pass a hashref so each one is named", |
| 641 |
|
| SvPV_nolen(name), n); |
| 642 |
5 |
| (void)hv_store_ent(args, only, newSVsv(value), 0); |
| 643 |
|
| } |
| 644 |
|
| |
| 645 |
|
| /* |
| 646 |
|
| # uncoverable branch true |
| 647 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 648 |
10 |
▶ | if (is_op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_op) { |
0 |
10 |
10 |
|
| 649 |
0 |
| AV *pair = (AV *)SvRV(target); /* # uncoverable statement */ |
| 650 |
0 |
| SV **mp = av_fetch(pair, 0, 0); /* # uncoverable statement */ |
| 651 |
0 |
| SV **tm = av_fetch(pair, 1, 0); /* # uncoverable statement */ |
| 652 |
0 |
▶ | ST(0) = sv_2mortal(pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */ |
Branch: 4 branches
| Branch | True | False | Total |
| ST(0) = sv_2mortal(pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */ |
0 |
0 |
0 |
| ST(0) = sv_2mortal(pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */ |
0 |
0 |
0 |
| ST(0) = sv_2mortal(pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */ |
0 |
0 |
0 |
| ST(0) = sv_2mortal(pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 653 |
|
| (mp && *mp) ? *mp : NULL, args, NULL, |
| 654 |
|
| (prefix && SvCUR(prefix)) ? prefix : NULL, |
| 655 |
|
| NULL, SvPV_nolen(name))); |
| 656 |
|
| } |
| 657 |
|
| else |
| 658 |
10 |
▶ | ST(0) = sv_2mortal(pk_url_build(aTHX_ rt, idx, args, NULL, |
Branch: 2 branches
| Branch | True | False | Total |
| ST(0) = sv_2mortal(pk_url_build(aTHX_ rt, idx, args, NULL, |
10 |
0 |
10 |
| ST(0) = sv_2mortal(pk_url_build(aTHX_ rt, idx, args, NULL, |
4 |
6 |
10 |
|
| 659 |
|
| (prefix && SvCUR(prefix)) ? prefix : NULL, |
| 660 |
|
| NULL, SvPV_nolen(name))); |
| 661 |
10 |
| XSRETURN(1); |
| 662 |
|
| } |
| 663 |
|
| |
| 664 |
64 |
| static SV *pk_url_filter(pTHX_ SV *app, SV *prefix_slot) { |
| 665 |
64 |
| AV *cap = newAV(); |
| 666 |
64 |
| SV *weak = newSVsv(app); |
| 667 |
64 |
| sv_rvweaken(weak); |
| 668 |
64 |
| av_store(cap, PKU_CAP_APP, weak); |
| 669 |
64 |
| av_store(cap, PKU_CAP_PREFIX, SvREFCNT_inc_simple_NN(prefix_slot)); |
| 670 |
64 |
| return punk_closure(aTHX_ pk_url_filter_cb, cap); |
| 671 |
|
| } |
| 672 |
|
| |
| 673 |
|
| /* ---- the `url` hash ----------------------------------------------------- |
| 674 |
|
| * |
| 675 |
|
| * <a href="{% url.books %}"> |
| 676 |
|
| * |
| 677 |
|
| * Static routes by name, so the commonest link on a page costs a hash lookup |
| 678 |
|
| * inside the engine rather than the filter's call boundary. Dynamic routes |
| 679 |
|
| * are not in it: they need captures, which is what the filter is for. |
| 680 |
|
| * |
| 681 |
|
| * TIED, for the reason punk_i18n.h gives about its catalogue, sharpened: |
| 682 |
|
| * Stencil resolves a missing path to the empty string, so a plain hash would |
| 683 |
|
| * render `{% url.typo %}` as `href=""` - a link to the current page that |
| 684 |
|
| * looks like it works. A name that does not exist is the template-side |
| 685 |
|
| * spelling of a capture with no value, and that already croaks. Needs |
| 686 |
|
| * Template::Stencil 0.10, where tied hashes started resolving in a path. |
| 687 |
|
| */ |
| 688 |
|
| #define PKU_T_APP 0 |
| 689 |
|
| #define PKU_T_PREFIX 1 |
| 690 |
|
| |
| 691 |
2165 |
| static SV *pk_url_tied(pTHX_ SV *app, SV *prefix_slot) { |
| 692 |
2165 |
| AV *o = newAV(); |
| 693 |
2165 |
| HV *h = newHV(); |
| 694 |
|
| SV *obj; |
| 695 |
2165 |
| av_extend(o, 2); |
| 696 |
2165 |
| av_store(o, PKU_T_APP, newSVsv(app)); |
| 697 |
2165 |
▶ | av_store(o, PKU_T_PREFIX, prefix_slot ? newSVsv(prefix_slot) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(o, PKU_T_PREFIX, prefix_slot ? newSVsv(prefix_slot) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2165 |
0 |
2165 |
|
| 698 |
2165 |
| obj = sv_bless(newRV_noinc((SV *)o), gv_stashpv("Punk::URL", GV_ADD)); |
| 699 |
2165 |
| hv_magic(h, (GV *)obj, PERL_MAGIC_tied); |
| 700 |
2165 |
| SvREFCNT_dec(obj); /* hv_magic took its own reference */ |
| 701 |
2165 |
| return newRV_noinc((SV *)h); |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| /* Is this name a static route - one the `url` hash can answer for? */ |
| 705 |
13 |
| static int pk_url_static_path(pTHX_ HV *app, SV *name, SV **path_out) { |
| 706 |
13 |
| pr_router *rt = pk_url_router(aTHX_ app); |
| 707 |
13 |
| IV idx = pk_url_idx(aTHX_ app, name); |
| 708 |
|
| SV **rr; |
| 709 |
|
| HV *rec; |
| 710 |
|
| /* |
| 711 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 712 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 713 |
13 |
▶ | if (!rt || idx < 0 || idx >= (IV)rt->nrecs) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!rt || idx < 0 || idx >= (IV)rt->nrecs) return 0; |
13 |
0 |
13 |
| if (!rt || idx < 0 || idx >= (IV)rt->nrecs) return 0; |
11 |
2 |
13 |
| if (!rt || idx < 0 || idx >= (IV)rt->nrecs) return 0; |
0 |
11 |
11 |
|
| 714 |
11 |
▶ | if (rt->dyn_of[idx] >= 0) return 0; /* dynamic: not here */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (rt->dyn_of[idx] >= 0) return 0; /* dynamic: not here */ |
1 |
10 |
11 |
|
| 715 |
10 |
| rr = av_fetch(rt->records, (SSize_t)idx, 0); |
| 716 |
|
| /* |
| 717 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 718 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 719 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 720 |
10 |
▶ | rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
10 |
0 |
10 |
| rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
10 |
0 |
10 |
| rec = (rr && *rr && SvROK(*rr)) ? (HV *)SvRV(*rr) : NULL; |
10 |
0 |
10 |
|
| 721 |
|
| /* |
| 722 |
|
| # uncoverable branch true |
| 723 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 724 |
10 |
▶ | if (!rec) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rec) return 0; |
0 |
10 |
10 |
|
| 725 |
|
| /* |
| 726 |
|
| # uncoverable branch false |
| 727 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 728 |
10 |
▶ | if (path_out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (path_out) { |
10 |
0 |
10 |
|
| 729 |
10 |
| SV **pp = hv_fetchs(rec, "path", 0); |
| 730 |
|
| /* |
| 731 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 732 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 733 |
10 |
▶ | *path_out = (pp && *pp) ? *pp : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| *path_out = (pp && *pp) ? *pp : NULL; |
10 |
0 |
10 |
| *path_out = (pp && *pp) ? *pp : NULL; |
10 |
0 |
10 |
|
| 734 |
|
| } |
| 735 |
10 |
| return 1; |
| 736 |
|
| } |
| 737 |
|
| |
| 738 |
|
| /* Parse an OpenAPI path template into the flat form pk_url_build_op walks: |
| 739 |
|
| * literals at even indices, hole names at odd ones, always odd length. |
| 740 |
|
| * |
| 741 |
|
| * /books/{id} -> [ "/books/", "id", "" ] |
| 742 |
|
| * /files/{name}.json -> [ "/files/", "name", ".json" ] |
| 743 |
|
| * /a/{x}/b/{y} -> [ "/a/", "x", "/b/", "y", "" ] |
| 744 |
|
| * |
| 745 |
|
| * An unclosed '{' is left as a literal rather than croaking: the spec is the |
| 746 |
|
| * application's, it has already been through Open::API, and a URL builder is |
| 747 |
|
| * not the place to start rejecting documents that route perfectly well. |
| 748 |
|
| */ |
| 749 |
67 |
| static AV *pk_url_parse_template(pTHX_ const char *p, STRLEN pl) { |
| 750 |
67 |
| AV *out = newAV(); |
| 751 |
67 |
| STRLEN i = 0, lit = 0; |
| 752 |
471 |
▶ | while (i < pl) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < pl) { |
404 |
67 |
471 |
|
| 753 |
404 |
▶ | if (p[i] == '{') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p[i] == '{') { |
16 |
388 |
404 |
|
| 754 |
16 |
| STRLEN close = i + 1; |
| 755 |
63 |
▶ | while (close < pl && p[close] != '}') close++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (close < pl && p[close] != '}') close++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
63 |
0 |
63 |
| while (close < pl && p[close] != '}') close++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
47 |
16 |
63 |
|
| 756 |
|
| /* |
| 757 |
|
| # uncoverable branch false |
| 758 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 759 |
16 |
▶ | if (close < pl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (close < pl) { |
16 |
0 |
16 |
|
| 760 |
16 |
| av_push(out, newSVpvn(p + lit, i - lit)); /* literal */ |
| 761 |
16 |
| av_push(out, newSVpvn(p + i + 1, close - i - 1)); /* hole */ |
| 762 |
16 |
| i = close + 1; |
| 763 |
16 |
| lit = i; |
| 764 |
16 |
| continue; |
| 765 |
|
| } |
| 766 |
|
| } |
| 767 |
388 |
| i++; |
| 768 |
|
| } |
| 769 |
67 |
| av_push(out, newSVpvn(p + lit, pl - lit)); /* the trailing literal */ |
| 770 |
67 |
| return out; |
| 771 |
|
| } |
| 772 |
|
| |
| 773 |
|
| /* Put this request's `url` hash and prefix where a template can reach them. |
| 774 |
|
| * |
| 775 |
|
| * SET, not set-if-absent: the reason pi_bind_vars gives about a handler that |
| 776 |
|
| * keeps a data hashref between requests. |
| 777 |
|
| * |
| 778 |
|
| * The prefix slot is shared with the filter closure, and getting it back to |
| 779 |
|
| * empty afterwards matters as much as setting it: a mail template rendered |
| 780 |
|
| * from a queue job has no request, and must not link under the prefix of |
| 781 |
|
| * whatever page this worker served last. It cannot be cleared from here - |
| 782 |
|
| * without a context there is no application to find the slot through - so it |
| 783 |
|
| * is `save_item`d instead, and the caller's ENTER/LEAVE puts it back. That |
| 784 |
|
| * also covers the two cases an explicit reset would not: a render that |
| 785 |
|
| * croaks part way, and a render nested inside another. |
| 786 |
|
| * |
| 787 |
|
| * The tied hash takes a COPY of the prefix, so restoring the slot afterwards |
| 788 |
|
| * does not reach back into a `url` hash the handler still holds. |
| 789 |
|
| * |
| 790 |
|
| * Must be called inside an ENTER scope. |
| 791 |
|
| */ |
| 792 |
2172 |
| static void pk_url_bind_vars(pTHX_ SV *c, SV *data) { |
| 793 |
2172 |
| SV *appsv = NULL, *slot; |
| 794 |
2172 |
| HV *app = NULL, *env = NULL; |
| 795 |
|
| AV *av; |
| 796 |
|
| SV **sp; |
| 797 |
|
| |
| 798 |
|
| /* |
| 799 |
|
| # uncoverable branch true |
| 800 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 801 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 802 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 803 |
2172 |
▶ | if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
2172 |
0 |
2172 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
2172 |
0 |
2172 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
0 |
2172 |
2172 |
|
| 804 |
|
| |
| 805 |
|
| /* |
| 806 |
|
| # uncoverable branch false |
| 807 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 808 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 809 |
2172 |
▶ | if (c && SvROK(c)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c && SvROK(c)) { |
2172 |
0 |
2172 |
| if (c && SvROK(c)) { |
2172 |
0 |
2172 |
|
| 810 |
2172 |
| av = pcx_av(aTHX_ c); |
| 811 |
|
| /* |
| 812 |
|
| # uncoverable branch false |
| 813 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 814 |
2172 |
▶ | if (av) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av) { |
2172 |
0 |
2172 |
|
| 815 |
2172 |
| SV *a = pcx_get(aTHX_ av, PCX_APP); |
| 816 |
2172 |
| SV *e = pcx_get(aTHX_ av, PCX_ENV); |
| 817 |
|
| /* |
| 818 |
|
| # uncoverable branch false |
| 819 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 820 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 821 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 822 |
2172 |
▶ | if (a && SvROK(a) && SvTYPE(SvRV(a)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (a && SvROK(a) && SvTYPE(SvRV(a)) == SVt_PVHV) { |
2172 |
0 |
2172 |
| if (a && SvROK(a) && SvTYPE(SvRV(a)) == SVt_PVHV) { |
2172 |
0 |
2172 |
| if (a && SvROK(a) && SvTYPE(SvRV(a)) == SVt_PVHV) { |
2172 |
0 |
2172 |
|
| 823 |
2172 |
| appsv = a; |
| 824 |
2172 |
| app = (HV *)SvRV(a); |
| 825 |
|
| } |
| 826 |
|
| /* |
| 827 |
|
| # uncoverable branch false |
| 828 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 829 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 830 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 831 |
2172 |
▶ | if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) |
2172 |
0 |
2172 |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) |
2172 |
0 |
2172 |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) |
2172 |
0 |
2172 |
|
| 832 |
2172 |
| env = (HV *)SvRV(e); |
| 833 |
|
| } |
| 834 |
|
| } |
| 835 |
|
| /* |
| 836 |
|
| # uncoverable branch true |
| 837 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 838 |
2172 |
▶ | if (!app) return; /* nothing named, nothing to bind */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!app) return; /* nothing named, nothing to bind */ |
0 |
2172 |
2172 |
|
| 839 |
2172 |
| sp = hv_fetchs(app, K_URL_PREFIX_C, 0); |
| 840 |
2172 |
▶ | if (!(sp && *sp)) return; /* no shipped engine at boot */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(sp && *sp)) return; /* no shipped engine at boot */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
2165 |
7 |
2172 |
| if (!(sp && *sp)) return; /* no shipped engine at boot */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
2165 |
2165 |
|
| 841 |
2165 |
| slot = *sp; |
| 842 |
|
| |
| 843 |
|
| { /* the filter reads this slot for the length of the render, and the |
| 844 |
|
| * savestack puts it back at LEAVE */ |
| 845 |
2165 |
| SV *pfx = pk_url_prefix(aTHX_ app, env); |
| 846 |
2165 |
| save_item(slot); |
| 847 |
2165 |
▶ | if (pfx) sv_setsv(slot, pfx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pfx) sv_setsv(slot, pfx); |
6 |
2159 |
2165 |
|
| 848 |
2159 |
| else sv_setpvs(slot, ""); |
| 849 |
|
| } |
| 850 |
2165 |
| (void)hv_stores((HV *)SvRV(data), "url", pk_url_tied(aTHX_ appsv, slot)); |
| 851 |
|
| } |
| 852 |
|
| |
| 853 |
|
| #endif /* PUNK_URL_H */ |