xs/sendfile.xs

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