Code Coverage: vinyl-cache/bin/vinyld/http1/cache_http1_vfp.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * HTTP1 Fetch Filters
31
 *
32
 * These filters are used for both req.body and beresp.body to handle
33
 * the HTTP/1 aspects (C-L/Chunked/EOF)
34
 *
35
 */
36
37
#include "config.h"
38
39
#include <inttypes.h>
40
#include <poll.h>
41
42
#include "cache/cache_int.h"
43
#ifndef TEST_DRIVER
44
# include "cache/cache_filter.h"
45
#endif
46
#include "cache_http1.h"
47
48
#include "vct.h"
49
#include "vtcp.h"
50
51
static const unsigned max_chunked_hdr = 32;     // adjust b00007.vtc if changed
52
53
#ifndef TEST_DRIVER
54
static ssize_t
55
v1f_rxbuf_init(struct http_conn *htc)
56 123
{
57
58 123
        AZ(htc->rxbuf_b);
59 123
        AZ(htc->rxbuf_e);
60
61 123
        htc->rxbuf_b = WS_Alloc(htc->ws, max_chunked_hdr);
62 123
        if (htc->rxbuf_b == NULL)
63 0
                return (-1);
64 123
        htc->rxbuf_e = htc->rxbuf_b + max_chunked_hdr;
65 123
        return (0);
66 123
}
67
#endif
68
69
/*
70
 * fill up rxbuf. If there is pipelined data, move it to the beginning and
71
 * continue reading after it
72
 */
73
static ssize_t
74
v1f_rxbuf_read(struct http_conn *htc)
75 213
{
76 213
        ssize_t i;
77 213
        size_t av, sz;
78 213
        char *p;
79
80 213
        if (htc->pipeline_b)
81 50
                AN(htc->pipeline_e);
82 163
        else
83 163
                AZ(htc->pipeline_e);
84 213
        AN(htc->rxbuf_b);
85 213
        AN(htc->rxbuf_e);
86
87 213
        sz = pdiff(htc->rxbuf_b, htc->rxbuf_e);
88
89 213
        if (htc->pipeline_b == NULL)
90 163
                p = htc->pipeline_b = htc->rxbuf_b;
91 50
        else {
92 50
                AN(htc->pipeline_e);
93 50
                av = pdiff(htc->pipeline_b, htc->pipeline_e);
94 50
                if (av >= sz) {
95
                        // VTCP_Check(): can not originate from read()
96 3
                        errno = ENOBUFS;
97 3
                        return (-1);
98 3
                }
99 47
                assert(av < sz);
100 47
                vmemmove(htc->rxbuf_b, htc->pipeline_b, av);
101 47
                htc->pipeline_b = htc->rxbuf_b;
102 47
                htc->pipeline_e = htc->rxbuf_b + av;
103 47
                p = htc->pipeline_e;
104 47
                sz -= av;
105 47
        }
106 210
        do {
107 210
                errno = 0;
108 210
                i = read(*htc->rfd, p, sz);
109 210
        } while (i < 0 && errno == EINTR);
110 210
        if (i < 0) {
111 0
                VTCP_Assert(i);
112 0
                return (i);
113 0
        }
114 210
        htc->pipeline_e = p + i;
115 210
        if (htc->pipeline_b == htc->pipeline_e)
116 8
                 htc->pipeline_b = htc->pipeline_e = NULL;
117 210
        return (i);
118 210
}
119
120
/*--------------------------------------------------------------------
121
 * Parse a chunk tail in the pipeline and return status as appropriate
122
 */
123
struct pct { const char *msg; };
124
125
static struct pct pct_more[]    = {{"tail more"}};
126
static struct pct pct_nonl[]    = {{"chunked tail no NL"}};
127
128
// the unused parameter is to simplify the macro calling different parsers
129
static struct pct *
130
v1f_parse_chunked_tail(char *b, const char *e, void *unused, char **nextp)
131 2772
{
132 2772
        AN(b);
133 2772
        AN(e);
134 2772
        (void)unused;
135 2772
        AN(nextp);
136
137 2772
        if (b == e)
138 5
                return (pct_more);
139 2767
        if (*b == '\r')
140 2715
                b++;
141 2767
        if (b == e)
142 6
                return (pct_more);
143 2761
        if (*b != '\n')
144 4
                return (pct_nonl);
145 2757
        b++;
146
147 2757
        *nextp = b;
148 2757
        return (NULL);
149 2761
}
150
151
152
/*--------------------------------------------------------------------
153
 * Parse a chunk header in the pipeline and return status as appropriate
154
 */
