include/punk/punk_url.h

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