include/punk/punk_mpstream.h

filter: Statementstmt 93.2% 188 / 207 (6 uncoverable) Branchbran 82.2% 120 / 180 (33 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.1% 308 / 387 (39 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_dbq.h ↑ Summary xs/log.xs »
1 #ifndef PUNK_MPSTREAM_H
2 #define PUNK_MPSTREAM_H
3  
4 /* punk_mpstream.h - multipart/form-data, parsed as it arrives.
5 *
6 * The whole-buffer parser beside this one is correct and cheap for a form.
7 * It is the wrong shape for a file, because it needs the entire body as one
8 * contiguous scalar before it starts - and then copies each part out of it
9 * again.
10 *
11 * Measured, on this machine, for a 64 MiB upload:
12 *
13 * Punk adds 2.02x the file size in RSS (the slurp, then the part copy)
14 * parsing and copying is 75% of the time (not the read)
15 *
16 * On top of the server's own buffer that is three resident copies of every
17 * uploaded file, per concurrent upload, per worker - which is why Hyperman's
18 * max_body defaults to 16 MB.
19 *
20 * This parser reads the handle in chunks and never holds more than a window,
21 * so a file part costs a temp file and a few kilobytes rather than itself.
22 *
23 * ---- the hard part --------------------------------------------------------
24 *
25 * A boundary can straddle any read boundary. So content is never flushed
26 * right up to the end of what has been read: the last (delimiter length + 3)
27 * bytes are held back, because a delimiter cannot be entirely inside the
28 * flushed region without having been found there. Everything before that is
29 * safe to write out and forget, which is what makes the memory flat.
30 */
31  
32 #define PQ_MP_CHUNK 65536 /* read size */
33 #define PQ_MP_SPILL 65536 /* a part above this becomes a file */
34 #define PQ_MP_HDR_MAX 16384 /* one part's headers; a MIME header set
35 * larger than this is not a header set */
36  
37 /* Where spilled parts go.
38 *
39 * `upload_dir` on the app when it names one, else the system temp directory.
40 * Named by the application because it decides which FILESYSTEM: save() is a
41 * rename only within one, and that is the difference between free and another
42 * whole copy of a large file. */
43 2062 static SV *pq_upload_dir(pTHX_ AV *req) {
44 2062 HV *env = punk_req_env(aTHX_ req);
45 2062 ▶ SV **d = env ? hv_fetchs(env, "punk.upload_dir", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **d = env ? hv_fetchs(env, "punk.upload_dir", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2062 0 2062
46 /*
47 # uncoverable condition right count:2 class:ignore_covered_err
48 # uncoverable condition right count:3 class:ignore_covered_err
49 # uncoverable condition right count:4 class:ignore_covered_err */
50 2062 ▶ if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d));
Branch: 4 branches
BranchTrueFalseTotal
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 2043 19 2062
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 2043 0 2043
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 2043 0 2043
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 2043 0 2043
51 {
52 19 const char *e = PerlEnv_getenv("TMPDIR");
53 /*
54 # uncoverable condition left count:1 class:ignore_covered_err
55 # uncoverable condition left count:2 class:ignore_covered_err
56 # uncoverable condition right count:2 class:ignore_covered_err */
57 19 ▶ return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0));
Branch: 2 branches
BranchTrueFalseTotal
return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0)); 0 19 19
return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0)); 0 0 0
58 }
59 }
60  
61 /* Where the current part's bytes are going. */
62 typedef struct {
63 SV *sv; /* small parts: accumulated here */
64 int fd; /* large parts: written here, or -1 */
65 SV *path; /* the temp file's path, or NULL */
66 UV len; /* bytes seen */
67 int is_file; /* a file part (has a filename) */
68 int err; /* errno of a write that fell short, else 0 */
69 } pq_sink;
70  
71 /* Temp file for a part that outgrew the threshold.
72 *
73 * O_EXCL, and the name owes NOTHING to the client's filename: a name is
74 * request bytes, and this workspace has enough reflected-path scars. It is
75 * the request's own random id plus a counter, in the configured directory.
76 */
77 1124 static int pq_sink_spill(pTHX_ pq_sink *s, SV *dir, UV seq) {
78 unsigned char rnd[9];
79 SV *path;
80 int fd;
81 /*
82 # uncoverable branch true
83 # uncoverable condition left count:1 class:ignore_covered_err */
84 1124 ▶ if (s->fd >= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) return 1; 0 1124 1124
85 1124 pk_ent_take(aTHX_ rnd, sizeof rnd);
86 1124 path = newSVsv(dir);
87 /*
88 # uncoverable branch false class:ignore_covered_err
89 # uncoverable condition right count:1 class:ignore_covered_err
90 # uncoverable condition right count:2 class:ignore_covered_err */
91 1124 ▶ if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/");
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/"); 1124 0 1124
if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/"); 1124 0 1124
92 1124 sv_catpvs(path, "punk-up-");
93 1124 { SV *b64 = pk_b64url(aTHX_ rnd, sizeof rnd);
94 1124 sv_catsv(path, b64);
95 1124 SvREFCNT_dec(b64);
96 }
97 1124 sv_catpvf(path, "-%" UVuf, seq);
98 1124 fd = PerlLIO_open3(SvPVX(path), O_WRONLY | O_CREAT | O_EXCL, 0600);
99 /*
100 # uncoverable branch true
101 # uncoverable condition left count:1 class:ignore_covered_err */
102 1124 ▶ if (fd < 0) { SvREFCNT_dec(path); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { SvREFCNT_dec(path); return 0; } 0 1124 1124
103 /* whatever was accumulated before the threshold goes out first */
104 /*
105 # uncoverable branch false
106 # uncoverable condition right count:1 class:ignore_covered_err
107 # uncoverable condition right count:2 class:ignore_covered_err */
108 1124 ▶ if (s->sv && SvCUR(s->sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->sv && SvCUR(s->sv)) { 1124 0 1124
if (s->sv && SvCUR(s->sv)) { 1124 0 1124
109 /*
110 # uncoverable branch true
111 # uncoverable condition left count:1 class:ignore_covered_err */
112 1124 ▶ if (PerlLIO_write(fd, SvPVX(s->sv), SvCUR(s->sv)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_write(fd, SvPVX(s->sv), SvCUR(s->sv)) < 0) { 0 1124 1124
113 0 PerlLIO_close(fd); SvREFCNT_dec(path); return 0; /* # uncoverable statement */
114 }
115 1124 SvCUR_set(s->sv, 0);
116 }
117 1124 s->fd = fd;
118 1124 s->path = path;
119 1124 return 1;
120 }
121  
122 7132 static void pq_sink_write(pTHX_ pq_sink *s, const char *p, STRLEN n,
123 SV *dir, UV seq) {
124 7132 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 5 7127 7132
125 7127 s->len += n;
126 7127 ▶ if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL)
Branch: 3 branches
BranchTrueFalseTotal
if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL) 6002 1125 7127
if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL) 2308 3694 6002
if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL) 1124 1184 2308
127 1124 (void)pq_sink_spill(aTHX_ s, dir, seq);
128 7127 ▶ if (s->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) { 2249 4878 7127
129 /* a short write (a full disk) is remembered and refused when the
130 * part ends: `size` must never say more than is on disk */
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err
134 # uncoverable condition right count:1 class:ignore_covered_err
135 # uncoverable condition left count:2 class:ignore_covered_err */
136 2249 ▶ if (!s->err && PerlLIO_write(s->fd, p, n) != (SSize_t)n)
Branch: 2 branches
BranchTrueFalseTotal
if (!s->err && PerlLIO_write(s->fd, p, n) != (SSize_t)n) 2249 0 2249
if (!s->err && PerlLIO_write(s->fd, p, n) != (SSize_t)n) 0 2249 2249
137 /*
138 # uncoverable statement
139 # uncoverable condition left count:1
140 # uncoverable condition right count:1 */
141 0 ▶ s->err = errno ? errno : EIO;
Branch: 1 branch
BranchTrueFalseTotal
s->err = errno ? errno : EIO; 0 0 0
142 2249 return;
143 }
144 4878 ▶ if (!s->sv) s->sv = newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (!s->sv) s->sv = newSVpvs(""); 3038 1840 4878
145 4878 sv_catpvn(s->sv, p, n);
146 }
147  
148 /* What bounds the read.
149 *
150 * `left` counts down CONTENT_LENGTH when there is one, so a body that lies
151 * about its length cannot make this read for ever. When there is none it is
152 * -1 and the walk runs to EOF, which is the only thing it can do - and which
153 * on HTTP/2 and HTTP/3 is the ordinary case, not the exotic one, because both
154 * forbid Transfer-Encoding and a streamed upload declares no length at all.
155 * `max` is the route's max_body ceiling and is what bounds that case; 0 is
156 * none. `got` is what has been taken off the handle, which is the number the
157 * ceiling is about. */
158 typedef struct { IV left; IV got; IV max; } pq_mp_lim;
159  
160 /* Read up to `want` more bytes onto the end of buf. Returns 0 at EOF with
161 * nothing added, and croaks past the ceiling. */
162 8172 static int pq_mp_fill(pTHX_ PerlIO *fp, SV *buf, STRLEN want, pq_mp_lim *lim) {
163 8172 STRLEN have = SvCUR(buf);
164 SSize_t got;
165 char *d;
166 8172 ▶ if (lim->left == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lim->left == 0) return 0; 1 8171 8172
167 8171 ▶ if (lim->left > 0 && (IV)want > lim->left) want = (STRLEN)lim->left;
Branch: 2 branches
BranchTrueFalseTotal
if (lim->left > 0 && (IV)want > lim->left) want = (STRLEN)lim->left; 79 8092 8171
if (lim->left > 0 && (IV)want > lim->left) want = (STRLEN)lim->left; 38 41 79
168 /*
169 # uncoverable branch true
170 # uncoverable condition left count:1 class:ignore_covered_err */
171 8171 ▶ if (!want) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) return 0; 0 8171 8171
172 8171 ▶ d = SvGROW(buf, have + want + 1);
Branch: 2 branches
BranchTrueFalseTotal
d = SvGROW(buf, have + want + 1); 8171 0 8171
d = SvGROW(buf, have + want + 1); 4102 4069 8171
173 8171 got = PerlIO_read(fp, d + have, want);
174 /*
175 # uncoverable branch true
176 # uncoverable condition left count:1 class:ignore_covered_err */
177 8171 ▶ if (got <= 0) { lim->left = 0; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) { lim->left = 0; return 0; } 0 8171 8171
178 8171 SvCUR_set(buf, have + (STRLEN)got);
179 8171 SvPVX(buf)[have + got] = '\0';
180 8171 ▶ if (lim->left > 0) lim->left -= got;
Branch: 1 branch
BranchTrueFalseTotal
if (lim->left > 0) lim->left -= got; 79 8092 8171
181 8171 lim->got += (IV)got;
182 8171 ▶ if (lim->max && lim->got > lim->max)
Branch: 2 branches
BranchTrueFalseTotal
if (lim->max && lim->got > lim->max) 8092 79 8171
if (lim->max && lim->got > lim->max) 2020 6072 8092
183 2020 croak("Punk::Request: the request body passed %" IVdf " bytes, "
184 "the ceiling this read was given", lim->max);
185 6151 return 1;
186 }
187  
188 /* Drop the first n bytes of buf. */
189 15280 static void pq_mp_consume(pTHX_ SV *buf, STRLEN n) {
190 15280 STRLEN have = SvCUR(buf);
191 /*
192 # uncoverable branch true
193 # uncoverable condition left count:1 class:ignore_covered_err */
194 15280 ▶ if (n >= have) { SvCUR_set(buf, 0); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= have) { SvCUR_set(buf, 0); return; } 0 15280 15280
195 15280 Move(SvPVX(buf) + n, SvPVX(buf), have - n, char);
196 15280 SvCUR_set(buf, have - n);
197 15280 SvPVX(buf)[have - n] = '\0';
198 }
199  
200 /* Let go of whatever the sink still holds: close the descriptor, remove the
201 * file, free the buffer. A part that was handed on has cleared the fields it
202 * gave away, so this takes only what nobody else owns. */
203 5105 static void pq_sink_drop(pTHX_ pq_sink *s) {
204 5105 ▶ if (s->fd >= 0) { (void)PerlLIO_close(s->fd); s->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) { (void)PerlLIO_close(s->fd); s->fd = -1; } 1100 4005 5105
205 5105 ▶ if (s->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) { 1100 4005 5105
206 1100 (void)PerlLIO_unlink(SvPVX(s->path));
207 1100 SvREFCNT_dec(s->path);
208 1100 s->path = NULL;
209 }
210 5105 ▶ if (s->sv) { SvREFCNT_dec(s->sv); s->sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->sv) { SvREFCNT_dec(s->sv); s->sv = NULL; } 2044 3061 5105
211 5105 }
212  
213 2062 static int pq_sink_free(pTHX_ SV *sv, MAGIC *mg) {
214 PERL_UNUSED_ARG(mg);
215 /*
216 # uncoverable branch false
217 # uncoverable condition right count:1 class:ignore_covered_err
218 # uncoverable condition right count:2 class:ignore_covered_err */
219 2062 ▶ if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink))
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink)) 2062 0 2062
if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink)) 2062 0 2062
220 2062 pq_sink_drop(aTHX_ (pq_sink *)SvPVX(sv));
221 2062 return 0;
222 }
223 static MGVTBL pq_sink_vtbl = { NULL, NULL, NULL, NULL, pq_sink_free,
224 NULL, NULL, NULL };
225  
226 /* The sink for a walk, owned by a mortal.
227 *
228 * The walk croaks from inside pq_mp_fill when the body passes its ceiling,
229 * with a part half read: a descriptor open, a temp file the request has not
230 * been told about, a buffer nobody else points at. A sink on the C stack is
231 * gone by then and takes none of it with it. This one is freed with the
232 * temporaries, on that path and on the ordinary one alike. */
233 2062 static pq_sink *pq_sink_guard(pTHX) {
234 2062 SV *g = sv_2mortal(newSV(sizeof(pq_sink)));
235 2062 pq_sink *s = (pq_sink *)SvPVX(g);
236 2062 SvPOK_on(g);
237 2062 SvCUR_set(g, sizeof(pq_sink));
238 2062 s->sv = NULL; s->fd = -1; s->path = NULL; s->len = 0; s->is_file = 0;
239 2062 s->err = 0;
240 2062 sv_magicext(g, NULL, PERL_MAGIC_ext, &pq_sink_vtbl, NULL, 0);
241 2062 return s;
242 }
243  
244 /* Removing the temp files.
245 *
246 * Attached as magic to the list itself, so it fires when the REQUEST is
247 * freed - which happens on every exit path there is, including a handler
248 * that died and a client that vanished. Hooking punk_deliver instead would
249 * have leaked on the four dispatcher exits that never reach it, which is the
250 * finding Punk::Plugin::RequestId already paid for.
251 *
252 * A part that save() renamed is marked `moved` and is not ours any more; the
253 * entry is dropped from the list at that point rather than unlinked here,
254 * because unlinking where the caller just put their file would be worse than
255 * leaking.
256 */
257 2062 static int pq_tmp_free(pTHX_ SV *sv, MAGIC *mg) {
258 2062 AV *av = (AV *)sv;
259 SSize_t i, n;
260 PERL_UNUSED_ARG(mg);
261 /*
262 # uncoverable branch true
263 # uncoverable condition left count:1 class:ignore_covered_err
264 # uncoverable condition left count:2 class:ignore_covered_err */
265 2062 ▶ if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0; 2062 0 2062
if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0; 0 2062 2062
266 2062 n = av_len(av) + 1;
267 2086 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24 2062 2086
268 24 SV **e = av_fetch(av, i, 0);
269 /*
270 # uncoverable condition right count:1 class:ignore_covered_err
271 # uncoverable condition right count:2 class:ignore_covered_err
272 # uncoverable condition right count:4 class:ignore_covered_err */
273 24 ▶ if (e && *e && SvOK(*e) && SvCUR(*e))
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) 24 0 24
if (e && *e && SvOK(*e) && SvCUR(*e)) 24 0 24
if (e && *e && SvOK(*e) && SvCUR(*e)) 23 1 24
if (e && *e && SvOK(*e) && SvCUR(*e)) 23 0 23
274 23 (void)PerlLIO_unlink(SvPVX(*e));
275 }
276 2062 return 0;
277 }
278 static MGVTBL pq_tmp_vtbl = { NULL, NULL, NULL, NULL, pq_tmp_free,
279 NULL, NULL, NULL };
280  
281 2062 static void pq_tmp_own(pTHX_ AV *av) {
282 2062 sv_magicext((SV *)av, NULL, PERL_MAGIC_ext, &pq_tmp_vtbl, NULL, 0);
283 2062 }
284  
285 /* The parse. Returns the number of parts seen. */
286 2062 static IV pq_parse_multipart_stream(pTHX_ PerlIO *fp, IV clen, IV max,
287 const char *bnd, STRLEN bl,
288 HV *form, HV *uploads, SV *dir,
289 AV *tempfiles) {
290 2062 SV *buf = sv_2mortal(newSVpvs(""));
291 2062 SV *dsv = sv_2mortal(newSVpvs("--"));
292 2062 SV *ndsv = sv_2mortal(newSVpvs("\r\n--"));
293 const char *D, *ND;
294 STRLEN Dl, NDl, keep;
295 pq_mp_lim lim;
296 2062 IV parts = 0;
297 2062 UV seq = 0;
298 2062 pq_sink *sink = pq_sink_guard(aTHX);
299  
300 2062 ▶ lim.left = clen > 0 ? clen : -1;
Branch: 1 branch
BranchTrueFalseTotal
lim.left = clen > 0 ? clen : -1; 38 2024 2062
301 2062 lim.got = 0;
302 2062 lim.max = max > 0 ? max : 0;
303  
304 2062 sv_catpvn(dsv, bnd, bl);
305 2062 sv_catpvn(ndsv, bnd, bl);
306 2062 D = SvPVX(dsv); Dl = SvCUR(dsv);
307 2062 ND = SvPVX(ndsv); NDl = SvCUR(ndsv);
308 2062 keep = NDl + 3; /* a delimiter cannot hide in less */
309  
310 /* the opening delimiter */
311 2062 for (;;) {
312 4124 char *b = SvPVX(buf);
313 8248 char *hit = SvCUR(buf) >= Dl
314 4124 ▶ ? ninstr(b, b + SvCUR(buf), (char *)D, (char *)D + Dl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? ninstr(b, b + SvCUR(buf), (char *)D, (char *)D + Dl) : NULL; 2062 2062 4124
315 4124 ▶ if (hit) { pq_mp_consume(aTHX_ buf, (STRLEN)(hit - b) + Dl); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (hit) { pq_mp_consume(aTHX_ buf, (STRLEN)(hit - b) + Dl); break; } 2061 2063 4124
316 2063 ▶ if (SvCUR(buf) > keep) pq_mp_consume(aTHX_ buf, SvCUR(buf) - keep);
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(buf) > keep) pq_mp_consume(aTHX_ buf, SvCUR(buf) - keep); 1 2062 2063
317 2063 ▶ if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return 0; 1 2062 2063
318 }
319  
320 1023 for (;;) {
321 3084 const char *hend = NULL;
322 3084 const char *disp = NULL, *ctype = NULL, *name = NULL, *fname = NULL;
323 3084 STRLEN displ = 0, ctypel = 0, namel = 0, fnamel = 0;
324 /* "--" here is the closing delimiter; CRLF starts another part */
325 /*
326 # uncoverable branch true
327 # uncoverable condition left count:1 class:ignore_covered_err */
328 3084 ▶ while (SvCUR(buf) < 2)
Branch: 1 branch
BranchTrueFalseTotal
while (SvCUR(buf) < 2) 0 3084 3084
329 /*
330 # uncoverable statement class:ignore_covered_err
331 # uncoverable branch true class:ignore_covered_err
332 # uncoverable branch false class:ignore_covered_err
333 # uncoverable condition left count:1
334 # uncoverable condition right count:1 */
335 41 ▶ if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return parts;
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return parts; 0 0 0
336 3084 ▶ if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts; /* # uncoverable condition right count:2 class:ignore_covered_err */ 41 3043 3084
if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts; /* # uncoverable condition right count:2 class:ignore_covered_err */ 41 0 41
337 /*
338 # uncoverable branch true class:ignore_covered_err
339 # uncoverable condition left count:1 class:ignore_covered_err
340 # uncoverable condition left count:2 class:ignore_covered_err */
341 3043 ▶ if (SvPVX(buf)[0] != '\r' || SvPVX(buf)[1] != '\n') return parts;
Branch: 2 branches
BranchTrueFalseTotal
if (SvPVX(buf)[0] != '\r' || SvPVX(buf)[1] != '\n') return parts; 3043 0 3043
if (SvPVX(buf)[0] != '\r' || SvPVX(buf)[1] != '\n') return parts; 0 3043 3043
342 3043 pq_mp_consume(aTHX_ buf, 2);
343  
344 /* the part's headers, which are small by definition */
345 0 for (;;) { /* # uncoverable statement class:ignore_covered_err */
346 3043 char *b = SvPVX(buf);
347 3043 { const char *crlf2 = "\r\n\r\n";
348 3043 hend = ninstr(b, b + SvCUR(buf),
349 (char *)crlf2, (char *)crlf2 + 4); }
350 /*
351 # uncoverable branch false
352 # uncoverable condition right count:1 class:ignore_covered_err */
353 3043 ▶ if (hend) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hend) break; 3043 0 3043
354 /*
355 # uncoverable statement
356 # uncoverable branch true
357 # uncoverable branch false
358 # uncoverable condition left count:1
359 # uncoverable condition right count:1 */
360 0 ▶ if (SvCUR(buf) > PQ_MP_HDR_MAX) return parts;
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(buf) > PQ_MP_HDR_MAX) return parts; 0 0 0
361 /*
362 # uncoverable statement
363 # uncoverable branch true
364 # uncoverable branch false
365 # uncoverable condition left count:1
366 # uncoverable condition right count:1 */
367 0 ▶ if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return parts;
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) return parts; 0 0 0
368 }
369 { /* the same header reading the whole-buffer parser does */
370 3043 const char *hp = SvPVX(buf);
371 /*
372 # uncoverable branch false class:ignore_covered_err
373 # uncoverable condition right count:1 class:ignore_covered_err */
374 3079 ▶ while (hp < hend) {
Branch: 1 branch
BranchTrueFalseTotal
while (hp < hend) { 3079 0 3079
375 3079 const char *crlf = "\r\n";
376 3079 const char *le = ninstr((char *)hp, (char *)hend,
377 (char *)crlf, (char *)crlf + 2);
378 3079 ▶ const char *lend = le ? le : hend;
Branch: 1 branch
BranchTrueFalseTotal
const char *lend = le ? le : hend; 36 3043 3079
379 3079 STRLEN ll = (STRLEN)(lend - hp);
380 3079 ▶ if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3079 0 3079
if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3043 36 3079
381 3043 disp = hp + 20; displ = ll - 20;
382 }
383 /*
384 # uncoverable branch false
385 # uncoverable condition right count:1 class:ignore_covered_err
386 # uncoverable condition right count:2 class:ignore_covered_err */
387 36 ▶ else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) { 36 0 36
else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) { 36 0 36
388 36 ctype = hp + 13;
389 72 ▶ while (ctype < hp + ll && *ctype == ' ') ctype++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (ctype < hp + ll && *ctype == ' ') ctype++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 72 0 72
while (ctype < hp + ll && *ctype == ' ') ctype++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 36 36 72
390 36 ctypel = (STRLEN)((hp + ll) - ctype);
391 }
392 3079 ▶ if (!le) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!le) break; 3043 36 3079
393 36 hp = le + 2;
394 }
395 /*
396 # uncoverable branch false class:ignore_covered_err
397 # uncoverable condition right count:1 class:ignore_covered_err */
398 3043 ▶ if (disp) {
Branch: 1 branch
BranchTrueFalseTotal
if (disp) { 3043 0 3043
399 3043 name = pq_hdr_param(disp, displ, "name", &namel);
400 3043 fname = pq_hdr_param(disp, displ, "filename", &fnamel);
401 }
402 }
403 /* headers are copied out before the buffer moves under them */
404 {
405 3043 ▶ SV *nsv = name ? sv_2mortal(newSVpvn(name, namel)) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *nsv = name ? sv_2mortal(newSVpvn(name, namel)) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3043 0 3043
406 3043 ▶ SV *fsv = fname ? sv_2mortal(newSVpvn(fname, fnamel)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *fsv = fname ? sv_2mortal(newSVpvn(fname, fnamel)) : NULL; 1184 1859 3043
407 36 ▶ SV *tsv = (ctype && ctypel)
Branch: 1 branch
BranchTrueFalseTotal
SV *tsv = (ctype && ctypel) 36 0 36
408 3079 ▶ ? sv_2mortal(newSVpvn(ctype, ctypel)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newSVpvn(ctype, ctypel)) : NULL; 36 3007 3043
409  
410 3043 pq_mp_consume(aTHX_ buf, (STRLEN)((hend + 4) - SvPVX(buf)));
411  
412 3043 pq_sink_drop(aTHX_ sink);
413 3043 sink->len = 0; sink->is_file = fsv ? 1 : 0;
414 3043 seq++;
415  
416 /* the content, up to the next delimiter */
417 4089 for (;;) {
418 7132 char *b = SvPVX(buf);
419 14264 char *hit = SvCUR(buf) >= NDl
420 7132 ? ninstr(b, b + SvCUR(buf), (char *)ND, (char *)ND + NDl)
421 7132 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 7132 0 7132
422 7132 ▶ if (hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (hit) { 1023 6109 7132
423 1023 pq_sink_write(aTHX_ sink, b, (STRLEN)(hit - b), dir, seq);
424 1023 pq_mp_consume(aTHX_ buf, (STRLEN)(hit - b) + NDl);
425 1023 break;
426 }
427 /*
428 # uncoverable branch false
429 # uncoverable condition right count:1 class:ignore_covered_err */
430 6109 ▶ if (SvCUR(buf) > keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(buf) > keep) { 6109 0 6109
431 6109 STRLEN flush = SvCUR(buf) - keep;
432 6109 pq_sink_write(aTHX_ sink, b, flush, dir, seq);
433 6109 pq_mp_consume(aTHX_ buf, flush);
434 }
435 /*
436 # uncoverable branch true
437 # uncoverable condition left count:1 class:ignore_covered_err */
438 6109 ▶ if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_mp_fill(aTHX_ fp, buf, PQ_MP_CHUNK, &lim)) { 0 4089 4089
439 /* truncated: the part never ended */
440 0 pq_sink_write(aTHX_ sink, SvPVX(buf), SvCUR(buf), dir, seq); /* # uncoverable statement */
441 0 pq_mp_consume(aTHX_ buf, SvCUR(buf)); /* # uncoverable statement */
442 0 break; /* # uncoverable statement */
443 }
444 }
445 1023 ▶ if (sink->fd >= 0) { PerlLIO_close(sink->fd); sink->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (sink->fd >= 0) { PerlLIO_close(sink->fd); sink->fd = -1; } 24 999 1023
446 /*
447 # uncoverable branch true
448 # uncoverable condition left count:1 class:ignore_covered_err */
449 1023 ▶ if (sink->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->err) { 0 1023 1023
450 /* the guard frees the sink and its temp file on the way out */
451 0 int e = sink->err; /* # uncoverable statement */
452 0 croak("Punk::Request: upload could not be written: %s", /* # uncoverable statement */
453 Strerror(e)); /* # uncoverable statement */
454 }
455  
456 1023 parts++;
457 /*
458 # uncoverable branch true
459 # uncoverable condition left count:1 class:ignore_covered_err */
460 1023 ▶ if (!nsv) { /* a part with no name is noise */
Branch: 1 branch
BranchTrueFalseTotal
if (!nsv) { /* a part with no name is noise */ 0 1023 1023
461 0 pq_sink_drop(aTHX_ sink); /* # uncoverable statement */
462 0 continue; /* # uncoverable statement */
463 }
464  
465 1023 ▶ if (fsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsv) { 84 939 1023
466 84 HV *up = newHV();
467 SV *obj;
468 84 (void)hv_stores(up, "name", newSVsv(nsv));
469 84 (void)hv_stores(up, "filename", newSVsv(fsv));
470 84 ▶ (void)hv_stores(up, "type", tsv ? newSVsv(tsv)
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(up, "type", tsv ? newSVsv(tsv) 36 48 84
471 : newSVpvs("application/octet-stream"));
472 84 (void)hv_stores(up, "size", newSVuv(sink->len));
473 84 ▶ if (sink->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->path) { 24 60 84
474 /* on disk: the path, and the request owns its removal.
475 * The upload keeps a reference to its own slot in that
476 * list, so save()'s rename can clear it - see
477 * pq_tmp_free on why unlinking a moved file would be
478 * worse than leaking one. */
479 24 (void)hv_stores(up, "path", newSVsv(sink->path));
480 /*
481 # uncoverable branch false
482 # uncoverable condition right count:1 class:ignore_covered_err */
483 24 ▶ if (tempfiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (tempfiles) { 24 0 24
484 24 SV *slot = newSVsv(sink->path);
485 24 av_push(tempfiles, slot);
486 24 (void)hv_stores(up, "tmpslot", newRV_inc(slot));
487 }
488 /* the request's now, or the upload's: not the sink's
489 * to unlink */
490 24 SvREFCNT_dec(sink->path);
491 24 sink->path = NULL;
492 }
493 else {
494 60 ▶ (void)hv_stores(up, "content",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(up, "content", 60 0 60
495 sink->sv ? sink->sv : newSVpvs(""));
496 60 sink->sv = NULL;
497 }
498 84 obj = sv_bless(newRV_noinc((SV *)up),
499 gv_stashpvs("Punk::Upload", GV_ADD));
500 84 pq_hv_add(aTHX_ uploads, SvPVX(nsv), SvCUR(nsv), obj);
501 }
502 else {
503 /* a field: it stays in memory, because a form value is
504 * something the application is about to read */
505 /*
506 # uncoverable branch true
507 # uncoverable condition left count:1 class:ignore_covered_err */
508 939 ▶ if (sink->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->path) { 0 939 939
509 /* it outgrew the threshold anyway - read it back rather
510 * than hand a form value a path nobody expects */
511 0 SV *v = newSVpvs(""); /* # uncoverable statement */
512 0 int fd = PerlLIO_open(SvPVX(sink->path), O_RDONLY); /* # uncoverable statement */
513 /*
514 # uncoverable statement
515 # uncoverable branch true
516 # uncoverable branch false
517 # uncoverable condition left count:1
518 # uncoverable condition right count:1 */
519 0 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 0 0 0
520 char tmp[PQ_MP_CHUNK];
521 SSize_t n;
522 /*
523 # uncoverable statement
524 # uncoverable branch true
525 # uncoverable branch false
526 # uncoverable condition left count:1
527 # uncoverable condition right count:1 */
528 0 ▶ while ((n = PerlLIO_read(fd, tmp, sizeof tmp)) > 0)
Branch: 1 branch
BranchTrueFalseTotal
while ((n = PerlLIO_read(fd, tmp, sizeof tmp)) > 0) 0 0 0
529 0 sv_catpvn(v, tmp, (STRLEN)n); /* # uncoverable statement */
530 0 PerlLIO_close(fd); /* # uncoverable statement */
531 }
532 0 pq_hv_add(aTHX_ form, SvPVX(nsv), SvCUR(nsv), v); /* # uncoverable statement */
533 }
534 else {
535 939 pq_hv_add(aTHX_ form, SvPVX(nsv), SvCUR(nsv),
536 939 ▶ sink->sv ? sink->sv : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
sink->sv ? sink->sv : newSVpvs("")); 934 5 939
537 939 sink->sv = NULL;
538 }
539 }
540 }
541 }
542 }
543  
544 #endif /* PUNK_MPSTREAM_H */
« include/punk/punk_dbq.h ↑ Summary xs/log.xs »