xs/sendfile.xs

filter: Statementstmt 82.4% 80 / 97 Branchbran 58.5% 57 / 94 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.6% 137 / 191 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/conditionalget.xs ↑ Summary xs/dbil.xs »
1 MODULE = Punk PACKAGE = Punk::Context
2  
3 PROTOTYPES: DISABLE
4  
5 # $c->send_file($path_or_scalarref, %opts) - a finished download response
6 # (punk_sendfile.h): validators, single-range 206/416, Content-Disposition,
7 # HEAD. Returns the triplet, so the handler returns it. Options: type,
8 # filename, inline, ranges, mtime, etag, cache_control, missing.
9 SV *
10 send_file(self, src, ...)
11 SV *self
12 SV *src
13 CODE:
14 {
15 130 AV *av = pcx_av(aTHX_ self);
16 130 AV *res = pcx_res_av(aTHX_ av);
17 130 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
18 HV *env;
19 psf_src s;
20 psf_opts o;
21 130 SV *mtime_opt = NULL, *missing = NULL;
22 Stat_t st;
23 int i;
24 SV *resp;
25  
26 130 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 130 0 130
# uncoverable branch true 130 0 130
# uncoverable branch true 0 130 130
27 0 # uncoverable condition left count:1 class:ignore_covered_err
28 130 # uncoverable condition left count:2 class:ignore_covered_err
29 # uncoverable condition left count:3 class:ignore_covered_err
30 130 if (!envsv || !SvROK(envsv) || SvTYPE(SvRV(envsv)) != SVt_PVHV)
31 130 croak("Punk::Context::send_file: no request env"); /* # uncoverable statement */
32 130 env = (HV *)SvRV(envsv);
33 130 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
57 73 130
34 Zero(&s, 1, psf_src);
35 130 ▶ Zero(&o, 1, psf_opts);
Branch: 1 branch
BranchTrueFalseTotal
Zero(&o, 1, psf_opts); 0 130 130
36 0 o.ranges = 1;
37 456 ▶ o.extra = res ? pcx_res_headers(aTHX_ res) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o.extra = res ? pcx_res_headers(aTHX_ res) : NULL; 326 130 456
38 326  
39 326 # uncoverable branch true
40 326 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 61 265 326
41 265 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 51 214 265
42 214 ▶ croak("Punk::Context::send_file: odd number of options"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Context::send_file: odd number of options"); /* # uncoverable statement */ 75 139 214
43 75 for (i = 2; i < items; i += 2) {
44 139 ▶ const char *k = SvPV_nolen(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_nolen(ST(i)); 2 137 139
45 137 ▶ SV *v = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
SV *v = ST(i + 1); 2 135 137
46 135 ▶ if (strEQ(k, "type")) o.type = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "type")) o.type = v; 22 113 135
47 113 ▶ else if (strEQ(k, "filename")) { o.filename = v; o.has_disp = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "filename")) { o.filename = v; o.has_disp = 1; } 57 56 113
48 56 ▶ else if (strEQ(k, "inline")) { o.inline_ = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "inline")) { o.inline_ = SvTRUE(v) ? 1 : 0; 56 0 56
49 0 o.has_disp = 1; }
50 else if (strEQ(k, "ranges")) o.ranges = SvTRUE(v) ? 1 : 0;
51 130 ▶ else if (strEQ(k, "mtime")) mtime_opt = v;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "mtime")) mtime_opt = v; 56 74 130
else if (strEQ(k, "mtime")) mtime_opt = v; 56 0 56
52 56 ▶ else if (strEQ(k, "etag")) o.etag = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "etag")) o.etag = v; 0 56 56
53 0 else if (strEQ(k, "cache_control")) o.cache_control = v;
54 # uncoverable branch false
55 # uncoverable condition right count:1 class:ignore_covered_err
56 130 ▶ else if (strEQ(k, "missing")) missing = v;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "missing")) missing = v; 4 126 130
else if (strEQ(k, "missing")) missing = v; 4 0 4
57 8 ▶ else croak("Punk::Context::send_file: unknown option '%s'", k); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
else croak("Punk::Context::send_file: unknown option '%s'", k); /* # uncoverable statement */ 4 0 4
58 }
59 4 # uncoverable branch true
60 4 # uncoverable condition left count:1 class:ignore_covered_err
61 4 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 2 2 4
# uncoverable condition left count:2 class:ignore_covered_err 2 0 2
62 2 # uncoverable condition right count:2 class:ignore_covered_err
63 2 # uncoverable condition left count:3 class:ignore_covered_err
64 if (missing && SvOK(missing)
65 && !strEQ(SvPV_nolen(missing), "not_found"))
66 croak("Punk::Context::send_file: missing => '%s' is not " /* # uncoverable statement */
67 126 "'not_found'", SvPV_nolen(missing)); /* # uncoverable statement */
68  
69 # uncoverable condition right count:2 class:ignore_covered_err
70 # uncoverable condition right count:3 class:ignore_covered_err
71 if (SvROK(src) && !sv_isobject(src)
72 126 ▶ && SvTYPE(SvRV(src)) < SVt_PVAV) { /* in-memory bytes */
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(src)) < SVt_PVAV) { /* in-memory bytes */ 4 122 126
73 4 ▶ STRLEN bl;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN bl; 1 3 4
STRLEN bl; 1 0 1
74 1 s.bytes = SvPV(SvRV(src), bl);
75 s.size = (UV)bl;
76 if (mtime_opt && SvOK(mtime_opt)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
77 s.mtime = (UV)SvUV(mtime_opt);
78 1 s.has_mtime = 1;
79 }
80 3 }
81 else { /* a file path */
82 122 ▶ s.path = SvPV(src, s.plen);
Branch: 2 branches
BranchTrueFalseTotal
s.path = SvPV(src, s.plen); 115 7 122
s.path = SvPV(src, s.plen); 0 115 115
83 7 ▶ /* stat and open stop at a NUL and the content type does not, so
Branch: 2 branches
BranchTrueFalseTotal
/* stat and open stop at a NUL and the content type does not, so 6 1 7
/* stat and open stop at a NUL and the content type does not, so 6 0 6
84 6 * `x.txt\0.html` would open one file and label it as another -
85 * and a caller that allowlists by extension would be opening
86 * whatever came before the NUL */
87 if (memchr(s.path, '\0', s.plen)) {
88 6 if (missing && SvOK(missing)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
89 RETVAL = punk_triplet(aTHX_ 404,
90 1 sv_2mortal(newSVpvs("application/json")),
91 newSVpvs("{\"errors\":[{\"message\":\"Not Found\"}]}"),
92 115 NULL);
93 115 ▶ goto done;
Branch: 2 branches
BranchTrueFalseTotal
goto done; 0 115 115
goto done; 0 0 0
94 }
95 115 croak("Punk::Context::send_file: the path contains a NUL");
96 115 }
97 if (PerlLIO_stat(s.path, &st) < 0 || !S_ISREG(st.st_mode)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
98 115 if (missing && SvOK(missing)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
99 RETVAL = punk_triplet(aTHX_ 404,
100 sv_2mortal(newSVpvs("application/json")),
101 newSVpvs("{\"errors\":[{\"message\":\"Not Found\"}]}"),
102 NULL);
103 goto done;
104 }
105 croak("Punk::Context::send_file: cannot read '%s'", s.path);
106 }
107 s.size = (UV)st.st_size;
108 # uncoverable branch true
109 # uncoverable condition left count:1 class:ignore_covered_err
110 # uncoverable condition left count:2 class:ignore_covered_err
111 # uncoverable condition right count:2 class:ignore_covered_err
112 if (mtime_opt && SvOK(mtime_opt)) s.mtime = (UV)SvUV(mtime_opt);
113 else {
114 s.mtime = (UV)st.st_mtime;
115 s.mns = PFC_MTIME_NS(st);
116 }
117 s.has_mtime = 1;
118 /* NOT eligible for the content cache, deliberately - has_id
119 119 * stays 0 here.
120 118 ▶ *
Branch: 3 branches
BranchTrueFalseTotal
* 0 118 118
* 0 0 0
* 0 0 0
121 0 ▶ * send_file's body is a FILEHANDLE, and that is not merely how it
Branch: 2 branches
BranchTrueFalseTotal
* send_file's body is a FILEHANDLE, and that is not merely how it 0 0 0
* send_file's body is a FILEHANDLE, and that is not merely how it 0 0 0
122 * happens to be built: other features read it as a fact about the
123 * response. Idempotency treats a filehandle body as unrecordable,
124 0 * because recording it would consume what is being sent, and
125 0 * ConditionalGet declines to hash one for the same reason. Serving
126 0 ▶ * these from memory instead would silently make those responses
Branch: 2 branches
BranchTrueFalseTotal
* these from memory instead would silently make those responses 0 0 0
* these from memory instead would silently make those responses 0 0 0
127 0 * recordable and replayable - a change to what two plugins do,
128 0 * arrived at through a cache, which is the last place anyone would
129 * look for it.
130 0 *
131 * The static mounts are where the measured cost was (19us a hit,
132 118 ▶ * two thirds of it syscalls) and they set has_id for themselves in
Branch: 1 branch
BranchTrueFalseTotal
* two thirds of it syscalls) and they set has_id for themselves in 0 118 118
133 0 ▶ * ps_serve_file_opt. A download through send_file is usually large
Branch: 2 branches
BranchTrueFalseTotal
* ps_serve_file_opt. A download through send_file is usually large 0 0 0
* ps_serve_file_opt. A download through send_file is usually large 0 0 0
134 0 * and usually cold, which is the case streaming is already right
135 * for. */
136 }
137  
138 resp = psf_respond(aTHX_ env, &s, &o);
139 0 # uncoverable branch true
140 # uncoverable condition left count:1 class:ignore_covered_err
141 118 # uncoverable condition left count:2 class:ignore_covered_err
142 125 # uncoverable condition right count:2 class:ignore_covered_err
143 # uncoverable condition left count:3 class:ignore_covered_err
144 # uncoverable condition right count:3 class:ignore_covered_err
145 # uncoverable condition left count:4 class:ignore_covered_err
146 # uncoverable condition right count:4 class:ignore_covered_err
147 # uncoverable condition left count:5 class:ignore_covered_err
148 # uncoverable condition right count:5 class:ignore_covered_err
149 if (!resp && s.changed && s.path
150 && PerlLIO_stat(s.path, &st) == 0 && S_ISREG(st.st_mode)) { /* # uncoverable statement */
151 /* rewritten between the stat and the open: take the file as it
152 * is now, once */
153 s.size = (UV)st.st_size; /* # uncoverable statement */
154 s.changed = 0; /* # uncoverable statement */
155 # uncoverable statement
156 # uncoverable branch true
157 # uncoverable branch false
158 # uncoverable condition left count:1
159 # uncoverable condition right count:1
160 # uncoverable condition left count:2
161 # uncoverable condition right count:2
162 if (!(mtime_opt && SvOK(mtime_opt))) {
163 s.mtime = (UV)st.st_mtime; /* # uncoverable statement */
164 s.mns = PFC_MTIME_NS(st); /* # uncoverable statement */
165 }
166 resp = psf_respond(aTHX_ env, &s, &o); /* # uncoverable statement */
167 }
168 # uncoverable branch true
169 # uncoverable condition left count:1 class:ignore_covered_err
170 if (!resp) { /* lost between stat and open */
171 # uncoverable statement
172 # uncoverable branch true
173 # uncoverable branch false
174 # uncoverable condition left count:1
175 # uncoverable condition right count:1
176 # uncoverable condition left count:2
177 # uncoverable condition right count:2
178 if (missing && SvOK(missing))
179 resp = punk_triplet(aTHX_ 404, /* # uncoverable statement */
180 sv_2mortal(newSVpvs("application/json")), /* # uncoverable statement */
181 newSVpvs("{\"errors\":[{\"message\":\"Not Found\"}]}"), /* # uncoverable statement */
182 NULL);
183 else
184 croak("Punk::Context::send_file: cannot read '%s'", s.path); /* # uncoverable statement */
185 }
186 RETVAL = resp;
187 done: ;
188 }
189 OUTPUT:
190 RETVAL
191  
192 MODULE = Punk PACKAGE = Punk::SendFile::Reader
193  
194 # The bounded 206 body: getline yields up to 64KB until the range is
195 # spent, close closes the handle. Never constructed by hand - send_file
196 # builds it; the struct lives in punk_sendfile.h so a C server can lift
197 # the handle and count out instead of calling getline.
198  
199 SV *
200 getline(self)
201 SV *self
202 CODE:
203 {
204 20 psf_reader *r = psf_reader_of(aTHX_ self);
205 UV want;
206 SSize_t got;
207 20 ▶ if (!r->fp || !r->remaining) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!r->fp || !r->remaining) XSRETURN_UNDEF; 19 1 20
if (!r->fp || !r->remaining) XSRETURN_UNDEF; 9 10 19
208 10 want = r->remaining < PSF_READER_CHUNK
209 ? r->remaining : PSF_READER_CHUNK;
210 10 RETVAL = newSV(want + 1);
211 10 SvPOK_on(RETVAL);
212 10 got = PerlIO_read(r->fp, SvPVX(RETVAL), want);
213 10 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 10 10
214 0 # uncoverable condition left count:1 class:ignore_covered_err
215 0 if (got <= 0) {
216 0 SvREFCNT_dec(RETVAL); /* # uncoverable statement */
217 r->remaining = 0; /* # uncoverable statement */
218 10 XSRETURN_UNDEF; /* # uncoverable statement */
219 10 }
220 10 SvCUR_set(RETVAL, (STRLEN)got);
221 *SvEND(RETVAL) = '\0';
222 r->remaining -= (UV)got;
223 }
224 OUTPUT:
225 RETVAL
226  
227 void
228 close(self)
229 SV *self
230 CODE:
231 {
232 10 psf_reader *r = psf_reader_of(aTHX_ self);
233 10 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
234 10 # uncoverable condition right count:1 class:ignore_covered_err
235 10 if (r->fp) {
236 PerlIO_close(r->fp);
237 10 r->fp = NULL;
238 }
239 r->remaining = 0;
240 }
241  
242 # The underlying file descriptor (or -1 once closed). This is the seam a
243 # streaming server keys on: fileno + getline + the response Content-Length
244 # is everything Hyperman needs to sendfile the range instead of calling
245 # getline (its hm_bsrc_lift; the fd is read at its current position).
246 SV *
247 fileno(self)
248 SV *self
249 CODE:
250 {
251 2 psf_reader *r = psf_reader_of(aTHX_ self);
252 2 ▶ RETVAL = newSViv(r->fp ? (IV)PerlIO_fileno(r->fp) : -1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSViv(r->fp ? (IV)PerlIO_fileno(r->fp) : -1); 1 1 2
253 }
254 OUTPUT:
255 RETVAL
256  
257 MODULE = Punk PACKAGE = Punk::SendFile
258  
259 # Test shims: the parser and builders alone, for the t/ grammar tables.
260  
261 void
262 _parse_range(header, size)
263 SV *header
264 SV *size
265 PPCODE:
266 {
267 STRLEN hl;
268 29 const char *h = SvPV(header, hl);
269 29 UV off = 0, n = 0;
270 29 int r = psf_parse_range(h, hl, (UV)SvUV(size), &off, &n);
271 29 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 29 29
272 29 mPUSHi(r);
273 29 mPUSHu(off);
274 29 mPUSHu(n);
275 }
276  
277 SV *
278 _disposition(filename, inline_)
279 SV *filename
280 SV *inline_
281 CODE:
282 5 RETVAL = psf_disposition(aTHX_ filename, SvTRUE(inline_) ? 1 : 0);
283 OUTPUT:
284 RETVAL
285  
286 SV *
287 _etag(mtime, size)
288 SV *mtime
289 SV *size
290 CODE:
291 {
292 char buf[48];
293 1 psf_etag(buf, sizeof buf, (UV)SvUV(mtime), 0, (UV)SvUV(size));
294 1 RETVAL = newSVpv(buf, 0);
295 }
296 OUTPUT:
297 RETVAL
« xs/conditionalget.xs ↑ Summary xs/dbil.xs »