xs/sitemap.xs

filter: Statementstmt 96.6% 114 / 118 Branchbran 58.0% 87 / 150 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.0% 201 / 268 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_context.h ↑ Summary include/punk/punk_cors.h »
1 MODULE = Punk PACKAGE = Punk::Plugin::Sitemap
2  
3 PROTOTYPES: DISABLE
4  
5 SV *
6 new(class)
7 SV *class
8 CODE:
9 38 RETVAL = sv_bless(newRV_noinc(newSViv(0)),
10 gv_stashpv(SvPV_nolen(class), GV_ADD));
11 OUTPUT:
12 RETVAL
13  
14 # use Punk::Plugin::Sitemap;
15 #
16 # Installs the `sitemap` keyword at COMPILE time, which is the only moment
17 # that makes the bareword form parse:
18 #
19 # sitemap users => sub { ... };
20 #
21 # `plugin 'Sitemap'` runs at RUNTIME of the package body, long after the
22 # statement above has been compiled, so a keyword installed only there is one
23 # perl has already refused to parse. Punk::Plugin::Queue and ::OpenTelemetry
24 # split it the same way and for the same reason.
25 #
26 # Without the `use`, the parenthesised form still works, because perl resolves
27 # a call with parentheses at runtime:
28 #
29 # sitemap('users' => sub { ... });
30 void
31 import(class, ...)
32 SV *class
33 CODE:
34 {
35 19 ▶ const char *pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
const char *pkg = CopSTASHPV(PL_curcop); 19 0 19
const char *pkg = CopSTASHPV(PL_curcop); 19 0 19
const char *pkg = CopSTASHPV(PL_curcop); 19 0 19
const char *pkg = CopSTASHPV(PL_curcop); 0 19 19
const char *pkg = CopSTASHPV(PL_curcop); 0 0 0
const char *pkg = CopSTASHPV(PL_curcop); 19 0 19
const char *pkg = CopSTASHPV(PL_curcop); 0 19 19
36 SV *pkgsv, *appsv;
37 PERL_UNUSED_VAR(class);
38 19 ▶ if (!pkg) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg) XSRETURN_EMPTY; 0 19 19
39 19 pkgsv = sv_2mortal(newSVpv(pkg, 0));
40 /* Not a Punk application: nothing to install a keyword on, and that
41 * is legitimate - a test or a script may load this for its class
42 * methods alone. Silence rather than a croak, because the failure
43 * mode for getting the order wrong (`use Punk` second) is perl
44 * refusing to compile the `sitemap` line, which says so itself. */
45 19 ▶ if (!pkc_can_meth(aTHX_ pkgsv, "punk_app")) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!pkc_can_meth(aTHX_ pkgsv, "punk_app")) XSRETURN_EMPTY; 4 15 19
46 15 appsv = pcx_call_meth(aTHX_ pkgsv, "punk_app", NULL, 0, 1);
47 15 ▶ if (appsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (appsv) { 15 0 15
48 15 pks_install_kw(aTHX_ sv_2mortal(appsv));
49 }
50 }
51  
52 # register($app, \%opts)
53 #
54 # `base` is configuration, with no fallback to the request's Host, ever. The
55 # sitemap protocol wants absolute URLs, and taking the host from the request
56 # means an attacker sending `Host: evil.example` gets a sitemap naming their
57 # host for every page on the site - delivered to search engines, and invisible
58 # to the owner, whose own request produces a correct file.
59 #
60 # When no `base` is passed it defaults to the application's `host` - which is
61 # still configuration, declared once at the app level. The check happens at
62 # to_app (pks_resolve_base) rather than here, because `host` may legitimately
63 # be declared on either side of the `plugin` line.
64 void
65 register(self, app, opts = &PL_sv_undef)
66 SV *self
67 SV *app
68 SV *opts
69 CODE:
70 {
71 38 HV *h = app_hv(aTHX_ app);
72 38 SV **b = NULL;
73 PERL_UNUSED_VAR(self);
74 38 ▶ if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
75 38 b = hv_fetchs((HV *)SvRV(opts), "base", 0);
76 38 ▶ if (b && *b && SvOK(*b) && SvCUR(*b))
Branch: 4 branches
BranchTrueFalseTotal
if (b && *b && SvOK(*b) && SvCUR(*b)) 31 7 38
if (b && *b && SvOK(*b) && SvCUR(*b)) 31 0 31
if (b && *b && SvOK(*b) && SvCUR(*b)) 31 0 31
if (b && *b && SvOK(*b) && SvCUR(*b)) 31 0 31
77 31 (void)hv_stores(h, "sitemap_base", newSVsv(*b));
78 { /* the routes this plugin adds for itself: out of the sitemap,
79 * and out of robots.txt's Disallow list too */
80 38 AV *own = newAV();
81 38 av_push(own, newSVpvs("/sitemap.xml"));
82 38 av_push(own, newSVpvs("/sitemap/:n"));
83 38 av_push(own, newSVpvs("/robots.txt"));
84 38 (void)hv_stores(h, "sitemap_own", newRV_noinc((SV *)own));
85 }
86  
87 /* Both documents are served from the site ROOT regardless of any
88 * mount prefix: /sitemap.xml is a fixed location and a crawler will
89 * not look anywhere else. */
90 {
91 SV *argv[5];
92 AV *cap;
93 SV *r;
94  
95 /* /sitemap.xml - the index, or the single part. Opts itself out,
96 * because a sitemap listing itself is noise a crawler follows. */
97 38 cap = newAV();
98 38 av_push(cap, newSVsv(app));
99 38 av_push(cap, newSViv(0));
100 38 argv[0] = sv_2mortal(newSVpvs("GET"));
101 38 argv[1] = sv_2mortal(newSVpvs("/sitemap.xml"));
102 38 argv[2] = sv_2mortal(punk_closure(aTHX_ pks_serve_cb, cap));
103 38 argv[3] = &PL_sv_undef;
104 {
105 38 HV *o = newHV();
106 38 (void)hv_stores(o, K_SITEMAP, newSViv(0));
107 38 argv[4] = sv_2mortal(newRV_noinc((SV *)o));
108 }
109 38 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
110 38 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 38 0 38
111  
112 /* /sitemap/1.xml, /2.xml and so on. A capture, so the listing
113 * rules keep it out of the sitemap with no option needed. */
114 38 cap = newAV();
115 38 av_push(cap, newSVsv(app));
116 38 av_push(cap, newSViv(1));
117 38 argv[0] = sv_2mortal(newSVpvs("GET"));
118 38 argv[1] = sv_2mortal(newSVpvs("/sitemap/:n"));
119 38 argv[2] = sv_2mortal(punk_closure(aTHX_ pks_serve_cb, cap));
120 38 argv[3] = &PL_sv_undef;
121 38 argv[4] = &PL_sv_undef;
122 38 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
123 38 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 38 0 38
124  
125 /* /robots.txt, from the same exclusion list. Opts itself out
126 * of the sitemap, and is served from the root for the same
127 * reason /sitemap.xml is. */
128 38 cap = newAV();
129 38 av_push(cap, newSVsv(app));
130 38 argv[0] = sv_2mortal(newSVpvs("GET"));
131 38 argv[1] = sv_2mortal(newSVpvs("/robots.txt"));
132 38 argv[2] = sv_2mortal(punk_closure(aTHX_ pks_robots_cb, cap));
133 38 argv[3] = &PL_sv_undef;
134 {
135 38 HV *o = newHV();
136 38 (void)hv_stores(o, K_SITEMAP, newSViv(0));
137 38 argv[4] = sv_2mortal(newRV_noinc((SV *)o));
138 }
139 38 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
140 38 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 38 0 38
141  
142 /* the `sitemap` keyword, for the URLs the route table cannot
143 * know, and the TTL that bounds how stale they may be. */
144 {
145 38 ▶ SV *t = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV *t = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
146 76 ▶ ? *(hv_fetchs((HV *)SvRV(opts), "ttl", 1)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? *(hv_fetchs((HV *)SvRV(opts), "ttl", 1)) : NULL; 38 0 38
147 38 ▶ SV **d = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **d = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
148 76 ▶ ? hv_fetchs((HV *)SvRV(opts), "disallow", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(opts), "disallow", 0) : NULL; 38 0 38
149 38 ▶ SV **da = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **da = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
150 76 ▶ ? hv_fetchs((HV *)SvRV(opts), "disallow_all", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(opts), "disallow_all", 0) : NULL; 38 0 38
151 38 ▶ SV **ag = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **ag = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 38 0 38
152 76 ▶ ? hv_fetchs((HV *)SvRV(opts), "agents", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(opts), "agents", 0) : NULL; 38 0 38
153 38 ▶ if (d && *d && SvROK(*d) && SvTYPE(SvRV(*d)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (d && *d && SvROK(*d) && SvTYPE(SvRV(*d)) == SVt_PVAV) 2 36 38
if (d && *d && SvROK(*d) && SvTYPE(SvRV(*d)) == SVt_PVAV) 2 0 2
if (d && *d && SvROK(*d) && SvTYPE(SvRV(*d)) == SVt_PVAV) 2 0 2
if (d && *d && SvROK(*d) && SvTYPE(SvRV(*d)) == SVt_PVAV) 2 0 2
154 2 (void)hv_stores(h, "sitemap_disallow", newSVsv(*d));
155 38 ▶ if (da && *da) (void)hv_stores(h, "sitemap_disallow_all",
Branch: 2 branches
BranchTrueFalseTotal
if (da && *da) (void)hv_stores(h, "sitemap_disallow_all", 1 37 38
if (da && *da) (void)hv_stores(h, "sitemap_disallow_all", 1 0 1
156 newSViv(SvTRUE(*da) ? 1 : 0));
157 /* The user agents each group is repeated for. Checked here
158 * rather than where the file is built, because a token with a
159 * space in it is a typo that would otherwise be discovered as
160 * a crawler reading a directive that was never written. */
161 38 ▶ if (ag && *ag) {
Branch: 2 branches
BranchTrueFalseTotal
if (ag && *ag) { 3 35 38
if (ag && *ag) { 3 0 3
162 AV *av;
163 SSize_t i, n;
164 3 ▶ if (!(SvROK(*ag) && SvTYPE(SvRV(*ag)) == SVt_PVAV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(*ag) && SvTYPE(SvRV(*ag)) == SVt_PVAV)) 3 0 3
if (!(SvROK(*ag) && SvTYPE(SvRV(*ag)) == SVt_PVAV)) 0 3 3
165 0 croak("Punk::Plugin::Sitemap: agents is an arrayref "
166 "of user-agent tokens");
167 3 av = (AV *)SvRV(*ag);
168 3 n = av_len(av) + 1;
169 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 2 4
170 2 SV **e = av_fetch(av, i, 0);
171 STRLEN l, j;
172 const char *s;
173 2 ▶ if (!(e && *e && SvOK(*e)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) 2 0 2
if (!(e && *e && SvOK(*e))) 2 0 2
if (!(e && *e && SvOK(*e))) 0 2 2
174 0 croak("Punk::Plugin::Sitemap: agents holds a "
175 "user-agent token, not undef");
176 2 s = SvPV_const(*e, l);
177 2 ▶ if (!l)
Branch: 1 branch
BranchTrueFalseTotal
if (!l) 0 2 2
178 0 croak("Punk::Plugin::Sitemap: an empty user-agent "
179 "token names no crawler");
180 20 ▶ for (j = 0; j < l; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; j++) 19 1 20
181 19 ▶ if (isSPACE(s[j]) || s[j] == ':'
Branch: 2 branches
BranchTrueFalseTotal
if (isSPACE(s[j]) || s[j] == ':' 18 1 19
if (isSPACE(s[j]) || s[j] == ':' 18 0 18
182 18 ▶ || (unsigned char)s[j] < 0x20)
Branch: 1 branch
BranchTrueFalseTotal
|| (unsigned char)s[j] < 0x20) 0 18 18
183 1 croak("Punk::Plugin::Sitemap: '%.*s' is not a "
184 "user-agent token - a token carries no "
185 "space, colon or newline",
186 (int)l, s);
187 }
188 2 (void)hv_stores(h, "sitemap_agents", newSVsv(*ag));
189 }
190 37 ▶ (void)hv_stores(h, "sitemap_ttl",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "sitemap_ttl", 37 0 37
(void)hv_stores(h, "sitemap_ttl", 2 35 37
(void)hv_stores(h, "sitemap_ttl", 2 0 2
191 (t && SvOK(t) && SvNV(t) > 0)
192 ? newSVnv(SvNV(t)) : newSVnv(3600.0));
193 37 pks_install_kw(aTHX_ app);
194 }
195  
196 /* and the build itself, at to_app */
197 37 cap = newAV();
198 37 av_push(cap, newSVsv(app));
199 37 argv[0] = sv_2mortal(punk_closure(aTHX_ pks_mw_cb, cap));
200 37 r = pcx_call_meth(aTHX_ app, "middleware", argv, 1, 1);
201 37 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 37 0 37
202 }
203 }
204  
205 # The rendered document for a part, or the index. Private, and what the
206 # tests parse.
207 SV *
208 _doc(class, app, n = 0)
209 SV *class
210 SV *app
211 IV n
212 CODE:
213 {
214 23 HV *h = app_hv(aTHX_ app);
215 23 SV *doc = NULL;
216 PERL_UNUSED_VAR(class);
217 23 ▶ if (!h) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) XSRETURN_UNDEF; 0 23 23
218 /* the same refresh a request would trigger - a seam that behaved
219 * differently from the serve path would prove nothing */
220 23 pks_ensure(aTHX_ app);
221 23 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 21 2 23
222 /* /sitemap.xml: the index when there is more than one part, and
223 * otherwise the single part itself. */
224 21 doc = app_get(aTHX_ h, "sitemap_index");
225 21 ▶ if (!(doc && SvOK(doc))) n = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!(doc && SvOK(doc))) n = 1; 21 0 21
if (!(doc && SvOK(doc))) n = 1; 20 1 21
226 }
227 23 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 22 1 23
228 22 SV *ps = app_get(aTHX_ h, "sitemap_parts");
229 22 ▶ AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 22 0 22
AV *parts = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 22 0 22
230 44 ▶ ? (AV *)SvRV(ps) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(ps) : NULL; 22 0 22
231 22 ▶ SV **e = parts ? av_fetch(parts, (SSize_t)(n - 1), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = parts ? av_fetch(parts, (SSize_t)(n - 1), 0) : NULL; 22 0 22
232 22 ▶ doc = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
doc = (e && *e) ? *e : NULL; 22 0 22
doc = (e && *e) ? *e : NULL; 22 0 22
233 }
234 23 ▶ if (!(doc && SvOK(doc))) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!(doc && SvOK(doc))) XSRETURN_UNDEF; 23 0 23
if (!(doc && SvOK(doc))) XSRETURN_UNDEF; 0 23 23
235 23 RETVAL = newSVsv(doc);
236 }
237 OUTPUT:
238 RETVAL
239  
240 # How many <urlset> parts the document split into.
241 IV
242 _parts(class, app)
243 SV *class
244 SV *app
245 CODE:
246 {
247 2 HV *h = app_hv(aTHX_ app);
248 SV *ps;
249 PERL_UNUSED_VAR(class);
250 2 ▶ if (h) pks_ensure(aTHX_ app);
Branch: 1 branch
BranchTrueFalseTotal
if (h) pks_ensure(aTHX_ app); 2 0 2
251 2 ▶ ps = h ? app_get(aTHX_ h, "sitemap_parts") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ps = h ? app_get(aTHX_ h, "sitemap_parts") : NULL; 2 0 2
252 4 ▶ RETVAL = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 2 0 2
RETVAL = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 2 0 2
RETVAL = (ps && SvROK(ps) && SvTYPE(SvRV(ps)) == SVt_PVAV) 0 2 2
253 4 ▶ ? (IV)(av_len((AV *)SvRV(ps)) + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
? (IV)(av_len((AV *)SvRV(ps)) + 1) : 0; 2 0 2
254 }
255 OUTPUT:
256 RETVAL
257  
258 # Render now. Called from the middleware wrapper below, which Punk invokes at
259 # to_app - the only moment every route has been declared and nothing has been
260 # served yet.
261 void
262 _build(class, app)
263 SV *class
264 SV *app
265 CODE:
266 PERL_UNUSED_VAR(class);
267 0 pks_build(aTHX_ app);
268  
269 # The robots.txt body. Private, and what the tests compare against the
270 # sitemap so the two cannot be shown to disagree.
271 SV *
272 _robots(class, app)
273 SV *class
274 SV *app
275 CODE:
276 PERL_UNUSED_VAR(class);
277 9 RETVAL = pks_robots(aTHX_ app);
278 OUTPUT:
279 RETVAL
280  
281 # The candidate paths, in sorted order. Private, and the seam the tests
282 # assert through: a guarded route being ABSENT is the claim, and a claim
283 # needs something to look at.
284 void
285 _paths(class, app)
286 SV *class
287 SV *app
288 PPCODE:
289 {
290 6 AV *out = (AV *)sv_2mortal((SV *)pks_paths(aTHX_ app));
291 6 SSize_t i, n = av_len(out) + 1;
292 PERL_UNUSED_VAR(class);
293 6 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 6 0 6
EXTEND(SP, n); 0 6 6
294 12 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 6 6 12
295 6 PUSHs(sv_2mortal(newSVsv(*av_fetch(out, i, 0))));
296 }
297  
298 # The configured base, joined onto each path.
299 SV *
300 _base(class, app)
301 SV *class
302 SV *app
303 CODE:
304 {
305 4 HV *h = app_hv(aTHX_ app);
306 4 ▶ SV *b = h ? app_get(aTHX_ h, "sitemap_base") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *b = h ? app_get(aTHX_ h, "sitemap_base") : NULL; 4 0 4
307 PERL_UNUSED_VAR(class);
308 4 ▶ if (!(b && SvOK(b))) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!(b && SvOK(b))) XSRETURN_UNDEF; 4 0 4
if (!(b && SvOK(b))) XSRETURN_UNDEF; 0 4 4
309 4 RETVAL = newSVsv(b);
310 }
311 OUTPUT:
312 RETVAL
« include/punk/punk_context.h ↑ Summary include/punk/punk_cors.h »