xs/upload.xs

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