include/punk/punk_mpstream.h

filter: Statementstmt 90.8% 188 / 207 Branchbran 65.5% 118 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.0% 306 / 387 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cachefront.h ↑ Summary xs/txn.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 536 static SV *pq_upload_dir(pTHX_ AV *req) {
44 536 HV *env = punk_req_env(aTHX_ req);
45 536 ▶ SV **d = env ? hv_fetchs(env, "punk.upload_dir", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **d = env ? hv_fetchs(env, "punk.upload_dir", 0) : NULL; 536 0 536
46 536 ▶ 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)); 522 14 536
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 522 0 522
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 522 0 522
if (d && *d && SvOK(*d) && SvCUR(*d)) return sv_2mortal(newSVsv(*d)); 522 0 522
47 {
48 14 const char *e = PerlEnv_getenv("TMPDIR");
49 14 ▶ return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0));
Branch: 2 branches
BranchTrueFalseTotal
return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0)); 0 14 14
return sv_2mortal(newSVpv(e && *e ? e : "/tmp", 0)); 0 0 0
50 }
51 }
52  
53 /* Where the current part's bytes are going. */
54 typedef struct {
55 SV *sv; /* small parts: accumulated here */
56 int fd; /* large parts: written here, or -1 */
57 SV *path; /* the temp file's path, or NULL */
58 UV len; /* bytes seen */
59 int is_file; /* a file part (has a filename) */
60 int err; /* errno of a write that fell short, else 0 */
61 } pq_sink;
62  
63 /* Temp file for a part that outgrew the threshold.
64 *
65 * O_EXCL, and the name owes NOTHING to the client's filename: a name is
66 * request bytes, and this workspace has enough reflected-path scars. It is
67 * the request's own random id plus a counter, in the configured directory.
68 */
69 293 static int pq_sink_spill(pTHX_ pq_sink *s, SV *dir, UV seq) {
70 unsigned char rnd[9];
71 SV *path;
72 int fd;
73 293 ▶ if (s->fd >= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) return 1; 0 293 293
74 293 pk_ent_take(aTHX_ rnd, sizeof rnd);
75 293 path = newSVsv(dir);
76 293 ▶ if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/");
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/"); 293 0 293
if (SvCUR(path) && SvEND(path)[-1] != '/') sv_catpvs(path, "/"); 293 0 293
77 293 sv_catpvs(path, "punk-up-");
78 293 { SV *b64 = pk_b64url(aTHX_ rnd, sizeof rnd);
79 293 sv_catsv(path, b64);
80 293 SvREFCNT_dec(b64);
81 }
82 293 sv_catpvf(path, "-%" UVuf, seq);
83 293 fd = PerlLIO_open3(SvPVX(path), O_WRONLY | O_CREAT | O_EXCL, 0600);
84 293 ▶ if (fd < 0) { SvREFCNT_dec(path); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { SvREFCNT_dec(path); return 0; } 0 293 293
85 /* whatever was accumulated before the threshold goes out first */
86 293 ▶ if (s->sv && SvCUR(s->sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->sv && SvCUR(s->sv)) { 293 0 293
if (s->sv && SvCUR(s->sv)) { 293 0 293
87 293 ▶ 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 293 293
88 0 PerlLIO_close(fd); SvREFCNT_dec(path); return 0;
89 }
90 293 SvCUR_set(s->sv, 0);
91 }
92 293 s->fd = fd;
93 293 s->path = path;
94 293 return 1;
95 }
96  
97 1839 static void pq_sink_write(pTHX_ pq_sink *s, const char *p, STRLEN n,
98 SV *dir, UV seq) {
99 1839 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 1839 1839
100 1839 s->len += n;
101 1839 ▶ 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) 1539 300 1839
if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL) 611 928 1539
if (s->fd < 0 && s->is_file && s->len > PQ_MP_SPILL) 293 318 611
102 293 (void)pq_sink_spill(aTHX_ s, dir, seq);
103 1839 ▶ if (s->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) { 593 1246 1839
104 /* a short write (a full disk) is remembered and refused when the
105 * part ends: `size` must never say more than is on disk */
106 593 ▶ 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) 593 0 593
if (!s->err && PerlLIO_write(s->fd, p, n) != (SSize_t)n) 0 593 593
107 0 ▶ s->err = errno ? errno : EIO;
Branch: 1 branch
BranchTrueFalseTotal
s->err = errno ? errno : EIO; 0 0 0
108 593 return;
109 }
110 1246 ▶ if (!s->sv) s->sv = newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (!s->sv) s->sv = newSVpvs(""); 786 460 1246
111 1246 sv_catpvn(s->sv, p, n);
112 }
113  
114 /* What bounds the read.
115 *
116 * `left` counts down CONTENT_LENGTH when there is one, so a body that lies
117 * about its length cannot make this read for ever. When there is none it is
118 * -1 and the walk runs to EOF, which is the only thing it can do - and which
119 * on HTTP/2 and HTTP/3 is the ordinary case, not the exotic one, because both
120 * forbid Transfer-Encoding and a streamed upload declares no length at all.
121 * `max` is the route's max_body ceiling and is what bounds that case; 0 is
122 * none. `got` is what has been taken off the handle, which is the number the
123 * ceiling is about. */
124 typedef struct { IV left; IV got; IV max; } pq_mp_lim;
125  
126 /* Read up to `want` more bytes onto the end of buf. Returns 0 at EOF with
127 * nothing added, and croaks past the ceiling. */
128 2095 static int pq_mp_fill(pTHX_ PerlIO *fp, SV *buf, STRLEN want, pq_mp_lim *lim) {
129 2095 STRLEN have = SvCUR(buf);
130 SSize_t got;
131 char *d;
132 2095 ▶ if (lim->left == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lim->left == 0) return 0; 1 2094 2095
133 2094 ▶ 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; 71 2023 2094
if (lim->left > 0 && (IV)want > lim->left) want = (STRLEN)lim->left; 30 41 71
134 2094 ▶ if (!want) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) return 0; 0 2094 2094
135 2094 ▶ d = SvGROW(buf, have + want + 1);
Branch: 2 branches
BranchTrueFalseTotal
d = SvGROW(buf, have + want + 1); 2094 0 2094
d = SvGROW(buf, have + want + 1); 1058 1036 2094
136 2094 got = PerlIO_read(fp, d + have, want);
137 2094 ▶ if (got <= 0) { lim->left = 0; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) { lim->left = 0; return 0; } 0 2094 2094
138 2094 SvCUR_set(buf, have + (STRLEN)got);
139 2094 SvPVX(buf)[have + got] = '\0';
140 2094 ▶ if (lim->left > 0) lim->left -= got;
Branch: 1 branch
BranchTrueFalseTotal
if (lim->left > 0) lim->left -= got; 71 2023 2094
141 2094 lim->got += (IV)got;
142 2094 ▶ if (lim->max && lim->got > lim->max)
Branch: 2 branches
BranchTrueFalseTotal
if (lim->max && lim->got > lim->max) 2023 71 2094
if (lim->max && lim->got > lim->max) 505 1518 2023
143 505 croak("Punk::Request: the request body passed %" IVdf " bytes, "
144 "the ceiling this read was given", lim->max);
145 1589 return 1;
146 }
147  
148 /* Drop the first n bytes of buf. */
149 3947 static void pq_mp_consume(pTHX_ SV *buf, STRLEN n) {
150 3947 STRLEN have = SvCUR(buf);
151 3947 ▶ if (n >= have) { SvCUR_set(buf, 0); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= have) { SvCUR_set(buf, 0); return; } 0 3947 3947
152 3947 Move(SvPVX(buf) + n, SvPVX(buf), have - n, char);
153 3947 SvCUR_set(buf, have - n);
154 3947 SvPVX(buf)[have - n] = '\0';
155 }
156  
157 /* Let go of whatever the sink still holds: close the descriptor, remove the
158 * file, free the buffer. A part that was handed on has cleared the fields it
159 * gave away, so this takes only what nobody else owns. */
160 1322 static void pq_sink_drop(pTHX_ pq_sink *s) {
161 1322 ▶ 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; } 275 1047 1322
162 1322 ▶ if (s->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) { 275 1047 1322
163 275 (void)PerlLIO_unlink(SvPVX(s->path));
164 275 SvREFCNT_dec(s->path);
165 275 s->path = NULL;
166 }
167 1322 ▶ if (s->sv) { SvREFCNT_dec(s->sv); s->sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->sv) { SvREFCNT_dec(s->sv); s->sv = NULL; } 523 799 1322
168 1322 }
169  
170 536 static int pq_sink_free(pTHX_ SV *sv, MAGIC *mg) {
171 PERL_UNUSED_ARG(mg);
172 536 ▶ if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink))
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink)) 536 0 536
if (SvPOK(sv) && SvCUR(sv) == sizeof(pq_sink)) 536 0 536
173 536 pq_sink_drop(aTHX_ (pq_sink *)SvPVX(sv));
174 536 return 0;
175 }
176 static MGVTBL pq_sink_vtbl = { NULL, NULL, NULL, NULL, pq_sink_free,
177 NULL, NULL, NULL };
178  
179 /* The sink for a walk, owned by a mortal.
180 *
181 * The walk croaks from inside pq_mp_fill when the body passes its ceiling,
182 * with a part half read: a descriptor open, a temp file the request has not
183 * been told about, a buffer nobody else points at. A sink on the C stack is
184 * gone by then and takes none of it with it. This one is freed with the
185 * temporaries, on that path and on the ordinary one alike. */
186 536 static pq_sink *pq_sink_guard(pTHX) {
187 536 SV *g = sv_2mortal(newSV(sizeof(pq_sink)));
188 536 pq_sink *s = (pq_sink *)SvPVX(g);
189 536 SvPOK_on(g);
190 536 SvCUR_set(g, sizeof(pq_sink));
191 536 s->sv = NULL; s->fd = -1; s->path = NULL; s->len = 0; s->is_file = 0;
192 536 s->err = 0;
193 536 sv_magicext(g, NULL, PERL_MAGIC_ext, &pq_sink_vtbl, NULL, 0);
194 536 return s;
195 }
196  
197 /* Removing the temp files.
198 *
199 * Attached as magic to the list itself, so it fires when the REQUEST is
200 * freed - which happens on every exit path there is, including a handler
201 * that died and a client that vanished. Hooking punk_deliver instead would
202 * have leaked on the four dispatcher exits that never reach it, which is the
203 * finding Punk::Plugin::RequestId already paid for.
204 *
205 * A part that save() renamed is marked `moved` and is not ours any more; the
206 * entry is dropped from the list at that point rather than unlinked here,
207 * because unlinking where the caller just put their file would be worse than
208 * leaking.
209 */
210 536 static int pq_tmp_free(pTHX_ SV *sv, MAGIC *mg) {
211 536 AV *av = (AV *)sv;
212 SSize_t i, n;
213 PERL_UNUSED_ARG(mg);
214 536 ▶ if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0; 536 0 536
if (!av || SvTYPE((SV *)av) != SVt_PVAV) return 0; 0 536 536
215 536 n = av_len(av) + 1;
216 554 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 536 554
217 18 SV **e = av_fetch(av, i, 0);
218 18 ▶ if (e && *e && SvOK(*e) && SvCUR(*e))
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) 18 0 18
if (e && *e && SvOK(*e) && SvCUR(*e)) 18 0 18
if (e && *e && SvOK(*e) && SvCUR(*e)) 17 1 18
if (e && *e && SvOK(*e) && SvCUR(*e)) 17 0 17
219 17 (void)PerlLIO_unlink(SvPVX(*e));
220 }
221 536 return 0;
222 }
223 static MGVTBL pq_tmp_vtbl = { NULL, NULL, NULL, NULL, pq_tmp_free,
224 NULL, NULL, NULL };
225  
226 536 static void pq_tmp_own(pTHX_ AV *av) {
227 536 sv_magicext((SV *)av, NULL, PERL_MAGIC_ext, &pq_tmp_vtbl, NULL, 0);
228 536 }
229  
230 /* The parse. Returns the number of parts seen. */
231 536 static IV pq_parse_multipart_stream(pTHX_ PerlIO *fp, IV clen, IV max,
232 const char *bnd, STRLEN bl,
233 HV *form, HV *uploads, SV *dir,
234 AV *tempfiles) {
235 536 SV *buf = sv_2mortal(newSVpvs(""));
236 536 SV *dsv = sv_2mortal(newSVpvs("--"));
237 536 SV *ndsv = sv_2mortal(newSVpvs("\r\n--"));
238 const char *D, *ND;
239 STRLEN Dl, NDl, keep;
240 pq_mp_lim lim;
241 536 IV parts = 0;
242 536 UV seq = 0;
243 536 pq_sink *sink = pq_sink_guard(aTHX);
244  
245 536 ▶ lim.left = clen > 0 ? clen : -1;
Branch: 1 branch
BranchTrueFalseTotal
lim.left = clen > 0 ? clen : -1; 30 506 536
246 536 lim.got = 0;
247 536 lim.max = max > 0 ? max : 0;
248  
249 536 sv_catpvn(dsv, bnd, bl);
250 536 sv_catpvn(ndsv, bnd, bl);
251 536 D = SvPVX(dsv); Dl = SvCUR(dsv);
252 536 ND = SvPVX(ndsv); NDl = SvCUR(ndsv);
253 536 keep = NDl + 3; /* a delimiter cannot hide in less */
254  
255 /* the opening delimiter */
256 536 for (;;) {
257 1072 char *b = SvPVX(buf);
258 2144 char *hit = SvCUR(buf) >= Dl
259 1072 ▶ ? 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; 536 536 1072
260 1072 ▶ 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; } 535 537 1072
261 537 ▶ 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 536 537
262 537 ▶ 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 536 537
263 }
264  
265 281 for (;;) {
266 816 const char *hend = NULL;
267 816 const char *disp = NULL, *ctype = NULL, *name = NULL, *fname = NULL;
268 816 STRLEN displ = 0, ctypel = 0, namel = 0, fnamel = 0;
269 /* "--" here is the closing delimiter; CRLF starts another part */
270 816 ▶ while (SvCUR(buf) < 2)
Branch: 1 branch
BranchTrueFalseTotal
while (SvCUR(buf) < 2) 0 816 816
271 30 ▶ 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
272 816 ▶ if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts;
Branch: 2 branches
BranchTrueFalseTotal
if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts; 30 786 816
if (SvPVX(buf)[0] == '-' && SvPVX(buf)[1] == '-') return parts; 30 0 30
273 786 ▶ 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; 786 0 786
if (SvPVX(buf)[0] != '\r' || SvPVX(buf)[1] != '\n') return parts; 0 786 786
274 786 pq_mp_consume(aTHX_ buf, 2);
275  
276 /* the part's headers, which are small by definition */
277 0 for (;;) {
278 786 char *b = SvPVX(buf);
279 786 { const char *crlf2 = "\r\n\r\n";
280 786 hend = ninstr(b, b + SvCUR(buf),
281 (char *)crlf2, (char *)crlf2 + 4); }
282 786 ▶ if (hend) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hend) break; 786 0 786
283 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
284 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
285 }
286 { /* the same header reading the whole-buffer parser does */
287 786 const char *hp = SvPVX(buf);
288 817 ▶ while (hp < hend) {
Branch: 1 branch
BranchTrueFalseTotal
while (hp < hend) { 817 0 817
289 817 const char *crlf = "\r\n";
290 817 const char *le = ninstr((char *)hp, (char *)hend,
291 (char *)crlf, (char *)crlf + 2);
292 817 ▶ const char *lend = le ? le : hend;
Branch: 1 branch
BranchTrueFalseTotal
const char *lend = le ? le : hend; 31 786 817
293 817 STRLEN ll = (STRLEN)(lend - hp);
294 817 ▶ if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) { 817 0 817
if (ll >= 20 && strncasecmp(hp, "Content-Disposition:", 20) == 0) { 786 31 817
295 786 disp = hp + 20; displ = ll - 20;
296 }
297 31 ▶ else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) { 31 0 31
else if (ll >= 13 && strncasecmp(hp, "Content-Type:", 13) == 0) { 31 0 31
298 31 ctype = hp + 13;
299 62 ▶ while (ctype < hp + ll && *ctype == ' ') ctype++;
Branch: 2 branches
BranchTrueFalseTotal
while (ctype < hp + ll && *ctype == ' ') ctype++; 62 0 62
while (ctype < hp + ll && *ctype == ' ') ctype++; 31 31 62
300 31 ctypel = (STRLEN)((hp + ll) - ctype);
301 }
302 817 ▶ if (!le) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!le) break; 786 31 817
303 31 hp = le + 2;
304 }
305 786 ▶ if (disp) {
Branch: 1 branch
BranchTrueFalseTotal
if (disp) { 786 0 786
306 786 name = pq_hdr_param(disp, displ, "name", &namel);
307 786 fname = pq_hdr_param(disp, displ, "filename", &fnamel);
308 }
309 }
310 /* headers are copied out before the buffer moves under them */
311 {
312 786 ▶ SV *nsv = name ? sv_2mortal(newSVpvn(name, namel)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *nsv = name ? sv_2mortal(newSVpvn(name, namel)) : NULL; 786 0 786
313 786 ▶ SV *fsv = fname ? sv_2mortal(newSVpvn(fname, fnamel)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *fsv = fname ? sv_2mortal(newSVpvn(fname, fnamel)) : NULL; 318 468 786
314 31 ▶ SV *tsv = (ctype && ctypel)
Branch: 1 branch
BranchTrueFalseTotal
SV *tsv = (ctype && ctypel) 31 0 31
315 817 ▶ ? sv_2mortal(newSVpvn(ctype, ctypel)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newSVpvn(ctype, ctypel)) : NULL; 31 755 786
316  
317 786 pq_mp_consume(aTHX_ buf, (STRLEN)((hend + 4) - SvPVX(buf)));
318  
319 786 pq_sink_drop(aTHX_ sink);
320 786 sink->len = 0; sink->is_file = fsv ? 1 : 0;
321 786 seq++;
322  
323 /* the content, up to the next delimiter */
324 1053 for (;;) {
325 1839 char *b = SvPVX(buf);
326 3678 char *hit = SvCUR(buf) >= NDl
327 1839 ? ninstr(b, b + SvCUR(buf), (char *)ND, (char *)ND + NDl)
328 1839 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 1839 0 1839
329 1839 ▶ if (hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (hit) { 281 1558 1839
330 281 pq_sink_write(aTHX_ sink, b, (STRLEN)(hit - b), dir, seq);
331 281 pq_mp_consume(aTHX_ buf, (STRLEN)(hit - b) + NDl);
332 281 break;
333 }
334 1558 ▶ if (SvCUR(buf) > keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(buf) > keep) { 1558 0 1558
335 1558 STRLEN flush = SvCUR(buf) - keep;
336 1558 pq_sink_write(aTHX_ sink, b, flush, dir, seq);
337 1558 pq_mp_consume(aTHX_ buf, flush);
338 }
339 1558 ▶ 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 1053 1053
340 /* truncated: the part never ended */
341 0 pq_sink_write(aTHX_ sink, SvPVX(buf), SvCUR(buf), dir, seq);
342 0 pq_mp_consume(aTHX_ buf, SvCUR(buf));
343 0 break;
344 }
345 }
346 281 ▶ 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; } 18 263 281
347 281 ▶ if (sink->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->err) { 0 281 281
348 /* the guard frees the sink and its temp file on the way out */
349 0 int e = sink->err;
350 0 croak("Punk::Request: upload could not be written: %s",
351 Strerror(e));
352 }
353  
354 281 parts++;
355 281 ▶ if (!nsv) { /* a part with no name is noise */
Branch: 1 branch
BranchTrueFalseTotal
if (!nsv) { /* a part with no name is noise */ 0 281 281
356 0 pq_sink_drop(aTHX_ sink);
357 0 continue;
358 }
359  
360 281 ▶ if (fsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (fsv) { 43 238 281
361 43 HV *up = newHV();
362 SV *obj;
363 43 (void)hv_stores(up, "name", newSVsv(nsv));
364 43 (void)hv_stores(up, "filename", newSVsv(fsv));
365 43 ▶ (void)hv_stores(up, "type", tsv ? newSVsv(tsv)
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(up, "type", tsv ? newSVsv(tsv) 31 12 43
366 : newSVpvs("application/octet-stream"));
367 43 (void)hv_stores(up, "size", newSVuv(sink->len));
368 43 ▶ if (sink->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->path) { 18 25 43
369 /* on disk: the path, and the request owns its removal.
370 * The upload keeps a reference to its own slot in that
371 * list, so save()'s rename can clear it - see
372 * pq_tmp_free on why unlinking a moved file would be
373 * worse than leaking one. */
374 18 (void)hv_stores(up, "path", newSVsv(sink->path));
375 18 ▶ if (tempfiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (tempfiles) { 18 0 18
376 18 SV *slot = newSVsv(sink->path);
377 18 av_push(tempfiles, slot);
378 18 (void)hv_stores(up, "tmpslot", newRV_inc(slot));
379 }
380 /* the request's now, or the upload's: not the sink's
381 * to unlink */
382 18 SvREFCNT_dec(sink->path);
383 18 sink->path = NULL;
384 }
385 else {
386 25 ▶ (void)hv_stores(up, "content",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(up, "content", 25 0 25
387 sink->sv ? sink->sv : newSVpvs(""));
388 25 sink->sv = NULL;
389 }
390 43 obj = sv_bless(newRV_noinc((SV *)up),
391 gv_stashpvs("Punk::Upload", GV_ADD));
392 43 pq_hv_add(aTHX_ uploads, SvPVX(nsv), SvCUR(nsv), obj);
393 }
394 else {
395 /* a field: it stays in memory, because a form value is
396 * something the application is about to read */
397 238 ▶ if (sink->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink->path) { 0 238 238
398 /* it outgrew the threshold anyway - read it back rather
399 * than hand a form value a path nobody expects */
400 0 SV *v = newSVpvs("");
401 0 int fd = PerlLIO_open(SvPVX(sink->path), O_RDONLY);
402 0 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 0 0 0
403 char tmp[PQ_MP_CHUNK];
404 SSize_t n;
405 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
406 0 sv_catpvn(v, tmp, (STRLEN)n);
407 0 PerlLIO_close(fd);
408 }
409 0 pq_hv_add(aTHX_ form, SvPVX(nsv), SvCUR(nsv), v);
410 }
411 else {
412 238 pq_hv_add(aTHX_ form, SvPVX(nsv), SvCUR(nsv),
413 238 ▶ sink->sv ? sink->sv : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
sink->sv ? sink->sv : newSVpvs("")); 238 0 238
414 238 sink->sv = NULL;
415 }
416 }
417 }
418 }
419 }
420  
421 #endif /* PUNK_MPSTREAM_H */
« include/punk/punk_cachefront.h ↑ Summary xs/txn.xs »