include/punk/punk_favicon.h

filter: Statementstmt 95.9% 71 / 74 Branchbran 65.6% 37 / 64 (7 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.8% 108 / 138 (7 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cachefile.h ↑ Summary xs/reqid.xs »
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 804 static void pfav_freeze(pTHX_ HV *h) {
40 804 SV *path = app_get(aTHX_ h, "favicon_path");
41 804 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 804 ▶ if (!(path && SvOK(path))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(path && SvOK(path))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 7 797 804
if (!(path && SvOK(path))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 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 /*
57 # uncoverable branch true
58 # uncoverable condition left count:1 class:ignore_covered_err */
59 12 ▶ if (got < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { 0 12 12
60 0 SvREFCNT_dec(body); /* # uncoverable statement */
61 0 PerlIO_close(fp); /* # uncoverable statement */
62 0 croak("Punk: favicon '%s' cannot be read: %s", pp, /* # uncoverable statement */
63 Strerror(errno)); /* # uncoverable statement */
64 }
65 12 ▶ if (got == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got == 0) break; 6 6 12
66 6 sv_catpvn(body, buf, (STRLEN)got);
67 }
68 6 PerlIO_close(fp);
69  
70 6 (void)hv_stores(h, "favicon_etag", pfav_tag(aTHX_ body));
71 6 (void)hv_stores(h, "favicon_body", body);
72 6 (void)hv_stores(h, "favicon_type",
73 newSVpv(ps_content_type(pp, pl), 0));
74 6 cc = newSVpvs("public, max-age=");
75 6 ▶ sv_catpvf(cc, "%" IVdf,
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvf(cc, "%" IVdf, 2 4 6
sv_catpvf(cc, "%" IVdf, 2 0 2
76 (ma && SvOK(ma)) ? SvIV(ma) : (IV)86400);
77 6 (void)hv_stores(h, "favicon_cc", cc);
78 }
79  
80 /* Serve. cap = [app]; the frozen values live on the app hash, so a request
81 * is one closure call and a handful of fetches.
82 *
83 * The 304 follows punk_cget.h's rule that a 304 is defined by what is
84 * ABSENT: the validator and the freshness lifetime go back, the headers
85 * describing a representation that is not being sent do not - above all
86 * Content-Length, which is how a client is taught to wait for a body that
87 * never arrives. If-None-Match parsing is psf_not_modified's, the same
88 * matcher send_file uses, so a list or a W/-prefixed tag behaves here
89 * exactly as it does there. */
90 XS_INTERNAL(pfav_serve_cb);
91 7 XS_INTERNAL(pfav_serve_cb) {
92 7 dXSARGS;
93 7 AV *cap = punk_clos_cap(aTHX_ cv);
94 7 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
95 7 ▶ SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
96 7 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
97 7 ▶ SV *body = h ? app_get(aTHX_ h, "favicon_body") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *body = h ? app_get(aTHX_ h, "favicon_body") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
98 7 ▶ SV *etag = h ? app_get(aTHX_ h, "favicon_etag") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *etag = h ? app_get(aTHX_ h, "favicon_etag") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
99 7 ▶ SV *type = h ? app_get(aTHX_ h, "favicon_type") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *type = h ? app_get(aTHX_ h, "favicon_type") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
100 7 ▶ SV *cc = h ? app_get(aTHX_ h, "favicon_cc") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *cc = h ? app_get(aTHX_ h, "favicon_cc") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
101 7 int matched = 0;
102 AV *resp, *headers, *bodyav;
103  
104 /*
105 # uncoverable branch true
106 # uncoverable condition left count:1 class:ignore_covered_err
107 # uncoverable condition left count:2 class:ignore_covered_err */
108 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
109  
110 /*
111 # uncoverable branch false
112 # uncoverable condition right count:1 class:ignore_covered_err
113 # uncoverable condition right count:2 class:ignore_covered_err
114 # uncoverable condition right count:3 class:ignore_covered_err */
115 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
116 7 AV *cav = pcx_av(aTHX_ c);
117 7 ▶ SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
118 /*
119 # uncoverable branch false
120 # uncoverable condition right count:1 class:ignore_covered_err
121 # uncoverable condition right count:2 class:ignore_covered_err
122 # uncoverable condition right count:3 class:ignore_covered_err */
123 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
124 STRLEN el;
125 7 const char *ep = SvPV_const(etag, el);
126 7 matched = psf_not_modified(aTHX_ (HV *)SvRV(envsv), ep, el, NULL);
127 }
128 }
129  
130 7 resp = newAV(); headers = newAV(); bodyav = newAV();
131 7 ▶ if (matched) {
Branch: 1 branch
BranchTrueFalseTotal
if (matched) { 1 6 7
132 1 av_push(headers, newSVpvs("ETag"));
133 1 av_push(headers, newSVsv(etag));
134 /*
135 # uncoverable branch false
136 # uncoverable condition right count:1 class:ignore_covered_err
137 # uncoverable condition right count:2 class:ignore_covered_err */
138 1 ▶ if (cc && SvOK(cc)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cc && SvOK(cc)) { 1 0 1
if (cc && SvOK(cc)) { 1 0 1
139 1 av_push(headers, newSVpvs("Cache-Control"));
140 1 av_push(headers, newSVsv(cc));
141 }
142 1 av_push(resp, newSViv(304));
143 }
144 else {
145 6 av_push(headers, newSVpvs("Content-Type"));
146 /*
147 # uncoverable condition right count:1 class:ignore_covered_err
148 # uncoverable condition right count:2 class:ignore_covered_err */
149 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
150 ? newSVsv(type)
151 : newSVpvs("application/octet-stream"));
152 6 av_push(headers, newSVpvs("Content-Length"));
153 6 av_push(headers, newSViv((IV)SvCUR(body)));
154 /*
155 # uncoverable branch false
156 # uncoverable condition right count:1 class:ignore_covered_err
157 # uncoverable condition right count:2 class:ignore_covered_err */
158 6 ▶ if (cc && SvOK(cc)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cc && SvOK(cc)) { 6 0 6
if (cc && SvOK(cc)) { 6 0 6
159 6 av_push(headers, newSVpvs("Cache-Control"));
160 6 av_push(headers, newSVsv(cc));
161 }
162 /*
163 # uncoverable branch false
164 # uncoverable condition right count:1 class:ignore_covered_err
165 # uncoverable condition right count:2 class:ignore_covered_err */
166 6 ▶ if (etag && SvOK(etag)) {
Branch: 2 branches
BranchTrueFalseTotal
if (etag && SvOK(etag)) { 6 0 6
if (etag && SvOK(etag)) { 6 0 6
167 6 av_push(headers, newSVpvs("ETag"));
168 6 av_push(headers, newSVsv(etag));
169 }
170 6 av_push(bodyav, newSVsv(body));
171 6 av_push(resp, newSViv(200));
172 }
173 7 av_push(resp, newRV_noinc((SV *)headers));
174 7 av_push(resp, newRV_noinc((SV *)bodyav));
175 7 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
176 7 XSRETURN(1);
177 }
178  
179 #endif /* PUNK_FAVICON_H */
« include/punk/punk_cachefile.h ↑ Summary xs/reqid.xs »