include/punk/punk_favicon.h

filter: Statementstmt 95.9% 71 / 74 Branchbran 57.8% 37 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.2% 108 / 138 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_config.h ↑ Summary include/punk/punk_future.h »
1 /* punk_favicon.h - GET /favicon.ico, from bytes frozen at to_app.
2 *
3 * favicon 'root/static/favicon.ico';
4 *
5 * A browser and Google's favicon crawler both request /favicon.ico at the
6 * site ROOT, where a /static mount does not answer - so every application
7 * ends up hand-rolling the same root route: send_file, a Cache-Control, and
8 * sitemap => 0. This is that route, once.
9 *
10 * The file is slurped at to_app and served from memory: an icon is a few
11 * kilobytes fetched constantly, and a missing file should croak at boot
12 * rather than 404 for as long as nobody notices. `punk dev` restarts on
13 * change, so a replaced icon is picked up by the next boot for free.
14 */
15  
16 #ifndef PUNK_FAVICON_H
17 #define PUNK_FAVICON_H
18  
19 /* A STRONG tag, unlike pcg_body_tag's weak one: the body is one frozen
20 * buffer, so byte-equality is exactly what the tag promises, and it is
21 * content-derived so every worker in a pool agrees without coordination. */
22 6 static SV *pfav_tag(pTHX_ SV *body) {
23 unsigned char sum[32];
24 char hex[PA_DIGEST_LEN + 1];
25 STRLEN bl;
26 6 const char *bp = SvPV_const(body, bl);
27 SV *out;
28 6 pk_sha256((const unsigned char *)bp, (size_t)bl, sum);
29 6 pa_hex(sum, PA_DIGEST_LEN / 2, hex);
30 6 out = newSVpvs("\"");
31 6 sv_catpvn(out, hex, PA_DIGEST_LEN);
32 6 sv_catpvs(out, "\"");
33 6 return out;
34 }
35  
36 /* Slurp and freeze, at to_app. The keyword stored only the path; everything
37 * a request needs - bytes, type, tag, Cache-Control - is derived here, once,
38 * so the serve closure is a write of things that already exist. */
39 610 static void pfav_freeze(pTHX_ HV *h) {
40 610 SV *path = app_get(aTHX_ h, "favicon_path");
41 610 SV *ma = app_get(aTHX_ h, "favicon_max_age");
42 STRLEN pl;
43 const char *pp;
44 PerlIO *fp;
45 SV *body, *cc;
46 char buf[8192];
47  
48 610 ▶ if (!(path && SvOK(path))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(path && SvOK(path))) return; 7 603 610
if (!(path && SvOK(path))) return; 0 7 7
49 7 pp = SvPV_const(path, pl);
50 7 fp = PerlIO_open(pp, "rb");
51 7 ▶ if (!fp)
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) 1 6 7
52 1 croak("Punk: favicon '%s' cannot be read: %s", pp, Strerror(errno));
53 6 body = newSVpvs("");
54 6 for (;;) {
55 12 SSize_t got = PerlIO_read(fp, buf, sizeof buf);
56 12 ▶ if (got < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { 0 12 12
57 0 SvREFCNT_dec(body);
58 0 PerlIO_close(fp);
59 0 croak("Punk: favicon '%s' cannot be read: %s", pp,
60 Strerror(errno));
61 }
62 12 ▶ if (got == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got == 0) break; 6 6 12
63 6 sv_catpvn(body, buf, (STRLEN)got);
64 }
65 6 PerlIO_close(fp);
66  
67 6 (void)hv_stores(h, "favicon_etag", pfav_tag(aTHX_ body));
68 6 (void)hv_stores(h, "favicon_body", body);
69 6 (void)hv_stores(h, "favicon_type",
70 newSVpv(ps_content_type(pp, pl), 0));
71 6 cc = newSVpvs("public, max-age=");
72 6 ▶ sv_catpvf(cc, "%" IVdf,
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(cc, "%" IVdf, 2 4 6
sv_catpvf(cc, "%" IVdf, 2 0 2
73 (ma && SvOK(ma)) ? SvIV(ma) : (IV)86400);
74 6 (void)hv_stores(h, "favicon_cc", cc);
75 }
76  
77 /* Serve. cap = [app]; the frozen values live on the app hash, so a request
78 * is one closure call and a handful of fetches.
79 *
80 * The 304 follows punk_cget.h's rule that a 304 is defined by what is
81 * ABSENT: the validator and the freshness lifetime go back, the headers
82 * describing a representation that is not being sent do not - above all
83 * Content-Length, which is how a client is taught to wait for a body that
84 * never arrives. If-None-Match parsing is psf_not_modified's, the same
85 * matcher send_file uses, so a list or a W/-prefixed tag behaves here
86 * exactly as it does there. */
87 XS_INTERNAL(pfav_serve_cb);
88 7 XS_INTERNAL(pfav_serve_cb) {
89 7 dXSARGS;
90 7 AV *cap = punk_clos_cap(aTHX_ cv);
91 7 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; 7 0 7
92 7 ▶ SV *c = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; 7 0 7
93 7 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; 7 0 7
94 7 ▶ SV *body = h ? app_get(aTHX_ h, "favicon_body") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *body = h ? app_get(aTHX_ h, "favicon_body") : NULL; 7 0 7
95 7 ▶ SV *etag = h ? app_get(aTHX_ h, "favicon_etag") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *etag = h ? app_get(aTHX_ h, "favicon_etag") : NULL; 7 0 7
96 7 ▶ SV *type = h ? app_get(aTHX_ h, "favicon_type") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *type = h ? app_get(aTHX_ h, "favicon_type") : NULL; 7 0 7
97 7 ▶ SV *cc = h ? app_get(aTHX_ h, "favicon_cc") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *cc = h ? app_get(aTHX_ h, "favicon_cc") : NULL; 7 0 7
98 7 int matched = 0;
99 AV *resp, *headers, *bodyav;
100  
101 7 ▶ if (!(body && SvOK(body))) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(body && SvOK(body))) XSRETURN_EMPTY; 7 0 7
if (!(body && SvOK(body))) XSRETURN_EMPTY; 0 7 7
102  
103 7 ▶ if (c && etag && SvOK(etag)) {
Branch: 3 branches
BranchTrueFalseTotal
if (c && etag && SvOK(etag)) { 7 0 7
if (c && etag && SvOK(etag)) { 7 0 7
if (c && etag && SvOK(etag)) { 7 0 7
104 7 AV *cav = pcx_av(aTHX_ c);
105 7 ▶ SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL; 7 0 7
106 7 ▶ if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 7 0 7
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 7 0 7
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 7 0 7
107 STRLEN el;
108 7 const char *ep = SvPV_const(etag, el);
109 7 matched = psf_not_modified(aTHX_ (HV *)SvRV(envsv), ep, el, NULL);
110 }
111 }
112  
113 7 resp = newAV(); headers = newAV(); bodyav = newAV();
114 7 ▶ if (matched) {
Branch: 1 branch
BranchTrueFalseTotal
if (matched) { 1 6 7
115 1 av_push(headers, newSVpvs("ETag"));
116 1 av_push(headers, newSVsv(etag));
117 1 ▶ if (cc && SvOK(cc)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cc && SvOK(cc)) { 1 0 1
if (cc && SvOK(cc)) { 1 0 1
118 1 av_push(headers, newSVpvs("Cache-Control"));
119 1 av_push(headers, newSVsv(cc));
120 }
121 1 av_push(resp, newSViv(304));
122 }
123 else {
124 6 av_push(headers, newSVpvs("Content-Type"));
125 6 ▶ av_push(headers, (type && SvOK(type))
Branch: 2 branches
BranchTrueFalseTotal
av_push(headers, (type && SvOK(type)) 6 0 6
av_push(headers, (type && SvOK(type)) 6 0 6
126 ? newSVsv(type)
127 : newSVpvs("application/octet-stream"));
128 6 av_push(headers, newSVpvs("Content-Length"));
129 6 av_push(headers, newSViv((IV)SvCUR(body)));
130 6 ▶ if (cc && SvOK(cc)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cc && SvOK(cc)) { 6 0 6
if (cc && SvOK(cc)) { 6 0 6
131 6 av_push(headers, newSVpvs("Cache-Control"));
132 6 av_push(headers, newSVsv(cc));
133 }
134 6 ▶ if (etag && SvOK(etag)) {
Branch: 2 branches
BranchTrueFalseTotal
if (etag && SvOK(etag)) { 6 0 6
if (etag && SvOK(etag)) { 6 0 6
135 6 av_push(headers, newSVpvs("ETag"));
136 6 av_push(headers, newSVsv(etag));
137 }
138 6 av_push(bodyav, newSVsv(body));
139 6 av_push(resp, newSViv(200));
140 }
141 7 av_push(resp, newRV_noinc((SV *)headers));
142 7 av_push(resp, newRV_noinc((SV *)bodyav));
143 7 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
144 7 XSRETURN(1);
145 }
146  
147 #endif /* PUNK_FAVICON_H */
« include/punk/punk_config.h ↑ Summary include/punk/punk_future.h »