155
156
struct pch { const char *msg; };
157
158
static struct pch pch_more[]    = {{"more"}};
159
static struct pch pch_nonhex[]  = {{"chunked header non-hex"}};
160
static struct pch pch_nonl[]    = {{"chunked header no NL"}};
161
static struct pch pch_syntax[]  = {{"chunked header number syntax"}}; // can't happen?
162
static struct pch pch_large[]   = {{"bogusly large chunk size"}};
163
static struct pch pch_toolong[] = {{"chunked header too long"}};
164
165
static struct pch *
166
v1f_parse_chunked_hdr_i(char *b, const char *e, ssize_t *szp, char **nextp)
167 8122
{
168 8122
        char *hb, *he, *q, s;
169 8122
        uintmax_t cll;
170 8122
        ssize_t cl;
171
172 8122
        AN(b);
173 8122
        AN(e);
174 8122
        AN(szp);
175 8122
        AN(nextp);
176
177
        /* Skip leading whitespace - XXX rfc9112 does not specify this */
178 12048
        while (b < e && vct_isows(*b))
179 3926
                b++;
180 8122
        if (b == e)
181 1082
                return (pch_more);
182 7040
        if (!vct_ishex(*b))
183 6
                return (pch_nonhex);
184
        /* Skip leading zeros */
185 9217
        while (b < e - 1 && b[0] == '0' && b[1] == '0')
186 2183
                b++;
187 7034
        if (b == e)
188 0
                return (pch_more);
189 7034
        hb = b;
190
        /* Collect hex digits */
191 34920
        while (b < e && vct_ishex(*b))
192 27886
                b++;
193 7034
        if (b == e)
194 2881
                return (pch_more);
195 4153
        he = b;
196
        /* Skip trailing whitespace. XXX rfc9112 does not specify this
197
         * XXX extension support missing https://httpwg.org/specs/rfc9112.html#chunked.extension
198
         */
199 5353
        while (b < e && vct_isows(*b))
200 1200
                b++;
201 4153
        if (b == e)
202 450
                return (pch_more);
203 3703
        if (*b == '\r')
204 3399
                b++;
205 3703
        if (b == e)
206 340
                return (pch_more);
207 3363
        if (*b != '\n')
208 2
                return (pch_nonl);
209 3361
        b++;
210
211 3361
        errno = 0;
212 3361
        s = *he;
213 3361
        *he = '\0';
214 3361
        cll = strtoumax(hb, &q, 16);
215
        // restore original for debug-/testability
216 3361
        *he = s;
217
218 3362
        if (q == NULL || q != he)
219 0
                return (pch_syntax);
220
221 3361
        cl = (ssize_t)cll;
222 3361
        if (cl < 0 || (uintmax_t)cl != cll)
223 4
                return (pch_large);
224
225
        // for a number larger than ULLONG_MAX, strtoumax() returns
226
        // ULLONG_MAX and sets errno to ERANGE. We catch this with the above
227
        // check already, but assert that we really do
228 3357
        AZ(errno);
229
230 3357
        *szp = cl;
231 3357
        *nextp = b;
232 3357
        return (NULL);
233 3357
}
234
235
// length check outside the actual parser for clarity
236
static struct pch *
237
v1f_parse_chunked_hdr(char *b, const char *e, ssize_t *szp, char **nextp)
238 8122
{
239 8122
        struct pch *r;
240 8122
        const char *ee;
241
242 8122
        ee = vmin_t(const char *, e, b + max_chunked_hdr);
243
244 8122
        r = v1f_parse_chunked_hdr_i(b, ee, szp, nextp);
245
246 8122
        if (r == pch_more && e != ee)
247 2
                return (pch_toolong);
248
249 8120
        return (r);
250 8122
}
251
252
#ifdef TEST_DRIVER
253
254
#include <stdlib.h>
255
#include <stdio.h>
256
257
// positive test cases have three constituents, we permutate all of them
258
static const char *t_ok_pre[] = {
259
        "",
260
        " ",
261
        "\t",
262
        " \t0",
263
        "00"
264
};
265
266
static const uintmax_t t_ok_sz[] = {
267
        0,
268
        1,
269
        0xa,
270
        0x10,
271
        0xaffe,
272
        SSIZE_MAX
273
};
274
275
static const char *t_ok_post[] = {
276
        "\r\n",
277
        "\n",
278
        " \r\n",
279
        " \t\n",
280
};
281
282
static const char *t_ok_next[] = {
283
        "",
284
        "\r\n",
285
        "\n",
286
        "GET",
287
        "\r\n01234567",
288
};
289
290
struct pch_neg {
291
        struct pch *r;
292
        const char *hdr;
293
};
294
295
// negative tests
296
static struct pch_neg t_neg[] = {
297
        {pch_more, ""},
298
        {pch_more, "\t "},
299
300
        {pch_nonhex, "x"},
301
        {pch_nonhex, " x"},
302
        {pch_nonhex, "\n"},
303
        {pch_nonhex, "\r"},
304
305
        {pch_more, "000"},
306
        {pch_more, "affe"},
307
        {pch_more, " a\r"},
308
309
        {pch_nonl, " a\rx"},
310
        {pch_nonl, " ax"},
311
312
        {pch_large, "8000000000000000\r\n"},
313
        {pch_large, "800000000000000000000000\r\n"},
314
};
315
316
// tail
317
static const char *t_ok_tail[] = {
318
        "\r\n",
319
        "\n",
320
};
321
322
static const char *t_ok_tail_next[] = {
323
        "",
324
        "0123",
325
};
326
327
struct pct_neg {
328
        struct pct *r;
329
        const char *hdr;
330
};
331
332
static struct pct_neg t_neg_tail[] = {
333
        {pct_more, "\r"},
334
        {pct_nonl, "\rx"},
335
};
336
337
338
static void
339
t_parse_chunked_hdr(char *b, char *e,
340
    const struct pch *r_exp, ssize_t sz_exp, const char *next_exp)
