include/punk/punk_sitemap.h

filter: Statementstmt 96.4% 618 / 644 (3 uncoverable) Branchbran 67.7% 524 / 802 (81 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.5% 1142 / 1446 (84 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/router.xs ↑ Summary include/punk/punk_computed.h »
1 /* punk_sitemap.h - which of an application's routes are actually URLs.
2 *
3 * Punk compiles every route at boot, so the application already holds a
4 * complete list of what it serves. A sitemap is that list with a filter over
5 * it, and this is the filter.
6 *
7 * WHAT GOES IN, AND WHY THE RULES ARE THESE.
8 *
9 * A route is a candidate when its method is GET, its path holds no capture,
10 * it carries no guard, and it did not opt out. Everything else is out, and
11 * OUT IS THE SAFE DIRECTION: a page missing from a sitemap is still crawled
12 * if anything links to it, while a page wrongly present is a crawler fetching
13 * a 404 or a login redirect on a schedule, for as long as the site exists.
14 *
15 * A ROUTE WITH A CAPTURE IS NOT A URL. `/users/:id` names a shape, and the
16 * ids are not something the route table knows. Those come from the
17 * application through the `sitemap` keyword instead.
18 *
19 * A GUARDED ROUTE IS EXCLUDED WITHOUT ANYONE MAINTAINING A LIST. `guards` is
20 * on the route record and `under($prefix, $guard)` copies its chain into
21 * every route declared inside it, so the filter can see what a human writing
22 * a sitemap by hand has to remember. That is the whole argument for this
23 * living in the framework rather than in a script beside it.
24 *
25 * Must be included after punk_route.h (pr_router and the raw records),
26 * punk_names.h (the K_* keys) and punk_app.h (app_hv / app_av).
27 */
28  
29 #ifndef PUNK_SITEMAP_H
30 #define PUNK_SITEMAP_H
31  
32 /* Is this path a concrete URL rather than a pattern?
33 *
34 * DELIBERATELY the same test punk_route.h's pr_build uses to decide whether a
35 * route is static:
36 *
37 * if (!memchr(p, ':', pl) && !memchr(p, '*', pl))
38 *
39 * If the two ever disagree, the sitemap lists a path the router does not
40 * serve as written - so the rule is copied rather than reinvented, and this
41 * comment is the reminder to change both. */
42 60145 static int pks_concrete(const char *p, STRLEN pl) {
43 /*
44 # uncoverable branch true
45 # uncoverable condition left count:1 class:ignore_covered_err
46 # uncoverable condition left count:2 class:ignore_covered_err
47 # uncoverable condition left count:3 class:ignore_covered_err */
48 60145 ▶ if (!p || !pl || p[0] != '/') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !pl || p[0] != '/') return 0; 60145 0 60145
if (!p || !pl || p[0] != '/') return 0; 60145 0 60145
if (!p || !pl || p[0] != '/') return 0; 0 60145 60145
49 60145 ▶ return !memchr(p, ':', pl) && !memchr(p, '*', pl);
Branch: 2 branches
BranchTrueFalseTotal
return !memchr(p, ':', pl) && !memchr(p, '*', pl); 60081 64 60145
return !memchr(p, ':', pl) && !memchr(p, '*', pl); 60079 2 60081
50 }
51  
52 /* GET only. HEAD is served off GET and is not a page; anything else is not
53 * something a crawler should be fetching at all. `route`'s ANY form does
54 * include GET, so it counts. */
55 60211 static int pks_method_ok(const char *m, STRLEN ml) {
56 /*
57 # uncoverable branch true
58 # uncoverable condition left count:1 class:ignore_covered_err */
59 60211 ▶ if (!m) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!m) return 0; 0 60211 60211
60 60211 ▶ if (ml == 3 && memEQ(m, "GET", 3)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ml == 3 && memEQ(m, "GET", 3)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 60207 4 60211
if (ml == 3 && memEQ(m, "GET", 3)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 60207 0 60207
61 /*
62 # uncoverable branch true
63 # uncoverable condition left count:1 class:ignore_covered_err
64 # uncoverable condition left count:2 class:ignore_covered_err
65 # uncoverable condition right count:2 class:ignore_covered_err */
66 4 ▶ if (ml == 3 && memEQ(m, "ANY", 3)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ml == 3 && memEQ(m, "ANY", 3)) return 1; 0 4 4
if (ml == 3 && memEQ(m, "ANY", 3)) return 1; 0 0 0
67 4 return 0;
68 }
69  
70 /* A guard chain of any length means the page is behind authentication. The
71 * chain is the DECLARED one here rather than the resolved one - `under`
72 * copies names in before anything resolves them - and emptiness is the only
73 * question being asked, which resolution does not change. */
74 60112 static int pks_guarded(pTHX_ HV *rec) {
75 60112 SV **g = hv_fetchs(rec, K_GUARDS, 0);
76 /*
77 # uncoverable branch true
78 # uncoverable condition left count:1 class:ignore_covered_err
79 # uncoverable condition left count:2 class:ignore_covered_err
80 # uncoverable condition left count:3 class:ignore_covered_err
81 # uncoverable condition left count:4 class:ignore_covered_err */
82 60112 ▶ if (!(g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV)) return 0; 60112 0 60112
if (!(g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV)) return 0; 60112 0 60112
if (!(g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV)) return 0; 60112 0 60112
if (!(g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV)) return 0; 0 60112 60112
83 60112 return av_len((AV *)SvRV(*g)) >= 0;
84 }
85  
86 /* What did this method+path say about itself?
87 *
88 * -1 nothing
89 * 0 sitemap => 0, keep it out
90 * 1 sitemap => 1, put it in even though something above excluded it
91 *
92 * The records are the ones app.xs pushed at declaration time, the way it does
93 * for validate, compress and max_body. */
94 60338 static int pks_override(pTHX_ HV *app, const char *m, STRLEN ml,
95 const char *p, STRLEN pl) {
96 60338 SV **x = hv_fetchs(app, K_SITEMAP_ROUTES, 0);
97 AV *av;
98 SSize_t i, n;
99 /*
100 # uncoverable branch true
101 # uncoverable condition left count:1 class:ignore_covered_err
102 # uncoverable condition left count:2 class:ignore_covered_err
103 # uncoverable condition left count:3 class:ignore_covered_err
104 # uncoverable condition left count:4 class:ignore_covered_err */
105 60338 ▶ if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) return -1; 60338 0 60338
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) return -1; 60338 0 60338
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) return -1; 60338 0 60338
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) return -1; 0 60338 60338
106 60338 av = (AV *)SvRV(*x);
107 60338 n = av_len(av) + 1;
108 180905 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 120727 60178 180905
109 120727 SV **e = av_fetch(av, i, 0);
110 HV *r;
111 SV **rm, **rp;
112 STRLEN xl, yl;
113 const char *xm, *xp;
114 /*
115 # uncoverable branch true
116 # uncoverable condition left count:1 class:ignore_covered_err
117 # uncoverable condition left count:2 class:ignore_covered_err
118 # uncoverable condition left count:3 class:ignore_covered_err
119 # uncoverable condition left count:4 class:ignore_covered_err */
120 120727 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 120727 0 120727
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 120727 0 120727
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 120727 0 120727
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 0 120727 120727
121 120727 r = (HV *)SvRV(*e);
122 120727 rm = hv_fetchs(r, K_METHOD, 0);
123 120727 rp = hv_fetchs(r, K_PATH, 0);
124 /*
125 # uncoverable branch true
126 # uncoverable condition left count:1 class:ignore_covered_err
127 # uncoverable condition left count:2 class:ignore_covered_err
128 # uncoverable condition left count:3 class:ignore_covered_err
129 # uncoverable condition left count:4 class:ignore_covered_err */
130 120727 ▶ if (!(rm && *rm && rp && *rp)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(rm && *rm && rp && *rp)) continue; 120727 0 120727
if (!(rm && *rm && rp && *rp)) continue; 120727 0 120727
if (!(rm && *rm && rp && *rp)) continue; 120727 0 120727
if (!(rm && *rm && rp && *rp)) continue; 0 120727 120727
131 120727 xm = SvPV_const(*rm, xl);
132 120727 ▶ xp = SvPV_const(*rp, yl);
Branch: 1 branch
BranchTrueFalseTotal
xp = SvPV_const(*rp, yl); 120727 0 120727
133 120727 ▶ if (xl == ml && yl == pl && memEQ(xm, m, ml) && memEQ(xp, p, pl)) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (xl == ml && yl == pl && memEQ(xm, m, ml) && memEQ(xp, p, pl)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 120709 18 120727
if (xl == ml && yl == pl && memEQ(xm, m, ml) && memEQ(xp, p, pl)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 247 120462 120709
if (xl == ml && yl == pl && memEQ(xm, m, ml) && memEQ(xp, p, pl)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 247 0 247
if (xl == ml && yl == pl && memEQ(xm, m, ml) && memEQ(xp, p, pl)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 160 87 247
134 160 SV **f = hv_fetchs(r, K_SITEMAP, 0);
135 /*
136 # uncoverable condition right count:1 class:ignore_covered_err
137 # uncoverable condition right count:2 class:ignore_covered_err */
138 160 ▶ return (f && *f && SvTRUE(*f)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (f && *f && SvTRUE(*f)) ? 1 : 0; 160 0 160
return (f && *f && SvTRUE(*f)) ? 1 : 0; 160 0 160
return (f && *f && SvTRUE(*f)) ? 1 : 0; 6 154 160
139 }
140 }
141 60178 return -1;
142 }
143  
144 /* The router's raw records: {method, path, target, guards}, one per declared
145 * route, in declaration order.
146 *
147 * The RAW list and not the compiled one, because the compiled records only
148 * exist after `compile` has run and this wants to be answerable from
149 * anywhere. Raw already carries the full path and the full guard chain -
150 * scopes concatenate the prefix and copy the guards in before `route` is
151 * reached - so nothing is missing for the questions above. */
152 68 static AV *pks_raw(pTHX_ HV *app) {
153 68 SV *router = app_get(aTHX_ app, K_ROUTER);
154 pr_router *rt;
155 /*
156 # uncoverable branch true
157 # uncoverable condition left count:1 class:ignore_covered_err
158 # uncoverable condition left count:2 class:ignore_covered_err
159 # uncoverable condition left count:3 class:ignore_covered_err
160 # uncoverable condition left count:4 class:ignore_covered_err */
161 68 ▶ if (!(router && SvROK(router) && SvIOK(SvRV(router)) && SvIV(SvRV(router))))
Branch: 4 branches
BranchTrueFalseTotal
if (!(router && SvROK(router) && SvIOK(SvRV(router)) && SvIV(SvRV(router)))) 68 0 68
if (!(router && SvROK(router) && SvIOK(SvRV(router)) && SvIV(SvRV(router)))) 68 0 68
if (!(router && SvROK(router) && SvIOK(SvRV(router)) && SvIV(SvRV(router)))) 68 0 68
if (!(router && SvROK(router) && SvIOK(SvRV(router)) && SvIV(SvRV(router)))) 0 68 68
162 0 return NULL; /* # uncoverable statement */
163 68 rt = INT2PTR(pr_router *, SvIV(SvRV(router)));
164 68 ▶ return rt ? rt->raw : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return rt ? rt->raw : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 68 0 68
165 }
166  
167 /* Sort helper: paths compare as bytes, so two boots of one application
168 * produce a byte-identical list. Hash order would make the sitemap's ETag
169 * and Last-Modified move on a deploy that changed nothing, and would make a
170 * diff of two environments noise. */
171 240493 static I32 pks_cmp(pTHX_ SV *a, SV *b) {
172 STRLEN al, bl;
173 240493 ▶ const char *ap = SvPV_const(a, al);
Branch: 1 branch
BranchTrueFalseTotal
const char *ap = SvPV_const(a, al); 240493 0 240493
174 240493 const char *bp = SvPV_const(b, bl);
175 240493 STRLEN n = al < bl ? al : bl;
176 240493 ▶ int c = n ? memcmp(ap, bp, n) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
int c = n ? memcmp(ap, bp, n) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 240493 0 240493
177 240493 ▶ if (c) return c < 0 ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (c) return c < 0 ? -1 : 1; 215923 24570 240493
if (c) return c < 0 ? -1 : 1; 110046 105877 215923
178 24570 ▶ return al == bl ? 0 : (al < bl ? -1 : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return al == bl ? 0 : (al < bl ? -1 : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 24570 0 24570
return al == bl ? 0 : (al < bl ? -1 : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 24564 6 24570
179 }
180  
181 /* The same ordering, over [loc, lastmod] pairs. */
182 109313 static I32 pks_cmp_pair(pTHX_ SV *a, SV *b) {
183 109313 SV **x = av_fetch((AV *)SvRV(a), 0, 0);
184 109313 SV **y = av_fetch((AV *)SvRV(b), 0, 0);
185 /*
186 # uncoverable condition right count:1 class:ignore_covered_err
187 # uncoverable condition right count:2 class:ignore_covered_err */
188 109313 ▶ return pks_cmp(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
return pks_cmp(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef); 109313 0 109313
return pks_cmp(aTHX_ x ? *x : &PL_sv_undef, y ? *y : &PL_sv_undef); 109313 0 109313
189 }
190  
191 /* The candidate paths, sorted and de-duplicated (+1).
192 *
193 * Paths, not URLs: the absolute form needs the configured base, and taking
194 * that from the request would be a host-header injection delivered to search
195 * engines. Joining happens where the base is known to be configuration. */
196 57 static AV *pks_paths(pTHX_ SV *appsv) {
197 57 HV *app = app_hv(aTHX_ appsv);
198 57 ▶ AV *raw = app ? pks_raw(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
AV *raw = app ? pks_raw(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 57 0 57
199 57 AV *out = newAV();
200 57 HV *seen = (HV *)sv_2mortal((SV *)newHV());
201 SSize_t i, n;
202  
203 /*
204 # uncoverable branch true
205 # uncoverable condition left count:1 class:ignore_covered_err */
206 57 ▶ if (!raw) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!raw) return out; 0 57 57
207 57 n = av_len(raw) + 1;
208 60333 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 60276 57 60333
209 60276 SV **e = av_fetch(raw, i, 0);
210 HV *rec;
211 SV **msv, **psv;
212 STRLEN ml, pl;
213 const char *m, *p;
214  
215 /*
216 # uncoverable branch true class:ignore_covered_err
217 # uncoverable condition left count:1 class:ignore_covered_err
218 # uncoverable condition left count:2 class:ignore_covered_err
219 # uncoverable condition left count:3 class:ignore_covered_err
220 # uncoverable condition left count:4 class:ignore_covered_err */
221 60484 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 60276 0 60276
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 60276 0 60276
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 60276 0 60276
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 0 60276 60276
222 60276 rec = (HV *)SvRV(*e);
223 60276 msv = hv_fetchs(rec, K_METHOD, 0);
224 60276 psv = hv_fetchs(rec, K_PATH, 0);
225 /*
226 # uncoverable branch true
227 # uncoverable condition left count:1 class:ignore_covered_err
228 # uncoverable condition left count:2 class:ignore_covered_err
229 # uncoverable condition left count:3 class:ignore_covered_err
230 # uncoverable condition left count:4 class:ignore_covered_err */
231 60276 ▶ if (!(msv && *msv && psv && *psv)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(msv && *msv && psv && *psv)) continue; 60276 0 60276
if (!(msv && *msv && psv && *psv)) continue; 60276 0 60276
if (!(msv && *msv && psv && *psv)) continue; 60276 0 60276
if (!(msv && *msv && psv && *psv)) continue; 0 60276 60276
232 60276 m = SvPV_const(*msv, ml);
233 60276 ▶ p = SvPV_const(*psv, pl);
Branch: 1 branch
BranchTrueFalseTotal
p = SvPV_const(*psv, pl); 60276 0 60276
234  
235 {
236 60276 int said = pks_override(aTHX_ app, m, ml, p, pl);
237 60276 ▶ if (said == 0) continue; /* sitemap => 0 */
Branch: 1 branch
BranchTrueFalseTotal
if (said == 0) continue; /* sitemap => 0 */ 127 60149 60276
238  
239 /* Method and shape are never overridable: a POST is not a page
240 * and `/users/:id` is not a URL, so `sitemap => 1` on either is a
241 * mistake rather than an instruction. The guard is the only
242 * exclusion an application can argue with, because it is the only
243 * one this cannot read the intent of. */
244 60149 ▶ if (!pks_method_ok(m, ml)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pks_method_ok(m, ml)) continue; 4 60145 60149
245 60145 ▶ if (!pks_concrete(p, pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pks_concrete(p, pl)) continue; 66 60079 60145
246 60079 ▶ if (said != 1 && pks_guarded(aTHX_ rec)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (said != 1 && pks_guarded(aTHX_ rec)) continue; 60077 2 60079
if (said != 1 && pks_guarded(aTHX_ rec)) continue; 11 60066 60077
247 }
248 /*
249 # uncoverable branch true
250 # uncoverable condition left count:1 class:ignore_covered_err */
251 60068 ▶ if (hv_exists(seen, p, (I32)pl)) continue; /* ANY + GET */
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, p, (I32)pl)) continue; /* ANY + GET */ 0 60068 60068
252  
253 /* newSViv and not &PL_sv_yes: hv_store takes ownership of the value's
254 * refcount, and an immortal handed to it is decremented when the hash
255 * is freed - which before 5.20 is a real decrement of a value that
256 * has no protection, and eventually a free underneath the
257 * interpreter. */
258 60068 (void)hv_store(seen, p, (I32)pl, newSViv(1), 0);
259 60068 av_push(out, newSVpvn(p, pl));
260 }
261  
262 57 ▶ if (av_len(out) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) > 0) 15 42 57
263 15 sortsv(AvARRAY(out), (size_t)(av_len(out) + 1), pks_cmp);
264 57 return out;
265 }
266  
267 /* ---- rendering -------------------------------------------------------------
268 *
269 * The protocol caps a sitemap at 50,000 URLs AND 50MB uncompressed, and past
270 * either one crawlers reject the file - the whole file, not the excess. So
271 * both are counted as the document is written and whichever binds first
272 * starts a new part.
273 *
274 * The byte cap binds more often than people expect: 50,000 URLs at 1KB each
275 * is 50MB, and a long path inside its <url> element is not far off 200 bytes.
276 * Which is why it is counted as written rather than estimated. */
277 #define PKS_MAX_URLS 50000
278 #define PKS_MAX_BYTES (50UL * 1024 * 1024)
279  
280 /* XML escaping, over every emitted URL without exception.
281 *
282 * One unescaped `&` makes the document not well-formed, and a crawler
283 * rejects ALL of it rather than the offending entry - so this is a
284 * correctness problem before it is a security one. The route half rarely
285 * contains anything needing it; the dynamic half routinely will, because a
286 * slug with an ampersand in it is ordinary. */
287 110100 static void pks_xml_cat(pTHX_ SV *out, const char *s, STRLEN l) {
288 110100 STRLEN i, start = 0;
289 2900189 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 2790089 110100 2900189
290 const char *rep;
291 /*
292 # uncoverable branch true count:1
293 # uncoverable branch true count:2
294 # uncoverable branch true count:3
295 # uncoverable branch true count:4
296 # uncoverable branch true count:5
297 # uncoverable branch false count:6 */
298 2790089 switch (s[i]) {
299 0 case '&': rep = "&amp;"; break; /* # uncoverable statement */
300 0 case '<': rep = "&lt;"; break; /* # uncoverable statement */
301 0 case '>': rep = "&gt;"; break; /* # uncoverable statement */
302 /*
303 # uncoverable statement */
304 0 case '"': rep = "&quot;"; break;
305 0 case '\'': rep = "&apos;"; break; /* # uncoverable statement */
306 2790089 default: continue;
307 }
308 /*
309 # uncoverable statement
310 # uncoverable branch true
311 # uncoverable branch false
312 # uncoverable condition left count:1
313 # uncoverable condition right count:1 */
314 0 ▶ if (i > start) sv_catpvn(out, s + start, i - start);
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) sv_catpvn(out, s + start, i - start); 0 0 0
315 0 sv_catpv(out, rep); /* # uncoverable statement */
316 0 start = i + 1; /* # uncoverable statement */
317 }
318 /*
319 # uncoverable branch false
320 # uncoverable condition right count:1 class:ignore_covered_err */
321 110100 ▶ if (l > start) sv_catpvn(out, s + start, l - start);
Branch: 1 branch
BranchTrueFalseTotal
if (l > start) sv_catpvn(out, s + start, l - start); 110100 0 110100
322 110100 }
323  
324 /* Percent-encoding, which is a DIFFERENT job from escaping and comes first.
325 *
326 * A path holding a space or a non-ASCII byte has to be percent-encoded to be
327 * a URL at all; escaping alone would produce a well-formed document
328 * containing an invalid URL. Encoding first also means everything reaching
329 * the escaper is ASCII, so a route path that is not valid UTF-8 cannot make
330 * the document unparseable - the encoder turned it into %XX long before.
331 *
332 * Unreserved plus '/' is kept and everything else encoded. Over-encoding a
333 * sub-delimiter is legal and costs three bytes; under-encoding one is a
334 * different URL. */
335 110098 static void pks_pct_cat(pTHX_ SV *out, const char *s, STRLEN l) {
336 static const char hex[] = "0123456789ABCDEF";
337 STRLEN i;
338 908513 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 798415 110098 908513
339 798415 unsigned char c = (unsigned char)s[i];
340 /*
341 # uncoverable condition left count:3 class:ignore_covered_err
342 # uncoverable condition left count:4 class:ignore_covered_err
343 # uncoverable condition left count:5 class:ignore_covered_err */
344 798415 ▶ if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~'
Branch: 5 branches
BranchTrueFalseTotal
if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~' 160143 638272 798415
if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~' 160142 1 160143
if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~' 160142 0 160142
if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~' 160142 0 160142
if (isALNUM(c) || c == '-' || c == '.' || c == '_' || c == '~' 160142 0 160142
345 160142 ▶ || c == '/') {
Branch: 1 branch
BranchTrueFalseTotal
|| c == '/') { 160138 4 160142
346 798411 sv_catpvn(out, (const char *)&c, 1);
347 }
348 else {
349 char e[3];
350 4 e[0] = '%'; e[1] = hex[c >> 4]; e[2] = hex[c & 0xF];
351 4 sv_catpvn(out, e, 3);
352 }
353 }
354 110098 }
355  
356 /* One <url> element onto `out`, base + path, encoded then escaped. */
357 /* ---- the dynamic half -------------------------------------------------------
358 *
359 * A section is application code returning data that goes straight into a
360 * structured document, so every field is checked on the way in. The route
361 * half needs none of this because its paths are declarations; a section's are
362 * database rows.
363 */
364  
365 /* Is this a location this application may publish?
366 *
367 * Rooted, concrete, same-origin, and free of anything that would break out of
368 * the element it lands in:
369 *
370 * - it must start with '/', because the base supplies the origin;
371 * - it must NOT start with '//', which is protocol-relative and names
372 * another host - `//evil.example/x` in a sitemap is somebody else's pages
373 * published under your name;
374 * - it must hold no ':' or '*', which means a section returned the route
375 * PATTERN by mistake and a literal `:id` would go in the file;
376 * - no control bytes and no backslash, the bug class
377 * reference_reflected_request_bytes exists for.
378 *
379 * `$c->safe_path` encodes the same rules for redirects, and the reasoning is
380 * identical: bytes the application did not write, reaching somewhere
381 * structured. */
382 50025 static int pks_loc_ok(const char *p, STRLEN l) {
383 STRLEN i;
384 /*
385 # uncoverable condition left count:1 class:ignore_covered_err
386 # uncoverable condition left count:2 class:ignore_covered_err */
387 50025 ▶ if (!p || !l || p[0] != '/') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !l || p[0] != '/') return 0; 50025 0 50025
if (!p || !l || p[0] != '/') return 0; 50025 0 50025
if (!p || !l || p[0] != '/') return 0; 4 50021 50025
388 50021 ▶ if (l > 1 && p[1] == '/') return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l > 1 && p[1] == '/') return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 50021 0 50021
if (l > 1 && p[1] == '/') return 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 50020 50021
389 50020 ▶ if (memchr(p, ':', l) || memchr(p, '*', l)) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(p, ':', l) || memchr(p, '*', l)) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 50019 1 50020
if (memchr(p, ':', l) || memchr(p, '*', l)) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 50019 50019
390 439062 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 389045 50017 439062
391 389045 unsigned char c = (unsigned char)p[i];
392 389045 ▶ if (c < 0x20 || c == 0x7F || c == '\\') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (c < 0x20 || c == 0x7F || c == '\\') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 389044 1 389045
if (c < 0x20 || c == 0x7F || c == '\\') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 389044 0 389044
if (c < 0x20 || c == 0x7F || c == '\\') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 389043 389044
393 }
394 50017 return 1;
395 }
396  
397 /* W3C Datetime, which is what <lastmod> takes: a date, optionally with a
398 * time. Parsed rather than passed through, because one unparseable value
399 * makes the whole document invalid and a crawler rejects all of it. Anything
400 * that does not fit is DROPPED and the URL still goes in - a missing lastmod
401 * costs nothing, and losing the page would. */
402 12 static int pks_lastmod_ok(const char *s, STRLEN l) {
403 STRLEN i;
404 /*
405 # uncoverable branch true
406 # uncoverable condition left count:1 class:ignore_covered_err */
407 12 ▶ if (l < 10) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (l < 10) return 0; 0 12 12
408 116 ▶ for (i = 0; i < 10; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 10; i++) { 106 10 116
409 106 ▶ if (i == 4 || i == 7) { if (s[i] != '-') return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (i == 4 || i == 7) { if (s[i] != '-') return 0; } 95 11 106
if (i == 4 || i == 7) { if (s[i] != '-') return 0; } 10 85 95
if (i == 4 || i == 7) { if (s[i] != '-') return 0; } 1 20 21
410 85 ▶ else if (!isDIGIT(s[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!isDIGIT(s[i])) return 0; 1 84 85
411 }
412 10 ▶ if (l == 10) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (l == 10) return 1; 9 1 10
413 /*
414 # uncoverable branch true
415 # uncoverable condition left count:1 class:ignore_covered_err */
416 1 ▶ if (s[10] != 'T') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s[10] != 'T') return 0; 0 1 1
417 /* the rest is time and zone: digits, ':', '+', '-', '.', 'Z' */
418 10 ▶ for (i = 11; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 11; i < l; i++) { 9 1 10
419 9 char c = s[i];
420 /*
421 # uncoverable branch true
422 # uncoverable condition left count:1 class:ignore_covered_err
423 # uncoverable condition left count:2 class:ignore_covered_err
424 # uncoverable condition left count:3 class:ignore_covered_err
425 # uncoverable condition right count:3 class:ignore_covered_err
426 # uncoverable condition left count:4 class:ignore_covered_err
427 # uncoverable condition right count:4 class:ignore_covered_err
428 # uncoverable condition left count:5 class:ignore_covered_err
429 # uncoverable condition right count:5 class:ignore_covered_err
430 # uncoverable condition left count:6 class:ignore_covered_err */
431 9 ▶ if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.'
Branch: 6 branches
BranchTrueFalseTotal
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 3 6 9
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 1 2 3
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 1 0 1
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 1 0 1
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 1 0 1
if (!(isDIGIT(c) || c == ':' || c == '+' || c == '-' || c == '.' 0 1 1
432 0 || c == 'Z')) return 0; /* # uncoverable statement class:ignore_covered_err */
433 }
434 1 return 1;
435 }
436  
437 /* One <url> element, with the optional <lastmod>. */
438 static void pks_url_cat_lm(pTHX_ SV *out, SV *base, const char *p, STRLEN pl,
439 SV *lastmod);
440  
441 /*
442 # uncoverable statement
443 # uncoverable subroutine */
444 0 static void pks_url_cat(pTHX_ SV *out, SV *base, const char *p, STRLEN pl) {
445 0 SV *loc = sv_2mortal(newSVpvn("", 0)); /* # uncoverable statement */
446 STRLEN bl;
447 0 const char *bp = SvPV_const(base, bl); /* # uncoverable statement */
448 /* A trailing slash on the base would double the one every path starts
449 * with, and `https://x//about` is a different URL to a crawler. */
450 /*
451 # uncoverable statement
452 # uncoverable branch true
453 # uncoverable branch false
454 # uncoverable condition left count:1
455 # uncoverable condition right count:1
456 # uncoverable condition left count:2
457 # uncoverable condition right count:2 */
458 0 ▶ while (bl && bp[bl - 1] == '/') bl--;
Branch: 2 branches
BranchTrueFalseTotal
while (bl && bp[bl - 1] == '/') bl--; 0 0 0
while (bl && bp[bl - 1] == '/') bl--; 0 0 0
459 0 sv_catpvn(loc, bp, bl); /* # uncoverable statement */
460 0 pks_pct_cat(aTHX_ loc, p, pl); /* # uncoverable statement */
461  
462 0 sv_catpvs(out, " <url><loc>"); /* # uncoverable statement */
463 {
464 STRLEN ll;
465 0 const char *lp = SvPV_const(loc, ll); /* # uncoverable statement */
466 0 pks_xml_cat(aTHX_ out, lp, ll); /* # uncoverable statement */
467 }
468 0 sv_catpvs(out, "</loc></url>\n"); /* # uncoverable statement */
469 0 }
470  
471 110086 static void pks_url_cat_lm(pTHX_ SV *out, SV *base, const char *p, STRLEN pl,
472 SV *lastmod) {
473 110086 SV *loc = sv_2mortal(newSVpvn("", 0));
474 STRLEN bl;
475 110086 const char *bp = SvPV_const(base, bl);
476 110088 ▶ while (bl && bp[bl - 1] == '/') bl--; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (bl && bp[bl - 1] == '/') bl--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 110088 0 110088
while (bl && bp[bl - 1] == '/') bl--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 110086 110088
477 110086 sv_catpvn(loc, bp, bl);
478 110086 pks_pct_cat(aTHX_ loc, p, pl);
479  
480 110086 sv_catpvs(out, " <url><loc>");
481 {
482 STRLEN ll;
483 110086 const char *lp = SvPV_const(loc, ll);
484 110086 pks_xml_cat(aTHX_ out, lp, ll);
485 }
486 110086 sv_catpvs(out, "</loc>");
487 110086 ▶ if (lastmod && SvOK(lastmod)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (lastmod && SvOK(lastmod)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 110086 0 110086
if (lastmod && SvOK(lastmod)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 110074 110086
488 STRLEN ml;
489 12 const char *mp = SvPV_const(lastmod, ml);
490 12 ▶ if (pks_lastmod_ok(mp, ml)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pks_lastmod_ok(mp, ml)) { 10 2 12
491 10 sv_catpvs(out, "<lastmod>");
492 10 pks_xml_cat(aTHX_ out, mp, ml);
493 10 sv_catpvs(out, "</lastmod>");
494 }
495 }
496 110086 sv_catpvs(out, "</url>\n");
497 110086 }
498  
499 /* Run one section and collect what it returned as [loc, lastmod] pairs.
500 *
501 * G_EVAL: a section reads a database, and a section that died must not take
502 * the request with it. A failed section warns and contributes nothing, which
503 * degrades the sitemap rather than the site. */
504 12 static void pks_run_section(pTHX_ SV *code, SV *name, AV *out) {
505 12 dSP;
506 int count, i;
507 12 AV *got = (AV *)sv_2mortal((SV *)newAV());
508 12 IV kept = 0;
509  
510 12 ENTER; SAVETMPS;
511 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
512 12 PUTBACK;
513 12 count = call_sv(code, G_ARRAY | G_EVAL);
514 12 SPAGAIN;
515 50040 ▶ for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs));
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs)); 50028 12 50040
516 12 PUTBACK;
517 12 ▶ if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 11 12
518 1 ▶ warn("Punk::Plugin::Sitemap: section '%" SVf "' died, and contributes "
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::Plugin::Sitemap: section '%" SVf "' died, and contributes " 1 0 1
519 "nothing: %" SVf, SVfARG(name), SVfARG(ERRSV));
520 12 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 12 0 12
521  
522 50039 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 50028 11 50039
523 50028 SV **e = av_fetch(got, (SSize_t)i, 0);
524 50028 SV *loc = NULL, *lm = NULL;
525 STRLEN ll;
526 const char *lp;
527 /*
528 # uncoverable condition left count:1 class:ignore_covered_err
529 # uncoverable condition left count:2 class:ignore_covered_err */
530 50038 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 50028 0 50028
if (!(e && *e && SvOK(*e))) continue; 50028 0 50028
if (!(e && *e && SvOK(*e))) continue; 1 50027 50028
531  
532 50027 ▶ if (SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 50017 50027
if (SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
533 10 HV *r = (HV *)SvRV(*e);
534 10 SV **x = hv_fetchs(r, "loc", 0);
535 10 SV **m = hv_fetchs(r, "lastmod", 0);
536 10 ▶ loc = (x && *x) ? *x : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
loc = (x && *x) ? *x : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 1 10
loc = (x && *x) ? *x : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 0 9
537 /*
538 # uncoverable condition right count:2 class:ignore_covered_err
539 # uncoverable condition right count:3 class:ignore_covered_err */
540 10 ▶ lm = (m && *m && SvOK(*m)) ? *m : NULL;
Branch: 3 branches
BranchTrueFalseTotal
lm = (m && *m && SvOK(*m)) ? *m : NULL; 6 4 10
lm = (m && *m && SvOK(*m)) ? *m : NULL; 6 0 6
lm = (m && *m && SvOK(*m)) ? *m : NULL; 6 0 6
541 }
542 50017 else loc = *e; /* a bare string is a loc */
543  
544 50027 ▶ if (!(loc && SvOK(loc))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(loc && SvOK(loc))) continue; 50026 1 50027
if (!(loc && SvOK(loc))) continue; 1 50025 50026
545 50025 lp = SvPV_const(loc, ll);
546  
547 50025 ▶ if (!pks_loc_ok(lp, ll)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pks_loc_ok(lp, ll)) { 8 50017 50025
548 8 warn("Punk::Plugin::Sitemap: section '%" SVf "' returned a loc "
549 "that is not a rooted, same-origin path and is dropped: "
550 "'%" SVf "'", SVfARG(name), SVfARG(loc));
551 8 continue;
552 }
553 /* A section walking a table that grew is how this becomes an
554 * out-of-memory at boot rather than a large file. */
555 50017 ▶ if (kept >= PKS_MAX_URLS) {
Branch: 1 branch
BranchTrueFalseTotal
if (kept >= PKS_MAX_URLS) { 1 50016 50017
556 1 warn("Punk::Plugin::Sitemap: section '%" SVf "' returned more "
557 "than %d locations and is truncated there",
558 SVfARG(name), (int)PKS_MAX_URLS);
559 1 break;
560 }
561 {
562 50016 AV *pair = newAV();
563 50016 av_push(pair, newSVsv(loc));
564 50016 ▶ av_push(pair, lm ? newSVsv(lm) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pair, lm ? newSVsv(lm) : newSV(0)); 6 50010 50016
565 50016 av_push(out, newRV_noinc((SV *)pair));
566 50016 kept++;
567 }
568 }
569 12 }
570  
571 /* The pages of every `markdown` mount, as [loc, lastmod] pairs sorted by loc.
572 *
573 * A mount is invisible to the route table, but a markdown mount is exactly the
574 * content a sitemap is for, and its page table is built by the time this runs
575 * (compile builds the mounts before it wraps the middleware). lastmod is the
576 * page's `updated:`, else its `date:`, as YYYY-MM-DD; an undated page goes in
577 * without one, as a route does.
578 *
579 * `robots:` naming noindex leaves a page out of the sitemap - and it is NOT
580 * added to robots.txt: a page a crawler may not fetch is a page whose noindex
581 * it never reads. `sitemap => 0` on the mount leaves out the whole tree. */
582 6 static int pks_noindex(const char *s, STRLEN l) {
583 STRLEN i;
584 /*
585 # uncoverable branch false
586 # uncoverable condition right count:1 class:ignore_covered_err */
587 6 ▶ for (i = 0; i + 7 <= l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 7 <= l; i++) 6 0 6
588 6 ▶ if (toLOWER(s[i]) == 'n' && toLOWER(s[i + 1]) == 'o' /* # uncoverable branch false */
Branch: 4 branches
BranchTrueFalseTotal
if (toLOWER(s[i]) == 'n' && toLOWER(s[i + 1]) == 'o' /* # uncoverable branch false */ 5 1 6
if (toLOWER(s[i]) == 'n' && toLOWER(s[i + 1]) == 'o' /* # uncoverable branch false */ 6 0 6
if (toLOWER(s[i]) == 'n' && toLOWER(s[i + 1]) == 'o' /* # uncoverable branch false */ 0 6 6
if (toLOWER(s[i]) == 'n' && toLOWER(s[i + 1]) == 'o' /* # uncoverable branch false */ 6 0 6
589 6 ▶ && toLOWER(s[i + 2]) == 'i' && toLOWER(s[i + 3]) == 'n'
Branch: 4 branches
BranchTrueFalseTotal
&& toLOWER(s[i + 2]) == 'i' && toLOWER(s[i + 3]) == 'n' 5 1 6
&& toLOWER(s[i + 2]) == 'i' && toLOWER(s[i + 3]) == 'n' 6 0 6
&& toLOWER(s[i + 2]) == 'i' && toLOWER(s[i + 3]) == 'n' 0 6 6
&& toLOWER(s[i + 2]) == 'i' && toLOWER(s[i + 3]) == 'n' 6 0 6
590 6 ▶ && toLOWER(s[i + 4]) == 'd' && toLOWER(s[i + 5]) == 'e'
Branch: 4 branches
BranchTrueFalseTotal
&& toLOWER(s[i + 4]) == 'd' && toLOWER(s[i + 5]) == 'e' 0 6 6
&& toLOWER(s[i + 4]) == 'd' && toLOWER(s[i + 5]) == 'e' 6 0 6
&& toLOWER(s[i + 4]) == 'd' && toLOWER(s[i + 5]) == 'e' 0 6 6
&& toLOWER(s[i + 4]) == 'd' && toLOWER(s[i + 5]) == 'e' 6 0 6
591 6 ▶ && toLOWER(s[i + 6]) == 'x')
Branch: 2 branches
BranchTrueFalseTotal
&& toLOWER(s[i + 6]) == 'x') 0 6 6
&& toLOWER(s[i + 6]) == 'x') 6 0 6
592 6 return 1;
593 0 return 0; /* # uncoverable statement */
594 }
595  
596 50 static AV *pks_md_entries(pTHX_ SV *appsv) {
597 50 HV *app = app_hv(aTHX_ appsv);
598 50 ▶ SV *mp = app ? app_get(aTHX_ app, K_MOUNTS_C) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *mp = app ? app_get(aTHX_ app, K_MOUNTS_C) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 50 0 50
599 50 AV *out = newAV();
600 AV *mounts;
601 SSize_t i, n;
602 /*
603 # uncoverable branch true
604 # uncoverable condition left count:1 class:ignore_covered_err
605 # uncoverable condition left count:2 class:ignore_covered_err
606 # uncoverable condition left count:3 class:ignore_covered_err */
607 50 ▶ if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) return out;
Branch: 3 branches
BranchTrueFalseTotal
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) return out; 50 0 50
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) return out; 50 0 50
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) return out; 0 50 50
608 50 mounts = (AV *)SvRV(mp);
609 50 n = av_len(mounts) + 1;
610 59 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 50 59
611 9 SV **e = av_fetch(mounts, i, 0);
612 HV *m;
613 SV **flag, **mapp, *opt;
614 AV *cap, *order;
615 HV *pages, *opts;
616 SV *prefix;
617 SSize_t j, pn;
618 /*
619 # uncoverable branch true
620 # uncoverable condition left count:1 class:ignore_covered_err
621 # uncoverable condition left count:2 class:ignore_covered_err
622 # uncoverable condition left count:3 class:ignore_covered_err */
623 9 ▶ if (!(e && *e && SvROK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e))) continue; 9 0 9
if (!(e && *e && SvROK(*e))) continue; 9 0 9
if (!(e && *e && SvROK(*e))) continue; 0 9 9
624 9 m = (HV *)SvRV(*e);
625 9 flag = hv_fetchs(m, K_MD_MOUNT, 0);
626 /*
627 # uncoverable branch true
628 # uncoverable condition left count:1 class:ignore_covered_err
629 # uncoverable condition left count:2 class:ignore_covered_err
630 # uncoverable condition left count:3 class:ignore_covered_err */
631 9 ▶ if (!(flag && *flag && SvTRUE(*flag))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(flag && *flag && SvTRUE(*flag))) continue; 9 0 9
if (!(flag && *flag && SvTRUE(*flag))) continue; 9 0 9
if (!(flag && *flag && SvTRUE(*flag))) continue; 0 9 9
632 9 mapp = hv_fetchs(m, K_APP, 0);
633 9 ▶ cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV)
Branch: 3 branches
BranchTrueFalseTotal
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 9 0 9
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 9 0 9
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 9 0 9
634 18 ▶ ? punk_clos_cap(aTHX_ (CV *)SvRV(*mapp)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? punk_clos_cap(aTHX_ (CV *)SvRV(*mapp)) : NULL; 9 0 9
635 /*
636 # uncoverable branch true
637 # uncoverable condition left count:1 class:ignore_covered_err */
638 9 ▶ if (!cap) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) continue; 0 9 9
639 9 opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
640 9 opt = pmd_opt(aTHX_ opts, "sitemap");
641 9 ▶ if (opt && !SvTRUE(opt)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (opt && !SvTRUE(opt)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 7 9
if (opt && !SvTRUE(opt)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
642 7 pages = (HV *)SvRV(*av_fetch(cap, PMDC_PAGES, 0));
643 7 order = (AV *)SvRV(*av_fetch(cap, PMDC_ORDER, 0));
644 7 prefix = *av_fetch(cap, PMDC_PREFIX, 0);
645 7 pn = av_len(order) + 1;
646 41 ▶ for (j = 0; j < pn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pn; j++) { 34 7 41
647 34 SV **u = av_fetch(order, j, 0);
648 HE *he;
649 AV *page, *pair;
650 34 SV **x, *loc, *lm = NULL;
651 /*
652 # uncoverable branch true
653 # uncoverable condition left count:1 class:ignore_covered_err
654 # uncoverable condition left count:2 class:ignore_covered_err */
655 34 ▶ if (!(u && *u)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(u && *u)) continue; 34 0 34
if (!(u && *u)) continue; 0 34 34
656 34 he = hv_fetch_ent(pages, *u, 0, 0);
657 /*
658 # uncoverable branch true
659 # uncoverable condition left count:1 class:ignore_covered_err
660 # uncoverable condition left count:2 class:ignore_covered_err
661 # uncoverable condition left count:3 class:ignore_covered_err */
662 34 ▶ if (!(he && HeVAL(he) && SvROK(HeVAL(he)))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(he && HeVAL(he) && SvROK(HeVAL(he)))) continue; 34 0 34
if (!(he && HeVAL(he) && SvROK(HeVAL(he)))) continue; 34 0 34
if (!(he && HeVAL(he) && SvROK(HeVAL(he)))) continue; 0 34 34
663 34 page = (AV *)SvRV(HeVAL(he));
664 34 x = av_fetch(page, PMD_META, 0);
665 /*
666 # uncoverable branch false
667 # uncoverable condition right count:1 class:ignore_covered_err
668 # uncoverable condition right count:2 class:ignore_covered_err
669 # uncoverable condition right count:3 class:ignore_covered_err */
670 34 ▶ if (x && *x && SvROK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x)) { 34 0 34
if (x && *x && SvROK(*x)) { 34 0 34
if (x && *x && SvROK(*x)) { 34 0 34
671 34 SV *r = pmd_opt(aTHX_ (HV *)SvRV(*x), "robots");
672 34 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 6 28 34
673 STRLEN rl;
674 6 const char *rp = SvPV_const(r, rl);
675 /*
676 # uncoverable branch false
677 # uncoverable condition right count:1 class:ignore_covered_err */
678 6 ▶ if (pks_noindex(rp, rl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pks_noindex(rp, rl)) continue; 6 0 6
679 }
680 }
681 28 loc = newSVsv(prefix);
682 28 sv_catsv(loc, *u);
683 /*
684 # uncoverable branch true
685 # uncoverable condition left count:1 class:ignore_covered_err */
686 28 ▶ if (!SvCUR(loc)) sv_catpvs(loc, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(loc)) sv_catpvs(loc, "/"); 0 28 28
687 28 x = av_fetch(page, PMD_UPDATED, 0);
688 /*
689 # uncoverable condition left count:1 class:ignore_covered_err
690 # uncoverable condition left count:2 class:ignore_covered_err */
691 28 ▶ if (!(x && *x && SvOK(*x))) x = av_fetch(page, PMD_DATE, 0);
Branch: 3 branches
BranchTrueFalseTotal
if (!(x && *x && SvOK(*x))) x = av_fetch(page, PMD_DATE, 0); 28 0 28
if (!(x && *x && SvOK(*x))) x = av_fetch(page, PMD_DATE, 0); 28 0 28
if (!(x && *x && SvOK(*x))) x = av_fetch(page, PMD_DATE, 0); 18 10 28
692 /*
693 # uncoverable condition right count:1 class:ignore_covered_err
694 # uncoverable condition right count:2 class:ignore_covered_err */
695 28 ▶ if (x && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { 28 0 28
if (x && *x && SvOK(*x)) { 28 0 28
if (x && *x && SvOK(*x)) { 10 18 28
696 10 SV *iso = sv_2mortal(pmd_iso(aTHX_ SvIV(*x)));
697 10 lm = newSVpvn(SvPVX(iso), 10); /* YYYY-MM-DD */
698 }
699 28 pair = newAV();
700 28 av_push(pair, loc);
701 28 ▶ av_push(pair, lm ? lm : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pair, lm ? lm : newSV(0)); 18 10 28
702 28 av_push(out, newRV_noinc((SV *)pair));
703 }
704 }
705 50 ▶ if (av_len(out) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) > 0) 6 44 50
706 6 sortsv(AvARRAY(out), (size_t)(av_len(out) + 1), pks_cmp_pair);
707 50 return out;
708 }
709  
710 /* Every entry that goes in the document: the route half first, then the
711 * markdown mounts' pages, then each section in declaration order.
712 *
713 * Both halves are sorted within themselves and de-duplicated across the
714 * whole, so a page that is both a static route and a section row appears
715 * once - a duplicated <url> is something crawlers dislike and nobody meant.
716 * Determinism survives the dynamic half this way: a section returning rows in
717 * database order would otherwise make every regeneration a different file. */
718 39 static AV *pks_entries(pTHX_ SV *appsv) {
719 39 HV *app = app_hv(aTHX_ appsv);
720 39 AV *paths = (AV *)sv_2mortal((SV *)pks_paths(aTHX_ appsv));
721 39 AV *out = newAV();
722 39 HV *seen = (HV *)sv_2mortal((SV *)newHV());
723 SV *secs;
724 SSize_t i, n;
725  
726 39 n = av_len(paths) + 1;
727 60082 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 60043 39 60082
728 60043 SV **e = av_fetch(paths, i, 0);
729 AV *pair;
730 STRLEN pl;
731 const char *p;
732 /*
733 # uncoverable branch true
734 # uncoverable condition left count:1 class:ignore_covered_err
735 # uncoverable condition left count:2 class:ignore_covered_err */
736 60043 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 60043 0 60043
if (!(e && *e)) continue; 0 60043 60043
737 60043 p = SvPV_const(*e, pl);
738 /*
739 # uncoverable branch true
740 # uncoverable condition left count:1 class:ignore_covered_err */
741 60043 ▶ if (hv_exists(seen, p, (I32)pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, p, (I32)pl)) continue; 0 60043 60043
742 60043 (void)hv_store(seen, p, (I32)pl, newSViv(1), 0);
743 60043 pair = newAV();
744 60043 av_push(pair, newSVsv(*e));
745 60043 av_push(pair, newSV(0));
746 60043 av_push(out, newRV_noinc((SV *)pair));
747 }
748  
749 {
750 39 AV *md = (AV *)sv_2mortal((SV *)pks_md_entries(aTHX_ appsv));
751 39 n = av_len(md) + 1;
752 57 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 39 57
753 18 SV **g = av_fetch(md, i, 0);
754 STRLEN pl;
755 const char *p;
756 /*
757 # uncoverable branch true
758 # uncoverable condition left count:1 class:ignore_covered_err
759 # uncoverable condition left count:2 class:ignore_covered_err
760 # uncoverable condition left count:3 class:ignore_covered_err */
761 18 ▶ if (!(g && *g && SvROK(*g))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(g && *g && SvROK(*g))) continue; 18 0 18
if (!(g && *g && SvROK(*g))) continue; 18 0 18
if (!(g && *g && SvROK(*g))) continue; 0 18 18
762 18 p = SvPV_const(*av_fetch((AV *)SvRV(*g), 0, 0), pl);
763 /*
764 # uncoverable branch true
765 # uncoverable condition left count:1 class:ignore_covered_err */
766 18 ▶ if (hv_exists(seen, p, (I32)pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, p, (I32)pl)) continue; 0 18 18
767 18 (void)hv_store(seen, p, (I32)pl, newSViv(1), 0);
768 18 av_push(out, newSVsv(*g));
769 }
770 }
771  
772 39 ▶ secs = app ? app_get(aTHX_ app, "sitemap_sections") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
secs = app ? app_get(aTHX_ app, "sitemap_sections") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 39 0 39
773 /*
774 # uncoverable condition right count:2 class:ignore_covered_err
775 # uncoverable condition right count:3 class:ignore_covered_err */
776 39 ▶ if (secs && SvROK(secs) && SvTYPE(SvRV(secs)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (secs && SvROK(secs) && SvTYPE(SvRV(secs)) == SVt_PVAV) { 11 28 39
if (secs && SvROK(secs) && SvTYPE(SvRV(secs)) == SVt_PVAV) { 11 0 11
if (secs && SvROK(secs) && SvTYPE(SvRV(secs)) == SVt_PVAV) { 11 0 11
777 11 AV *list = (AV *)SvRV(secs);
778 11 SSize_t si, sn = av_len(list) + 1;
779 23 ▶ for (si = 0; si < sn; si++) {
Branch: 1 branch
BranchTrueFalseTotal
for (si = 0; si < sn; si++) { 12 11 23
780 12 SV **se = av_fetch(list, si, 0);
781 AV *rec;
782 AV *got;
783 SSize_t gi, gn;
784 /*
785 # uncoverable branch true
786 # uncoverable condition left count:1 class:ignore_covered_err
787 # uncoverable condition left count:2 class:ignore_covered_err
788 # uncoverable condition left count:3 class:ignore_covered_err
789 # uncoverable condition left count:4 class:ignore_covered_err */
790 12 ▶ if (!(se && *se && SvROK(*se) && SvTYPE(SvRV(*se)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(se && *se && SvROK(*se) && SvTYPE(SvRV(*se)) == SVt_PVAV)) 12 0 12
if (!(se && *se && SvROK(*se) && SvTYPE(SvRV(*se)) == SVt_PVAV)) 12 0 12
if (!(se && *se && SvROK(*se) && SvTYPE(SvRV(*se)) == SVt_PVAV)) 12 0 12
if (!(se && *se && SvROK(*se) && SvTYPE(SvRV(*se)) == SVt_PVAV)) 0 12 12
791 0 continue; /* # uncoverable statement */
792 12 rec = (AV *)SvRV(*se);
793 12 got = (AV *)sv_2mortal((SV *)newAV());
794 12 pks_run_section(aTHX_ *av_fetch(rec, 1, 0),
795 12 *av_fetch(rec, 0, 0), got);
796 12 ▶ if (av_len(got) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(got) > 0) 6 6 12
797 6 sortsv(AvARRAY(got), (size_t)(av_len(got) + 1), pks_cmp_pair);
798 12 gn = av_len(got) + 1;
799 50028 ▶ for (gi = 0; gi < gn; gi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (gi = 0; gi < gn; gi++) { 50016 12 50028
800 50016 SV **g = av_fetch(got, gi, 0);
801 AV *pair;
802 STRLEN pl;
803 const char *p;
804 /*
805 # uncoverable branch true class:ignore_covered_err
806 # uncoverable condition left count:1 class:ignore_covered_err
807 # uncoverable condition left count:2 class:ignore_covered_err
808 # uncoverable condition left count:3 class:ignore_covered_err */
809 50017 ▶ if (!(g && *g && SvROK(*g))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(g && *g && SvROK(*g))) continue; 50016 0 50016
if (!(g && *g && SvROK(*g))) continue; 50016 0 50016
if (!(g && *g && SvROK(*g))) continue; 0 50016 50016
810 50016 pair = (AV *)SvRV(*g);
811 50016 p = SvPV_const(*av_fetch(pair, 0, 0), pl);
812 50016 ▶ if (hv_exists(seen, p, (I32)pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, p, (I32)pl)) continue; 1 50015 50016
813 50015 (void)hv_store(seen, p, (I32)pl, newSViv(1), 0);
814 50015 av_push(out, newSVsv(*g));
815 }
816 }
817 }
818 39 return out;
819 }
820  
821 #define PKS_HEAD "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
822 "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
823 #define PKS_TAIL "</urlset>\n"
824 #define PKS_IHEAD "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
825 "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
826 #define PKS_ITAIL "</sitemapindex>\n"
827  
828 /* Render every candidate path into one or more <urlset> documents (+1 each).
829 *
830 * No <lastmod> on this half: "when did this static page last change" is not
831 * something the route table knows, and stamping boot time would tell crawlers
832 * every page changed on every deploy - the fastest way to have the file
833 * ignored. The dynamic half supplies its own, where the application does
834 * know. */
835 41 static AV *pks_render_parts(pTHX_ AV *ents, SV *base) {
836 41 AV *parts = newAV();
837 41 SSize_t i, n = av_len(ents) + 1;
838 41 SV *cur = NULL;
839 41 IV urls = 0;
840  
841 110125 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 110084 41 110125
842 110084 SV **e = av_fetch(ents, i, 0);
843 AV *pair;
844 SV *lm;
845 STRLEN pl;
846 const char *p;
847 /*
848 # uncoverable branch true class:ignore_covered_err
849 # uncoverable condition left count:1 class:ignore_covered_err
850 # uncoverable condition left count:2 class:ignore_covered_err
851 # uncoverable condition left count:3 class:ignore_covered_err */
852 220126 ▶ if (!(e && *e && SvROK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e))) continue; 110084 0 110084
if (!(e && *e && SvROK(*e))) continue; 110084 0 110084
if (!(e && *e && SvROK(*e))) continue; 0 110084 110084
853 110084 pair = (AV *)SvRV(*e);
854 110084 p = SvPV_const(*av_fetch(pair, 0, 0), pl);
855 110084 lm = *av_fetch(pair, 1, 0);
856  
857 110084 ▶ if (cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur) { 110044 40 110084
858 /* Would this one take the part past either cap? The URL is
859 * rendered into a scratch buffer so the byte cost is known
860 * rather than guessed. */
861 110044 SV *probe = sv_2mortal(newSVpvn("", 0));
862 110044 pks_url_cat_lm(aTHX_ probe, base, p, pl, lm);
863 110044 ▶ if (urls + 1 > PKS_MAX_URLS /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (urls + 1 > PKS_MAX_URLS /* # uncoverable condition left count:2 class:ignore_covered_err */ 110042 2 110044
864 110042 ▶ || SvCUR(cur) + SvCUR(probe) + (sizeof(PKS_TAIL) - 1)
Branch: 1 branch
BranchTrueFalseTotal
|| SvCUR(cur) + SvCUR(probe) + (sizeof(PKS_TAIL) - 1) 0 110042 110042
865 > PKS_MAX_BYTES) {
866 2 sv_catpvs(cur, PKS_TAIL);
867 2 av_push(parts, cur);
868 2 cur = NULL;
869 2 urls = 0;
870 }
871 else {
872 110042 sv_catsv(cur, probe);
873 110042 urls++;
874 110042 continue;
875 }
876 }
877 /*
878 # uncoverable branch false
879 # uncoverable condition right count:1 class:ignore_covered_err */
880 42 ▶ if (!cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cur) { 42 0 42
881 42 cur = newSVpvn(PKS_HEAD, sizeof(PKS_HEAD) - 1);
882 42 urls = 0;
883 }
884 42 pks_url_cat_lm(aTHX_ cur, base, p, pl, lm);
885 42 urls++;
886 }
887  
888 41 ▶ if (cur) { sv_catpvs(cur, PKS_TAIL); av_push(parts, cur); }
Branch: 1 branch
BranchTrueFalseTotal
if (cur) { sv_catpvs(cur, PKS_TAIL); av_push(parts, cur); } 40 1 41
889 /* An application with no listable route still serves a valid, empty
890 * document rather than a 404 - a crawler asking gets an answer, and an
891 * empty urlset is a legitimate one. */
892 41 ▶ if (av_len(parts) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(parts) < 0) { 1 40 41
893 1 SV *empty = newSVpvn(PKS_HEAD, sizeof(PKS_HEAD) - 1);
894 1 sv_catpvs(empty, PKS_TAIL);
895 1 av_push(parts, empty);
896 }
897 41 return parts;
898 }
899  
900 /* The index over the parts (+1), when there is more than one.
901 *
902 * With one part there is no index and /sitemap.xml is that part: an index
903 * wrapping a single sitemap is a second fetch for nothing. */
904 2 static SV *pks_render_index(pTHX_ SV *base, SSize_t nparts) {
905 2 SV *out = newSVpvn(PKS_IHEAD, sizeof(PKS_IHEAD) - 1);
906 STRLEN bl;
907 2 const char *bp = SvPV_const(base, bl);
908 SSize_t i;
909 /*
910 # uncoverable branch true class:ignore_covered_err
911 # uncoverable condition left count:1 class:ignore_covered_err
912 # uncoverable condition right count:1 class:ignore_covered_err
913 # uncoverable condition left count:2 class:ignore_covered_err */
914 2 ▶ while (bl && bp[bl - 1] == '/') bl--;
Branch: 2 branches
BranchTrueFalseTotal
while (bl && bp[bl - 1] == '/') bl--; 2 0 2
while (bl && bp[bl - 1] == '/') bl--; 0 2 2
915 6 ▶ for (i = 0; i < nparts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nparts; i++) { 4 2 6
916 4 SV *loc = sv_2mortal(newSVpvn(bp, bl));
917 4 sv_catpvf(loc, "/sitemap/%" IVdf ".xml", (IV)(i + 1));
918 4 sv_catpvs(out, " <sitemap><loc>");
919 {
920 STRLEN ll;
921 4 const char *lp = SvPV_const(loc, ll);
922 4 pks_xml_cat(aTHX_ out, lp, ll);
923 }
924 4 sv_catpvs(out, "</loc></sitemap>\n");
925 }
926 2 sv_catpvs(out, PKS_ITAIL);
927 2 return out;
928 }
929  
930 /* Render everything and stash it on the app: "sitemap_parts" is the AV of
931 * <urlset> documents, "sitemap_index" the index when there is more than one.
932 *
933 * Once, at to_app. The route half changes only when the routes do, which is
934 * never while the process runs, so a request for the document is a write of
935 * bytes that already exist. */
936 39 static void pks_build(pTHX_ SV *appsv) {
937 39 HV *h = app_hv(aTHX_ appsv);
938 39 ▶ SV *base = h ? app_get(aTHX_ h, "sitemap_base") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *base = h ? app_get(aTHX_ h, "sitemap_base") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 39 0 39
939 AV *ents, *parts;
940 /*
941 # uncoverable branch true
942 # uncoverable condition left count:1 class:ignore_covered_err
943 # uncoverable condition left count:2 class:ignore_covered_err
944 # uncoverable condition left count:3 class:ignore_covered_err */
945 39 ▶ if (!(h && base && SvOK(base))) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(h && base && SvOK(base))) return; 39 0 39
if (!(h && base && SvOK(base))) return; 39 0 39
if (!(h && base && SvOK(base))) return; 0 39 39
946 /* The entries are kept as well as the rendered parts: a request from an
947 * allowlisted tenant host renders a document naming itself from them
948 * (pks_doc_for), so the sections run once per TTL however many hosts
949 * ask. */
950 39 ents = pks_entries(aTHX_ appsv);
951 39 (void)hv_stores(h, "sitemap_entries", newRV_noinc((SV *)ents));
952 39 parts = pks_render_parts(aTHX_ ents, base);
953 39 (void)hv_stores(h, "sitemap_parts", newRV_noinc((SV *)parts));
954 39 ▶ (void)hv_stores(h, "sitemap_index",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "sitemap_index", 2 37 39
955 av_len(parts) > 0
956 ? pks_render_index(aTHX_ base, av_len(parts) + 1)
957 : newSV(0));
958 39 (void)hv_stores(h, "sitemap_built_at", newSVnv(pc_now(aTHX)));
959 }
960  
961 /* Does the document need rebuilding before it is served?
962 *
963 * The route half never does - routes cannot change while the process runs -
964 * so an application with no sections is built at to_app, served from memory
965 * and never rebuilt.
966 *
967 * A section reads a database, so its answer changes underneath us. Running it
968 * per request would be a query nobody is watching, on a schedule somebody
969 * else chooses; running it once at boot would produce a sitemap that is
970 * correct on the day of the deploy and progressively wrong afterwards. So it
971 * is rebuilt when the TTL has passed, and the document is stale by up to that
972 * long.
973 *
974 * That staleness is fine and is worth stating rather than engineering away: a
975 * crawler learning about a page an hour late is a crawler behaving normally,
976 * and no search engine promises to fetch a sitemap promptly anyway.
977 *
978 * There is no stampede to guard against inside a worker. Hyperman is
979 * single-threaded and a worker serves its requests one after another, so the
980 * second of two simultaneous crawler requests finds what the first built. The
981 * cost across a pool is one rebuild per worker per TTL. */
982 36 static void pks_ensure(pTHX_ SV *appsv) {
983 36 HV *h = app_hv(aTHX_ appsv);
984 SV *secs, *at, *ttl;
985 /*
986 # uncoverable branch true
987 # uncoverable condition left count:1 class:ignore_covered_err */
988 36 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 36 36
989 36 secs = app_get(aTHX_ h, "sitemap_sections");
990 /*
991 # uncoverable condition left count:2 class:ignore_covered_err
992 # uncoverable condition left count:3 class:ignore_covered_err */
993 36 ▶ if (!(secs && SvROK(secs) && av_len((AV *)SvRV(secs)) >= 0)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(secs && SvROK(secs) && av_len((AV *)SvRV(secs)) >= 0)) return; 18 18 36
if (!(secs && SvROK(secs) && av_len((AV *)SvRV(secs)) >= 0)) return; 18 0 18
if (!(secs && SvROK(secs) && av_len((AV *)SvRV(secs)) >= 0)) return; 0 18 18
994  
995 18 at = app_get(aTHX_ h, "sitemap_built_at");
996 18 ttl = app_get(aTHX_ h, "sitemap_ttl");
997 /*
998 # uncoverable condition right count:1 class:ignore_covered_err
999 # uncoverable condition right count:2 class:ignore_covered_err
1000 # uncoverable condition right count:3 class:ignore_covered_err
1001 # uncoverable condition right count:4 class:ignore_covered_err */
1002 18 ▶ if (at && SvOK(at) && ttl && SvOK(ttl)
Branch: 4 branches
BranchTrueFalseTotal
if (at && SvOK(at) && ttl && SvOK(ttl) 18 0 18
if (at && SvOK(at) && ttl && SvOK(ttl) 18 0 18
if (at && SvOK(at) && ttl && SvOK(ttl) 18 0 18
if (at && SvOK(at) && ttl && SvOK(ttl) 18 0 18
1003 18 ▶ && pc_now(aTHX) - SvNV(at) < SvNV(ttl)) return;
Branch: 1 branch
BranchTrueFalseTotal
&& pc_now(aTHX) - SvNV(at) < SvNV(ttl)) return; 17 1 18
1004 1 pks_build(aTHX_ appsv);
1005 }
1006  
1007 /* The document for another origin - a tenant host on the allowlist -
1008 * rendered from the entries the last build kept. Costs the XML emission and
1009 * no section run, and is NOT cached: a wildcard allow makes the set of hosts
1010 * unbounded, and a cache keyed by hostnames a client chooses is a memory
1011 * leak with a name. Mortal, or NULL when nothing has been built. */
1012 2 static SV *pks_doc_for(pTHX_ HV *h, SV *base, int is_part, IV n) {
1013 2 SV *es = app_get(aTHX_ h, "sitemap_entries");
1014 AV *parts;
1015 2 SV *doc = NULL;
1016 /*
1017 # uncoverable branch true
1018 # uncoverable condition left count:1 class:ignore_covered_err
1019 # uncoverable condition left count:2 class:ignore_covered_err
1020 # uncoverable condition left count:3 class:ignore_covered_err */
1021 2 ▶ if (!(es && SvROK(es) && SvTYPE(SvRV(es)) == SVt_PVAV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(es && SvROK(es) && SvTYPE(SvRV(es)) == SVt_PVAV)) return NULL; 2 0 2
if (!(es && SvROK(es) && SvTYPE(SvRV(es)) == SVt_PVAV)) return NULL; 2 0 2
if (!(es && SvROK(es) && SvTYPE(SvRV(es)) == SVt_PVAV)) return NULL; 0 2 2
1022 2 parts = (AV *)sv_2mortal((SV *)pks_render_parts(aTHX_ (AV *)SvRV(es),
1023 base));
1024 /*
1025 # uncoverable branch false
1026 # uncoverable condition right count:1 class:ignore_covered_err */
1027 2 ▶ if (!is_part) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_part) { 2 0 2
1028 /*
1029 # uncoverable branch true
1030 # uncoverable condition left count:1 class:ignore_covered_err */
1031 2 ▶ if (av_len(parts) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(parts) > 0) 0 2 2
1032 0 return sv_2mortal(pks_render_index(aTHX_ base, av_len(parts) + 1)); /* # uncoverable statement */
1033 2 n = 1;
1034 }
1035 /*
1036 # uncoverable branch false
1037 # uncoverable condition right count:1 class:ignore_covered_err */
1038 2 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 2 0 2
1039 2 SV **e = av_fetch(parts, (SSize_t)(n - 1), 0);
1040 /*
1041 # uncoverable branch false
1042 # uncoverable condition right count:1 class:ignore_covered_err
1043 # uncoverable condition right count:2 class:ignore_covered_err */
1044 2 ▶ if (e && *e) doc = sv_2mortal(newSVsv(*e));
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) doc = sv_2mortal(newSVsv(*e)); 2 0 2
if (e && *e) doc = sv_2mortal(newSVsv(*e)); 2 0 2
1045 }
1046 2 return doc;
1047 }
1048  
1049 /* Serve a rendered document. cap = [app, is_part].
1050 *
1051 * The bytes already exist - the whole document was built at to_app - so a
1052 * request is a write of something in memory and not a render. */
1053 XS_INTERNAL(pks_serve_cb);
1054 7 XS_INTERNAL(pks_serve_cb) {
1055 7 dXSARGS;
1056 7 AV *cap = punk_clos_cap(aTHX_ cv);
1057 7 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
1058 7 ▶ int is_part = cap ? (int)SvIV(*av_fetch(cap, 1, 0)) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
int is_part = cap ? (int)SvIV(*av_fetch(cap, 1, 0)) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
1059 7 ▶ SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
1060 7 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
1061 7 IV n = 0;
1062 7 SV *doc = NULL;
1063 AV *resp, *headers, *body;
1064  
1065 /*
1066 # uncoverable branch true
1067 # uncoverable condition left count:1 class:ignore_covered_err */
1068 7 ▶ if (!h) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) XSRETURN_EMPTY; 0 7 7
1069 7 pks_ensure(aTHX_ app); /* a section may have gone stale since the last hit */
1070  
1071 7 ▶ if (is_part && c) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (is_part && c) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 5 7
if (is_part && c) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
1072 /* Punk captures whole SEGMENTS, so the pattern is /sitemap/:n and the
1073 * capture arrives as "1.xml" rather than "1". Parsed rather than
1074 * handed to SvIV, which would take "9evil.xml" as 9 and serve a part
1075 * for a URL nobody published. */
1076 2 SV *arg = sv_2mortal(newSVpvs("n"));
1077 2 SV *v = pcx_call_meth(aTHX_ c, "param", &arg, 1, 1);
1078 /*
1079 # uncoverable branch false
1080 # uncoverable condition right count:1 class:ignore_covered_err */
1081 2 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 2 0 2
1082 /*
1083 # uncoverable branch false
1084 # uncoverable condition right count:1 class:ignore_covered_err */
1085 2 ▶ if (SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) { 2 0 2
1086 STRLEN vl;
1087 2 const char *vp = SvPV_const(v, vl);
1088 2 STRLEN d = 0;
1089 4 ▶ while (d < vl && isDIGIT(vp[d])) d++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (d < vl && isDIGIT(vp[d])) d++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
while (d < vl && isDIGIT(vp[d])) d++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 2 4
1090 /* at most nine digits: a longer number overflows the
1091 * arithmetic below, and no sitemap has a billion parts */
1092 /*
1093 # uncoverable branch false
1094 # uncoverable condition right count:1 class:ignore_covered_err
1095 # uncoverable condition right count:2 class:ignore_covered_err
1096 # uncoverable condition right count:3 class:ignore_covered_err
1097 # uncoverable condition right count:4 class:ignore_covered_err
1098 # uncoverable condition right count:5 class:ignore_covered_err */
1099 2 ▶ if (d > 0 && d <= 9 && d < vl && vl - d == 4
Branch: 4 branches
BranchTrueFalseTotal
if (d > 0 && d <= 9 && d < vl && vl - d == 4 2 0 2
if (d > 0 && d <= 9 && d < vl && vl - d == 4 2 0 2
if (d > 0 && d <= 9 && d < vl && vl - d == 4 2 0 2
if (d > 0 && d <= 9 && d < vl && vl - d == 4 2 0 2
1100 2 ▶ && memEQ(vp + d, ".xml", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(vp + d, ".xml", 4)) { 2 0 2
1101 STRLEN k;
1102 4 ▶ for (k = 0; k < d; k++) n = n * 10 + (vp[k] - '0');
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < d; k++) n = n * 10 + (vp[k] - '0'); 2 2 4
1103 }
1104 }
1105 2 SvREFCNT_dec(v);
1106 }
1107 }
1108  
1109 { /* A tenant host on the allowlist gets a document naming itself;
1110 * the canonical host, an unknown one and a malformed one all get
1111 * the frozen canonical document - never one naming the header. */
1112 7 int st = 0;
1113 7 ▶ SV *origin = c ? pk_origin_of(aTHX_ c, &st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *origin = c ? pk_origin_of(aTHX_ c, &st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
1114 7 ▶ if (origin) sv_2mortal(origin);
Branch: 1 branch
BranchTrueFalseTotal
if (origin) sv_2mortal(origin); 4 3 7
1115 7 ▶ if (st == 2) doc = pks_doc_for(aTHX_ h, origin, is_part, n);
Branch: 1 branch
BranchTrueFalseTotal
if (st == 2) doc = pks_doc_for(aTHX_ h, origin, is_part, n); 2 5 7
1116 else {
1117 5 ▶ if (!is_part) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_part) { 3 2 5
1118 3 doc = app_get(aTHX_ h, "sitemap_index");
1119 /*
1120 # uncoverable branch false
1121 # uncoverable condition left count:1 class:ignore_covered_err
1122 # uncoverable condition right count:1 class:ignore_covered_err
1123 # uncoverable condition right count:2 class:ignore_covered_err */
1124 3 ▶ if (!(doc && SvOK(doc))) n = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!(doc && SvOK(doc))) n = 1; 3 0 3
if (!(doc && SvOK(doc))) n = 1; 3 0 3
1125 }
1126 /*
1127 # uncoverable branch false
1128 # uncoverable condition right count:1 class:ignore_covered_err */
1129 5 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 5 0 5
1130 5 SV *ps = app_get(aTHX_ h, "sitemap_parts");
1131 5 ▶ AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 5 0 5
AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 5 0 5
1132 10 ▶ ? (AV *)SvRV(ps) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(ps) : NULL; 5 0 5
1133 5 ▶ SV **e = (parts && n >= 1)
Branch: 1 branch
BranchTrueFalseTotal
SV **e = (parts && n >= 1) 5 0 5
1134 10 ▶ ? av_fetch(parts, (SSize_t)(n - 1), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? av_fetch(parts, (SSize_t)(n - 1), 0) : NULL; 5 0 5
1135 5 ▶ doc = (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
doc = (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 1 5
doc = (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
1136 }
1137 }
1138 }
1139  
1140 /* A part number nobody generated is a 404 and not an empty document: a
1141 * crawler following a stale index should be told the part is gone. */
1142 7 ▶ if (!(doc && SvOK(doc))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(doc && SvOK(doc))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 6 1 7
if (!(doc && SvOK(doc))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 6 6
1143 1 SV *r = pcx_call_meth(aTHX_ c, "not_found", NULL, 0, 1);
1144 /*
1145 # uncoverable branch false
1146 # uncoverable condition right count:1 class:ignore_covered_err */
1147 1 ▶ if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); } 1 0 1
1148 0 XSRETURN_EMPTY; /* # uncoverable statement */
1149 }
1150  
1151 6 resp = newAV(); headers = newAV(); body = newAV();
1152 6 av_push(headers, newSVpvs("Content-Type"));
1153 6 av_push(headers, newSVpvs("application/xml; charset=utf-8"));
1154 6 av_push(headers, newSVpvs("Content-Length"));
1155 6 av_push(headers, newSViv((IV)SvCUR(doc)));
1156 6 av_push(body, newSVsv(doc));
1157 6 av_push(resp, newSViv(200));
1158 6 av_push(resp, newRV_noinc((SV *)headers));
1159 6 av_push(resp, newRV_noinc((SV *)body));
1160 6 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
1161 6 XSRETURN(1);
1162 }
1163  
1164 static SV *pks_robots(pTHX_ SV *appsv);
1165 static SV *pks_robots_for(pTHX_ SV *appsv, SV *base);
1166  
1167 /* Serve robots.txt. Rebuilt on demand like the sitemap, because a dynamic
1168 * section cannot change it but a rebuilt document should not disagree with a
1169 * stale robots either. */
1170 XS_INTERNAL(pks_robots_cb);
1171 3 XS_INTERNAL(pks_robots_cb) {
1172 3 dXSARGS;
1173 3 AV *cap = punk_clos_cap(aTHX_ cv);
1174 3 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
1175 3 ▶ SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
1176 SV *body;
1177 AV *resp, *headers, *bodyav;
1178  
1179 /*
1180 # uncoverable branch true
1181 # uncoverable condition left count:1 class:ignore_covered_err */
1182 3 ▶ if (!app) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!app) XSRETURN_EMPTY; 0 3 3
1183 { /* per tenant, like the sitemap it advertises */
1184 3 int st = 0;
1185 3 ▶ SV *origin = c ? pk_origin_of(aTHX_ c, &st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *origin = c ? pk_origin_of(aTHX_ c, &st) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
1186 3 ▶ if (origin) sv_2mortal(origin);
Branch: 1 branch
BranchTrueFalseTotal
if (origin) sv_2mortal(origin); 2 1 3
1187 3 ▶ body = sv_2mortal(st == 2 ? pks_robots_for(aTHX_ app, origin)
Branch: 1 branch
BranchTrueFalseTotal
body = sv_2mortal(st == 2 ? pks_robots_for(aTHX_ app, origin) 1 2 3
1188 : pks_robots(aTHX_ app));
1189 }
1190  
1191 3 resp = newAV(); headers = newAV(); bodyav = newAV();
1192 3 av_push(headers, newSVpvs("Content-Type"));
1193 3 av_push(headers, newSVpvs("text/plain; charset=utf-8"));
1194 3 av_push(headers, newSVpvs("Content-Length"));
1195 3 av_push(headers, newSViv((IV)SvCUR(body)));
1196 3 av_push(bodyav, newSVsv(body));
1197 3 av_push(resp, newSViv(200));
1198 3 av_push(resp, newRV_noinc((SV *)headers));
1199 3 av_push(resp, newRV_noinc((SV *)bodyav));
1200 3 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
1201 3 XSRETURN(1);
1202 }
1203  
1204 /* The `sitemap` keyword. cap = [app].
1205 *
1206 * sitemap 'users' => sub { ... };
1207 *
1208 * NAMED, so a second declaration of the same name REPLACES rather than
1209 * appends: a base class can declare a section and a subclass override it, and
1210 * an application reloading its own definition does not end up with the
1211 * section twice. Declaration order is the emission order, and a replaced
1212 * section keeps its original position so the document does not reshuffle. */
1213 XS_INTERNAL(pks_kw_cb);
1214 14 XS_INTERNAL(pks_kw_cb) {
1215 14 dXSARGS;
1216 14 AV *cap = punk_clos_cap(aTHX_ cv);
1217 14 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
1218 14 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
1219 14 ▶ SV *name = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *name = items > 0 ? ST(0) : NULL; 13 1 14
1220 14 ▶ SV *code = items > 1 ? ST(1) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *code = items > 1 ? ST(1) : NULL; 13 1 14
1221 AV *list;
1222 SSize_t i, n;
1223  
1224 /*
1225 # uncoverable branch true
1226 # uncoverable condition left count:1 class:ignore_covered_err */
1227 14 ▶ if (!h) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) XSRETURN_EMPTY; 0 14 14
1228 /*
1229 # uncoverable condition left count:2 class:ignore_covered_err
1230 # uncoverable condition left count:3 class:ignore_covered_err */
1231 14 ▶ if (!(name && SvOK(name) && SvCUR(name)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(name && SvOK(name) && SvCUR(name))) 13 1 14
if (!(name && SvOK(name) && SvCUR(name))) 13 0 13
if (!(name && SvOK(name) && SvCUR(name))) 0 13 13
1232 1 croak("Punk: sitemap needs a name - sitemap 'users' => sub { ... }");
1233 /*
1234 # uncoverable condition left count:1 class:ignore_covered_err
1235 # uncoverable condition left count:3 class:ignore_covered_err */
1236 13 ▶ if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 13 0 13
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 12 1 13
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 0 12 12
1237 1 croak("Punk: sitemap '%" SVf "' needs a code reference that returns "
1238 "locations", SVfARG(name));
1239  
1240 12 list = app_av(aTHX_ h, "sitemap_sections");
1241 12 n = av_len(list) + 1;
1242 13 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 11 13
1243 2 SV **e = av_fetch(list, i, 0);
1244 AV *rec;
1245 /*
1246 # uncoverable branch true
1247 # uncoverable condition left count:1 class:ignore_covered_err
1248 # uncoverable condition left count:2 class:ignore_covered_err
1249 # uncoverable condition left count:3 class:ignore_covered_err */
1250 2 ▶ if (!(e && *e && SvROK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e))) continue; 2 0 2
if (!(e && *e && SvROK(*e))) continue; 2 0 2
if (!(e && *e && SvROK(*e))) continue; 0 2 2
1251 2 rec = (AV *)SvRV(*e);
1252 2 ▶ if (sv_eq(*av_fetch(rec, 0, 0), name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(*av_fetch(rec, 0, 0), name)) { 1 1 2
1253 1 av_store(rec, 1, newSVsv(code)); /* replace, keep position */
1254 1 XSRETURN_EMPTY;
1255 }
1256 }
1257 {
1258 11 AV *rec = newAV();
1259 11 av_push(rec, newSVsv(name));
1260 11 av_push(rec, newSVsv(code));
1261 11 av_push(list, newRV_noinc((SV *)rec));
1262 }
1263 11 XSRETURN_EMPTY;
1264 }
1265  
1266 /* ---- robots.txt -------------------------------------------------------------
1267 *
1268 * The same decision, made once, spelled twice. A sitemap says "crawl these"
1269 * and robots.txt says "do not crawl those"; written apart they drift within a
1270 * release, and the drift is silent in both directions - a path disallowed but
1271 * listed is a crawler told two things, and a path excluded but not disallowed
1272 * is crawled anyway, which is how a staging environment gets indexed.
1273 *
1274 * So both come out of one pass over the same records.
1275 *
1276 * ROBOTS.TXT IS NOT ACCESS CONTROL. It is a request to well-behaved crawlers,
1277 * ignored by everything else, and a Disallow line is a signpost to anything
1278 * hostile - a published list of the paths worth attacking. The guarding is
1279 * auth_guard's job; this only tells Google not to bother.
1280 */
1281  
1282 /* Is this one of the routes the plugin registered for itself? */
1283 32 static int pks_is_own(pTHX_ HV *app, const char *p, STRLEN pl) {
1284 32 SV *own = app_get(aTHX_ app, "sitemap_own");
1285 AV *av;
1286 SSize_t i, n;
1287 /*
1288 # uncoverable branch true
1289 # uncoverable condition left count:1 class:ignore_covered_err
1290 # uncoverable condition left count:2 class:ignore_covered_err
1291 # uncoverable condition left count:3 class:ignore_covered_err */
1292 32 ▶ if (!(own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV)) return 0; 32 0 32
if (!(own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV)) return 0; 32 0 32
if (!(own && SvROK(own) && SvTYPE(SvRV(own)) == SVt_PVAV)) return 0; 0 32 32
1293 32 av = (AV *)SvRV(own);
1294 32 n = av_len(av) + 1;
1295 84 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 74 10 84
1296 74 SV **e = av_fetch(av, i, 0);
1297 STRLEN ol;
1298 const char *op;
1299 /*
1300 # uncoverable branch true
1301 # uncoverable condition left count:1 class:ignore_covered_err
1302 # uncoverable condition left count:2 class:ignore_covered_err */
1303 74 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 74 0 74
if (!(e && *e)) continue; 0 74 74
1304 74 op = SvPV_const(*e, ol);
1305 74 ▶ if (ol == pl && memEQ(op, p, pl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ol == pl && memEQ(op, p, pl)) return 1; 35 39 74
if (ol == pl && memEQ(op, p, pl)) return 1; 22 13 35
1306 }
1307 10 return 0;
1308 }
1309  
1310 /* How much of a path to disallow.
1311 *
1312 * A capture makes the rest of the path meaningless as a literal - nobody
1313 * benefits from `Disallow: /account/orders/:id` - so the path is truncated at
1314 * the first capture segment and the prefix stands for everything under it,
1315 * which is exactly the set the route matches. `/account/orders/:id` becomes
1316 * `/account/orders/`, and `/account/settings` stays as it is. */
1317 10 static SV *pks_disallow_form(pTHX_ const char *p, STRLEN pl) {
1318 10 STRLEN i, cut = pl;
1319 118 ▶ for (i = 0; i < pl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pl; i++) { 110 8 118
1320 110 ▶ if (p[i] == ':' || p[i] == '*') { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == ':' || p[i] == '*') { /* # uncoverable condition left count:2 class:ignore_covered_err */ 108 2 110
if (p[i] == ':' || p[i] == '*') { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 108 108
1321 /* back up to the segment boundary, keeping the slash */
1322 2 cut = i;
1323 /*
1324 # uncoverable branch true class:ignore_covered_err
1325 # uncoverable condition left count:1 class:ignore_covered_err
1326 # uncoverable condition right count:1 class:ignore_covered_err
1327 # uncoverable condition left count:2 class:ignore_covered_err */
1328 2 ▶ while (cut > 1 && p[cut - 1] != '/') cut--;
Branch: 2 branches
BranchTrueFalseTotal
while (cut > 1 && p[cut - 1] != '/') cut--; 2 0 2
while (cut > 1 && p[cut - 1] != '/') cut--; 0 2 2
1329 2 break;
1330 }
1331 }
1332 10 return newSVpvn(p, cut);
1333 }
1334  
1335 /* Does `pfx` cover `path`? A robots prefix match: everything starting with
1336 * it. */
1337 38 static int pks_covers(const char *pfx, STRLEN fl, const char *path, STRLEN pl) {
1338 38 ▶ if (fl > pl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fl > pl) return 0; 30 8 38
1339 8 return memEQ(pfx, path, fl);
1340 }
1341  
1342 /* Every prefix this application asks crawlers to leave alone (+1).
1343 *
1344 * Guarded routes and routes that said `sitemap => 0`, plus whatever the
1345 * `disallow` option added - then filtered against what the SITEMAP lists, so
1346 * a Disallow can never shadow a URL the same file is advertising. That filter
1347 * is the whole "they cannot disagree" property, and it is cheaper to enforce
1348 * than to test for afterwards. */
1349 11 static AV *pks_disallows(pTHX_ SV *appsv) {
1350 11 HV *app = app_hv(aTHX_ appsv);
1351 11 AV *listed = (AV *)sv_2mortal((SV *)pks_paths(aTHX_ appsv));
1352 11 ▶ AV *raw = app ? pks_raw(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
AV *raw = app ? pks_raw(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
1353 { /* a markdown page the sitemap lists is listed, as a route is */
1354 11 AV *md = (AV *)sv_2mortal((SV *)pks_md_entries(aTHX_ appsv));
1355 11 SSize_t k, kn = av_len(md) + 1;
1356 21 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 10 11 21
1357 10 SV **g = av_fetch(md, k, 0);
1358 /*
1359 # uncoverable branch false
1360 # uncoverable condition right count:1 class:ignore_covered_err
1361 # uncoverable condition right count:2 class:ignore_covered_err
1362 # uncoverable condition right count:3 class:ignore_covered_err */
1363 10 ▶ if (g && *g && SvROK(*g))
Branch: 3 branches
BranchTrueFalseTotal
if (g && *g && SvROK(*g)) 10 0 10
if (g && *g && SvROK(*g)) 10 0 10
if (g && *g && SvROK(*g)) 10 0 10
1364 10 av_push(listed, newSVsv(*av_fetch((AV *)SvRV(*g), 0, 0)));
1365 }
1366 }
1367 11 AV *cand = (AV *)sv_2mortal((SV *)newAV());
1368 11 AV *out = newAV();
1369 11 HV *seen = (HV *)sv_2mortal((SV *)newHV());
1370 SV *extra;
1371 SSize_t i, n;
1372  
1373 11 ▶ n = raw ? av_len(raw) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
n = raw ? av_len(raw) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
1374 73 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 62 11 73
1375 62 SV **e = av_fetch(raw, i, 0);
1376 HV *rec;
1377 SV **msv, **psv;
1378 STRLEN ml, pl;
1379 const char *m, *p;
1380 int said;
1381  
1382 /*
1383 # uncoverable branch true class:ignore_covered_err
1384 # uncoverable condition left count:1 class:ignore_covered_err
1385 # uncoverable condition left count:2 class:ignore_covered_err
1386 # uncoverable condition left count:3 class:ignore_covered_err
1387 # uncoverable condition left count:4 class:ignore_covered_err */
1388 114 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 62 0 62
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 62 0 62
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 62 0 62
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 0 62 62
1389 62 rec = (HV *)SvRV(*e);
1390 62 msv = hv_fetchs(rec, K_METHOD, 0);
1391 62 psv = hv_fetchs(rec, K_PATH, 0);
1392 /*
1393 # uncoverable branch true
1394 # uncoverable condition left count:1 class:ignore_covered_err
1395 # uncoverable condition left count:2 class:ignore_covered_err
1396 # uncoverable condition left count:3 class:ignore_covered_err
1397 # uncoverable condition left count:4 class:ignore_covered_err */
1398 62 ▶ if (!(msv && *msv && psv && *psv)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(msv && *msv && psv && *psv)) continue; 62 0 62
if (!(msv && *msv && psv && *psv)) continue; 62 0 62
if (!(msv && *msv && psv && *psv)) continue; 62 0 62
if (!(msv && *msv && psv && *psv)) continue; 0 62 62
1399 62 m = SvPV_const(*msv, ml);
1400 62 ▶ p = SvPV_const(*psv, pl);
Branch: 1 branch
BranchTrueFalseTotal
p = SvPV_const(*psv, pl); 62 0 62
1401  
1402 /* Only what a crawler would have fetched. A guarded POST is not
1403 * something to disallow: crawlers do not POST, and the same path may
1404 * carry a public GET that must stay listed. */
1405 /*
1406 # uncoverable branch true
1407 # uncoverable condition left count:1 class:ignore_covered_err */
1408 62 ▶ if (!pks_method_ok(m, ml)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pks_method_ok(m, ml)) continue; 0 62 62
1409 62 said = pks_override(aTHX_ app, m, ml, p, pl);
1410 /*
1411 # uncoverable branch true
1412 # uncoverable condition left count:1 class:ignore_covered_err */
1413 62 ▶ if (said == 1) continue; /* explicitly public */
Branch: 1 branch
BranchTrueFalseTotal
if (said == 1) continue; /* explicitly public */ 0 62 62
1414 62 ▶ if (!(said == 0 || pks_guarded(aTHX_ rec))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(said == 0 || pks_guarded(aTHX_ rec))) continue; 35 27 62
if (!(said == 0 || pks_guarded(aTHX_ rec))) continue; 30 5 35
1415 /* The plugin's OWN routes are kept out of the sitemap - a sitemap
1416 * listing itself is noise - but they are emphatically not "do not
1417 * crawl". Disallowing /sitemap.xml while the Sitemap: line below
1418 * advertises it is precisely the self-contradiction this file exists
1419 * to prevent, and /robots.txt disallowing itself is nonsense. */
1420 32 ▶ if (pks_is_own(aTHX_ app, p, pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pks_is_own(aTHX_ app, p, pl)) continue; 22 10 32
1421 10 av_push(cand, pks_disallow_form(aTHX_ p, pl));
1422 }
1423  
1424 11 ▶ extra = app ? app_get(aTHX_ app, "sitemap_disallow") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
extra = app ? app_get(aTHX_ app, "sitemap_disallow") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
1425 /*
1426 # uncoverable condition right count:2 class:ignore_covered_err
1427 # uncoverable condition right count:3 class:ignore_covered_err */
1428 11 ▶ if (extra && SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (extra && SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVAV) { 3 8 11
if (extra && SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVAV) { 3 0 3
if (extra && SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVAV) { 3 0 3
1429 3 AV *ex = (AV *)SvRV(extra);
1430 3 SSize_t xi, xn = av_len(ex) + 1;
1431 6 ▶ for (xi = 0; xi < xn; xi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (xi = 0; xi < xn; xi++) { 3 3 6
1432 3 SV **e = av_fetch(ex, xi, 0);
1433 /*
1434 # uncoverable branch false
1435 # uncoverable condition right count:1 class:ignore_covered_err
1436 # uncoverable condition right count:2 class:ignore_covered_err
1437 # uncoverable condition right count:3 class:ignore_covered_err */
1438 3 ▶ if (e && *e && SvOK(*e)) av_push(cand, newSVsv(*e));
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) av_push(cand, newSVsv(*e)); 3 0 3
if (e && *e && SvOK(*e)) av_push(cand, newSVsv(*e)); 3 0 3
if (e && *e && SvOK(*e)) av_push(cand, newSVsv(*e)); 3 0 3
1439 }
1440 }
1441  
1442 11 ▶ if (av_len(cand) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(cand) > 0) 3 8 11
1443 3 sortsv(AvARRAY(cand), (size_t)(av_len(cand) + 1), pks_cmp);
1444  
1445 11 n = av_len(cand) + 1;
1446 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 11 24
1447 13 SV **e = av_fetch(cand, i, 0);
1448 STRLEN fl;
1449 const char *f;
1450 SSize_t j, ln;
1451 13 int shadows = 0, covered = 0;
1452  
1453 /*
1454 # uncoverable branch true class:ignore_covered_err
1455 # uncoverable condition left count:1 class:ignore_covered_err
1456 # uncoverable condition left count:2 class:ignore_covered_err */
1457 14 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 13 0 13
if (!(e && *e)) continue; 0 13 13
1458 13 f = SvPV_const(*e, fl);
1459 /*
1460 # uncoverable branch true
1461 # uncoverable condition left count:1 class:ignore_covered_err
1462 # uncoverable condition left count:2 class:ignore_covered_err */
1463 13 ▶ if (!fl || f[0] != '/') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!fl || f[0] != '/') continue; 13 0 13
if (!fl || f[0] != '/') continue; 0 13 13
1464 /*
1465 # uncoverable branch true
1466 # uncoverable condition left count:1 class:ignore_covered_err */
1467 13 ▶ if (hv_exists(seen, f, (I32)fl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, f, (I32)fl)) continue; 0 13 13
1468  
1469 /* Never disallow something the sitemap lists. */
1470 13 ln = av_len(listed) + 1;
1471 37 ▶ for (j = 0; j < ln; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ln; j++) { 25 12 37
1472 25 SV **l = av_fetch(listed, j, 0);
1473 STRLEN pl;
1474 const char *p;
1475 /*
1476 # uncoverable branch true
1477 # uncoverable condition left count:1 class:ignore_covered_err
1478 # uncoverable condition left count:2 class:ignore_covered_err */
1479 25 ▶ if (!(l && *l)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(l && *l)) continue; 25 0 25
if (!(l && *l)) continue; 0 25 25
1480 25 p = SvPV_const(*l, pl);
1481 25 ▶ if (pks_covers(f, fl, p, pl)) { shadows = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (pks_covers(f, fl, p, pl)) { shadows = 1; break; } 1 24 25
1482 }
1483 13 ▶ if (shadows) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (shadows) continue; 1 12 13
1484  
1485 /* and drop what a shorter prefix already covers */
1486 12 ln = av_len(out) + 1;
1487 25 ▶ for (j = 0; j < ln; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ln; j++) { 13 12 25
1488 13 SV **o = av_fetch(out, j, 0);
1489 STRLEN ol;
1490 const char *op;
1491 /*
1492 # uncoverable branch true
1493 # uncoverable condition left count:1 class:ignore_covered_err
1494 # uncoverable condition left count:2 class:ignore_covered_err */
1495 13 ▶ if (!(o && *o)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(o && *o)) continue; 13 0 13
if (!(o && *o)) continue; 0 13 13
1496 13 op = SvPV_const(*o, ol);
1497 /*
1498 # uncoverable branch true
1499 # uncoverable condition left count:1 class:ignore_covered_err */
1500 13 ▶ if (pks_covers(op, ol, f, fl)) { covered = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (pks_covers(op, ol, f, fl)) { covered = 1; break; } 0 13 13
1501 }
1502 /*
1503 # uncoverable branch true
1504 # uncoverable condition left count:1 class:ignore_covered_err */
1505 12 ▶ if (covered) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (covered) continue; 0 12 12
1506  
1507 12 (void)hv_store(seen, f, (I32)fl, newSViv(1), 0);
1508 12 av_push(out, newSVpvn(f, fl));
1509 }
1510 11 return out;
1511 }
1512  
1513 /* The crawlers that never read the `*` group.
1514 *
1515 * AdsBot-Google, AdsBot-Google-Mobile and Mediapartners-Google (AdSense) obey
1516 * only a group naming their own token; Google documents the global user agent
1517 * as ignored for all three. So a Disallow written for `*` alone is not a
1518 * Disallow for them, and nothing says so: the file looks right, the sitemap
1519 * agrees with it, and the path is crawled anyway. It costs most where it
1520 * matters most, because the paths worth disallowing are the ones that should
1521 * not be fetched on a schedule by anybody.
1522 *
1523 * Each group is written out again in full rather than as extra User-agent
1524 * lines on one group. Several agents may share a group, but a parser that
1525 * reads a second User-agent line as the start of a new one would then leave
1526 * the `*` group with no rules in it - and of the two ways to be wrong, a
1527 * silently empty `*` group is the one that opens the whole site.
1528 *
1529 * `agents` replaces this list, and `agents => []` asks for the `*` group
1530 * alone. */
1531 static const char *const pks_agents[] = {
1532 "AdsBot-Google",
1533 "AdsBot-Google-Mobile",
1534 "Mediapartners-Google",
1535 NULL
1536 };
1537  
1538 /* One group: a blank line unless this is the first, the User-agent line, then
1539 * the rules every group shares. */
1540 25 static void pks_group(pTHX_ SV *out, const char *agent, STRLEN alen,
1541 SV *rules) {
1542 25 ▶ if (SvCUR(out)) sv_catpvs(out, "\n");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, "\n"); 13 12 25
1543 25 sv_catpvs(out, "User-agent: ");
1544 25 sv_catpvn(out, agent, alen);
1545 25 sv_catpvs(out, "\n");
1546 25 sv_catsv(out, rules);
1547 25 }
1548  
1549 /* The robots.txt body (+1), advertising the sitemap at `base`. */
1550 12 static SV *pks_robots_for(pTHX_ SV *appsv, SV *base) {
1551 12 HV *app = app_hv(aTHX_ appsv);
1552 12 ▶ SV *all = app ? app_get(aTHX_ app, "sitemap_disallow_all") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *all = app ? app_get(aTHX_ app, "sitemap_disallow_all") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
1553 12 ▶ SV *ags = app ? app_get(aTHX_ app, "sitemap_agents") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *ags = app ? app_get(aTHX_ app, "sitemap_agents") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
1554 2 ▶ AV *agents = (ags && SvROK(ags) && SvTYPE(SvRV(ags)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *agents = (ags && SvROK(ags) && SvTYPE(SvRV(ags)) == SVt_PVAV) 2 0 2
AV *agents = (ags && SvROK(ags) && SvTYPE(SvRV(ags)) == SVt_PVAV) 2 0 2
1555 14 ▶ ? (AV *)SvRV(ags) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(ags) : NULL; 2 10 12
1556 12 SV *rules = sv_2mortal(newSVpvs(""));
1557 12 SV *out = newSVpvs("");
1558 12 ▶ int staging = (all && SvTRUE(all)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int staging = (all && SvTRUE(all)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 11 12
int staging = (all && SvTRUE(all)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
1559  
1560 12 ▶ if (staging) {
Branch: 1 branch
BranchTrueFalseTotal
if (staging) { 1 11 12
1561 /* A staging environment being indexed is routine, embarrassing and
1562 * slow to undo, and it is usually a robots.txt copied from
1563 * production. No Sitemap line below: advertising a sitemap while
1564 * disallowing everything says two opposite things. */
1565 1 sv_catpvs(rules, "Disallow: /\n");
1566 }
1567 else {
1568 11 AV *dis = (AV *)sv_2mortal((SV *)pks_disallows(aTHX_ appsv));
1569 11 SSize_t i, n = av_len(dis) + 1;
1570 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 11 23
1571 12 SV **e = av_fetch(dis, i, 0);
1572 STRLEN l;
1573 const char *p;
1574 /*
1575 # uncoverable branch true
1576 # uncoverable condition left count:1 class:ignore_covered_err
1577 # uncoverable condition left count:2 class:ignore_covered_err */
1578 12 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 12 0 12
if (!(e && *e)) continue; 0 12 12
1579 12 p = SvPV_const(*e, l);
1580 12 sv_catpvs(rules, "Disallow: ");
1581 /* percent-encoded, so a path with a space or a newline in it
1582 * cannot become a second directive */
1583 12 pks_pct_cat(aTHX_ rules, p, l);
1584 12 sv_catpvs(rules, "\n");
1585 }
1586 }
1587  
1588 12 pks_group(aTHX_ out, "*", 1, rules);
1589  
1590 /* Nothing is disallowed, so there is nothing the named crawlers are
1591 * missing: a group naming one of them and carrying no rule says exactly
1592 * what their absence already says. */
1593 12 ▶ if (SvCUR(rules)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(rules)) { 6 6 12
1594 6 ▶ if (agents) {
Branch: 1 branch
BranchTrueFalseTotal
if (agents) { 2 4 6
1595 2 SSize_t i, n = av_len(agents) + 1;
1596 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 2 3
1597 1 SV **e = av_fetch(agents, i, 0);
1598 STRLEN l;
1599 const char *a;
1600 /*
1601 # uncoverable branch true
1602 # uncoverable condition left count:1 class:ignore_covered_err
1603 # uncoverable condition left count:2 class:ignore_covered_err
1604 # uncoverable condition left count:3 class:ignore_covered_err */
1605 1 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 1 0 1
if (!(e && *e && SvOK(*e))) continue; 1 0 1
if (!(e && *e && SvOK(*e))) continue; 0 1 1
1606 1 a = SvPV_const(*e, l);
1607 /*
1608 # uncoverable branch true
1609 # uncoverable condition left count:1 class:ignore_covered_err */
1610 1 ▶ if (!l) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) continue; 0 1 1
1611 1 pks_group(aTHX_ out, a, l, rules);
1612 }
1613 }
1614 else {
1615 int i;
1616 16 ▶ for (i = 0; pks_agents[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; pks_agents[i]; i++) 12 4 16
1617 12 pks_group(aTHX_ out, pks_agents[i],
1618 12 strlen(pks_agents[i]), rules);
1619 }
1620 }
1621  
1622 12 ▶ if (staging) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (staging) return out; 1 11 12
1623  
1624 /* The line most hand-written robots.txt files are missing, and the one
1625 * that makes a sitemap discoverable without anybody being told. */
1626 /*
1627 # uncoverable branch false
1628 # uncoverable condition right count:1 class:ignore_covered_err
1629 # uncoverable condition right count:2 class:ignore_covered_err */
1630 11 ▶ if (base && SvOK(base)) {
Branch: 2 branches
BranchTrueFalseTotal
if (base && SvOK(base)) { 11 0 11
if (base && SvOK(base)) { 11 0 11
1631 STRLEN bl;
1632 11 const char *bp = SvPV_const(base, bl);
1633 /*
1634 # uncoverable branch true class:ignore_covered_err
1635 # uncoverable condition left count:1 class:ignore_covered_err
1636 # uncoverable condition right count:1 class:ignore_covered_err
1637 # uncoverable condition left count:2 class:ignore_covered_err */
1638 11 ▶ while (bl && bp[bl - 1] == '/') bl--;
Branch: 2 branches
BranchTrueFalseTotal
while (bl && bp[bl - 1] == '/') bl--; 11 0 11
while (bl && bp[bl - 1] == '/') bl--; 0 11 11
1639 11 sv_catpvs(out, "\nSitemap: ");
1640 11 sv_catpvn(out, bp, bl);
1641 11 sv_catpvs(out, "/sitemap.xml\n");
1642 }
1643 11 return out;
1644 }
1645  
1646 /* ... at the canonical base */
1647 11 static SV *pks_robots(pTHX_ SV *appsv) {
1648 11 HV *app = app_hv(aTHX_ appsv);
1649 22 ▶ return pks_robots_for(aTHX_ appsv,
Branch: 1 branch
BranchTrueFalseTotal
return pks_robots_for(aTHX_ appsv, 11 0 11
1650 11 app ? app_get(aTHX_ app, "sitemap_base") : NULL);
1651 }
1652  
1653 /* Does this class have that method? A `can` through the ordinary lookup, so
1654 * inheritance and AUTOLOAD behave as they would anywhere else. */
1655 20 static int pkc_can_meth(pTHX_ SV *obj, const char *meth) {
1656 20 SV *m = sv_2mortal(newSVpv(meth, 0));
1657 20 SV *r = pcx_call_meth(aTHX_ obj, "can", &m, 1, 1);
1658 20 ▶ int ok = (r && SvTRUE(r)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int ok = (r && SvTRUE(r)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 0 20
int ok = (r && SvTRUE(r)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 5 20
1659 /*
1660 # uncoverable branch false
1661 # uncoverable condition right count:1 class:ignore_covered_err */
1662 20 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 20 0 20
1663 20 return ok;
1664 }
1665  
1666 /* Install the `sitemap` keyword on this app. Idempotent: install_kw treats a
1667 * second install by the same owner as a no-op, so `use` and `plugin` can both
1668 * ask and only the first does anything. */
1669 57 static void pks_install_kw(pTHX_ SV *app) {
1670 57 AV *cap = newAV();
1671 SV *argv[3], *r;
1672 57 av_push(cap, newSVsv(app));
1673 57 argv[0] = sv_2mortal(newSVpvs("sitemap"));
1674 57 argv[1] = sv_2mortal(punk_closure(aTHX_ pks_kw_cb, cap));
1675 57 argv[2] = sv_2mortal(newSVpvs("Punk::Plugin::Sitemap"));
1676 57 r = pcx_call_meth(aTHX_ app, "install_kw", argv, 3, 1);
1677 /*
1678 # uncoverable branch false
1679 # uncoverable condition right count:1 class:ignore_covered_err */
1680 57 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 57 0 57
1681 57 }
1682  
1683 /* Resolve the base, at to_app. An explicit `base` was stored by register and
1684 * wins; without one, the application's `host` is the same fact declared once
1685 * at the app level - and because this runs at compile rather than at the
1686 * `plugin` line, `host` may sit on either side of it in the package body.
1687 *
1688 * There is still no fallback to the request's Host header, and the croak
1689 * says why: a request-derived origin is attacker-supplied bytes delivered to
1690 * search engines. */
1691 40 static void pks_resolve_base(pTHX_ SV *appsv) {
1692 40 HV *h = app_hv(aTHX_ appsv);
1693 40 ▶ SV *b = h ? app_get(aTHX_ h, "sitemap_base") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *b = h ? app_get(aTHX_ h, "sitemap_base") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 40 0 40
1694 SV *host;
1695 /*
1696 # uncoverable condition left count:1 class:ignore_covered_err
1697 # uncoverable condition right count:3 class:ignore_covered_err
1698 # uncoverable condition right count:4 class:ignore_covered_err */
1699 40 ▶ if (!h || (b && SvOK(b) && SvCUR(b))) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!h || (b && SvOK(b) && SvCUR(b))) return; 40 0 40
if (!h || (b && SvOK(b) && SvCUR(b))) return; 33 7 40
if (!h || (b && SvOK(b) && SvCUR(b))) return; 33 0 33
if (!h || (b && SvOK(b) && SvCUR(b))) return; 33 0 33
1700 7 host = app_get(aTHX_ h, "host");
1701 /*
1702 # uncoverable condition right count:2 class:ignore_covered_err
1703 # uncoverable condition right count:3 class:ignore_covered_err */
1704 7 ▶ if (host && SvOK(host) && SvCUR(host)) {
Branch: 3 branches
BranchTrueFalseTotal
if (host && SvOK(host) && SvCUR(host)) { 5 2 7
if (host && SvOK(host) && SvCUR(host)) { 5 0 5
if (host && SvOK(host) && SvCUR(host)) { 5 0 5
1705 5 (void)hv_stores(h, "sitemap_base", newSVsv(host));
1706 5 return;
1707 }
1708 2 croak("Punk::Plugin::Sitemap: `base` is required - declare the "
1709 "application's `host`, or pass base => 'https://...' - the "
1710 "sitemap protocol needs absolute URLs, and taking the host from "
1711 "the request would let a crawler be handed somebody else's "
1712 "hostname");
1713 }
1714  
1715 /* The to_app seam. Punk calls every registered middleware wrapper while it
1716 * compiles, which is the one moment every route has been declared and nothing
1717 * has been served - so the document is built there and the wrapper hands the
1718 * application back untouched. */
1719 XS_INTERNAL(pks_mw_cb);
1720 40 XS_INTERNAL(pks_mw_cb) {
1721 40 dXSARGS;
1722 40 AV *cap = punk_clos_cap(aTHX_ cv);
1723 40 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 40 0 40
1724 /*
1725 # uncoverable branch false class:ignore_covered_err
1726 # uncoverable condition right count:1 class:ignore_covered_err */
1727 40 ▶ if (app) {
Branch: 1 branch
BranchTrueFalseTotal
if (app) { 40 0 40
1728 40 pks_resolve_base(aTHX_ app);
1729 38 pks_build(aTHX_ app);
1730 }
1731 /*
1732 # uncoverable branch false
1733 # uncoverable condition right count:1 class:ignore_covered_err */
1734 38 ▶ if (items > 0) XSRETURN(1); /* ST(0) is the inner app, unchanged */
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) XSRETURN(1); /* ST(0) is the inner app, unchanged */ 38 0 38
1735 0 XSRETURN_EMPTY; /* # uncoverable statement */
1736 }
1737  
1738 #endif /* PUNK_SITEMAP_H */
« xs/router.xs ↑ Summary include/punk/punk_computed.h »