xs/upload.xs

filter: Statementstmt 73.8% 62 / 84 Branchbran 60.4% 74 / 124 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.8% 136 / 208 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/i18n.xs ↑ Summary xs/websocket.xs »
1 MODULE = Punk PACKAGE = Punk::Upload
2  
3 PROTOTYPES: DISABLE
4  
5 # An uploaded file part from a multipart/form-data request - a blessed hashref
6 # the multipart parser (punk_multipart.h) built. lib/Punk/Upload.pm is docs.
7  
8 SV *
9 filename(self)
10 SV *self
11 ALIAS:
12 name = 1
13 type = 2
14 content = 3
15 path = 4
16 CODE:
17 {
18 static const char *const f[] = { "filename", "name", "type",
19 "content", "path" };
20 HV *h;
21 SV **v;
22 129 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 129 0 129
# uncoverable branch true 0 129 129
23 0 # uncoverable condition left count:1 class:ignore_covered_err
24 129 # uncoverable condition left count:2 class:ignore_covered_err
25 129 if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
26 croak("Punk::Upload: not an upload"); /* # uncoverable statement */
27 h = (HV *)SvRV(self);
28 v = hv_fetch(h, f[ix], (I32)strlen(f[ix]), 0);
29  
30 /* `content` on a spilled part reads the file back.
31 *
32 * That costs the file's size in memory, which is precisely what
33 * streaming it to disk avoided - so the POD names the cost, the way
34 * Plack::Request::Upload names its equivalent `slurp`. It is kept
35 129 ▶ * working rather than made to croak because every application that
Branch: 3 branches
BranchTrueFalseTotal
* working rather than made to croak because every application that 12 117 129
* working rather than made to croak because every application that 8 4 12
* working rather than made to croak because every application that 0 8 8
36 133 ▶ * already handles uploads calls it. */
Branch: 3 branches
BranchTrueFalseTotal
* already handles uploads calls it. */ 4 125 129
* already handles uploads calls it. */ 4 0 4
* already handles uploads calls it. */ 4 0 4
37 8 {
38 4 SV **pp = (ix == 3 && !(v && *v)) ? hv_fetchs(h, "path", 0) : NULL; /* # uncoverable condition left count:3 class:ignore_covered_err */
39 4 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
40 # uncoverable condition right count:3 class:ignore_covered_err
41 if (pp && *pp && SvOK(*pp)) {
42 22 ▶ PerlIO *fh = PerlIO_open(SvPV_nolen(*pp), "rb");
Branch: 1 branch
BranchTrueFalseTotal
PerlIO *fh = PerlIO_open(SvPV_nolen(*pp), "rb"); 18 4 22
43 18 RETVAL = newSVpvs("");
44 4 # uncoverable branch false
45 # uncoverable condition right count:1 class:ignore_covered_err
46 if (fh) {
47 char tmp[65536];
48 SSize_t n;
49 while ((n = PerlIO_read(fh, tmp, sizeof tmp)) > 0)
50 sv_catpvn(RETVAL, tmp, (STRLEN)n);
51 PerlIO_close(fh);
52 125 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 115 10 125
} 115 0 115
53 }
54 /* NOT an early XSRETURN here: with `OUTPUT: RETVAL` the generated
55 * code pushes RETVAL after this block, so returning early hands
56 * back ST(0) - which is `self`. That is exactly what it did, and
57 * `length($up->content)` came out as 30: the length of
58 * "Punk::Upload=HASH(0x...)". */
59 else RETVAL = (v && *v) ? newSVsv(*v) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
60 }
61 }
62 OUTPUT:
63 RETVAL
64  
65 # The client's file name reduced to a name: what follows the last / or \,
66 # without leading dots and without control bytes. undef when nothing is left.
67 # Safe to show; a path is still better built from a name the server chose.
68 SV *
69 basename(self)
70 SV *self
71 CODE:
72 {
73 SV **v;
74 40 STRLEN l, i, start = 0;
75 const char *p;
76 40 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 40 0 40
# uncoverable branch true 0 40 40
77 0 # uncoverable condition left count:1 class:ignore_covered_err
78 40 # uncoverable condition left count:2 class:ignore_covered_err
79 40 if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
80 40 ▶ croak("Punk::Upload: not an upload"); /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk::Upload: not an upload"); /* # uncoverable statement */ 40 0 40
croak("Punk::Upload: not an upload"); /* # uncoverable statement */ 40 0 40
croak("Punk::Upload: not an upload"); /* # uncoverable statement */ 40 0 40
81 40 v = hv_fetchs((HV *)SvRV(self), "filename", 0);
82 360 ▶ RETVAL = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSV(0); 320 40 360
83 320 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 300 20 320
# uncoverable branch false 12 288 300
84 40 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 32 8 40
85 32 # uncoverable condition right count:2 class:ignore_covered_err
86 # uncoverable condition right count:3 class:ignore_covered_err
87 if (v && *v && SvOK(*v)) {
88 32 p = SvPV_const(*v, l);
89 220 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 188 32 220
90 188 if (p[i] == '/' || p[i] == '\\') start = i + 1;
91 188 ▶ if (start < l) {
Branch: 2 branches
BranchTrueFalseTotal
if (start < l) { 176 12 188
if (start < l) { 4 172 176
92 172 STRLEN lead = 0;
93 /* control bytes first, dots second: stripping the dots
94 60 ▶ * before the bytes let "\x01.env" come out as ".env" */
Branch: 2 branches
BranchTrueFalseTotal
* before the bytes let "\x01.env" come out as ".env" */ 52 8 60
* before the bytes let "\x01.env" come out as ".env" */ 28 24 52
95 32 ▶ sv_setpvs(RETVAL, "");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(RETVAL, ""); 16 16 32
96 32 ▶ for (i = start; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < l; i++) { 8 24 32
97 24 ▶ unsigned char ch = (unsigned char)p[i];
Branch: 1 branch
BranchTrueFalseTotal
unsigned char ch = (unsigned char)p[i]; 0 24 24
98 if (ch < 0x20 || ch == 0x7f) continue;
99 sv_catpvn(RETVAL, (const char *)&ch, 1);
100 }
101 while (lead < SvCUR(RETVAL) && SvPVX(RETVAL)[lead] == '.') lead++;
102 if (lead) sv_chop(RETVAL, SvPVX(RETVAL) + lead);
103 if (!SvCUR(RETVAL)) sv_setsv(RETVAL, &PL_sv_undef);
104 # uncoverable branch true
105 # uncoverable condition left count:1 class:ignore_covered_err
106 else if (SvUTF8(*v)) SvUTF8_on(RETVAL);
107 }
108 }
109 }
110 OUTPUT:
111 RETVAL
112  
113 IV
114 size(self)
115 SV *self
116 CODE:
117 {
118 26 ▶ SV **v = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **v = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV) 26 0 26
119 52 ▶ ? hv_fetchs((HV *)SvRV(self), "size", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(self), "size", 0) : NULL; 26 0 26
120 26 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 26 0 26
# uncoverable condition right count:1 class:ignore_covered_err 26 0 26
# uncoverable condition right count:1 class:ignore_covered_err 18 8 26
121 # uncoverable condition right count:2 class:ignore_covered_err
122 RETVAL = (v && *v) ? SvIV(*v) : 0;
123 }
124 OUTPUT:
125 RETVAL
126  
127 # save($path): put the uploaded bytes at $path. Returns true.
128 #
129 # A RENAME when the part is already a file and the destination shares its
130 # filesystem - which is the whole point of streaming it there, and why
131 # upload_dir wants to be on the same filesystem as wherever things are kept.
132 # A copy otherwise, and a plain write when the bytes are in memory.
133 IV
134 save(self, path)
135 SV *self
136 SV *path
137 CODE:
138 {
139 HV *h;
140 SV **cv, **pp;
141 const char *p;
142 PerlIO *fh;
143 3 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 3 0 3
# uncoverable branch true 0 3 3
144 0 # uncoverable condition left count:1 class:ignore_covered_err
145 3 # uncoverable condition left count:2 class:ignore_covered_err
146 if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
147 croak("Punk::Upload: not an upload"); /* # uncoverable statement */
148 3 h = (HV *)SvRV(self);
149 {
150 STRLEN plen;
151 3 ▶ p = SvPV(path, plen);
Branch: 1 branch
BranchTrueFalseTotal
p = SvPV(path, plen); 1 2 3
152 1 /* open() stops at a NUL and the caller's allowlist did not:
153 * "x.php\0.txt" is x.php on disk */
154 2 if (memchr(p, 0, plen))
155 croak("Punk::Upload: save path contains a NUL byte");
156 2 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 1 1 2
} 1 0 1
} 1 0 1
157 1 pp = hv_fetchs(h, "path", 0);
158 1 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
1 0 1
159 # uncoverable condition right count:2 class:ignore_covered_err
160 # uncoverable condition right count:3 class:ignore_covered_err
161 1 if (pp && *pp && SvOK(*pp)) {
162 1 const char *from = SvPV_nolen(*pp);
163 # uncoverable branch false
164 # uncoverable condition right count:1 class:ignore_covered_err
165 if (PerlLIO_rename(from, p) == 0) {
166 /* it is no longer ours to clean up, and no longer a temp
167 1 * file: the object now points at where the caller put it */
168 1 ▶ (void)hv_stores(h, "path", newSVsv(path));
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(h, "path", newSVsv(path)); 1 0 1
(void)hv_stores(h, "path", newSVsv(path)); 1 0 1
(void)hv_stores(h, "path", newSVsv(path)); 1 0 1
169 (void)hv_stores(h, "moved", newSViv(1));
170 1 /* the temp file is gone from where the request left it, so
171 1 * the cleanup must not go looking for it - and must not
172 * unlink the place the caller just asked for */
173 {
174 0 SV **t = hv_fetchs(h, "tmpslot", 0);
175 # uncoverable branch false
176 # uncoverable condition right count:1 class:ignore_covered_err
177 0 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 0 0 0
178 # uncoverable condition right count:3 class:ignore_covered_err
179 0 if (t && *t && SvROK(*t)) sv_setsv(SvRV(*t), &PL_sv_undef);
180 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
181 0 RETVAL = 1;
182 XSRETURN_IV(RETVAL);
183 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
184 0 ▶ { /* across filesystems rename fails; copy instead */
Branch: 1 branch
BranchTrueFalseTotal
{ /* across filesystems rename fails; copy instead */ 0 0 0
185 0 PerlIO *in = PerlIO_open(from, "rb"); /* # uncoverable statement */
186 0 char buf[65536];
187 0 SSize_t n;
188 # uncoverable statement
189 # uncoverable branch true
190 0 # uncoverable branch false
191 0 ▶ # uncoverable branch true count:2
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true count:2 0 0 0
192 0 # uncoverable branch false count:2
193 # uncoverable branch true count:3
194 0 # uncoverable branch false count:3
195 0 # uncoverable condition left count:1
196 # uncoverable condition right count:1
197 if (!in) croak("Punk::Upload: cannot read '%s': %s",
198 from, Strerror(errno)); /* # uncoverable statement */
199 1 fh = PerlIO_open(p, "wb"); /* # uncoverable statement */
200 # uncoverable statement
201 1 # uncoverable branch true
202 2 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 1 0 1
# uncoverable branch false 1 0 1
# uncoverable branch false 1 0 1
203 1 # uncoverable condition left count:1
204 1 ▶ # uncoverable condition right count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 0 1 1
205 if (!fh) { PerlIO_close(in);
206 1 ▶ croak("Punk::Upload: cannot write '%s': %s", /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk::Upload: cannot write '%s': %s", /* # uncoverable statement */ 1 0 1
croak("Punk::Upload: cannot write '%s': %s", /* # uncoverable statement */ 0 1 1
207 0 p, Strerror(errno)); } /* # uncoverable statement */
208 0 # uncoverable statement
209 0 # uncoverable branch true
210 # uncoverable branch false
211 1 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 1 1
212 0 # uncoverable condition right count:1
213 while ((n = PerlIO_read(in, buf, sizeof buf)) > 0)
214 # uncoverable statement
215 1 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 1 1
216 # uncoverable branch false
217 # uncoverable condition left count:1
218 # uncoverable condition right count:1
219 if (PerlIO_write(fh, buf, n) != n) {
220 int e = errno; /* # uncoverable statement */
221 PerlIO_close(in); PerlIO_close(fh); /* # uncoverable statement */
222 croak("Punk::Upload: short write to '%s': %s", p, /* # uncoverable statement */
223 Strerror(e)); /* # uncoverable statement */
224 }
225 PerlIO_close(in); /* # uncoverable statement */
226 # uncoverable statement
227 # uncoverable branch true
228 # uncoverable branch false
229 # uncoverable condition left count:1
230 # uncoverable condition right count:1
231 if (PerlIO_close(fh) != 0)
232 croak("Punk::Upload: cannot finish '%s': %s", p, /* # uncoverable statement */
233 Strerror(errno)); /* # uncoverable statement */
234 RETVAL = 1; /* # uncoverable statement */
235 XSRETURN_IV(RETVAL); /* # uncoverable statement */
236 }
237 }
238  
239 cv = hv_fetchs(h, "content", 0);
240 {
241 STRLEN cl = 0;
242 # uncoverable condition right count:1 class:ignore_covered_err
243 # uncoverable condition right count:2 class:ignore_covered_err
244 # uncoverable condition right count:3 class:ignore_covered_err
245 const char *c = (cv && *cv && SvOK(*cv)) ? SvPV_const(*cv, cl) : "";
246 fh = PerlIO_open(p, "wb");
247 # uncoverable branch true
248 # uncoverable branch true count:2 class:ignore_covered_err
249 # uncoverable branch false count:3 class:ignore_covered_err
250 # uncoverable condition left count:1 class:ignore_covered_err
251 if (!fh) croak("Punk::Upload: cannot write '%s': %s",
252 p, Strerror(errno)); /* # uncoverable statement */
253 # uncoverable branch true
254 # uncoverable condition left count:1 class:ignore_covered_err
255 # uncoverable condition right count:1 class:ignore_covered_err
256 # uncoverable condition left count:2 class:ignore_covered_err
257 if (cl && PerlIO_write(fh, c, cl) != (SSize_t)cl) {
258 int e = errno; /* # uncoverable statement */
259 PerlIO_close(fh); /* # uncoverable statement */
260 croak("Punk::Upload: short write to '%s': %s", p, Strerror(e)); /* # uncoverable statement */
261 }
262 # uncoverable branch true
263 # uncoverable condition left count:1 class:ignore_covered_err
264 if (PerlIO_close(fh) != 0)
265 croak("Punk::Upload: cannot finish '%s': %s", p, /* # uncoverable statement */
266 Strerror(errno)); /* # uncoverable statement */
267 }
268 RETVAL = 1;
269 }
270 OUTPUT:
271 RETVAL
272  
273 # _disown: the bytes have been moved somewhere permanent, so the request's
274 # cleanup must stop tracking them. Punk::Plugin::Blob calls this after moving
275 # an upload into the store, for the same reason save() clears its own slot:
276 # unlinking a file the caller was just handed would be far worse than leaking
277 # one.
278 void
279 _disown(self)
280 SV *self
281 CODE:
282 {
283 HV *h;
284 SV **t;
285 5 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 5 0 5
# uncoverable branch true 0 5 5
286 5 # uncoverable condition left count:1 class:ignore_covered_err
287 5 # uncoverable condition left count:2 class:ignore_covered_err
288 5 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_EMPTY;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_EMPTY; 0 5 5
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_EMPTY; 0 0 0
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_EMPTY; 0 0 0
289 5 h = (HV *)SvRV(self);
290 t = hv_fetchs(h, "tmpslot", 0);
291 # uncoverable branch true
292 # uncoverable condition left count:1 class:ignore_covered_err
293 # uncoverable condition left count:2 class:ignore_covered_err
294 # uncoverable condition right count:2 class:ignore_covered_err
295 # uncoverable condition left count:3 class:ignore_covered_err
296 # uncoverable condition right count:3 class:ignore_covered_err
297 if (t && *t && SvROK(*t)) sv_setsv(SvRV(*t), &PL_sv_undef);
298 (void)hv_stores(h, "moved", newSViv(1));
299 }
« xs/i18n.xs ↑ Summary xs/websocket.xs »