341 5303
{
342 5303
        const struct pch *r;
343 5303
        char *next = NULL;
344 5303
        ssize_t sz = -1;
345 5303
        r = v1f_parse_chunked_hdr(b, e, &sz, &next);
346
#ifdef DEBUG
347
        printf("r = %s, sz = 0x%zx, n = %s\n", r ? r->msg : "NULL", sz,
348
            next ? next : "NULL");
349
#endif
350 5303
        assert(r == r_exp);
351 5303
        assert(sz == sz_exp);
352 5303
        assert(next == next_exp);
353 5303
}
354
355
static void
356
t_parse_chunked_hdr_err(char *b, char *e, const struct pch *err)
357 4703
{
358 4703
        t_parse_chunked_hdr(b, e, err, -1, NULL);
359 4703
}
360
361
static void
362
t_parse_chunked_hdr_ok(char *b, char *e,
363
    ssize_t sz_exp, const char *next_exp)
364 600
{
365 600
        t_parse_chunked_hdr(b, e, NULL, sz_exp, next_exp);
366 600
}
367
368
static void
369
t_parse_chunked_tail(char *b, char *e,
370
    const struct pct *r_exp, const char *next_exp)
371 12
{
372 12
        const struct pct *r;
373 12
        char *next = NULL;
374 12
        r = v1f_parse_chunked_tail(b, e, NULL, &next);
375
#ifdef DEBUG
376
        printf("r = %s, n = %s\n", r ? r->msg : "NULL",
377
            next ? next : "NULL");
378
#endif
379 12
        assert(r == r_exp);
380 12
        assert(next == next_exp);
381 12
}
382
383
void
384
VSLbs(struct vsl_log *vsl, enum VSL_tag_e tag, const struct strands *s)
385 0
{
386 0
        (void)vsl;
387 0
        (void)tag;
388 0
        (void)s;
389 0
}
390
/*
391
static ssize_t
392
v1f_rxbuf_read(struct http_conn *htc);
393
*/
394
static void
395 1
t_rxbuf_read(void) {
396 1
        struct http_conn htc[1];
397 1
        const char *data = "0123456789abcdef";
398 1
        char rxbuf[16];
399 1
        int fd[2], i, r;
400
401 1
        assert(vstrlen(data) == sizeof rxbuf);
402
403 1
        INIT_OBJ(htc, HTTP_CONN_MAGIC);
404
        // v1f_rxbuf_init without the workspace
405 1
        htc->rxbuf_b = rxbuf;
406 1
        htc->rxbuf_e = htc->rxbuf_b + sizeof rxbuf;
407
408 1
        AZ(pipe(fd));
409 1
        htc->rfd = &fd[0];
410
411 17
        for (i = 0; i < vstrlen(data); i++) {
412 16
                r = write(fd[1], data + i, 1);
413 16
                assert(r == 1);
414 16
                r = v1f_rxbuf_read(htc);
415 16
                assert(r == 1);
416 16
                size_t av = pdiff(htc->pipeline_b, htc->pipeline_e);
417 16
                assert(av == i + 1);
418 16
                AZ(vmemcmp(htc->pipeline_b, data, av));
419 16
                if (i % 2 == 0) {
420
                        // v1f_rxbuf_read moves pipelined data to the beginning
421 8
                        assert(htc->pipeline_b == htc->rxbuf_b);
422 8
                        vmemmove(htc->pipeline_b + 1, htc->pipeline_b, av);
423 8
                        htc->pipeline_b++;
424 8
                        htc->pipeline_e++;
425 8
                }
426
427 16
        }
428
        // buffer is now full
429 1
        r = v1f_rxbuf_read(htc);
430 1
        assert(r == -1);
431 1
        assert(errno == ENOBUFS);
432
433 1
        close(fd[0]);
434 1
        close(fd[1]);
435 1
}
436
437
int
438
main(int argc, char *argv[])
439 1
{
440 1
        (void) argc;
441 1
        (void) argv;
442
443 1
        printf("-- rxbuf_read\n");
444 1
        t_rxbuf_read();
445
446 1
        printf("-- head postitive test permutations\n");
447
        // avoid nested loops
448 1
        unsigned n_ok = vcountof(t_ok_pre) * vcountof(t_ok_sz) *
449 1
            vcountof(t_ok_post) * vcountof(t_ok_next);
450 1
        char buf[80];
451 601
        for (unsigned n = 0; n < n_ok; n++) {
452 600
                unsigned n_pre = n % vcountof(t_ok_pre);
453 600
                unsigned n_sz = n / vcountof(t_ok_pre);
454 600
                unsigned n_post = n_sz / vcountof(t_ok_sz);
455 600
                unsigned n_next = n_post / vcountof(t_ok_post);
456 600
                n_sz %= vcountof(t_ok_sz);
457 600
                n_post %= vcountof(t_ok_post);
458 600
                assert(n_next < vcountof(t_ok_next));
459
460
#ifdef DEBUG
461
                printf("n_next=%u n_post=%u n_sz=%u n_pre=%u\n",
462
                    n_next, n_post, n_sz, n_pre);
463
#endif
464 600
                bprintf(buf, "%s%jx%s%s", t_ok_pre[n_pre], t_ok_sz[n_sz],
465 600
                    t_ok_post[n_post], t_ok_next[n_next]);
466
467 600
                char *ee = buf + vstrlen(buf) - vstrlen(t_ok_next[n_next]);
468
469 5290
                for (char *e = buf; e < ee; e++)
470 4690
                        t_parse_chunked_hdr_err(buf, e, pch_more);
471
472 600
                t_parse_chunked_hdr_ok(buf, ee, t_ok_sz[n_sz], ee);
473 600
        }
474
475 1
        printf("-- head negative tests\n");
476 14
        for (struct pch_neg *neg = t_neg; neg < t_neg + vcountof(t_neg); neg++) {
477 13
                size_t l = vstrlen(neg->hdr);
478 13
                assert(l < sizeof buf);
479
480 13
                vmemcpy(buf, neg->hdr, l + 1);
481 13
                char *e = buf + l;
482
483 13
                t_parse_chunked_hdr_err(buf, e, neg->r);
484 13
        }
485
486 1
        printf("-- tail postitive test permutations\n");
487 1
        n_ok = vcountof(t_ok_tail) * vcountof(t_ok_tail_next);
488 5
        for (unsigned n = 0; n < n_ok; n++) {
489 4
                unsigned n_tail = n % vcountof(t_ok_tail);
490 4
                unsigned n_next = n / vcountof(t_ok_tail_next);
491 4
                assert(n_next < vcountof(t_ok_tail_next));
492
493 4
                bprintf(buf, "%s%s", t_ok_tail[n_tail], t_ok_tail_next[n_next]);
494 4
                char *ee = buf + vstrlen(buf) - vstrlen(t_ok_tail_next[n_next]);
495
496 10
                for (char *e = buf; e < ee; e++)
497 6
                        t_parse_chunked_tail(buf, e, pct_more, NULL);
498
499 4
                t_parse_chunked_tail(buf, ee, NULL, ee);
500 4
        }
501
502 1
        printf("-- tail negative tests\n");
503 3
        for (struct pct_neg *neg = t_neg_tail; neg < t_neg_tail + vcountof(t_neg_tail); neg++) {
504 2
                size_t l = vstrlen(neg->hdr);
505 2
                assert(l < sizeof buf);
506
507 2
                vmemcpy(buf, neg->hdr, l + 1);
508 2
                char *e = buf + l;
509
510 2
                t_parse_chunked_tail(buf, e, neg->r, NULL);
511 2
        }
512
513 1
        printf("OK\n");
514 1
        return (0);
515 1
}
516
#else
517
518
/*--------------------------------------------------------------------
519
 * Read up to len bytes, returning pipelined data first.
520
 */
