xs/static.xs

filter: Statementstmt 98.4% 65 / 66 Branchbran 75.9% 80 / 104 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 145 / 170 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_names.h ↑ Summary xs/app.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 67 HV *opts = NULL;
26 SV **o;
27 67 SV *cc = NULL, *index = NULL;
28 67 int list = 0, dotfiles = 0;
29 67 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 67 int fingerprint = 0, dev = 0, i;
35 PERL_UNUSED_VAR(class);
36  
37 67 ▶ if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 66 1 67
if (PerlLIO_stat(d, &st) < 0 || !S_ISDIR(st.st_mode)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 66 66
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 66 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 51 15 66
# uncoverable condition right count:2 class:ignore_covered_err 51 0 51
# uncoverable condition right count:2 class:ignore_covered_err 51 0 51
43 51 # uncoverable condition right count:3 class:ignore_covered_err
44 15 ▶ if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) 13 2 15
45 13 ▶ opts = (HV *)SvRV(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
opts = (HV *)SvRV(ST(2)); 0 13 13
46 0 else if (items > 2) {
47 # uncoverable branch true
48 13 # uncoverable condition left count:1 class:ignore_covered_err
49 29 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 16 13 29
50 croak("Punk::Static: app takes a directory and an " /* # uncoverable statement */
51 16 "even-sized option list");
52 16 opts = (HV *)sv_2mortal((SV *)newHV());
53 for (i = 2; i + 1 < items; i += 2) {
54 STRLEN kl;
55 const char *kp = SvPV_const(ST(i), kl);
56 66 ▶ (void)hv_store(opts, kp, (I32)kl, newSVsv(ST(i + 1)), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(opts, kp, (I32)kl, newSVsv(ST(i + 1)), 0); 64 2 66
57 }
58 }
59  
60 if (opts) {
61 static const char *const known[] = {
62 64 "max_age", "cache_control", "fingerprint", "dev",
63 143 ▶ "index", "list", "dotfiles", NULL
Branch: 1 branch
BranchTrueFalseTotal
"index", "list", "dotfiles", NULL 81 62 143
64 81 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
}; 0 81 81
65 81 HE *he;
66 648 ▶ hv_iterinit(opts);
Branch: 2 branches
BranchTrueFalseTotal
hv_iterinit(opts); 79 488 567
hv_iterinit(opts); 567 81 648
67 81 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 2 79 81
68 2 const char *k = HePV(he, PL_na);
69 int ok = 0;
70 62 for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1;
71 62 ▶ if (!ok)
Branch: 2 branches
BranchTrueFalseTotal
if (!ok) 6 56 62
if (!ok) 6 0 6
72 62 croak("Punk::Static: unknown option '%s'", k);
73 62 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 51 11 62
} 51 0 51
74 22 o = hv_fetchs(opts, "fingerprint", 0);
75 11 ▶ if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 7 4 11
if (o && *o) fingerprint = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 7 7
76 o = hv_fetchs(opts, "dev", 0);
77 if (o && *o) dev = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
78 else dev = (getenv("PUNK_ENV")
79 # uncoverable condition left count:1 class:ignore_covered_err
80 # uncoverable condition left count:2 class:ignore_covered_err
81 && strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0;
82 62 /* `index => 1` is the spelling for "the usual one", so a mount
83 62 ▶ * that only wants directories to work does not have to name the
Branch: 3 branches
BranchTrueFalseTotal
* that only wants directories to work does not have to name the 8 54 62
* that only wants directories to work does not have to name the 8 0 8
* that only wants directories to work does not have to name the 8 0 8
84 8 ▶ * file. A name with a separator in it would be a path, and a
Branch: 2 branches
BranchTrueFalseTotal
* file. A name with a separator in it would be a path, and a 1 7 8
* file. A name with a separator in it would be a path, and a 1 0 1
85 7 ▶ * path is how a directory index turns into a way out of the
Branch: 1 branch
BranchTrueFalseTotal
* path is how a directory index turns into a way out of the 7 0 7
86 * directory - so it is refused here rather than guarded for on
87 7 * every request. */
88 7 ▶ o = hv_fetchs(opts, "index", 0);
Branch: 3 branches
BranchTrueFalseTotal
o = hv_fetchs(opts, "index", 0); 7 0 7
o = hv_fetchs(opts, "index", 0); 6 1 7
o = hv_fetchs(opts, "index", 0); 5 1 6
89 5 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 0 5 5
90 2 # uncoverable condition right count:3 class:ignore_covered_err
91 if (o && *o && SvOK(*o)) {
92 5 if (!SvPOK(*o) && SvTRUE(*o)) index = newSVpvs("index.html"); /* # uncoverable condition right count:2 class:ignore_covered_err */
93 # uncoverable branch false
94 # uncoverable condition right count:1 class:ignore_covered_err
95 60 else if (SvTRUE(*o)) {
96 60 ▶ STRLEN il;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN il; 8 52 60
STRLEN il; 8 0 8
97 60 const char *ip = SvPV_const(*o, il);
98 60 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 2 58 60
# uncoverable condition left count:1 class:ignore_covered_err 2 0 2
99 # uncoverable condition left count:4 class:ignore_covered_err
100 if (!il || memchr(ip, '/', il) || memchr(ip, '\\', il)
101 60 || memchr(ip, '\0', il))
102 60 ▶ croak("Punk::Static: index must be a filename inside "
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk::Static: index must be a filename inside " 1 59 60
croak("Punk::Static: index must be a filename inside " 1 0 1
croak("Punk::Static: index must be a filename inside " 1 0 1
103 "the directory, not a path");
104 59 index = newSVpvn(ip, il);
105 59 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 2 57 59
} 2 0 2
} 2 0 2
106 2 }
107 2 o = hv_fetchs(opts, "list", 0);
108 if (o && *o) list = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
109 o = hv_fetchs(opts, "dotfiles", 0);
110 if (o && *o) dotfiles = SvTRUE(*o) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
111 /* cache_control is verbatim and wins; max_age is the spelling
112 4 * that covers what almost everyone means by it */
113 2 ▶ o = hv_fetchs(opts, "cache_control", 0);
Branch: 2 branches
BranchTrueFalseTotal
o = hv_fetchs(opts, "cache_control", 0); 1 1 2
o = hv_fetchs(opts, "cache_control", 0); 0 1 1
114 # uncoverable condition right count:2 class:ignore_covered_err
115 62 # uncoverable condition right count:3 class:ignore_covered_err
116 62 if (o && *o && SvOK(*o)) cc = newSVsv(*o);
117 else {
118 o = hv_fetchs(opts, "max_age", 0);
119 # uncoverable condition right count:2 class:ignore_covered_err
120 62 # uncoverable condition right count:3 class:ignore_covered_err
121 62 ▶ if (o && *o && SvOK(*o)) {
Branch: 2 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) { 62 0 62
if (o && *o && SvOK(*o)) { 0 62 62
122 62 cc = newSVpvs("public, max-age=");
123 sv_catpvf(cc, "%" IVdf, (IV)SvIV(*o));
124 62 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 59 3 62
125 }
126 }
127 else
128 dev = (getenv("PUNK_ENV")
129 62 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 5 57 62
130 # uncoverable condition left count:2 class:ignore_covered_err
131 && strEQ(getenv("PUNK_ENV"), "development")) ? 1 : 0;
132 62 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
5 57 62
133 cap = newAV();
134 62 ▶ av_extend(cap, PSC_DOT);
Branch: 1 branch
BranchTrueFalseTotal
av_extend(cap, PSC_DOT); 1 61 62
135 62 ▶ { /* keep the directory without a trailing slash, so joining a
Branch: 1 branch
BranchTrueFalseTotal
{ /* keep the directory without a trailing slash, so joining a 56 6 62
136 62 ▶ * PATH_INFO (which always starts with one) cannot double it */
Branch: 1 branch
BranchTrueFalseTotal
* PATH_INFO (which always starts with one) cannot double it */ 8 54 62
137 62 ▶ STRLEN dlen;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN dlen; 2 60 62
138 62 const char *p = SvPV_const(dir, dlen);
139 # uncoverable branch true class:ignore_covered_err
140 # uncoverable condition left count:1 class:ignore_covered_err
141 # uncoverable condition right count:1 class:ignore_covered_err
142 # uncoverable condition left count:2 class:ignore_covered_err
143 while (dlen > 1 && p[dlen - 1] == '/') dlen--;
144 (void)av_store(cap, PSC_DIR, newSVpvn(p, dlen));
145 }
146 (void)av_store(cap, PSC_CC, cc ? cc : newSV(0));
147 /* A year and `immutable`, the only lifetime a content-addressed URL
148 * wants: it is the largest value RFC 9111 asks caches to honour, and
149 * `immutable` stops a reload revalidating a URL that cannot have
150 * changed. It is sent only after the digest has been checked. */
151 (void)av_store(cap, PSC_CC_IMM,
152 fingerprint ? newSVpvs("public, max-age=31536000, immutable")
153 : newSV(0));
154 (void)av_store(cap, PSC_CACHE,
155 fingerprint ? newRV_noinc((SV *)newHV()) : newSV(0));
156 (void)av_store(cap, PSC_DEV, dev ? newSViv(1) : newSV(0));
157 (void)av_store(cap, PSC_INDEX, index ? index : newSV(0));
158 (void)av_store(cap, PSC_LIST, list ? newSViv(1) : newSV(0));
159 (void)av_store(cap, PSC_DOT, dotfiles ? newSViv(1) : newSV(0));
160 RETVAL = punk_closure(aTHX_ punk_static_cb, cap);
161 }
162 OUTPUT:
163 RETVAL
164  
165 # The digest a fingerprinted URL for $url would carry, or undef when the
166 # mount does not fingerprint or the file cannot be read. $c->asset is the
167 # way to reach this; it is here so the mount's own cache answers.
168 SV *
169 _asset(app, url)
170 SV *app
171 SV *url
172 CODE:
173 {
174 4 RETVAL = pa_asset_url(aTHX_ app, url);
175 4 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 4 4
176 # uncoverable condition left count:1 class:ignore_covered_err
177 if (!RETVAL) RETVAL = newSVsv(url);
178 }
179 OUTPUT:
180 RETVAL
« include/punk/punk_names.h ↑ Summary xs/app.xs »