include/punk/punk_asset.h

filter: Statementstmt 96.6% 87 / 90 Branchbran 81.9% 48 / 72 (14 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.1% 135 / 162 (14 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_multipart.h ↑ Summary include/punk/punk_plural.h »
1 /* punk_asset.h - content-addressed URLs for static files.
2 *
3 * A validator (ETag, Last-Modified) makes a stale copy cheap to detect; it
4 * does not make it unnecessary to ask. With no freshness lifetime a browser
5 * must revalidate every asset on every page load - correct, small on the
6 * wire, and still a round trip each. The lifetime that would remove the
7 * round trip cannot safely be given to `/static/app.css`, because that URL
8 * means something different after every deploy.
9 *
10 * So the URL changes with the bytes: `/static/app.9f3a1c2b0d4e5f60.css` is
11 * served from `root/static/app.css`, and gets a year and `immutable` -
12 * which is true of that URL, whatever happens to the file. A URL whose
13 * digest does NOT match what the file now holds (a page cached from an
14 * older deploy) still serves the current bytes, just with the ordinary
15 * revalidating headers. `immutable` is never sent for a URL that could
16 * mean something else tomorrow.
17 *
18 * The digest is the first 8 bytes of SHA-256 over the file's contents, hex.
19 * Not the mtime and size the ETag is built from: mtime differs per machine
20 * and per deploy, so a fleet would serve a different URL per box for
21 * identical bytes - every deploy a cold cache, and HTML from one box naming
22 * an asset URL another box has never heard of. The bytes are the thing that
23 * changed, so the bytes are what the URL is keyed on.
24 *
25 * Include after punk_session.h (the SHA-256 core).
26 */
27  
28 #ifndef PUNK_ASSET_H
29 #define PUNK_ASSET_H
30  
31 /* 8 bytes of SHA-256, hex. Long enough that two versions of one file
32 * colliding is not a thing that happens; short enough to read in a log. */
33 #define PA_DIGEST_LEN 16
34  
35 /* Reading the whole file to hash it is not an option: a static directory may
36 * hold a video. 64KB at a time, and the compression loop is punk_session.h's
37 * so there is only ever one of it. */
38 #define PA_HASH_CHUNK 65536
39  
40 9 static int pa_sha256_file(pTHX_ const char *path, unsigned char out[32]) {
41 9 PerlIO *fp = PerlIO_open(path, "rb");
42 unsigned char *buf;
43 U32 h[8];
44 9 size_t total = 0, rem = 0;
45 9 int ok = 1;
46  
47 /*
48 # uncoverable branch true
49 # uncoverable condition left count:1 class:ignore_covered_err */
50 9 ▶ if (!fp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return 0; 0 9 9
51 9 Newx(buf, PA_HASH_CHUNK + 64, unsigned char);
52 9 memcpy(h, ps_sha256_iv, sizeof h);
53 9 for (;;) {
54 18 SSize_t got = PerlIO_read(fp, buf + rem, PA_HASH_CHUNK);
55 size_t have, whole;
56 /*
57 # uncoverable branch true
58 # uncoverable condition left count:1 class:ignore_covered_err */
59 18 ▶ if (got < 0) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { ok = 0; break; } 0 18 18
60 18 ▶ if (got == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got == 0) break; 9 9 18
61 9 have = rem + (size_t)got;
62 9 total += (size_t)got;
63 9 whole = have / 64;
64 9 ▶ if (whole) ps_sha256_blocks(h, buf, whole);
Branch: 1 branch
BranchTrueFalseTotal
if (whole) ps_sha256_blocks(h, buf, whole); 1 8 9
65 9 rem = have - whole * 64;
66 /*
67 # uncoverable branch false
68 # uncoverable condition right count:1 class:ignore_covered_err */
69 9 ▶ if (rem) memmove(buf, buf + whole * 64, rem);
Branch: 1 branch
BranchTrueFalseTotal
if (rem) memmove(buf, buf + whole * 64, rem); 9 0 9
70 }
71 9 PerlIO_close(fp);
72 /*
73 # uncoverable branch false
74 # uncoverable condition right count:1 class:ignore_covered_err */
75 9 ▶ if (ok) ps_sha256_final(h, buf, rem, total, out);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) ps_sha256_final(h, buf, rem, total, out); 9 0 9
76 9 Safefree(buf);
77 9 return ok;
78 }
79  
80 154 static void pa_hex(const unsigned char *in, int nbytes, char *out) {
81 static const char hex[] = "0123456789abcdef";
82 int i;
83 2218 ▶ for (i = 0; i < nbytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nbytes; i++) { 2064 154 2218
84 2064 out[2*i] = hex[(in[i] >> 4) & 0xF];
85 2064 out[2*i + 1] = hex[in[i] & 0xF];
86 }
87 154 out[2*nbytes] = '\0';
88 154 }
89  
90 /* The file's digest into out[PA_DIGEST_LEN + 1]; 0 when it cannot be read. */
91 9 static int pa_digest_file(pTHX_ const char *path, char *out) {
92 unsigned char sum[32];
93 /*
94 # uncoverable branch true
95 # uncoverable condition left count:1 class:ignore_covered_err */
96 9 ▶ if (!pa_sha256_file(aTHX_ path, sum)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pa_sha256_file(aTHX_ path, sum)) return 0; 0 9 9
97 9 pa_hex(sum, PA_DIGEST_LEN / 2, out);
98 9 return 1;
99 }
100  
101 /* ---- the digest cache -------------------------------------------------------
102 * path => [ digest, mtime, size ], one per mount, filled on demand.
103 *
104 * In production a digest is computed once and then believed: the files are
105 * not going to change under a running process, and re-reading them to find
106 * that out would be the whole cost of the feature paid on every page. In
107 * development the entry is checked against the file's mtime and size, so
108 * editing a stylesheet and hitting reload shows the edit. */
109  
110 enum { PA_CE_DIGEST = 0, PA_CE_MTIME = 1, PA_CE_SIZE = 2 };
111  
112 /* The cached digest for `path` (a borrowed SV), or NULL when the file is
113 * gone or unreadable. */
114 17 static SV *pa_digest_cached(pTHX_ HV *cache, const char *path, STRLEN plen,
115 int dev) {
116 17 SV **slot = hv_fetch(cache, path, (I32)plen, 0);
117 17 AV *ent = NULL;
118 Stat_t st;
119 char digest[PA_DIGEST_LEN + 1];
120 17 int have_stat = 0;
121  
122 /*
123 # uncoverable condition right count:2 class:ignore_covered_err
124 # uncoverable condition right count:3 class:ignore_covered_err
125 # uncoverable condition right count:4 class:ignore_covered_err */
126 17 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 7 10 17
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 7 0 7
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 7 0 7
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 7 0 7
127 7 ent = (AV *)SvRV(*slot);
128 7 ▶ if (!dev) return *av_fetch(ent, PA_CE_DIGEST, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!dev) return *av_fetch(ent, PA_CE_DIGEST, 0); 6 1 7
129 /*
130 # uncoverable branch true
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition left count:2 class:ignore_covered_err */
133 1 ▶ if (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode)) return NULL; 1 0 1
if (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode)) return NULL; 0 1 1
134 1 have_stat = 1;
135 /*
136 # uncoverable branch true
137 # uncoverable condition left count:1 class:ignore_covered_err
138 # uncoverable condition left count:2 class:ignore_covered_err
139 # uncoverable condition right count:2 class:ignore_covered_err */
140 1 ▶ if ((UV)st.st_mtime == SvUV(*av_fetch(ent, PA_CE_MTIME, 0))
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)st.st_mtime == SvUV(*av_fetch(ent, PA_CE_MTIME, 0)) 0 1 1
141 0 ▶ && (UV)st.st_size == SvUV(*av_fetch(ent, PA_CE_SIZE, 0))) /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)st.st_size == SvUV(*av_fetch(ent, PA_CE_SIZE, 0))) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
142 0 return *av_fetch(ent, PA_CE_DIGEST, 0); /* # uncoverable statement */
143 }
144  
145 11 ▶ if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 10 1 11
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 8 2 10
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 8 8
146 2 return NULL;
147 /*
148 # uncoverable branch true
149 # uncoverable condition left count:1 class:ignore_covered_err */
150 9 ▶ if (!pa_digest_file(aTHX_ path, digest)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pa_digest_file(aTHX_ path, digest)) return NULL; 0 9 9
151  
152 9 ▶ if (ent) {
Branch: 1 branch
BranchTrueFalseTotal
if (ent) { 1 8 9
153 1 sv_setpvn(*av_fetch(ent, PA_CE_DIGEST, 0), digest, PA_DIGEST_LEN);
154 1 sv_setuv(*av_fetch(ent, PA_CE_MTIME, 0), (UV)st.st_mtime);
155 1 sv_setuv(*av_fetch(ent, PA_CE_SIZE, 0), (UV)st.st_size);
156 1 return *av_fetch(ent, PA_CE_DIGEST, 0);
157 }
158 8 ent = newAV();
159 8 av_extend(ent, 2);
160 8 av_push(ent, newSVpvn(digest, PA_DIGEST_LEN));
161 8 av_push(ent, newSVuv((UV)st.st_mtime));
162 8 av_push(ent, newSVuv((UV)st.st_size));
163 8 (void)hv_store(cache, path, (I32)plen, newRV_noinc((SV *)ent), 0);
164 8 return *av_fetch(ent, PA_CE_DIGEST, 0);
165 }
166  
167 /* ---- the URL shape ----------------------------------------------------------
168 * `<name>.<16 hex>.<ext>`, and only that: the digest goes before the final
169 * extension so the file keeps its type to everything downstream that reads
170 * the URL - a CDN, a log, a Content-Type guess of our own. A path with no
171 * extension has nowhere to put the digest and is left alone; it serves with
172 * the ordinary revalidating headers, which is what it did before.
173 */
174  
175 6 static int pa_is_digest(const char *p, STRLEN n) {
176 STRLEN i;
177 /*
178 # uncoverable branch true
179 # uncoverable condition left count:1 class:ignore_covered_err */
180 6 ▶ if (n != PA_DIGEST_LEN) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != PA_DIGEST_LEN) return 0; 0 6 6
181 102 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 96 6 102
182 96 char c = p[i];
183 /*
184 # uncoverable branch true class:ignore_covered_err
185 # uncoverable condition left count:1 class:ignore_covered_err
186 # uncoverable condition left count:2 class:ignore_covered_err
187 # uncoverable condition left count:3 class:ignore_covered_err
188 # uncoverable condition left count:4 class:ignore_covered_err */
189 96 ▶ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0; 96 0 96
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0; 34 62 96
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0; 34 0 34
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0; 0 34 34
190 }
191 6 return 1;
192 }
193  
194 /* The offset of the final '.' in the last path segment, or -1. */
195 23 static SSize_t pa_ext_dot(const char *p, STRLEN len) {
196 STRLEN i;
197 /*
198 # uncoverable branch false
199 # uncoverable condition right count:1 class:ignore_covered_err */
200 175 ▶ for (i = len; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i > 0; i--) { 175 0 175
201 175 ▶ if (p[i - 1] == '/' || p[i - 1] == '\\') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (p[i - 1] == '/' || p[i - 1] == '\\') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 173 2 175
if (p[i - 1] == '/' || p[i - 1] == '\\') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 173 173
202 173 ▶ if (p[i - 1] == '.') return (SSize_t)(i - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (p[i - 1] == '.') return (SSize_t)(i - 1); 21 152 173
203 }
204 0 return -1; /* # uncoverable statement */
205 }
206  
207 /* Strip a fingerprint: "app.9f3a....css" -> "app.css" in `out` (which must
208 * hold `len` bytes plus a NUL), with the digest copied to `digest`. Returns
209 * the length of the stripped path, or 0 when there was no fingerprint. */
210 6 static STRLEN pa_defingerprint(const char *p, STRLEN len, char *out,
211 char *digest) {
212 6 SSize_t ext = pa_ext_dot(p, len);
213 SSize_t dot;
214 STRLEN dlen;
215 /*
216 # uncoverable branch true
217 # uncoverable condition left count:1 class:ignore_covered_err */
218 6 ▶ if (ext <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ext <= 0) return 0; 0 6 6
219 6 dot = pa_ext_dot(p, (STRLEN)ext); /* the dot before the ext */
220 /*
221 # uncoverable branch true
222 # uncoverable condition left count:1 class:ignore_covered_err */
223 6 ▶ if (dot < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (dot < 0) return 0; 0 6 6
224 6 dlen = (STRLEN)(ext - dot - 1);
225 /*
226 # uncoverable branch true
227 # uncoverable condition left count:1 class:ignore_covered_err */
228 6 ▶ if (!pa_is_digest(p + dot + 1, dlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pa_is_digest(p + dot + 1, dlen)) return 0; 0 6 6
229 6 memcpy(digest, p + dot + 1, PA_DIGEST_LEN);
230 6 digest[PA_DIGEST_LEN] = '\0';
231 6 memcpy(out, p, (STRLEN)dot);
232 6 memcpy(out + dot, p + ext, len - (STRLEN)ext);
233 6 out[len - dlen - 1] = '\0';
234 6 return len - dlen - 1;
235 }
236  
237 /* The other direction: "/static/app.css" + digest -> a new SV holding
238 * "/static/app.<digest>.css". NULL when the path has no extension to sit
239 * in front of. */
240 11 static SV *pa_fingerprint(pTHX_ const char *p, STRLEN len, const char *digest) {
241 11 SSize_t ext = pa_ext_dot(p, len);
242 SV *out;
243 11 ▶ if (ext <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ext <= 0) return NULL; 2 9 11
244 9 out = newSVpvn(p, (STRLEN)ext);
245 9 sv_catpvs(out, ".");
246 9 sv_catpvn(out, digest, PA_DIGEST_LEN);
247 9 sv_catpvn(out, p + ext, len - (STRLEN)ext);
248 9 return out;
249 }
250  
251 #endif /* PUNK_ASSET_H */
« include/punk/punk_multipart.h ↑ Summary include/punk/punk_plural.h »