521
522
enum ahead {
523
        NO_READ_AHEAD,
524
        READ_AHEAD
525
};
526
527
static ssize_t
528
v1f_readahead(struct http_conn *htc, char *p, ssize_t len)
529 2178
{
530 2178
        struct iovec iov[2];
531 2178
        ssize_t i;
532
533 2178
        AZ(htc->pipeline_b);
534 2178
        if (htc->rxbuf_b == NULL && v1f_rxbuf_init(htc)) {
535 0
                errno = ENOMEM;
536 0
                return (-ENOMEM);
537 0
        }
538 2178
        AN(htc->rxbuf_b);
539
540 2178
        iov[0].iov_base = p;
541 2178
        iov[0].iov_len = len;
542 2178
        iov[1].iov_base = htc->rxbuf_b;
543 2178
        iov[1].iov_len = pdiff(htc->rxbuf_b, htc->rxbuf_e);
544
545 2178
        i = readv(*htc->rfd, iov, vcountof(iov));
546 2178
        if (i <= len)
547 7
                return (i);
548 2171
        i -= len;
549 2171
        htc->pipeline_b = htc->rxbuf_b;
550 2171
        htc->pipeline_e = htc->rxbuf_b + i;
551
552 2171
        return (len);
553 2178
}
554
555
static ssize_t
556
v1f_read(const struct vfp_ctx *vc, struct http_conn *htc, void *d, ssize_t len,
557
    enum ahead ahead)
