include/punk/punk_asset.h

filter: Statementstmt 96.6% 87 / 90 Branchbran 66.6% 48 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 135 / 162 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cookie.h ↑ Summary xs/mrows.xs »
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 9 ▶ if (!fp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return 0; 0 9 9
48 9 Newx(buf, PA_HASH_CHUNK + 64, unsigned char);
49 9 memcpy(h, ps_sha256_iv, sizeof h);
50 9 for (;;) {
51 18 SSize_t got = PerlIO_read(fp, buf + rem, PA_HASH_CHUNK);
52 size_t have, whole;
53 18 ▶ if (got < 0) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { ok = 0; break; } 0 18 18
54 18 ▶ if (got == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got == 0) break; 9 9 18
55 9 have = rem + (size_t)got;
56 9 total += (size_t)got;
57 9 whole = have / 64;
58 9 ▶ if (whole) ps_sha256_blocks(h, buf, whole);
Branch: 1 branch
BranchTrueFalseTotal
if (whole) ps_sha256_blocks(h, buf, whole); 1 8 9
59 9 rem = have - whole * 64;
60 9 ▶ if (rem) memmove(buf, buf + whole * 64, rem);
Branch: 1 branch
BranchTrueFalseTotal
if (rem) memmove(buf, buf + whole * 64, rem); 9 0 9
61 }
62 9 PerlIO_close(fp);
63 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
64 9 Safefree(buf);
65 9 return ok;
66 }
67  
68 142 static void pa_hex(const unsigned char *in, int nbytes, char *out) {
69 static const char hex[] = "0123456789abcdef";
70 int i;
71 2014 ▶ for (i = 0; i < nbytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nbytes; i++) { 1872 142 2014
72 1872 out[2*i] = hex[(in[i] >> 4) & 0xF];
73 1872 out[2*i + 1] = hex[in[i] & 0xF];
74 }
75 142 out[2*nbytes] = '\0';
76 142 }
77  
78 /* The file's digest into out[PA_DIGEST_LEN + 1]; 0 when it cannot be read. */
79 9 static int pa_digest_file(pTHX_ const char *path, char *out) {
80 unsigned char sum[32];
81 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
82 9 pa_hex(sum, PA_DIGEST_LEN / 2, out);
83 9 return 1;
84 }
85  
86 /* ---- the digest cache -------------------------------------------------------
87 * path => [ digest, mtime, size ], one per mount, filled on demand.
88 *
89 * In production a digest is computed once and then believed: the files are
90 * not going to change under a running process, and re-reading them to find
91 * that out would be the whole cost of the feature paid on every page. In
92 * development the entry is checked against the file's mtime and size, so
93 * editing a stylesheet and hitting reload shows the edit. */
94  
95 enum { PA_CE_DIGEST = 0, PA_CE_MTIME = 1, PA_CE_SIZE = 2 };
96  
97 /* The cached digest for `path` (a borrowed SV), or NULL when the file is
98 * gone or unreadable. */
99 17 static SV *pa_digest_cached(pTHX_ HV *cache, const char *path, STRLEN plen,
100 int dev) {
101 17 SV **slot = hv_fetch(cache, path, (I32)plen, 0);
102 17 AV *ent = NULL;
103 Stat_t st;
104 char digest[PA_DIGEST_LEN + 1];
105 17 int have_stat = 0;
106  
107 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
108 7 ent = (AV *)SvRV(*slot);
109 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
110 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
111 1 have_stat = 1;
112 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
113 0 ▶ && (UV)st.st_size == SvUV(*av_fetch(ent, PA_CE_SIZE, 0)))
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)st.st_size == SvUV(*av_fetch(ent, PA_CE_SIZE, 0))) 0 0 0
114 0 return *av_fetch(ent, PA_CE_DIGEST, 0);
115 }
116  
117 11 ▶ if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode)))
Branch: 3 branches
BranchTrueFalseTotal
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) 10 1 11
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) 8 2 10
if (!have_stat && (PerlLIO_stat(path, &st) < 0 || !S_ISREG(st.st_mode))) 0 8 8
118 2 return NULL;
119 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
120  
121 9 ▶ if (ent) {
Branch: 1 branch
BranchTrueFalseTotal
if (ent) { 1 8 9
122 1 sv_setpvn(*av_fetch(ent, PA_CE_DIGEST, 0), digest, PA_DIGEST_LEN);
123 1 sv_setuv(*av_fetch(ent, PA_CE_MTIME, 0), (UV)st.st_mtime);
124 1 sv_setuv(*av_fetch(ent, PA_CE_SIZE, 0), (UV)st.st_size);
125 1 return *av_fetch(ent, PA_CE_DIGEST, 0);
126 }
127 8 ent = newAV();
128 8 av_extend(ent, 2);
129 8 av_push(ent, newSVpvn(digest, PA_DIGEST_LEN));
130 8 av_push(ent, newSVuv((UV)st.st_mtime));
131 8 av_push(ent, newSVuv((UV)st.st_size));
132 8 (void)hv_store(cache, path, (I32)plen, newRV_noinc((SV *)ent), 0);
133 8 return *av_fetch(ent, PA_CE_DIGEST, 0);
134 }
135  
136 /* ---- the URL shape ----------------------------------------------------------
137 * `<name>.<16 hex>.<ext>`, and only that: the digest goes before the final
138 * extension so the file keeps its type to everything downstream that reads
139 * the URL - a CDN, a log, a Content-Type guess of our own. A path with no
140 * extension has nowhere to put the digest and is left alone; it serves with
141 * the ordinary revalidating headers, which is what it did before.
142 */
143  
144 6 static int pa_is_digest(const char *p, STRLEN n) {
145 STRLEN i;
146 6 ▶ if (n != PA_DIGEST_LEN) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != PA_DIGEST_LEN) return 0; 0 6 6
147 102 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 96 6 102
148 96 char c = p[i];
149 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
150 }
151 6 return 1;
152 }
153  
154 /* The offset of the final '.' in the last path segment, or -1. */
155 23 static SSize_t pa_ext_dot(const char *p, STRLEN len) {
156 STRLEN i;
157 175 ▶ for (i = len; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i > 0; i--) { 175 0 175
158 175 ▶ if (p[i - 1] == '/' || p[i - 1] == '\\') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (p[i - 1] == '/' || p[i - 1] == '\\') return -1; 173 2 175
if (p[i - 1] == '/' || p[i - 1] == '\\') return -1; 0 173 173
159 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
160 }
161 0 return -1;
162 }
163  
164 /* Strip a fingerprint: "app.9f3a....css" -> "app.css" in `out` (which must
165 * hold `len` bytes plus a NUL), with the digest copied to `digest`. Returns
166 * the length of the stripped path, or 0 when there was no fingerprint. */
167 6 static STRLEN pa_defingerprint(const char *p, STRLEN len, char *out,
168 char *digest) {
169 6 SSize_t ext = pa_ext_dot(p, len);
170 SSize_t dot;
171 STRLEN dlen;
172 6 ▶ if (ext <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ext <= 0) return 0; 0 6 6
173 6 dot = pa_ext_dot(p, (STRLEN)ext); /* the dot before the ext */
174 6 ▶ if (dot < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (dot < 0) return 0; 0 6 6
175 6 dlen = (STRLEN)(ext - dot - 1);
176 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
177 6 memcpy(digest, p + dot + 1, PA_DIGEST_LEN);
178 6 digest[PA_DIGEST_LEN] = '\0';
179 6 memcpy(out, p, (STRLEN)dot);
180 6 memcpy(out + dot, p + ext, len - (STRLEN)ext);
181 6 out[len - dlen - 1] = '\0';
182 6 return len - dlen - 1;
183 }
184  
185 /* The other direction: "/static/app.css" + digest -> a new SV holding
186 * "/static/app.<digest>.css". NULL when the path has no extension to sit
187 * in front of. */
188 11 static SV *pa_fingerprint(pTHX_ const char *p, STRLEN len, const char *digest) {
189 11 SSize_t ext = pa_ext_dot(p, len);
190 SV *out;
191 11 ▶ if (ext <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ext <= 0) return NULL; 2 9 11
192 9 out = newSVpvn(p, (STRLEN)ext);
193 9 sv_catpvs(out, ".");
194 9 sv_catpvn(out, digest, PA_DIGEST_LEN);
195 9 sv_catpvn(out, p + ext, len - (STRLEN)ext);
196 9 return out;
197 }
198  
199 #endif /* PUNK_ASSET_H */
« include/punk/punk_cookie.h ↑ Summary xs/mrows.xs »