include/punk/punk_sendfile.h

filter: Statementstmt 97.7% 382 / 391 Branchbran 81.9% 398 / 510 (29 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.7% 780 / 901 (29 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/markdown.xs ↑ Summary include/punk/punk_proxy.h »
1 /* punk_sendfile.h - $c->send_file: downloads with validators and ranges.
2 *
3 * One decision core (psf_respond) turns a source - a file path or an
4 * in-memory SV - plus the request's validator and Range headers into a
5 * finished PSGI triplet: 200, 206 with Content-Range, 304, or 416. A 206
6 * from a file rides Punk::SendFile::Reader, a bounded getline/close body,
7 * so the range is byte-correct on any PSGI server without trusting it to
8 * stop at Content-Length on a filehandle; a full-file 200 stays a real
9 * glob, the Punk::Static convention. The path is used as given - the
10 * traversal guard belongs to whoever built it out of untrusted input.
11 */
12  
13 #ifndef PUNK_SENDFILE_H
14 #define PUNK_SENDFILE_H
15  
16 /* ---- Range: bytes=first-last / bytes=-suffix ------------------------------
17 * Single range only. RFC 9110 lets a server ignore any Range header, so a
18 * multi-range set, another unit or a malformed value is IGNORE (the full
19 * 200); a syntactically valid range past the end is UNSAT (416). An
20 * invalid spec (last < first) makes the header invalid: IGNORE, per the
21 * RFC's "MUST ignore", not 416. */
22  
23 enum {
24 PSF_RANGE_NONE = 0, /* no Range header */
25 PSF_RANGE_OK = 1, /* one satisfiable range in *off / *n */
26 PSF_RANGE_UNSAT = 2, /* valid but unsatisfiable: 416 */
27 PSF_RANGE_IGNORE = 3 /* malformed or multi-range: full 200 */
28 };
29  
30 /* 1*DIGIT into a UV; 1 ok, 0 no digits, -1 overflow. */
31 84 static int psf_digits(const char **pp, const char *end, UV *out) {
32 84 const char *p = *pp;
33 84 UV acc = 0;
34 84 int any = 0;
35 240 ▶ while (p < end && *p >= '0' && *p <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') { 204 36 240
while (p < end && *p >= '0' && *p <= '9') { 159 45 204
while (p < end && *p >= '0' && *p <= '9') { 157 2 159
36 157 UV d = (UV)(*p - '0');
37 157 ▶ if (acc > (UV_MAX - d) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (acc > (UV_MAX - d) / 10) return -1; 1 156 157
38 156 acc = acc * 10 + d;
39 156 any = 1;
40 156 p++;
41 }
42 83 ▶ if (!any) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!any) return 0; 3 80 83
43 80 *pp = p;
44 80 *out = acc;
45 80 return 1;
46 }
47  
48 55 static int psf_parse_range(const char *v, STRLEN len, UV size,
49 UV *off, UV *n) {
50 55 const char *p = v, *end = v + len;
51 static const char unit[5] = { 'b','y','t','e','s' };
52 55 UV first = 0, last = 0;
53 55 int have_last = 0, i, r;
54  
55 56 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 55 1 56
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 1 54 55
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 54 54
56 55 ▶ if (end - p < 6) return PSF_RANGE_IGNORE;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) return PSF_RANGE_IGNORE; 1 54 55
57 314 ▶ for (i = 0; i < 5; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) 262 52 314
58 262 ▶ if (toLOWER((U8)p[i]) != unit[i]) return PSF_RANGE_IGNORE;
Branch: 2 branches
BranchTrueFalseTotal
if (toLOWER((U8)p[i]) != unit[i]) return PSF_RANGE_IGNORE; 5 257 262
if (toLOWER((U8)p[i]) != unit[i]) return PSF_RANGE_IGNORE; 2 260 262
59 52 ▶ if (p[5] != '=') return PSF_RANGE_IGNORE;
Branch: 1 branch
BranchTrueFalseTotal
if (p[5] != '=') return PSF_RANGE_IGNORE; 1 51 52
60 51 p += 6;
61  
62 51 ▶ if (p < end && *p == '-') { /* suffix: -N */
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '-') { /* suffix: -N */ 50 1 51
if (p < end && *p == '-') { /* suffix: -N */ 8 42 50
63 8 p++;
64 8 ▶ if (psf_digits(&p, end, &first) != 1) return PSF_RANGE_IGNORE;
Branch: 1 branch
BranchTrueFalseTotal
if (psf_digits(&p, end, &first) != 1) return PSF_RANGE_IGNORE; 1 7 8
65 /*
66 # uncoverable branch true count:2
67 # uncoverable branch true class:ignore_covered_err
68 # uncoverable condition left count:1 class:ignore_covered_err
69 # uncoverable condition left count:2 class:ignore_covered_err
70 # uncoverable condition left count:3 class:ignore_covered_err */
71 7 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 1 6 7
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 1 1
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 1 1
72 7 ▶ if (p != end) return PSF_RANGE_IGNORE; /* comma: multi-range */
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) return PSF_RANGE_IGNORE; /* comma: multi-range */ 1 6 7
73 6 ▶ if (first == 0 || size == 0) return PSF_RANGE_UNSAT;
Branch: 2 branches
BranchTrueFalseTotal
if (first == 0 || size == 0) return PSF_RANGE_UNSAT; 5 1 6
if (first == 0 || size == 0) return PSF_RANGE_UNSAT; 1 4 5
74 4 ▶ if (first > size) first = size;
Branch: 1 branch
BranchTrueFalseTotal
if (first > size) first = size; 1 3 4
75 4 *off = size - first;
76 4 *n = first;
77 4 return PSF_RANGE_OK;
78 }
79  
80 43 r = psf_digits(&p, end, &first);
81 /*
82 # uncoverable condition left count:2 class:ignore_covered_err
83 # uncoverable condition left count:3 class:ignore_covered_err */
84 43 ▶ if (r != 1 || p >= end || *p != '-') return PSF_RANGE_IGNORE;
Branch: 3 branches
BranchTrueFalseTotal
if (r != 1 || p >= end || *p != '-') return PSF_RANGE_IGNORE; 40 3 43
if (r != 1 || p >= end || *p != '-') return PSF_RANGE_IGNORE; 40 0 40
if (r != 1 || p >= end || *p != '-') return PSF_RANGE_IGNORE; 0 40 40
85 40 p++;
86 /*
87 # uncoverable condition right count:2 class:ignore_covered_err
88 # uncoverable condition right count:3 class:ignore_covered_err */
89 40 ▶ if (p < end && *p >= '0' && *p <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && *p >= '0' && *p <= '9') { 33 7 40
if (p < end && *p >= '0' && *p <= '9') { 33 0 33
if (p < end && *p >= '0' && *p <= '9') { 33 0 33
90 /*
91 # uncoverable branch true
92 # uncoverable condition left count:1 class:ignore_covered_err */
93 33 ▶ if (psf_digits(&p, end, &last) != 1) return PSF_RANGE_IGNORE;
Branch: 1 branch
BranchTrueFalseTotal
if (psf_digits(&p, end, &last) != 1) return PSF_RANGE_IGNORE; 0 33 33
94 33 have_last = 1;
95 }
96 41 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 5 36 41
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 1 4 5
while (p < end && (*p == ' ' || *p == '\t')) p++; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 4 4
97 40 ▶ if (p != end) return PSF_RANGE_IGNORE; /* comma or junk */
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) return PSF_RANGE_IGNORE; /* comma or junk */ 4 36 40
98 36 ▶ if (have_last && last < first) return PSF_RANGE_IGNORE;
Branch: 2 branches
BranchTrueFalseTotal
if (have_last && last < first) return PSF_RANGE_IGNORE; 29 7 36
if (have_last && last < first) return PSF_RANGE_IGNORE; 1 28 29
99 35 ▶ if (first >= size) return PSF_RANGE_UNSAT; /* covers size == 0 */
Branch: 1 branch
BranchTrueFalseTotal
if (first >= size) return PSF_RANGE_UNSAT; /* covers size == 0 */ 5 30 35
100 30 ▶ if (!have_last || last > size - 1) last = size - 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!have_last || last > size - 1) last = size - 1; 27 3 30
if (!have_last || last > size - 1) last = size - 1; 1 26 27
101 30 *off = first;
102 30 *n = last - first + 1;
103 30 return PSF_RANGE_OK;
104 }
105  
106 /* ---- validators ------------------------------------------------------------
107 * The ETag is strong and nginx-shaped: "<hex mtime>-<hex size>", with the
108 * mtime's nanoseconds as a third field when the filesystem keeps them. Without
109 * that field a file rewritten in place to the same size inside one second
110 * keeps its tag, and If-None-Match answers 304 for bytes the client has never
111 * seen. */
112  
113 #define PSF_ETAG_MAX 64
114  
115 153 static void psf_etag(char *out, size_t outlen, UV mtime, UV ns, UV size) {
116 153 ▶ if (ns)
Branch: 1 branch
BranchTrueFalseTotal
if (ns) 129 24 153
117 129 my_snprintf(out, outlen, "\"%" UVxf "-%" UVxf "-%" UVxf "\"",
118 mtime, size, ns);
119 else
120 24 my_snprintf(out, outlen, "\"%" UVxf "-%" UVxf "\"", mtime, size);
121 153 }
122  
123 /* Is `etag` in a comma-separated entity-tag list (If-None-Match)? Weak
124 * comparison: a W/ prefix on a list entry is skipped. An entity-tag may
125 * legally contain a comma, but a split fragment of one can never carry
126 * both quotes, so it can never falsely equal our fully quoted tag. */
127 57 static int psf_etag_in(const char *h, STRLEN hlen,
128 const char *etag, STRLEN elen) {
129 57 const char *p = h, *end = h + hlen;
130 75 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 62 13 75
131 const char *e;
132 /*
133 # uncoverable condition right count:1 class:ignore_covered_err
134 # uncoverable condition left count:3 class:ignore_covered_err */
135 72 ▶ while (p < end && (*p == ' ' || *p == '\t' || *p == ',')) p++;
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t' || *p == ',')) p++; 72 0 72
while (p < end && (*p == ' ' || *p == '\t' || *p == ',')) p++; 5 67 72
while (p < end && (*p == ' ' || *p == '\t' || *p == ',')) p++; 0 67 67
while (p < end && (*p == ' ' || *p == '\t' || *p == ',')) p++; 5 62 67
136 /*
137 # uncoverable branch true
138 # uncoverable condition left count:1 class:ignore_covered_err */
139 62 ▶ if (p >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) break; 0 62 62
140 62 e = p;
141 896 ▶ while (e < end && *e != ',') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < end && *e != ',') e++; 839 57 896
while (e < end && *e != ',') e++; 834 5 839
142 {
143 62 const char *q = e;
144 /*
145 # uncoverable branch true count:2
146 # uncoverable branch true class:ignore_covered_err
147 # uncoverable condition left count:1 class:ignore_covered_err
148 # uncoverable condition right count:1 class:ignore_covered_err
149 # uncoverable condition left count:2 class:ignore_covered_err
150 # uncoverable condition left count:3 class:ignore_covered_err */
151 62 ▶ while (q > p && (q[-1] == ' ' || q[-1] == '\t')) q--;
Branch: 3 branches
BranchTrueFalseTotal
while (q > p && (q[-1] == ' ' || q[-1] == '\t')) q--; 62 0 62
while (q > p && (q[-1] == ' ' || q[-1] == '\t')) q--; 0 62 62
while (q > p && (q[-1] == ' ' || q[-1] == '\t')) q--; 0 62 62
152 62 ▶ if (q - p == 1 && *p == '*') return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (q - p == 1 && *p == '*') return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 60 62
if (q - p == 1 && *p == '*') return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
153 /*
154 # uncoverable condition right count:1 class:ignore_covered_err
155 # uncoverable condition right count:3 class:ignore_covered_err */
156 60 ▶ if (q - p > 2 && p[0] == 'W' && p[1] == '/') p += 2;
Branch: 3 branches
BranchTrueFalseTotal
if (q - p > 2 && p[0] == 'W' && p[1] == '/') p += 2; 60 0 60
if (q - p > 2 && p[0] == 'W' && p[1] == '/') p += 2; 13 47 60
if (q - p > 2 && p[0] == 'W' && p[1] == '/') p += 2; 13 0 13
157 /* weak comparison both ways: our own tag may be a weak one */
158 /*
159 # uncoverable condition right count:1 class:ignore_covered_err
160 # uncoverable condition right count:3 class:ignore_covered_err */
161 60 ▶ if (elen > 2 && etag[0] == 'W' && etag[1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (elen > 2 && etag[0] == 'W' && etag[1] == '/') { 60 0 60
if (elen > 2 && etag[0] == 'W' && etag[1] == '/') { 5 55 60
if (elen > 2 && etag[0] == 'W' && etag[1] == '/') { 5 0 5
162 5 ▶ if ((STRLEN)(q - p) == elen - 2
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)(q - p) == elen - 2 4 1 5
163 4 ▶ && memEQ(p, etag + 2, elen - 2)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(p, etag + 2, elen - 2)) return 1; 3 1 4
164 }
165 55 ▶ else if ((STRLEN)(q - p) == elen && memEQ(p, etag, elen)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
else if ((STRLEN)(q - p) == elen && memEQ(p, etag, elen)) return 1; 43 12 55
else if ((STRLEN)(q - p) == elen && memEQ(p, etag, elen)) return 1; 39 4 43
166 }
167 18 p = e;
168 }
169 13 return 0;
170 }
171  
172 /* If-None-Match wins over If-Modified-Since (RFC 9110 13.1.3); the date
173 * comparison is the ps_serve_file convention - an exact match is all we
174 * need, the date they were given is the date we would send. */
175 316 static int psf_not_modified(pTHX_ HV *env, const char *etag, STRLEN elen,
176 const char *date) {
177 316 SV **e = hv_fetchs(env, "HTTP_IF_NONE_MATCH", 0);
178 /*
179 # uncoverable condition right count:2 class:ignore_covered_err
180 # uncoverable condition right count:3 class:ignore_covered_err */
181 316 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 62 254 316
if (e && *e && SvOK(*e)) { 62 0 62
if (e && *e && SvOK(*e)) { 62 0 62
182 STRLEN l, i;
183 62 const char *h = SvPV(*e, l);
184 /* an empty one is no condition at all, and must not switch off the
185 * date check the way a real one does */
186 66 ▶ for (i = 0; i < l && (h[i] == ' ' || h[i] == '\t'); i++) ;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < l && (h[i] == ' ' || h[i] == '\t'); i++) ; 62 4 66
for (i = 0; i < l && (h[i] == ' ' || h[i] == '\t'); i++) ; 3 59 62
for (i = 0; i < l && (h[i] == ' ' || h[i] == '\t'); i++) ; 1 58 59
187 62 ▶ if (i < l) return etag ? psf_etag_in(h, l, etag, elen) : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i < l) return etag ? psf_etag_in(h, l, etag, elen) : 0; 58 4 62
if (i < l) return etag ? psf_etag_in(h, l, etag, elen) : 0; 57 1 58
188 }
189 258 e = hv_fetchs(env, "HTTP_IF_MODIFIED_SINCE", 0);
190 /*
191 # uncoverable condition right count:2 class:ignore_covered_err
192 # uncoverable condition right count:3 class:ignore_covered_err */
193 258 ▶ if (e && *e && SvOK(*e) && date)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && date) 13 245 258
if (e && *e && SvOK(*e) && date) 13 0 13
if (e && *e && SvOK(*e) && date) 13 0 13
if (e && *e && SvOK(*e) && date) 12 1 13
194 24 return strEQ(SvPV_nolen(*e), date);
195 246 return 0;
196 }
197  
198 /* If-Range: ranges stay honoured only on an exact validator match. A weak
199 * tag never matches (RFC 9110 13.1.5), a mismatch means the full 200. */
200 6 static int psf_if_range_ok(const char *h, STRLEN hlen,
201 const char *etag, STRLEN elen,
202 const char *date) {
203 6 const char *p = h, *end = h + hlen;
204 /*
205 # uncoverable branch true count:2
206 # uncoverable branch true class:ignore_covered_err
207 # uncoverable condition left count:1 class:ignore_covered_err
208 # uncoverable condition right count:1 class:ignore_covered_err
209 # uncoverable condition left count:2 class:ignore_covered_err
210 # uncoverable condition left count:3 class:ignore_covered_err */
211 6 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 6 0 6
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 6 6
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 6 6
212 /*
213 # uncoverable branch true count:2
214 # uncoverable branch true class:ignore_covered_err
215 # uncoverable condition left count:1 class:ignore_covered_err
216 # uncoverable condition right count:1 class:ignore_covered_err
217 # uncoverable condition left count:2 class:ignore_covered_err
218 # uncoverable condition left count:3 class:ignore_covered_err */
219 6 ▶ while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 6 0 6
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 6 6
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 6 6
220 /*
221 # uncoverable branch true
222 # uncoverable condition left count:1 class:ignore_covered_err */
223 6 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 0 6 6
224 /*
225 # uncoverable condition right count:1 class:ignore_covered_err
226 # uncoverable condition right count:3 class:ignore_covered_err */
227 6 ▶ if (end - p > 2 && p[0] == 'W' && p[1] == '/') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (end - p > 2 && p[0] == 'W' && p[1] == '/') return 0; 6 0 6
if (end - p > 2 && p[0] == 'W' && p[1] == '/') return 0; 2 4 6
if (end - p > 2 && p[0] == 'W' && p[1] == '/') return 0; 2 0 2
228 4 ▶ if (*p == '"')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') 3 1 4
229 /*
230 # uncoverable condition right count:1 class:ignore_covered_err
231 # uncoverable condition right count:3 class:ignore_covered_err */
232 3 ▶ return etag && (STRLEN)(end - p) == elen && memEQ(p, etag, elen);
Branch: 3 branches
BranchTrueFalseTotal
return etag && (STRLEN)(end - p) == elen && memEQ(p, etag, elen); 3 0 3
return etag && (STRLEN)(end - p) == elen && memEQ(p, etag, elen); 1 2 3
return etag && (STRLEN)(end - p) == elen && memEQ(p, etag, elen); 1 0 1
233 1 ▶ return date && (STRLEN)(end - p) == strlen(date)
Branch: 1 branch
BranchTrueFalseTotal
return date && (STRLEN)(end - p) == strlen(date) 1 0 1
234 2 ▶ && memEQ(p, date, (STRLEN)(end - p));
Branch: 2 branches
BranchTrueFalseTotal
&& memEQ(p, date, (STRLEN)(end - p)); 1 0 1
&& memEQ(p, date, (STRLEN)(end - p)); 1 0 1
235 }
236  
237 /* ---- Content-Disposition (RFC 6266 / RFC 5987) ---------------------------- */
238  
239 56 static SV *psf_disposition(pTHX_ SV *filename, int inline_) {
240 56 ▶ SV *out = newSVpv(inline_ ? "inline" : "attachment", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *out = newSVpv(inline_ ? "inline" : "attachment", 0); 11 45 56
241 56 ▶ if (filename && SvOK(filename)) {
Branch: 2 branches
BranchTrueFalseTotal
if (filename && SvOK(filename)) { 51 5 56
if (filename && SvOK(filename)) { 50 1 51
242 STRLEN flen, i;
243 50 const char *fn = SvPVutf8(filename, flen);
244 50 int ascii = 1;
245 289 ▶ for (i = 0; i < flen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < flen; i++) { 241 48 289
246 241 U8 c = (U8)fn[i];
247 241 ▶ if (c < 0x20 || c > 0x7E) { ascii = 0; break; } /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c > 0x7E) { ascii = 0; break; } /* # uncoverable condition left count:1 class:ignore_covered_err */ 241 0 241
if (c < 0x20 || c > 0x7E) { ascii = 0; break; } /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 239 241
248 }
249 /* the plain parameter always: non-ASCII bytes degrade to '_' so
250 * a client that ignores filename* still gets a usable name */
251 50 sv_catpvs(out, "; filename=\"");
252 309 ▶ for (i = 0; i < flen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < flen; i++) { 259 50 309
253 259 U8 c = (U8)fn[i];
254 259 ▶ if (c < 0x20 || c > 0x7E) sv_catpvs(out, "_"); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c > 0x7E) sv_catpvs(out, "_"); /* # uncoverable condition left count:1 class:ignore_covered_err */ 259 0 259
if (c < 0x20 || c > 0x7E) sv_catpvs(out, "_"); /* # uncoverable condition left count:1 class:ignore_covered_err */ 4 255 259
255 else {
256 /*
257 # uncoverable condition left count:2 class:ignore_covered_err */
258 255 ▶ if (c == '"' || c == '\\') sv_catpvs(out, "\\");
Branch: 2 branches
BranchTrueFalseTotal
if (c == '"' || c == '\\') sv_catpvs(out, "\\"); 253 2 255
if (c == '"' || c == '\\') sv_catpvs(out, "\\"); 0 253 253
259 255 sv_catpvn(out, &fn[i], 1);
260 }
261 }
262 50 sv_catpvs(out, "\"");
263 50 ▶ if (!ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ascii) { 2 48 50
264 static const char hex[] = "0123456789ABCDEF";
265 2 sv_catpvs(out, "; filename*=UTF-8''");
266 24 ▶ for (i = 0; i < flen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < flen; i++) { 22 2 24
267 22 U8 c = (U8)fn[i];
268 22 ▶ if (isALNUM(c) || (c && strchr("!#$&+-.^_`|~", c) != NULL)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (isALNUM(c) || (c && strchr("!#$&+-.^_`|~", c) != NULL)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 6 16 22
if (isALNUM(c) || (c && strchr("!#$&+-.^_`|~", c) != NULL)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 6 0 6
if (isALNUM(c) || (c && strchr("!#$&+-.^_`|~", c) != NULL)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 4 6
269 18 sv_catpvn(out, &fn[i], 1);
270 else {
271 char pct[3];
272 4 pct[0] = '%';
273 4 pct[1] = hex[c >> 4];
274 4 pct[2] = hex[c & 0xF];
275 4 sv_catpvn(out, pct, 3);
276 }
277 }
278 }
279 }
280 56 return out;
281 }
282  
283 /* ---- Punk::SendFile::Reader ------------------------------------------------
284 * The bounded body for a 206 from a file: getline yields 64KB chunks until
285 * `remaining` is spent, close closes the PerlIO. Satisfies the PSGI body
286 * protocol everywhere (Hyperman's hm_slurp_body takes the getline branch);
287 * fileno is the streaming seam - a server holding it plus the response's
288 * Content-Length (Hyperman's hm_bsrc_lift) sends the window straight from
289 * the fd at its current position and never calls getline. */
290  
291 typedef struct psf_reader {
292 PerlIO *fp;
293 UV remaining;
294 } psf_reader;
295  
296 #define PSF_READER_CHUNK 65536
297 #define PSF_READER_CLASS "Punk::SendFile::Reader"
298  
299 16 static int psf_reader_free(pTHX_ SV *sv, MAGIC *mg) {
300 16 psf_reader *r = (psf_reader *)mg->mg_ptr;
301 PERL_UNUSED_ARG(sv);
302 /*
303 # uncoverable branch false
304 # uncoverable condition right count:1 class:ignore_covered_err */
305 16 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 16 0 16
306 16 ▶ if (r->fp) PerlIO_close(r->fp);
Branch: 1 branch
BranchTrueFalseTotal
if (r->fp) PerlIO_close(r->fp); 6 10 16
307 16 Safefree(r);
308 }
309 16 return 0;
310 }
311 static MGVTBL psf_reader_vtbl = { NULL, NULL, NULL, NULL, psf_reader_free,
312 NULL, NULL, NULL };
313  
314 16 static SV *psf_reader_new(pTHX_ PerlIO *fp, UV remaining) {
315 16 SV *obj = newSV(0);
316 SV *rv;
317 psf_reader *r;
318 16 Newxz(r, 1, psf_reader);
319 16 r->fp = fp;
320 16 r->remaining = remaining;
321 16 sv_magicext(obj, NULL, PERL_MAGIC_ext, &psf_reader_vtbl, (char *)r, 0);
322 16 rv = newRV_noinc(obj);
323 16 return sv_bless(rv, gv_stashpvs(PSF_READER_CLASS, GV_ADD));
324 }
325  
326 32 static psf_reader *psf_reader_of(pTHX_ SV *self) {
327 MAGIC *mg;
328 /*
329 # uncoverable branch false
330 # uncoverable condition right count:1 class:ignore_covered_err
331 # uncoverable condition right count:2 class:ignore_covered_err */
332 32 ▶ if (SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self) 32 0 32
333 32 ▶ && (mg = mg_findext(SvRV(self), PERL_MAGIC_ext, &psf_reader_vtbl)))
Branch: 1 branch
BranchTrueFalseTotal
&& (mg = mg_findext(SvRV(self), PERL_MAGIC_ext, &psf_reader_vtbl))) 32 0 32
334 32 return (psf_reader *)mg->mg_ptr;
335 0 croak(PSF_READER_CLASS ": not a reader"); /* # uncoverable statement */
336 return NULL; /* not reached */
337 }
338  
339 /* ---- the decision core ----------------------------------------------------- */
340  
341 typedef struct psf_src {
342 const char *path; /* file source when non-NULL ... */
343 STRLEN plen;
344 const char *bytes; /* ... else the in-memory source */
345 UV size;
346 UV mtime;
347 UV mns; /* its nanoseconds, 0 when unknown */
348 UV ctime; /* 0 when unknown */
349 int has_mtime; /* scalar sources only via the option */
350 /* The rest of the caller's stat, so the descriptor cache can tell
351 * "the same path" from "the same FILE" without a syscall of its own.
352 * has_id is 0 for a caller that did not stat (an in-memory source, or
353 * one that built psf_src by hand), and then the cache is not consulted -
354 * an unverifiable descriptor is worse than an open. */
355 UV dev, ino;
356 int has_id;
357 /* Set on the way out when the file opened was not the size the caller's
358 * stat said: a caller whose stat came from a cache retries with a fresh
359 * one rather than send a Content-Length the body will not match. */
360 int changed;
361 /* the validators already formatted for this stat (the file cache keeps
362 * them per entry), or NULL to format them here */
363 const char *etag_pre;
364 const char *date_pre;
365 } psf_src;
366  
367 /* The ETag and Last-Modified for the file pfc_stat just answered for: from
368 * its cache entry when that entry still holds them for this stat, formatted
369 * into the entry otherwise. Copied out, so the caller holds no pointer into
370 * the cache. Returns 0 when the cache is off or the path is not cached. */
371 108 static int psf_cached_validators(pTHX_ const char *path, STRLEN plen,
372 const Stat_t *st, char *etag, size_t etagl,
373 char *date, size_t datel) {
374 108 punk_filecache *c = &PFC_CACHE;
375 pfc_entry *e;
376 /*
377 # uncoverable branch true
378 # uncoverable condition left count:1 class:ignore_covered_err
379 # uncoverable condition left count:2 class:ignore_covered_err
380 # uncoverable condition left count:3 class:ignore_covered_err
381 # uncoverable condition left count:4 class:ignore_covered_err */
382 108 ▶ if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX)
Branch: 4 branches
BranchTrueFalseTotal
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 108 0 108
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 108 0 108
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 108 0 108
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 0 108 108
383 0 return 0; /* # uncoverable statement */
384 108 e = pfc_lookup(c, path, plen, pfc_hash(path, plen));
385 /*
386 # uncoverable branch true
387 # uncoverable condition left count:1 class:ignore_covered_err
388 # uncoverable condition left count:2 class:ignore_covered_err */
389 108 ▶ if (!e || !e->exists) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !e->exists) return 0; 108 0 108
if (!e || !e->exists) return 0; 0 108 108
390 108 ▶ if (!e->hdr_ok || !pfc_same(st, &e->st)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!e->hdr_ok || !pfc_same(st, &e->st)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 62 46 108
if (!e->hdr_ok || !pfc_same(st, &e->st)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 62 62
391 46 psf_etag(e->hdr_etag, sizeof e->hdr_etag, (UV)st->st_mtime,
392 46 PFC_MTIME_NS(*st), (UV)st->st_size);
393 46 ps_http_date(e->hdr_date, sizeof e->hdr_date, (time_t)st->st_mtime);
394 46 e->hdr_ok = 1;
395 }
396 108 my_strlcpy(etag, e->hdr_etag, etagl);
397 108 my_strlcpy(date, e->hdr_date, datel);
398 108 return 1;
399 }
400  
401 typedef struct psf_opts {
402 SV *type; /* Content-Type, or NULL to infer */
403 SV *filename; /* Content-Disposition filename */
404 int inline_; /* disposition token */
405 int has_disp; /* emit Content-Disposition at all */
406 int ranges; /* honour Range / advertise bytes */
407 SV *etag; /* override: bare, quoted, or a weak
408 W/"..."; or NULL */
409 SV *cache_control; /* freshness lifetime, or NULL */
410 AV *extra; /* $c->header pairs (borrowed), or NULL */
411 } psf_opts;
412  
413 1175 static void psf_hdr(pTHX_ AV *h, const char *k, SV *v) {
414 1175 av_push(h, newSVpv(k, 0));
415 1175 av_push(h, v);
416 1175 }
417  
418 226 static void psf_hdr_extra(pTHX_ AV *h, AV *extra) {
419 SSize_t i, n;
420 226 ▶ if (!extra) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!extra) return; 66 160 226
421 160 n = av_len(extra) + 1;
422 502 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 342 160 502
423 342 SV **e = av_fetch(extra, i, 0);
424 /*
425 # uncoverable condition right count:1 class:ignore_covered_err
426 # uncoverable condition right count:2 class:ignore_covered_err */
427 342 ▶ av_push(h, e && *e ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(h, e && *e ? newSVsv(*e) : newSV(0)); 342 0 342
av_push(h, e && *e ? newSVsv(*e) : newSV(0)); 342 0 342
428 }
429 }
430  
431 /* Cache-Control, when one was asked for. It goes on the 304 as well as the
432 * 200: a 304 that omits it leaves the stored response with the freshness
433 * lifetime it already had, which has just run out - so the next load
434 * revalidates again, and the header buys nothing after the first hit. */
435 224 static void psf_hdr_cc(pTHX_ AV *hdr, psf_opts *opt) {
436 224 ▶ if (opt->cache_control && SvOK(opt->cache_control)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (opt->cache_control && SvOK(opt->cache_control)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 59 165 224
if (opt->cache_control && SvOK(opt->cache_control)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 59 0 59
437 59 psf_hdr(aTHX_ hdr, "Cache-Control", newSVsv(opt->cache_control));
438 224 }
439  
440 225 static SV *psf_wrap(pTHX_ IV status, AV *hdr, SV *body) {
441 225 AV *resp = newAV();
442 225 AV *bodyav = NULL;
443 225 av_extend(resp, 2);
444 225 av_push(resp, newSViv(status));
445 225 av_push(resp, newRV_noinc((SV *)hdr));
446 225 ▶ if (body && SvROK(body)) { /* glob or reader */
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvROK(body)) { /* glob or reader */ 195 30 225
if (body && SvROK(body)) { /* glob or reader */ 98 97 195
447 98 av_push(resp, body);
448 }
449 else {
450 127 bodyav = newAV();
451 127 ▶ av_push(bodyav, body ? body : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
av_push(bodyav, body ? body : newSVpvs("")); 30 97 127
452 127 av_push(resp, newRV_noinc((SV *)bodyav));
453 }
454 225 return newRV_noinc((SV *)resp);
455 }
456  
457 /* The finished triplet (+1 owned), or NULL when a file source vanished
458 * between the caller's stat and the open here - the caller decides what
459 * its missing-file answer looks like. */
460 227 static SV *psf_respond(pTHX_ HV *env, psf_src *src, psf_opts *opt) {
461 char etagbuf[PSF_ETAG_MAX], date[64];
462 227 const char *etag = NULL, *ldate = NULL;
463 227 STRLEN elen = 0;
464 const char *method;
465 227 STRLEN mlen = 3;
466 227 int is_get = 0, is_head = 0;
467 227 int rstat = PSF_RANGE_NONE;
468 227 UV off = 0, n = 0;
469 SV **e;
470 SV *ct;
471 AV *hdr;
472  
473 /* validators */
474 259 ▶ if (opt->etag && SvOK(opt->etag)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (opt->etag && SvOK(opt->etag)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 33 194 227
if (opt->etag && SvOK(opt->etag)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 33 0 33
475 /* Any length: a SHA-256 in hex is the natural thing to pass and is
476 * 64 characters. Given already quoted, or as a weak tag, it goes out
477 * as it came; given bare it is quoted here. */
478 STRLEN ol;
479 33 const char *ov = SvPV(opt->etag, ol);
480 SV *tag;
481 /*
482 # uncoverable condition right count:5 class:ignore_covered_err
483 # uncoverable condition right count:6 class:ignore_covered_err */
484 33 ▶ if ((ol >= 2 && ov[0] == '"')
Branch: 2 branches
BranchTrueFalseTotal
if ((ol >= 2 && ov[0] == '"') 32 1 33
if ((ol >= 2 && ov[0] == '"') 20 12 32
485 21 ▶ || (ol >= 4 && ov[0] == 'W' && ov[1] == '/' && ov[2] == '"'))
Branch: 4 branches
BranchTrueFalseTotal
|| (ol >= 4 && ov[0] == 'W' && ov[1] == '/' && ov[2] == '"')) 18 3 21
|| (ol >= 4 && ov[0] == 'W' && ov[1] == '/' && ov[2] == '"')) 7 11 18
|| (ol >= 4 && ov[0] == 'W' && ov[1] == '/' && ov[2] == '"')) 7 0 7
|| (ol >= 4 && ov[0] == 'W' && ov[1] == '/' && ov[2] == '"')) 7 0 7
486 19 tag = newSVpvn(ov, ol);
487 else
488 14 tag = newSVpvf("\"%" SVf "\"", SVfARG(opt->etag));
489 33 sv_2mortal(tag);
490 33 etag = SvPV(tag, elen);
491 {
492 STRLEN i;
493 976 ▶ for (i = 0; i < elen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < elen; i++) 944 32 976
494 944 ▶ if ((U8)etag[i] < 0x20 || (U8)etag[i] == 0x7f) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if ((U8)etag[i] < 0x20 || (U8)etag[i] == 0x7f) /* # uncoverable condition left count:2 class:ignore_covered_err */ 943 1 944
if ((U8)etag[i] < 0x20 || (U8)etag[i] == 0x7f) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 943 943
495 1 croak("Punk::Context::send_file: etag may not contain "
496 "a control character");
497 }
498 }
499 194 ▶ else if (src->has_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->has_mtime) { 192 2 194
500 192 ▶ if (src->etag_pre) etag = src->etag_pre;
Branch: 1 branch
BranchTrueFalseTotal
if (src->etag_pre) etag = src->etag_pre; 97 95 192
501 else {
502 95 psf_etag(etagbuf, sizeof etagbuf, src->mtime, src->mns, src->size);
503 95 etag = etagbuf;
504 }
505 192 elen = strlen(etag);
506 }
507 226 ▶ if (src->has_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->has_mtime) { 224 2 226
508 224 ▶ if (src->date_pre) ldate = src->date_pre;
Branch: 1 branch
BranchTrueFalseTotal
if (src->date_pre) ldate = src->date_pre; 97 127 224
509 else {
510 127 ps_http_date(date, sizeof date, (time_t)src->mtime);
511 127 ldate = date;
512 }
513 }
514  
515 226 e = hv_fetchs(env, "REQUEST_METHOD", 0);
516 /*
517 # uncoverable condition right count:1 class:ignore_covered_err
518 # uncoverable condition right count:2 class:ignore_covered_err
519 # uncoverable condition right count:3 class:ignore_covered_err */
520 452 ▶ method = (e && *e && SvOK(*e)) ? SvPV(*e, mlen) : "GET";
Branch: 3 branches
BranchTrueFalseTotal
method = (e && *e && SvOK(*e)) ? SvPV(*e, mlen) : "GET"; 226 0 226
method = (e && *e && SvOK(*e)) ? SvPV(*e, mlen) : "GET"; 226 0 226
method = (e && *e && SvOK(*e)) ? SvPV(*e, mlen) : "GET"; 226 0 226
521 226 ▶ if (mlen == 3 && memEQ(method, "GET", 3)) is_get = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (mlen == 3 && memEQ(method, "GET", 3)) is_get = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 219 7 226
if (mlen == 3 && memEQ(method, "GET", 3)) is_get = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 219 0 219
522 7 ▶ else if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
else if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 3 7
523  
524 /* 304: a validator match on GET/HEAD (RFC 9110 13.1.3) */
525 226 ▶ if ((is_get || is_head) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if ((is_get || is_head) /* # uncoverable condition left count:2 class:ignore_covered_err */ 7 219 226
if ((is_get || is_head) /* # uncoverable condition left count:2 class:ignore_covered_err */ 4 3 7
526 223 ▶ && psf_not_modified(aTHX_ env, etag, elen, ldate)) {
Branch: 1 branch
BranchTrueFalseTotal
&& psf_not_modified(aTHX_ env, etag, elen, ldate)) { 24 199 223
527 24 hdr = newAV();
528 /*
529 # uncoverable branch false
530 # uncoverable condition right count:1 class:ignore_covered_err */
531 24 ▶ if (etag) psf_hdr(aTHX_ hdr, "ETag", newSVpv(etag, elen));
Branch: 1 branch
BranchTrueFalseTotal
if (etag) psf_hdr(aTHX_ hdr, "ETag", newSVpv(etag, elen)); 24 0 24
532 /*
533 # uncoverable branch false
534 # uncoverable condition right count:1 class:ignore_covered_err */
535 24 ▶ if (ldate) psf_hdr(aTHX_ hdr, "Last-Modified", newSVpv(ldate, 0));
Branch: 1 branch
BranchTrueFalseTotal
if (ldate) psf_hdr(aTHX_ hdr, "Last-Modified", newSVpv(ldate, 0)); 24 0 24
536 24 psf_hdr_cc(aTHX_ hdr, opt);
537 24 psf_hdr_extra(aTHX_ hdr, opt->extra);
538 24 return psf_wrap(aTHX_ 304, hdr, NULL);
539 }
540  
541 /* Range applies to GET alone (RFC 9110 14.2) */
542 202 ▶ if (is_get && opt->ranges) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_get && opt->ranges) { 195 7 202
if (is_get && opt->ranges) { 193 2 195
543 193 e = hv_fetchs(env, "HTTP_RANGE", 0);
544 /*
545 # uncoverable condition right count:2 class:ignore_covered_err
546 # uncoverable condition right count:3 class:ignore_covered_err */
547 193 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 26 167 193
if (e && *e && SvOK(*e)) { 26 0 26
if (e && *e && SvOK(*e)) { 26 0 26
548 STRLEN l;
549 26 const char *h = SvPV(*e, l);
550 26 rstat = psf_parse_range(h, l, src->size, &off, &n);
551 26 ▶ if (rstat == PSF_RANGE_OK || rstat == PSF_RANGE_UNSAT) {
Branch: 2 branches
BranchTrueFalseTotal
if (rstat == PSF_RANGE_OK || rstat == PSF_RANGE_UNSAT) { 5 21 26
if (rstat == PSF_RANGE_OK || rstat == PSF_RANGE_UNSAT) { 2 3 5
552 23 SV **ir = hv_fetchs(env, "HTTP_IF_RANGE", 0);
553 /*
554 # uncoverable condition right count:2 class:ignore_covered_err
555 # uncoverable condition right count:3 class:ignore_covered_err */
556 23 ▶ if (ir && *ir && SvOK(*ir)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ir && *ir && SvOK(*ir)) { 6 17 23
if (ir && *ir && SvOK(*ir)) { 6 0 6
if (ir && *ir && SvOK(*ir)) { 6 0 6
557 STRLEN il;
558 6 const char *iv = SvPV(*ir, il);
559 6 ▶ if (!psf_if_range_ok(iv, il, etag, elen, ldate))
Branch: 1 branch
BranchTrueFalseTotal
if (!psf_if_range_ok(iv, il, etag, elen, ldate)) 4 2 6
560 4 rstat = PSF_RANGE_IGNORE;
561 }
562 }
563 }
564 }
565  
566 202 ▶ if (rstat == PSF_RANGE_UNSAT) {
Branch: 1 branch
BranchTrueFalseTotal
if (rstat == PSF_RANGE_UNSAT) { 2 200 202
567 2 SV *cr = newSVpvs("bytes */");
568 2 sv_catpvf(cr, "%" UVuf, src->size);
569 2 hdr = newAV();
570 2 psf_hdr(aTHX_ hdr, "Content-Range", cr);
571 2 psf_hdr(aTHX_ hdr, "Content-Length", newSViv(0));
572 /*
573 # uncoverable branch false
574 # uncoverable condition right count:1 class:ignore_covered_err */
575 2 ▶ if (opt->ranges) psf_hdr(aTHX_ hdr, "Accept-Ranges", newSVpvs("bytes"));
Branch: 1 branch
BranchTrueFalseTotal
if (opt->ranges) psf_hdr(aTHX_ hdr, "Accept-Ranges", newSVpvs("bytes")); 2 0 2
576 2 psf_hdr_extra(aTHX_ hdr, opt->extra);
577 2 return psf_wrap(aTHX_ 416, hdr, NULL);
578 }
579  
580 /* content type: the option, else the path's extension, else the
581 * filename option's extension, else octet-stream */
582 200 ▶ if (opt->type && SvOK(opt->type)) ct = newSVsv(opt->type); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (opt->type && SvOK(opt->type)) ct = newSVsv(opt->type); /* # uncoverable condition right count:2 class:ignore_covered_err */ 64 136 200
if (opt->type && SvOK(opt->type)) ct = newSVsv(opt->type); /* # uncoverable condition right count:2 class:ignore_covered_err */ 64 0 64
583 136 ▶ else if (src->path)
Branch: 1 branch
BranchTrueFalseTotal
else if (src->path) 134 2 136
584 134 ct = newSVpv(ps_content_type(src->path, src->plen), 0);
585 /*
586 # uncoverable branch true
587 # uncoverable condition left count:1 class:ignore_covered_err
588 # uncoverable condition left count:2 class:ignore_covered_err
589 # uncoverable condition right count:2 class:ignore_covered_err */
590 2 ▶ else if (opt->filename && SvOK(opt->filename)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (opt->filename && SvOK(opt->filename)) { 0 2 2
else if (opt->filename && SvOK(opt->filename)) { 0 0 0
591 STRLEN fl;
592 0 const char *fn = SvPV(opt->filename, fl); /* # uncoverable statement */
593 0 ct = newSVpv(ps_content_type(fn, fl), 0); /* # uncoverable statement */
594 }
595 2 else ct = newSVpvs("application/octet-stream");
596  
597 {
598 200 int partial = (rstat == PSF_RANGE_OK);
599 200 ▶ UV bl = partial ? n : src->size;
Branch: 1 branch
BranchTrueFalseTotal
UV bl = partial ? n : src->size; 183 17 200
600 200 SV *body = NULL;
601  
602 200 hdr = newAV();
603 200 psf_hdr(aTHX_ hdr, "Content-Type", ct);
604 200 psf_hdr(aTHX_ hdr, "Content-Length", newSVuv(bl));
605 200 ▶ if (partial) {
Branch: 1 branch
BranchTrueFalseTotal
if (partial) { 17 183 200
606 17 SV *cr = newSVpvs("bytes ");
607 17 sv_catpvf(cr, "%" UVuf "-%" UVuf "/%" UVuf,
608 off, off + n - 1, src->size);
609 17 psf_hdr(aTHX_ hdr, "Content-Range", cr);
610 }
611 200 ▶ if (opt->ranges) psf_hdr(aTHX_ hdr, "Accept-Ranges", newSVpvs("bytes"));
Branch: 1 branch
BranchTrueFalseTotal
if (opt->ranges) psf_hdr(aTHX_ hdr, "Accept-Ranges", newSVpvs("bytes")); 198 2 200
612 200 ▶ if (etag) psf_hdr(aTHX_ hdr, "ETag", newSVpv(etag, elen));
Branch: 1 branch
BranchTrueFalseTotal
if (etag) psf_hdr(aTHX_ hdr, "ETag", newSVpv(etag, elen)); 198 2 200
613 200 ▶ if (ldate) psf_hdr(aTHX_ hdr, "Last-Modified", newSVpv(ldate, 0));
Branch: 1 branch
BranchTrueFalseTotal
if (ldate) psf_hdr(aTHX_ hdr, "Last-Modified", newSVpv(ldate, 0)); 198 2 200
614 200 psf_hdr_cc(aTHX_ hdr, opt);
615 200 ▶ if (opt->has_disp)
Branch: 1 branch
BranchTrueFalseTotal
if (opt->has_disp) 51 149 200
616 51 psf_hdr(aTHX_ hdr, "Content-Disposition",
617 psf_disposition(aTHX_ opt->filename, opt->inline_));
618 200 psf_hdr_extra(aTHX_ hdr, opt->extra);
619  
620 200 ▶ if (is_head)
Branch: 1 branch
BranchTrueFalseTotal
if (is_head) 4 196 200
621 4 body = NULL; /* headers, no body */
622 196 ▶ else if (src->path) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->path) { 193 3 196
623 /* The content cache first: open + read + close is about two
624 * thirds of a small static request, and a hit does none of them.
625 * It validates against the stat the caller has already done, so
626 * consulting it costs no syscall. NULL means "read it yourself"
627 * - too large, unreadable, or not worth holding - never "the
628 * file is missing". */
629 PerlIO *fp;
630 /* Not for a RANGE. A 206 body is a Punk::SendFile::Reader - an
631 * object with getline/close - and callers read it as one. Slicing
632 * the cached bytes instead would hand back a plain arrayref and
633 * break that contract, for a request shape that is rare (seeking
634 * a video, resuming a download) and that streaming already suits.
635 * The whole-file 200 is what static traffic actually is, and what
636 * was measured. */
637 98 ▶ SV *cached = (src->has_id && !partial)
Branch: 1 branch
BranchTrueFalseTotal
SV *cached = (src->has_id && !partial) 96 2 98
638 96 ? pfc_get(aTHX_ src->path, src->plen,
639 src->dev, src->ino, src->mtime, src->mns,
640 src->ctime, src->size)
641 195 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 98 95 193
642 193 ▶ if (cached) { body = cached; goto have_body; }
Branch: 1 branch
BranchTrueFalseTotal
if (cached) { body = cached; goto have_body; } 94 99 193
643 99 fp = PerlIO_open(src->path, "rb");
644 /*
645 # uncoverable branch true
646 # uncoverable condition left count:1 class:ignore_covered_err */
647 99 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 0 99 99
648 0 SvREFCNT_dec((SV *)hdr); /* # uncoverable statement */
649 0 return NULL; /* # uncoverable statement */
650 }
651 /* The headers above were built from the caller's stat, and that
652 * may be a second old. The handle is the file as it is now: if it
653 * is not the size that was promised, promise nothing. */
654 {
655 Stat_t fst;
656 99 ▶ if (PerlLIO_fstat(PerlIO_fileno(fp), &fst) == 0 /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_fstat(PerlIO_fileno(fp), &fst) == 0 /* # uncoverable condition right count:1 class:ignore_covered_err */ 99 0 99
657 99 ▶ && (UV)fst.st_size != src->size) {
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)fst.st_size != src->size) { 1 98 99
658 1 PerlIO_close(fp);
659 1 SvREFCNT_dec((SV *)hdr);
660 1 src->changed = 1;
661 1 return NULL;
662 }
663 }
664 98 ▶ if (partial) {
Branch: 1 branch
BranchTrueFalseTotal
if (partial) { 16 82 98
665 /*
666 # uncoverable branch true
667 # uncoverable condition left count:1 class:ignore_covered_err */
668 16 ▶ if (PerlIO_seek(fp, (Off_t)off, SEEK_SET) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_seek(fp, (Off_t)off, SEEK_SET) < 0) { 0 16 16
669 0 PerlIO_close(fp); /* # uncoverable statement */
670 0 SvREFCNT_dec((SV *)hdr); /* # uncoverable statement */
671 0 return NULL; /* # uncoverable statement */
672 }
673 16 body = psf_reader_new(aTHX_ fp, n);
674 }
675 else {
676 /* a real filehandle, the Punk::Static convention */
677 82 GV *gv = newGVgen("Punk::SendFile");
678 82 ▶ IO *io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
IO *io = GvIOn(gv); 82 0 82
IO *io = GvIOn(gv); 0 82 82
IO *io = GvIOn(gv); 0 0 0
IO *io = GvIOn(gv); 82 0 82
IO *io = GvIOn(gv); 0 82 82
679 82 IoIFP(io) = fp;
680 82 IoTYPE(io) = IoTYPE_RDONLY;
681 82 body = newRV_inc((SV *)gv);
682 }
683 }
684 else
685 3 ▶ body = newSVpvn(src->bytes + (partial ? off : 0), bl);
Branch: 1 branch
BranchTrueFalseTotal
body = newSVpvn(src->bytes + (partial ? off : 0), bl); 1 2 3
686  
687 199 have_body:
688 199 ▶ return psf_wrap(aTHX_ partial ? 206 : 200, hdr, body);
Branch: 1 branch
BranchTrueFalseTotal
return psf_wrap(aTHX_ partial ? 206 : 200, hdr, body); 17 182 199
689 }
690 }
691  
692 /* ---- precompressed siblings -------------------------------------------------
693 *
694 * If `style.css.gz` sits next to `style.css` and the client accepts gzip, we
695 * serve the sibling's BYTES under the original's IDENTITY: its Content-Type,
696 * its URL, its cache entry keyed by an ETag of its own. Nothing is
697 * compressed at request time - the win is a build step's, paid once - so
698 * this needs no zlib and costs one stat.
699 *
700 * `.br` is listed first and preferred when the client offers both. Serving a
701 * brotli file needs no brotli library, only this table entry: the bytes are
702 * already encoded and we are choosing a file, not compressing one.
703 */
704 typedef struct { const char *suffix; STRLEN slen; const char *token; STRLEN tlen; }
705 psf_encoding;
706  
707 static const psf_encoding PSF_ENCODINGS[] = {
708 { ".br", 3, "br", 2 },
709 { ".gz", 3, "gzip", 4 },
710 };
711 #define PSF_NENCODINGS 2
712  
713 /* Is `token` an acceptable encoding per this Accept-Encoding header? A
714 * left-to-right walk: no split, no hash, no SV. `q=0` is an explicit refusal
715 * and loses to nothing; `*` matches anything not separately refused. */
716 31 static int psf_accepts(pTHX_ const char *ae, STRLEN len, const char *tok, STRLEN tlen) {
717 31 STRLEN i = 0;
718 31 int star = 0;
719 49 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 32 17 49
720 STRLEN start, end, j;
721 32 int refused = 0, is_star, is_tok;
722 /*
723 # uncoverable condition right count:1 class:ignore_covered_err
724 # uncoverable condition left count:3 class:ignore_covered_err */
725 34 ▶ while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 34 0 34
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 1 33 34
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 0 33 33
while (i < len && (ae[i] == ' ' || ae[i] == '\t' || ae[i] == ',')) i++; 1 32 33
726 32 start = i;
727 167 ▶ while (i < len && ae[i] != ',' && ae[i] != ';') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 143 24 167
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 141 2 143
while (i < len && ae[i] != ',' && ae[i] != ';') i++; 135 6 141
728 32 end = i;
729 /*
730 # uncoverable branch true count:2
731 # uncoverable branch true class:ignore_covered_err
732 # uncoverable condition left count:1 class:ignore_covered_err
733 # uncoverable condition right count:1 class:ignore_covered_err
734 # uncoverable condition left count:2 class:ignore_covered_err
735 # uncoverable condition left count:3 class:ignore_covered_err */
736 32 ▶ while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; 32 0 32
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; 0 32 32
while (end > start && (ae[end - 1] == ' ' || ae[end - 1] == '\t')) end--; 0 32 32
737 /* parameters: only q matters, and only q=0 (with any zero spelling) */
738 32 ▶ if (i < len && ae[i] == ';') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && ae[i] == ';') { 8 24 32
if (i < len && ae[i] == ';') { 6 2 8
739 6 STRLEN ps = i;
740 42 ▶ while (i < len && ae[i] != ',') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && ae[i] != ',') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 36 6 42
while (i < len && ae[i] != ',') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 36 0 36
741 /*
742 # uncoverable branch false
743 # uncoverable condition right count:1 class:ignore_covered_err */
744 12 ▶ for (j = ps; j + 1 < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = ps; j + 1 < i; j++) { 12 0 12
745 12 ▶ if ((ae[j] | 32) != 'q') continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((ae[j] | 32) != 'q') continue; 6 6 12
746 /*
747 # uncoverable branch true count:2
748 # uncoverable condition right count:1 class:ignore_covered_err
749 # uncoverable condition left count:2 class:ignore_covered_err */
750 12 ▶ while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 12 0 12
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 0 12 12
while (j + 1 < i && (ae[j + 1] == ' ' || ae[j + 1] == '=')) j++; 6 6 12
751 /*
752 # uncoverable branch false
753 # uncoverable condition right count:1 class:ignore_covered_err
754 # uncoverable condition right count:2 class:ignore_covered_err */
755 6 ▶ if (j + 1 < i && ae[j + 1] == '0') {
Branch: 2 branches
BranchTrueFalseTotal
if (j + 1 < i && ae[j + 1] == '0') { 6 0 6
if (j + 1 < i && ae[j + 1] == '0') { 6 0 6
756 /* q=0, q=0.0, q=0.000 - anything that is not > 0 */
757 6 STRLEN k = j + 2;
758 6 int nonzero = 0;
759 6 ▶ if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 2 6
if (k < i && ae[k] == '.') /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
760 /*
761 # uncoverable condition right count:2 class:ignore_covered_err
762 # uncoverable condition right count:3 class:ignore_covered_err */
763 12 ▶ for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++)
Branch: 3 branches
BranchTrueFalseTotal
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 8 4 12
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 8 0 8
for (k++; k < i && ae[k] >= '0' && ae[k] <= '9'; k++) 8 0 8
764 8 ▶ if (ae[k] != '0') nonzero = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ae[k] != '0') nonzero = 1; 2 6 8
765 6 ▶ if (!nonzero) refused = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!nonzero) refused = 1; 4 2 6
766 }
767 6 break;
768 }
769 }
770 32 ▶ is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 31 32
is_star = (end - start == 1 && ae[start] == '*'); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
771 32 ▶ is_tok = (end - start == tlen && foldEQ(ae + start, tok, (I32)tlen)); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
is_tok = (end - start == tlen && foldEQ(ae + start, tok, (I32)tlen)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 18 32
is_tok = (end - start == tlen && foldEQ(ae + start, tok, (I32)tlen)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 0 14
772 32 ▶ if (is_tok) return !refused; /* named: it decides */
Branch: 1 branch
BranchTrueFalseTotal
if (is_tok) return !refused; /* named: it decides */ 14 18 32
773 18 ▶ if (is_star && !refused) star = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (is_star && !refused) star = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 17 18
if (is_star && !refused) star = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
774 }
775 17 return star;
776 }
777  
778 /* The env's Accept-Encoding, or NULL. */
779 108 static const char *psf_accept_encoding(pTHX_ HV *env, STRLEN *len) {
780 108 SV **e = hv_fetchs(env, "HTTP_ACCEPT_ENCODING", 0);
781 /*
782 # uncoverable condition left count:2 class:ignore_covered_err
783 # uncoverable condition left count:3 class:ignore_covered_err */
784 108 ▶ if (!(e && *e && SvOK(*e))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) return NULL; 18 90 108
if (!(e && *e && SvOK(*e))) return NULL; 18 0 18
if (!(e && *e && SvOK(*e))) return NULL; 0 18 18
785 36 return SvPV_const(*e, *len);
786 }
787  
788 /* ---- ps_serve_file ----------------------------------------------------------
789 * The static/markdown "serve one file" (declared in punk_static.h): the
790 * decision core with a fixed option block - no disposition, ranges on,
791 * validators from the file itself. This is where a `static` mount's ETag,
792 * If-None-Match 304 and 206/416 answers come from. The is_head flag the
793 * callers computed is the same REQUEST_METHOD the core reads for itself.
794 *
795 * `fo` is the caller's freshness policy, or NULL: the mount decides what
796 * Cache-Control a file gets, because only the mount knows whether the URL
797 * that asked for it was content-addressed. */
798  
799 132 static SV *ps_serve_file_opt(pTHX_ HV *env, const char *file, STRLEN flen,
800 int is_head, ps_fopts *fo) {
801 Stat_t st, sst;
802 psf_src s;
803 psf_opts o;
804 char sib[MAXPATHLEN + 8];
805 char etagbuf[PSF_ETAG_MAX + 8], datebuf[40];
806 132 AV *extra = NULL;
807 const char *ae;
808 132 STRLEN ael = 0;
809 132 int i, retried = 0;
810 SV *resp;
811 PERL_UNUSED_ARG(is_head);
812 133 again:
813 133 ▶ if (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode)) return NULL; 111 22 133
if (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode)) return NULL; 3 108 111
814 108 Zero(&s, 1, psf_src);
815 108 Zero(&o, 1, psf_opts);
816 108 s.path = file;
817 108 s.plen = flen;
818 108 s.size = (UV)st.st_size;
819 108 s.mtime = (UV)st.st_mtime;
820 108 s.mns = PFC_MTIME_NS(st);
821 108 s.ctime = (UV)st.st_ctime;
822 108 s.has_mtime = 1;
823 /* the rest of the stat we just did, for the descriptor cache to validate
824 * against - no extra syscall, this one was already paid for */
825 108 s.dev = (UV)st.st_dev;
826 108 s.ino = (UV)st.st_ino;
827 108 s.has_id = 1;
828 108 o.ranges = 1;
829 /* the validators the cache already formatted for this stat */
830 /*
831 # uncoverable branch false
832 # uncoverable condition right count:1 class:ignore_covered_err */
833 108 ▶ if (psf_cached_validators(aTHX_ file, flen, &st, etagbuf, sizeof etagbuf,
Branch: 1 branch
BranchTrueFalseTotal
if (psf_cached_validators(aTHX_ file, flen, &st, etagbuf, sizeof etagbuf, 108 0 108
834 datebuf, sizeof datebuf)) {
835 108 s.etag_pre = etagbuf;
836 108 s.date_pre = datebuf;
837 }
838 108 ▶ if (fo) o.cache_control = fo->cache_control;
Branch: 1 branch
BranchTrueFalseTotal
if (fo) o.cache_control = fo->cache_control; 103 5 108
839  
840 /* Vary goes on EVERY response from here, compressed or not: a shared
841 * cache that stored the identity bytes without it would hand them to a
842 * client that asked for gzip, and vice versa. It costs one header and
843 * it is the only spelling that is correct for both. */
844 108 extra = (AV *)sv_2mortal((SV *)newAV());
845 108 av_push(extra, newSVpvs("Vary"));
846 108 av_push(extra, newSVpvs("Accept-Encoding"));
847  
848 108 ae = psf_accept_encoding(aTHX_ env, &ael);
849 108 ▶ if (ae && ael && flen + 4 < sizeof(sib)) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (ae && ael && flen + 4 < sizeof(sib)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 18 90 108
if (ae && ael && flen + 4 < sizeof(sib)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 17 1 18
if (ae && ael && flen + 4 < sizeof(sib)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 17 0 17
850 37 ▶ for (i = 0; i < PSF_NENCODINGS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PSF_NENCODINGS; i++) { 31 6 37
851 31 const psf_encoding *enc = &PSF_ENCODINGS[i];
852 31 ▶ if (!psf_accepts(aTHX_ ae, ael, enc->token, enc->tlen)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!psf_accepts(aTHX_ ae, ael, enc->token, enc->tlen)) continue; 18 13 31
853 13 memcpy(sib, file, flen);
854 13 memcpy(sib + flen, enc->suffix, enc->slen + 1);
855 /* Through the cache, which remembers ABSENCE too: every browser
856 * sends Accept-Encoding, so on a site with no build step this
857 * probe asks for a .br and a .gz that have never existed, on
858 * every single request. */
859 13 ▶ if (!pfc_stat(aTHX_ sib, flen + enc->slen, &sst) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (!pfc_stat(aTHX_ sib, flen + enc->slen, &sst) /* # uncoverable condition left count:2 class:ignore_covered_err */ 12 1 13
860 13 ▶ || !S_ISREG(sst.st_mode)) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| !S_ISREG(sst.st_mode)) continue; 0 12 12
861 /* A sibling older than its source is a stale build artefact.
862 * Serving it would ship last week's CSS forever, silently,
863 * which is the failure nobody thinks to look for. */
864 12 ▶ if ((UV)sst.st_mtime < (UV)st.st_mtime) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)sst.st_mtime < (UV)st.st_mtime) continue; 1 11 12
865  
866 /* the bytes, the length and the validators are the sibling's;
867 * the Content-Type stays the original's, so ps_content_type
868 * must never see the .gz/.br suffix */
869 11 o.type = sv_2mortal(newSVpv(ps_content_type(file, flen), 0));
870 11 s.path = sib;
871 11 s.plen = flen + enc->slen;
872 11 s.size = (UV)sst.st_size;
873 11 s.mtime = (UV)sst.st_mtime;
874 11 s.mns = PFC_MTIME_NS(sst);
875 11 s.ctime = (UV)sst.st_ctime;
876 /* the sibling is a different file, so it carries its own identity
877 * - reusing the original's would key the cache on one path and
878 * validate it against another */
879 11 s.dev = (UV)sst.st_dev;
880 11 s.ino = (UV)sst.st_ino;
881 11 s.etag_pre = NULL; /* the original's, not this file's */
882 11 s.date_pre = NULL;
883  
884 /* an encoding-tagged ETag, so an identity client and this one
885 * never collide in a shared cache even if the two files should
886 * ever share an mtime and a size */
887 11 psf_etag(etagbuf, sizeof etagbuf, s.mtime, s.mns, s.size);
888 {
889 11 STRLEN el = strlen(etagbuf);
890 /*
891 # uncoverable branch false
892 # uncoverable condition right count:1 class:ignore_covered_err
893 # uncoverable condition right count:2 class:ignore_covered_err
894 # uncoverable condition right count:3 class:ignore_covered_err */
895 11 ▶ if (el > 1 && etagbuf[el - 1] == '"'
Branch: 2 branches
BranchTrueFalseTotal
if (el > 1 && etagbuf[el - 1] == '"' 11 0 11
if (el > 1 && etagbuf[el - 1] == '"' 11 0 11
896 11 ▶ && el + enc->tlen + 1 < sizeof etagbuf) {
Branch: 1 branch
BranchTrueFalseTotal
&& el + enc->tlen + 1 < sizeof etagbuf) { 11 0 11
897 11 etagbuf[el - 1] = '-';
898 11 memcpy(etagbuf + el, enc->token, enc->tlen);
899 11 etagbuf[el + enc->tlen] = '"';
900 11 etagbuf[el + enc->tlen + 1] = '\0';
901 }
902 11 o.etag = sv_2mortal(newSVpv(etagbuf, 0));
903 }
904 11 av_push(extra, newSVpvs("Content-Encoding"));
905 11 av_push(extra, newSVpvn(enc->token, enc->tlen));
906 11 break;
907 }
908 }
909 108 o.extra = extra;
910 108 resp = psf_respond(aTHX_ env, &s, &o);
911 /*
912 # uncoverable condition right count:2 class:ignore_covered_err
913 # uncoverable condition right count:3 class:ignore_covered_err */
914 108 ▶ if (!resp && s.changed && !retried) {
Branch: 3 branches
BranchTrueFalseTotal
if (!resp && s.changed && !retried) { 1 107 108
if (!resp && s.changed && !retried) { 1 0 1
if (!resp && s.changed && !retried) { 1 0 1
915 /* The file is not the one the stat cache remembers. Ask again, once:
916 * a second disagreement is a file being written as we look, and the
917 * caller's 404 is as good an answer as any to that. */
918 1 pfc_expire(aTHX_ file, flen);
919 /*
920 # uncoverable branch true
921 # uncoverable condition left count:1 class:ignore_covered_err */
922 1 ▶ if (s.path != file) pfc_expire(aTHX_ s.path, s.plen);
Branch: 1 branch
BranchTrueFalseTotal
if (s.path != file) pfc_expire(aTHX_ s.path, s.plen); 0 1 1
923 1 retried = 1;
924 1 goto again;
925 }
926 107 return resp;
927 }
928  
929 16 static SV *ps_serve_file(pTHX_ HV *env, const char *file, STRLEN flen,
930 int is_head) {
931 16 return ps_serve_file_opt(aTHX_ env, file, flen, is_head, NULL);
932 }
933  
934 #endif /* PUNK_SENDFILE_H */
« xs/markdown.xs ↑ Summary include/punk/punk_proxy.h »