558 58269
{
559 58269
        ssize_t l;
560 58269
        char *p;
561 58269
        ssize_t i;
562
563 58269
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
564 58269
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
565 58269
        assert(len > 0);
566 58269
        l = 0;
567 58269
        p = d;
568 58269
        i = 0;
569 58269
        if (htc->pipeline_b) {
570 4739
                l = htc->pipeline_e - htc->pipeline_b;
571 4739
                assert(l > 0);
572 4739
                l = vmin(l, len);
573 4739
                vmemcpy(p, htc->pipeline_b, l);
574 4739
                p += l;
575 4739
                len -= l;
576 4739
                htc->pipeline_b += l;
577 4739
                if (htc->pipeline_b == htc->pipeline_e)
578 3784
                        htc->pipeline_b = htc->pipeline_e = NULL;
579 4739
        }
580 58269
        if (len > 0) {
581 55767
                do {
582 55767
                        errno = 0;
583 55767
                        if (ahead == NO_READ_AHEAD)
584 53590
                                i = read(*htc->rfd, p, len);
585 2177
                        else if (ahead == READ_AHEAD)
586 2177
                                i = v1f_readahead(htc, p, len);
587 0
                        else
588 0
                                WRONG("ahead");
589
590 55767
                } while (i < 0 && errno == EINTR);
591 55767
                if (i < 0) {
592 9
                        if (ahead == NO_READ_AHEAD || i != -ENOMEM)
593 9
                                VTCP_Assert(i);
594 9
                        VSLbs(vc->wrk->vsl, SLT_FetchError,
595 9
                            TOSTRAND(VAS_errtxt(errno)));
596 9
                        return (i);
597 9
                }
598 55758
                assert(i <= len);
599 55758
        }
600 58260
        assert(i >= 0);
601 58260
        assert(l >= 0);
602 58260
        assert(i < SSIZE_MAX / 2);
603 58260
        assert(l < SSIZE_MAX / 2);
604 58260
        return (i + l);
605 58260
}
606
607
static enum vfp_status
608
v1f_ok(struct http_conn *htc)
609 5352
{
610 5352
        if ((htc)->pipeline_b == (htc)->pipeline_e)
611 299
                (htc)->pipeline_b = (htc)->pipeline_e = NULL;
612 5352
        return (VFP_OK);
613 5352
}
614
615
616
/*--------------------------------------------------------------------
617
 * Call parser on pipeline:
618
 * - If pipeline filled, try to return a parse result without reading
619
 * - else read until either the rxbuf is filled, or we have a parse
620
 *
621
 * this is a macro because the code for calling the head and tail parser is
622
 * _almost_ (but not quite) identical
623
 */
