xs/static.xs

filter: Statementstmt 98.4% 65 / 66 Branchbran 76.9% 80 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.2% 145 / 170 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_proxy.h ↑ Summary xs/computed.xs »
1 MODULE = Punk PACKAGE = Punk::Static
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Static->app($dir, %opts) - the PSGI coderef serving that directory.
6 # The request path itself is punk_static_cb in include/punk/punk_static.h: a
7 # magic CV carrying the mount's whole configuration, so a request for a file
8 # crosses no Perl frame at all.
9 #
10 # Options: max_age (seconds of freshness for a plain URL), cache_control (a
11 # verbatim header value, overriding max_age), fingerprint (content-addressed
12 # URLs, on by default), dev (re-check digests against the filesystem;
13 # defaults from PUNK_ENV, and the compiler passes the app's own env), index
14 # (the file a directory resolves to), list (render a listing for a
15 # directory that has no index) and dotfiles (serve and list names that begin
16 # with a dot, which are otherwise a 404).
17 SV *
18 app(class, dir, ...)
19 SV *class
20 SV *dir
21 CODE:
22 {
23 AV *cap;
24 Stat_t st;
25 53 HV *opts = NULL;
26 SV **o;
27 53 SV *cc = NULL, *index = NULL;
28 53 int list = 0, dotfiles = 0;
29 53 const char *d = SvPV_nolen(dir);
30 /* fingerprinting is opt-in: it changes what a path MEANS - a URL
31 * shaped like a fingerprint stops being a 404 and starts resolving
32 * to another file - and that is not something a mount should start
33 * doing because it was upgraded */
34 53 int fingerprint = 0, dev = 0, i;
35 PERL_UNUSED_VAR(class);
36  
37 53 ▶ if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode))
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode)) 52 1 53
if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode)) 0 52 52
38 1 croak("Punk::Static: '%s' is not a directory", d);
39  
40 /* a hashref or a flat list, so the compiler can hand over a mount
41 * record's options without flattening them first */
42 52 ▶ if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) 38 14 52
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) 38 0 38
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) 38 0 38
43 38 opts = (HV *)SvRV(ST(2));
44 14 ▶ else if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 2) { 12 2 14
45 12 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 0 12 12
46 0 croak("Punk::Static: app takes a directory and an "
47 "even-sized option list");
48 12 opts = (HV *)sv_2mortal((SV *)newHV());
49 27 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 15 12 27
50 STRLEN kl;
51 15 const char *kp = SvPV_const(ST(i), kl);
52 15 (void)hv_store(opts, kp, (I32)kl, newSVsv(ST(i + 1)), 0);
53 }
54 }
55  
56 52 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 50 2 52
57 static const char *const known[] = {
58 "max_age", "cache_control", "fingerprint", "dev",
59 "index", "list", "dotfiles", NULL
60 };
61 HE *he;
62 50 hv_iterinit(opts);
63 115 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 67 48 115
64 67 ▶ const char *k = HePV(he, PL_na);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, PL_na); 0 67 67
65 67 int ok = 0;
66 536 ▶ for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 65 404 469
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 469 67 536
67 67 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 2 65 67
68 2 croak("Punk::Static: unknown option '%s'", k);
69 }
70 48 o = hv_fetchs(opts, "fingerprint", 0);
71 48 ▶ if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0; 6 42 48
if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0; 6 0 6
72 48 o = hv_fetchs(opts, "dev", 0);
73 48 ▶ if (o && *o) dev = SvTRUE(*o) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o) dev = SvTRUE(*o) ? 1 : 0; 38 10 48
if (o && *o) dev = SvTRUE(*o) ? 1 : 0; 38 0 38
74 20 else dev = (getenv("PUNK_ENV")
75 10 ▶ && strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0; 7 3 10
&& strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0; 0 7 7
76 /* `index => 1` is the spelling for "the usual one", so a mount
77 * that only wants directories to work does not have to name the
78 * file. A name with a separator in it would be a path, and a
79 * path is how a directory index turns into a way out of the
80 * directory - so it is refused here rather than guarded for on
81 * every request. */
82 48 o = hv_fetchs(opts, "index", 0);
83 48 ▶ if (o && *o && SvOK(*o)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) { 7 41 48
if (o && *o && SvOK(*o)) { 7 0 7
if (o && *o && SvOK(*o)) { 7 0 7
84 7 ▶ if (!SvPOK(*o) && SvTRUE(*o)) index = newSVpvs("index.html");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvPOK(*o) && SvTRUE(*o)) index = newSVpvs("index.html"); 1 6 7
if (!SvPOK(*o) && SvTRUE(*o)) index = newSVpvs("index.html"); 1 0 1
85 6 ▶ else if (SvTRUE(*o)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE(*o)) { 6 0 6
86 STRLEN il;
87 6 const char *ip = SvPV_const(*o, il);
88 6 ▶ if (!il || memchr(ip, '/', il) || memchr(ip, '\\', il)
Branch: 3 branches
BranchTrueFalseTotal
if (!il || memchr(ip, '/', il) || memchr(ip, '\\', il) 6 0 6
if (!il || memchr(ip, '/', il) || memchr(ip, '\\', il) 5 1 6
if (!il || memchr(ip, '/', il) || memchr(ip, '\\', il) 4 1 5
89 4 ▶ || memchr(ip, '\0', il))
Branch: 1 branch
BranchTrueFalseTotal
|| memchr(ip, '\0', il)) 0 4 4
90 2 croak("Punk::Static: index must be a filename inside "
91 "the directory, not a path");
92 4 index = newSVpvn(ip, il);
93 }
94 }
95 46 o = hv_fetchs(opts, "list", 0);
96 46 ▶ if (o && *o) list = SvTRUE(*o) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o) list = SvTRUE(*o) ? 1 : 0; 8 38 46
if (o && *o) list = SvTRUE(*o) ? 1 : 0; 8 0 8
97 46 o = hv_fetchs(opts, "dotfiles", 0);
98 46 ▶ if (o && *o) dotfiles = SvTRUE(*o) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o) dotfiles = SvTRUE(*o) ? 1 : 0; 2 44 46
if (o && *o) dotfiles = SvTRUE(*o) ? 1 : 0; 2 0 2
99 /* cache_control is verbatim and wins; max_age is the spelling
100 * that covers what almost everyone means by it */
101 46 o = hv_fetchs(opts, "cache_control", 0);
102 46 ▶ if (o && *o && SvOK(*o)) cc = newSVsv(*o);
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) cc = newSVsv(*o); 1 45 46
if (o && *o && SvOK(*o)) cc = newSVsv(*o); 1 0 1
if (o && *o && SvOK(*o)) cc = newSVsv(*o); 1 0 1
103 else {
104 45 o = hv_fetchs(opts, "max_age", 0);
105 45 ▶ if (o && *o && SvOK(*o)) {
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) { 2 43 45
if (o && *o && SvOK(*o)) { 2 0 2
if (o && *o && SvOK(*o)) { 2 0 2
106 2 cc = newSVpvs("public, max-age=");
107 2 sv_catpvf(cc, "%" IVdf, (IV)SvIV(*o));
108 }
109 }
110 }
111 else
112 4 dev = (getenv("PUNK_ENV")
113 2 ▶ && strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0; 1 1 2
&& strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0; 0 1 1
114  
115 48 cap = newAV();
116 48 av_extend(cap, PSC_DOT);
117 { /* keep the directory without a trailing slash, so joining a
118 * PATH_INFO (which always starts with one) cannot double it */
119 STRLEN dlen;
120 48 const char *p = SvPV_const(dir, dlen);
121 48 ▶ while (dlen > 1 && p[dlen - 1] == '/') dlen--;
Branch: 2 branches
BranchTrueFalseTotal
while (dlen > 1 && p[dlen - 1] == '/') dlen--; 48 0 48
while (dlen > 1 && p[dlen - 1] == '/') dlen--; 0 48 48
122 48 (void)av_store(cap, PSC_DIR, newSVpvn(p, dlen));
123 }
124 48 ▶ (void)av_store(cap, PSC_CC, cc ? cc : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_CC, cc ? cc : newSV(0)); 45 3 48
125 /* A year and `immutable`, the only lifetime a content-addressed URL
126 * wants: it is the largest value RFC 9111 asks caches to honour, and
127 * `immutable` stops a reload revalidating a URL that cannot have
128 * changed. It is sent only after the digest has been checked. */
129 48 ▶ (void)av_store(cap, PSC_CC_IMM,
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_CC_IMM, 5 43 48
130 fingerprint ? newSVpvs("public, max-age=31536000, immutable")
131 : newSV(0));
132 48 ▶ (void)av_store(cap, PSC_CACHE,
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_CACHE, 5 43 48
133 fingerprint ? newRV_noinc((SV *)newHV()) : newSV(0));
134 48 ▶ (void)av_store(cap, PSC_DEV, dev ? newSViv(1) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_DEV, dev ? newSViv(1) : newSV(0)); 1 47 48
135 48 ▶ (void)av_store(cap, PSC_INDEX, index ? index : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_INDEX, index ? index : newSV(0)); 43 5 48
136 48 ▶ (void)av_store(cap, PSC_LIST, list ? newSViv(1) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_LIST, list ? newSViv(1) : newSV(0)); 8 40 48
137 48 ▶ (void)av_store(cap, PSC_DOT, dotfiles ? newSViv(1) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)av_store(cap, PSC_DOT, dotfiles ? newSViv(1) : newSV(0)); 2 46 48
138 48 RETVAL = punk_closure(aTHX_ punk_static_cb, cap);
139 }
140 OUTPUT:
141 RETVAL
142  
143 # The digest a fingerprinted URL for $url would carry, or undef when the
144 # mount does not fingerprint or the file cannot be read. $c->asset is the
145 # way to reach this; it is here so the mount's own cache answers.
146 SV *
147 _asset(app, url)
148 SV *app
149 SV *url
150 CODE:
151 {
152 4 RETVAL = pa_asset_url(aTHX_ app, url);
153 4 ▶ if (!RETVAL) RETVAL = newSVsv(url);
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) RETVAL = newSVsv(url); 0 4 4
154 }
155 OUTPUT:
156 RETVAL
« include/punk/punk_proxy.h ↑ Summary xs/computed.xs »