624
625
#define CHUNKED_PARSER(vc, htc, func, func_arg, more, what)                     \
626
                                                                                \
627 2
        ssize_t sz;                                                             \
628 2
                                                                                \
629 3099
        if ((htc)->pipeline_b) {                                                \
630 2937
                r = func((htc)->pipeline_b, (htc)->pipeline_e,                  \
631 2937
                        func_arg, &(htc)->pipeline_b);                          \
632 2937
                if (r == NULL)                                                  \
633 2937
                        return (v1f_ok(htc));                                   \
634 2937
                if (r != more)                                                  \
635 39
                        return (VFP_Error(vc, "%s", r->msg));                   \
636 39
        }                                                                       \
637 194
        if ((htc)->rxbuf_b == NULL && v1f_rxbuf_init(htc) != 0)                 \
638 194
                return (VFP_Error(vc, "No workspace for rxbuf"));               \
639 196
        while ((sz = v1f_rxbuf_read(htc)) > 0) {                                \
640 186
                r = func((htc)->pipeline_b, (htc)->pipeline_e,                  \
641 186
                    func_arg, &(htc)->pipeline_b);                              \
642 186
                if (r == NULL)                                                  \
643 186
                        return (v1f_ok(htc));                                   \
644 186
                if (r == more)                                                  \
645 2
                        continue;                                               \
646 2
                VSLb((vc)->wrk->vsl, SLT_Debug, "%.*s",                         \
647 0
                    (int)pdiff((htc)->pipeline_b, (htc)->pipeline_e),           \
648 0
                    (htc)->pipeline_b);                                         \
649 0
                return (VFP_Error(vc, "%s", r->msg));                           \
650 2
        }                                                                       \
651 194
        if (sz == 0)                                                            \
652 10
                return (VFP_Error(vc, "chunked " what " EOF"));                 \
653 10
        assert(sz < 0);                                                         \
654 2
        VSLbs(vc->wrk->vsl, SLT_FetchError, TOSTRAND(VAS_errtxt(errno)));       \
655 2
        return (VFP_Error(vc, "^^^ error reading chunk " what));
656
657
/*--------------------------------------------------------------------
658
 * read (CR)?LF at the end of a chunk
659
 */
660
661
static enum vfp_status
662
v1f_chunk_end(struct vfp_ctx *vc, struct http_conn *htc)
663 2756
{
664 2756
        const struct pct *r;
665 2756
        CHUNKED_PARSER(vc, htc, v1f_parse_chunked_tail, NULL, pct_more, "tail")
666 22
}
667
668
/*--------------------------------------------------------------------
669
 * Parse a chunk header and, for VFP_OK, return size in a pointer
670
 */
671
672
static enum vfp_status
673
v1f_chunked_hdr(struct vfp_ctx *vc, struct http_conn *htc, ssize_t *szp)
674 343
{
675 343
        const struct pch *r;
676 378
        CHUNKED_PARSER(vc, htc, v1f_parse_chunked_hdr, szp, pch_more, "header")
677 378
}
678
679
#undef CHUNKED_PARSER
680
681
/*--------------------------------------------------------------------
682
 * Read a chunked HTTP object.
683
 *
684
 */
685
686
static enum vfp_status v_matchproto_(vfp_pull_f)
687
v1f_chunked_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *ptr,
688
    ssize_t *lp)
689 55333
{
690 55333
        enum vfp_status vfps;
691 55333
        struct http_conn *htc;
692 55333
        ssize_t l, lr;
693
694 55333
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
695 55333
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
696 55333
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
697 55333
        AN(ptr);
698 55333
        AN(lp);
699
700 55333
        l = *lp;
701 55333
        *lp = 0;
702 55333
        if (vfe->priv2 == -1) {
703 343
                vfps = v1f_chunked_hdr(vc, htc, &vfe->priv2);
704 343
                if (vfps != VFP_OK)
705 14
                        return (vfps);
706 343
        }
707 55319
        if (vfe->priv2 > 0) {
708 55275
                if (vfe->priv2 <= l) {
709 2533
                        l = vfe->priv2;
710 2533
                        lr = v1f_read(vc, htc, ptr, l, READ_AHEAD);
711 2533
                } else
712 52742
                        lr = v1f_read(vc, htc, ptr, l, NO_READ_AHEAD);
713 55275
                if (lr <= 0)
714 1
                        return (VFP_Error(vc, "chunked insufficient bytes"));
715 55274
                *lp = lr;
716 55274
                vfe->priv2 -= lr;
717 55274
                if (vfe->priv2 != 0)
718 52748
                        return (VFP_OK);
719
720 2526
                vfe->priv2 = -1;
721
722 2526
                vfps = v1f_chunk_end(vc, htc);
723 2526
                if (vfps != VFP_OK)
724 3
                        return (vfps);
725
726
                /* opportunistically check for next chunk header read ahead */
727 2523
                if (! htc->pipeline_b)
728 71
                        return (VFP_OK);
729
730 2452
                const struct pch *r = v1f_parse_chunked_hdr(
731 2452
                    htc->pipeline_b, htc->pipeline_e,
732 2452
                    &vfe->priv2, &htc->pipeline_b);
733 2452
                if (r == pch_more)
734 26
                        return (v1f_ok(htc));
735 2426
                if (r != NULL)
736 2
                        return (VFP_Error(vc, "%s", r->msg));
737 2424
                if (vfe->priv2 != 0)
738 2247
                        return (v1f_ok(htc));
739 2424
        }
740 221
        AZ(vfe->priv2);
741 221
        vfps = v1f_chunk_end(vc, htc);
742 221
        if (vfps != VFP_OK)
743 0
                return (vfps);
744
        /* the tail is the last CRLF, we do not accept a trailer section */
745 221
        htc->body_status = BS_TAKEN;
746 221
        return (VFP_END);
747 221
}
748
749
static const struct vfp v1f_chunked = {
750
        .name = "V1F_CHUNKED",
751
        .pull = v1f_chunked_pull,
752
};
753
754
755
/*--------------------------------------------------------------------*/
756
757
static enum vfp_status v_matchproto_(vfp_pull_f)
758
v1f_straight_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
759
    ssize_t *lp)
760 2926
{
761 2926
        ssize_t l, lr;
762 2926
        struct http_conn *htc;
763
764 2926
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
765 2926
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
766 2926
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
767 2926
        AN(p);
768 2926
        AN(lp);
769
770 2926
        l = *lp;
771 2926
        *lp = 0;
772
773 2926
        if (vfe->priv2 == 0) { // XXX: Optimize Content-Len: 0 out earlier
774 0
                htc->body_status = BS_TAKEN;
775 0
                return (VFP_END);
776 0
        }
777 2926
        l = vmin(l, vfe->priv2);
778 2926
        lr = v1f_read(vc, htc, p, l, NO_READ_AHEAD);
779 2926
        if (lr <= 0)
780 25
                return (VFP_Error(vc, "straight insufficient bytes"));
781 2901
        *lp = lr;
782 2901
        vfe->priv2 -= lr;
783 2901
        if (vfe->priv2 == 0) {
784 1612
                htc->body_status = BS_TAKEN;
785 1612
                return (VFP_END);
786 1612
        }
787 1289
        return (VFP_OK);
788 2901
}
789
790
static const struct vfp v1f_straight = {
791
        .name = "V1F_STRAIGHT",
792
        .pull = v1f_straight_pull,
793
};
794
795
/*--------------------------------------------------------------------*/
796
797
static enum vfp_status v_matchproto_(vfp_pull_f)
798
v1f_eof_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p, ssize_t *lp)
799 69
{
800 69
        ssize_t l, lr;
801 69
        struct http_conn *htc;
802
803 69
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
804 69
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
805 69
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
806 69
        AN(p);
807
808 69
        AN(lp);
809
810 69
        l = *lp;
811 69
        *lp = 0;
812 69
        lr = v1f_read(vc, htc, p, l, NO_READ_AHEAD);
813 69
        if (lr < 0)
814 3
                return (VFP_Error(vc, "eof socket fail"));
815 66
        if (lr == 0) {
816 20
                htc->doclose = SC_RESP_CLOSE;
817 20
                return (VFP_END);
818 20
        }
819 46
        *lp = lr;
820 46
        return (VFP_OK);
821 66
}
822
823
static const struct vfp v1f_eof = {
824
        .name = "V1F_EOF",
825
        .pull = v1f_eof_pull,
826
};
827
828
/*--------------------------------------------------------------------
829
 */
830
831
int
832
V1F_Setup_Fetch(struct vfp_ctx *vfc, struct http_conn *htc)
833 2037
{
834 2037
        struct vfp_entry *vfe;
835
836 2037
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
837 2037
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
838
839 2037
        AN(htc->ws);
840 2037
        AZ(htc->ws->r);
841
842 2037
        AZ(htc->rxbuf_b);
843 2037
        AZ(htc->rxbuf_e);
844
845 2037
        if (htc->body_status == BS_EOF) {
846 23
                assert(htc->content_length == -1);
847 23
                vfe = VFP_Push(vfc, &v1f_eof);
848 23
                if (vfe == NULL)
849 0
                        return (ENOSPC);
850 23
                vfe->priv2 = 0;
851 2014
        } else if (htc->body_status == BS_LENGTH) {
852 1746
                assert(htc->content_length > 0);
853 1746
                vfe = VFP_Push(vfc, &v1f_straight);
854 1746
                if (vfe == NULL)
855 0
                        return (ENOSPC);
856 1746
                vfe->priv2 = htc->content_length;
857 1746
        } else if (htc->body_status == BS_CHUNKED) {
858 268
                assert(htc->content_length == -1);
859 268
                vfe = VFP_Push(vfc, &v1f_chunked);
860 268
                if (vfe == NULL)
861 16
                        return (ENOSPC);
862 252
                vfe->priv2 = -1;
863 252
        } else {
864 0
                WRONG("Wrong body_status");
865 0
        }
866 2021
        vfe->priv1 = htc;
867 2021
        return (0);
868 2037
}
869
#endif