varnish-cache/bin/varnishtest/vtc_http.c
0
/*-
1
 * Copyright (c) 2008-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <sys/socket.h>
33
34
#include <math.h>
35
#include <poll.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <unistd.h>
39
#include <string.h>
40
41
#include "vtc.h"
42
#include "vtc_http.h"
43
44
#include "vct.h"
45
#include "vfil.h"
46
#include "vnum.h"
47
#include "vrnd.h"
48
#include "vtcp.h"
49
#include "hpack.h"
50
51
extern const struct cmds http_cmds[];
52
53
/* SECTION: client-server client/server
54
 *
55
 * Client and server threads are fake HTTP entities used to test your Varnish
56
 * and VCL. They take any number of arguments, and the one that are not
57
 * recognized, assuming they don't start with '-', are treated as
58
 * specifications, laying out the actions to undertake::
59
 *
60
 *         client cNAME [...]
61
 *         server sNAME [...]
62
 *
63
 * Clients and server are identified by a string that's the first argument,
64
 * clients' names start with 'c' and servers' names start with 's'.
65
 *
66
 * As the client and server commands share a good deal of arguments and
67
 * specification actions, they are grouped in this single section, specific
68
 * items will be explicitly marked as such.
69
 *
70
 * SECTION: client-server.macros Macros and automatic behaviour
71
 *
72
 * To make things easier in the general case, clients will connect by default
73
 * to a Varnish server called v1. To connect to a different Varnish server, use
74
 * '-connect ${vNAME_sock}'.
75
 *
76
 * The -vcl+backend switch of the ``varnish`` command will add all the declared
77
 * servers as backends. Be careful though, servers will by default listen to
78
 * the 127.0.0.1 IP and will pick a random port, and publish 3 macros:
79
 * sNAME_addr, sNAME_port and sNAME_sock, but only once they are started. For
80
 * 'varnish -vcl+backend' to create the vcl with the correct values, the server
81
 * must be started first.
82
 *
83
 * SECTION: client-server.args Arguments
84
 *
85
 * \-start
86
 *        Start the thread in background, processing the last given
87
 *        specification.
88
 *
89
 * \-wait
90
 *        Block until the thread finishes.
91
 *
92
 * \-run (client only)
93
 *        Equivalent to "-start -wait".
94
 *
95
 * \-repeat NUMBER
96
 *        Instead of processing the specification only once, do it NUMBER times.
97
 *
98
 * \-keepalive
99
 *        For repeat, do not open new connections but rather run all
100
 *        iterations in the same connection
101
 *
102
 * \-break (server only)
103
 *        Stop the server.
104
 *
105
 * \-listen STRING (server only)
106
 *        Dictate the listening socket for the server. STRING is of the form
107
 *        "IP PORT", or "/PATH/TO/SOCKET" for a Unix domain socket. In the
108
 *        latter case, the path must begin with '/', and the server must be
109
 *        able to create it.
110
 *
111
 * \-connect STRING (client only)
112
 *        Indicate the server to connect to. STRING is also of the form
113
 *        "IP PORT", or "/PATH/TO/SOCKET". As with "server -listen", a
114
 *        Unix domain socket is recognized when STRING begins with a '/'.
115
 *
116
 * \-dispatch (server only, s0 only)
117
 *        Normally, to keep things simple, server threads only handle one
118
 *        connection at a time, but the -dispatch switch allows to accept
119
 *        any number of connection and handle them following the given spec.
120
 *
121
 *        However, -dispatch is only allowed for the server name "s0".
122
 *
123
 * \-proxy1 STRING (client only)
124
 *        Use the PROXY protocol version 1 for this connection. STRING
125
 *        is of the form "CLIENTIP:PORT SERVERIP:PORT".
126
 *
127
 * \-proxy2 STRING (client only)
128
 *        Use the PROXY protocol version 2 for this connection. STRING
129
 *        is of the form "CLIENTIP:PORT SERVERIP:PORT".
130
 *
131
 * SECTION: client-server.spec Specification
132
 *
133
 * It's a string, either double-quoted "like this", but most of the time
134
 * enclosed in curly brackets, allowing multilining. Write a command per line in
135
 * it, empty line are ignored, and long line can be wrapped by using a
136
 * backslash. For example::
137
 *
138
 *     client c1 {
139
 *         txreq -url /foo \
140
 *               -hdr "bar: baz"
141
 *
142
 *         rxresp
143
 *     } -run
144
 */
145
146
#define ONLY_CLIENT(hp, av)                                             \
147
        do {                                                            \
148
                if (hp->h2)                                             \
149
                        vtc_fatal(hp->vl,                               \
150
                            "\"%s\" only possible before H/2 upgrade",  \
151
                                        av[0]);                         \
152
                if (hp->sfd != NULL)                                    \
153
                        vtc_fatal(hp->vl,                               \
154
                            "\"%s\" only possible in client", av[0]);   \
155
        } while (0)
156
157
#define ONLY_SERVER(hp, av)                                             \
158
        do {                                                            \
159
                if (hp->h2)                                             \
160
                        vtc_fatal(hp->vl,                               \
161
                            "\"%s\" only possible before H/2 upgrade",  \
162
                                        av[0]);                         \
163
                if (hp->sfd == NULL)                                    \
164
                        vtc_fatal(hp->vl,                               \
165
                            "\"%s\" only possible in server", av[0]);   \
166
        } while (0)
167
168
169
/* XXX: we may want to vary this */
170
static const char * const nl = "\r\n";
171
172
/**********************************************************************
173
 * Generate a synthetic body
174
 */
175
176
char *
177 21120
synth_body(const char *len, int rnd)
178
{
179
        int i, j, k, l;
180
        char *b;
181
182
183 21120
        AN(len);
184 21120
        i = strtoul(len, NULL, 0);
185 21120
        assert(i > 0);
186 21120
        b = malloc(i + 1L);
187 21120
        AN(b);
188 21120
        l = k = '!';
189 1437557880
        for (j = 0; j < i; j++) {
190 1437536760
                if ((j % 64) == 63) {
191 22448600
                        b[j] = '\n';
192 22448600
                        k++;
193 22448600
                        if (k == '~')
194 239320
                                k = '!';
195 22448600
                        l = k;
196 1437536760
                } else if (rnd) {
197 165120
                        b[j] = (VRND_RandomTestable() % 95) + ' ';
198 165120
                } else {
199 1414923040
                        b[j] = (char)l;
200 1414923040
                        if (++l == '~')
201 15155080
                                l = '!';
202
                }
203 1437536760
        }
204 21120
        b[i - 1] = '\n';
205 21120
        b[i] = '\0';
206 21120
        return (b);
207
}
208
209
/**********************************************************************
210
 * Finish and write the vsb to the fd
211
 */
212
213
static void
214 216291
http_write(const struct http *hp, int lvl, const char *pfx)
215
{
216
217 216291
        AZ(VSB_finish(hp->vsb));
218 216291
        vtc_dump(hp->vl, lvl, pfx, VSB_data(hp->vsb), VSB_len(hp->vsb));
219 216291
        if (VSB_tofile(hp->vsb, hp->sess->fd))
220 534
                vtc_log(hp->vl, hp->fatal, "Write failed: %s",
221 267
                    strerror(errno));
222 216291
}
223
224
/**********************************************************************
225
 * find header
226
 */
227
228
static char *
229 548129
http_find_header(char * const *hh, const char *hdr)
230
{
231
        int n, l;
232
        char *r;
233
234 548129
        l = strlen(hdr);
235
236 4018253
        for (n = 3; hh[n] != NULL; n++) {
237 3708595
                if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
238 3470124
                        continue;
239 477339
                for (r = hh[n] + l + 1; vct_issp(*r); r++)
240 238868
                        continue;
241 238471
                return (r);
242
        }
243 309658
        return (NULL);
244 548129
}
245
246
/**********************************************************************
247
 * count header
248
 */
249
250
static int
251 214153
http_count_header(char * const *hh, const char *hdr)
252
{
253 214153
        int n, l, r = 0;
254
255 214153
        l = strlen(hdr);
256
257 1713175
        for (n = 3; hh[n] != NULL; n++) {
258 1499022
                if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
259 1394745
                        continue;
260 104277
                r++;
261 104277
        }
262 214153
        return (r);
263
}
264
265
/* SECTION: client-server.spec.expect
266
 *
267
 * expect STRING1 OP STRING2
268
 *         Test if "STRING1 OP STRING2" is true, and if not, fails the test.
269
 *         OP can be ==, <, <=, >, >= when STRING1 and STRING2 represent numbers
270
 *         in which case it's an order operator. If STRING1 and STRING2 are
271
 *         meant as strings OP is a matching operator, either == (exact match)
272
 *         or ~ (regex match).
273
 *
274
 *         varnishtest will first try to resolve STRING1 and STRING2 by looking
275
 *         if they have special meanings, in which case, the resolved value is
276
 *         use for the test. Note that this value can be a string representing a
277
 *         number, allowing for tests such as::
278
 *
279
 *                 expect req.http.x-num > 2
280
 *
281
 *         Here's the list of recognized strings, most should be obvious as they
282
 *         either match VCL logic, or the txreq/txresp options:
283
 *
284
 *         - remote.ip
285
 *         - remote.port
286
 *         - remote.path
287
 *         - req.method
288
 *         - req.url
289
 *         - req.proto
290
 *         - resp.proto
291
 *         - resp.status
292
 *         - resp.reason
293
 *         - resp.chunklen
294
 *         - req.bodylen
295
 *         - req.body
296
 *         - resp.bodylen
297
 *         - resp.body
298
 *         - req.http.NAME
299
 *         - resp.http.NAME
300
 */
301
302
static const char *
303 521284
cmd_var_resolve(struct http *hp, char *spec)
304
{
305
        char **hh, *hdr;
306 521284
        if (!strcmp(spec, "remote.ip"))
307 160
                return (hp->rem_ip);
308 521124
        if (!strcmp(spec, "remote.port"))
309 160
                return (hp->rem_port);
310 520964
        if (!strcmp(spec, "remote.path"))
311 160
                return (hp->rem_path);
312 520804
        if (!strcmp(spec, "req.method"))
313 1960
                return (hp->req[0]);
314 518844
        if (!strcmp(spec, "req.url"))
315 24520
                return (hp->req[1]);
316 494324
        if (!strcmp(spec, "req.proto"))
317 1800
                return (hp->req[2]);
318 492524
        if (!strcmp(spec, "resp.proto"))
319 1360
                return (hp->resp[0]);
320 491164
        if (!strcmp(spec, "resp.status"))
321 61600
                return (hp->resp[1]);
322 429564
        if (!strcmp(spec, "resp.reason"))
323 7160
                return (hp->resp[2]);
324 422404
        if (!strcmp(spec, "resp.chunklen"))
325 0
                return (hp->chunklen);
326 422404
        if (!strcmp(spec, "req.bodylen"))
327 1240
                return (hp->bodylen);
328 421164
        if (!strcmp(spec, "req.body"))
329 320
                return (hp->body != NULL ? hp->body : spec);
330 420844
        if (!strcmp(spec, "resp.bodylen"))
331 25997
                return (hp->bodylen);
332 394847
        if (!strcmp(spec, "resp.body"))
333 8120
                return (hp->body != NULL ? hp->body : spec);
334 386727
        if (!strncmp(spec, "req.http.", 9)) {
335 13640
                hh = hp->req;
336 13640
                hdr = spec + 9;
337 386727
        } else if (!strncmp(spec, "resp.http.", 10)) {
338 119836
                hh = hp->resp;
339 119836
                hdr = spec + 10;
340 373087
        } else if (!strcmp(spec, "h2.state")) {
341 292
                if (hp->h2)
342 132
                        return ("true");
343
                else
344 160
                        return ("false");
345
        } else
346 252959
                return (spec);
347 133476
        hdr = http_find_header(hh, hdr);
348 133476
        return (hdr);
349 521284
}
350
351
static void
352 260641
cmd_http_expect(CMD_ARGS)
353
{
354
        struct http *hp;
355
        const char *lhs;
356
        char *cmp;
357
        const char *rhs;
358
359 260641
        (void)vl;
360 260641
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
361 260641
        AZ(strcmp(av[0], "expect"));
362 260641
        av++;
363
364 260641
        AN(av[0]);
365 260641
        AN(av[1]);
366 260641
        AN(av[2]);
367 260641
        AZ(av[3]);
368 260641
        lhs = cmd_var_resolve(hp, av[0]);
369 260641
        cmp = av[1];
370 260641
        rhs = cmd_var_resolve(hp, av[2]);
371
372 260641
        vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
373 260641
}
374
375
/* SECTION: client-server.spec.expect_pattern
376
 *
377
 * expect_pattern
378
 *
379
 * Expect as the http body the test pattern generated by chunkedlen ('0'..'7'
380
 * repeating).
381
 */
382
static void
383 160
cmd_http_expect_pattern(CMD_ARGS)
384
{
385
        char *p;
386
        struct http *hp;
387 160
        char t = '0';
388
389 160
        (void)vl;
390 160
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
391 160
        AZ(strcmp(av[0], "expect_pattern"));
392 160
        av++;
393 160
        AZ(av[0]);
394 10485920
        for (p = hp->body; *p != '\0'; p++) {
395 10485760
                if (*p != t)
396 0
                        vtc_fatal(hp->vl,
397
                            "EXPECT PATTERN FAIL @%zd should 0x%02x is 0x%02x",
398 0
                            (ssize_t) (p - hp->body), t, *p);
399 10485760
                t += 1;
400 10485760
                t &= ~0x08;
401 10485760
        }
402 160
        vtc_log(hp->vl, 4, "EXPECT PATTERN SUCCESS");
403 160
}
404
405
/**********************************************************************
406
 * Split a HTTP protocol header
407
 */
408
409
static void
410 214147
http_splitheader(struct http *hp, int req)
411
{
412
        char *p, *q, **hh;
413
        int n;
414
        char buf[20];
415
416 214147
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
417 214147
        if (req) {
418 91107
                memset(hp->req, 0, sizeof hp->req);
419 91107
                hh = hp->req;
420 91107
        } else {
421 123040
                memset(hp->resp, 0, sizeof hp->resp);
422 123040
                hh = hp->resp;
423
        }
424
425 214147
        n = 0;
426 214147
        p = hp->rx_b;
427 214147
        if (*p == '\0') {
428 2480
                vtc_log(hp->vl, 4, "No headers");
429 2480
                return;
430
        }
431
432
        /* REQ/PROTO */
433 211707
        while (vct_islws(*p))
434 40
                p++;
435 211667
        hh[n++] = p;
436 1453082
        while (!vct_islws(*p))
437 1241415
                p++;
438 211667
        AZ(vct_iscrlf(p, hp->rx_e));
439 211667
        *p++ = '\0';
440
441
        /* URL/STATUS */
442 211667
        while (vct_issp(*p))            /* XXX: H space only */
443 0
                p++;
444 211667
        AZ(vct_iscrlf(p, hp->rx_e));
445 211667
        hh[n++] = p;
446 1040132
        while (!vct_islws(*p))
447 828465
                p++;
448 211667
        if (vct_iscrlf(p, hp->rx_e)) {
449 40
                hh[n++] = NULL;
450 40
                q = p;
451 40
                p = vct_skipcrlf(p, hp->rx_e);
452 40
                *q = '\0';
453 40
        } else {
454 211627
                *p++ = '\0';
455
                /* PROTO/MSG */
456 211627
                while (vct_issp(*p))            /* XXX: H space only */
457 0
                        p++;
458 211627
                hh[n++] = p;
459 1598360
                while (!vct_iscrlf(p, hp->rx_e))
460 1386733
                        p++;
461 211627
                q = p;
462 211627
                p = vct_skipcrlf(p, hp->rx_e);
463 211627
                *q = '\0';
464
        }
465 211667
        assert(n == 3);
466
467 1710499
        while (*p != '\0') {
468 1710623
                assert(n < MAX_HDR);
469 1710623
                if (vct_iscrlf(p, hp->rx_e))
470 211667
                        break;
471 1498956
                hh[n++] = p++;
472 32875523
                while (*p != '\0' && !vct_iscrlf(p, hp->rx_e))
473 31376567
                        p++;
474 1498832
                if (*p == '\0') {
475 0
                        break;
476
                }
477 1498832
                q = p;
478 1498832
                p = vct_skipcrlf(p, hp->rx_e);
479 1498832
                *q = '\0';
480
        }
481 211801
        p = vct_skipcrlf(p, hp->rx_e);
482 211801
        assert(*p == '\0');
483
484 2345872
        for (n = 0; n < 3 || hh[n] != NULL; n++) {
485 2134071
                bprintf(buf, "http[%2d] ", n);
486 2134071
                vtc_dump(hp->vl, 4, buf, hh[n], -1);
487 2134071
        }
488 214281
}
489
490
491
/**********************************************************************
492
 * Receive another character
493
 */
494
495
static int
496 48941769
http_rxchar(struct http *hp, int n, int eof)
497
{
498
        int i;
499
        struct pollfd pfd[1];
500
501 97933857
        while (n > 0) {
502 48995528
                pfd[0].fd = hp->sess->fd;
503 48995528
                pfd[0].events = POLLIN;
504 48995528
                pfd[0].revents = 0;
505 48995528
                i = poll(pfd, 1, (int)(hp->timeout * 1000));
506 48995528
                if (i < 0 && errno == EINTR)
507 0
                        continue;
508 48995528
                if (i == 0) {
509 0
                        vtc_log(hp->vl, hp->fatal,
510
                            "HTTP rx timeout (fd:%d %.3fs)",
511 0
                            hp->sess->fd, hp->timeout);
512 0
                        continue;
513
                }
514 48995528
                if (i < 0) {
515 0
                        vtc_log(hp->vl, hp->fatal,
516
                            "HTTP rx failed (fd:%d poll: %s)",
517 0
                            hp->sess->fd, strerror(errno));
518 0
                        continue;
519
                }
520 48995528
                assert(i > 0);
521 48995528
                assert(hp->rx_p + n < hp->rx_e);
522 48995528
                i = read(hp->sess->fd, hp->rx_p, n);
523 48995528
                if (!(pfd[0].revents & POLLIN))
524 0
                        vtc_log(hp->vl, 4,
525
                            "HTTP rx poll (fd:%d revents: %x n=%d, i=%d)",
526 0
                            hp->sess->fd, pfd[0].revents, n, i);
527 48995528
                if (i == 0 && eof)
528 2680
                        return (i);
529 48992848
                if (i == 0) {
530 1520
                        vtc_log(hp->vl, hp->fatal,
531
                            "HTTP rx EOF (fd:%d read: %s) %d",
532 760
                            hp->sess->fd, strerror(errno), n);
533 760
                        return (-1);
534
                }
535 48992088
                if (i < 0) {
536 0
                        vtc_log(hp->vl, hp->fatal,
537
                            "HTTP rx failed (fd:%d read: %s)",
538 0
                            hp->sess->fd, strerror(errno));
539 0
                        return (-1);
540
                }
541 48992088
                hp->rx_p += i;
542 48992088
                *hp->rx_p = '\0';
543 48992088
                n -= i;
544
        }
545 48938329
        return (1);
546 48941769
}
547
548
static int
549 52781
http_rxchunk(struct http *hp)
550
{
551
        char *q, *old;
552
        int i;
553
554 52781
        old = hp->rx_p;
555 52781
        do {
556 264527
                if (http_rxchar(hp, 1, 0) < 0)
557 408
                        return (-1);
558 264119
        } while (hp->rx_p[-1] != '\n');
559 52373
        vtc_dump(hp->vl, 4, "len", old, -1);
560 52373
        i = strtoul(old, &q, 16);
561 52373
        bprintf(hp->chunklen, "%d", i);
562 52373
        if ((q == old) || (q == hp->rx_p) || (*q != '\0' && !vct_islws(*q))) {
563 0
                vtc_log(hp->vl, hp->fatal, "Chunklen fail (%02x @ %td)",
564 0
                    (*q & 0xff), q - old);
565 0
                return (-1);
566
        }
567 52373
        assert(*q == '\0' || vct_islws(*q));
568 52373
        hp->rx_p = old;
569 52373
        if (i > 0) {
570 42331
                if (http_rxchar(hp, i, 0) < 0)
571 72
                        return (-1);
572 42259
                vtc_dump(hp->vl, 4, "chunk", old, i);
573 42259
        }
574 52301
        old = hp->rx_p;
575 52301
        if (http_rxchar(hp, 2, 0) < 0)
576 0
                return (-1);
577 52301
        if (!vct_iscrlf(old, hp->rx_e)) {
578 0
                vtc_log(hp->vl, hp->fatal, "Chunklen without CRLF");
579 0
                return (-1);
580
        }
581 52301
        hp->rx_p = old;
582 52301
        *hp->rx_p = '\0';
583 52301
        return (i);
584 52781
}
585
586
/**********************************************************************
587
 * Swallow a HTTP message body
588
 *
589
 * max: 0 is all
590
 */
591
592
static void
593 207267
http_swallow_body(struct http *hp, char * const *hh, int body, int max)
594
{
595
        const char *p, *q;
596
        int i, l, ll;
597
598 207267
        l = hp->rx_p - hp->body;
599
600 207267
        p = http_find_header(hh, "transfer-encoding");
601 207267
        q = http_find_header(hh, "content-length");
602 207267
        if (p != NULL && !strcasecmp(p, "chunked")) {
603 10642
                if (q != NULL) {
604 0
                        vtc_log(hp->vl, hp->fatal, "Both C-E: Chunked and C-L");
605 0
                        return;
606
                }
607 10642
                ll = 0;
608 52261
                while (http_rxchunk(hp) > 0) {
609 41859
                        ll = (hp->rx_p - hp->body) - l;
610 41859
                        if (max && ll >= max)
611 240
                                break;
612
                }
613 10642
                p = "chunked";
614 207269
        } else if (q != NULL) {
615 102234
                ll = strtoul(q, NULL, 10);
616 102234
                if (max && ll > l + max)
617 80
                        ll = max;
618
                else
619 102154
                        ll -= l;
620 102234
                i = http_rxchar(hp, ll, 0);
621 102234
                if (i < 0)
622 280
                        return;
623 101954
                p = "c-l";
624 196347
        } else if (body) {
625 280
                ll = 0;
626 280
                do  {
627 7869000
                        i = http_rxchar(hp, 1, 1);
628 7869000
                        if (i < 0)
629 0
                                return;
630 7869000
                        ll += i;
631 7869000
                        if (max && ll >= max)
632 80
                                break;
633 7868920
                } while (i > 0);
634 280
                p = "eof";
635 280
        } else {
636 94113
                p = "none";
637 94113
                ll = l = 0;
638
        }
639 206989
        vtc_dump(hp->vl, 4, p, hp->body + l, ll);
640 206989
        l += ll;
641 206989
        hp->bodyl = l;
642 206989
        bprintf(hp->bodylen, "%d", l);
643 207269
}
644
645
/**********************************************************************
646
 * Receive a HTTP protocol header
647
 */
648
649
static void
650 214142
http_rxhdr(struct http *hp)
651
{
652 214142
        int i, s = 0;
653
        char *p;
654
        ssize_t l;
655
656 214142
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
657 214142
        hp->rx_p = hp->rx_b;
658 214142
        *hp->rx_p = '\0';
659 214142
        hp->body = NULL;
660 214142
        bprintf(hp->bodylen, "%s", "<undef>");
661 40579392
        while (1) {
662 40579392
                p = hp->rx_p;
663 40579392
                i = http_rxchar(hp, 1, 1);
664 40579392
                if (i < 1)
665 2480
                        break;
666 40576912
                if (s == 0 && *p == '\r')
667 1710575
                        s = 1;
668 38866337
                else if ((s == 0 || s == 1) && *p == '\n')
669 1710679
                        s = 2;
670 37155658
                else if (s == 2 && *p == '\r')
671 211626
                        s = 3;
672 36944032
                else if ((s == 2 || s == 3) && *p == '\n')
673 211662
                        break;
674
                else
675 36732370
                        s = 0;
676
        }
677 214142
        l = hp->rx_p - hp->rx_b;
678 214142
        vtc_dump(hp->vl, 4, "rxhdr", hp->rx_b, l);
679 214142
        vtc_log(hp->vl, 4, "rxhdrlen = %zd", l);
680 214142
        if (i < 1)
681 2480
                vtc_log(hp->vl, hp->fatal, "HTTP header is incomplete");
682 214142
        *hp->rx_p = '\0';
683 214142
        hp->body = hp->rx_p;
684 214142
}
685
686
/* SECTION: client-server.spec.rxresp
687
 *
688
 * rxresp [-no_obj] (client only)
689
 *         Receive and parse a response's headers and body. If -no_obj is
690
 *         present, only get the headers.
691
 */
692
693
static void
694 121174
cmd_http_rxresp(CMD_ARGS)
695
{
696
        struct http *hp;
697 121174
        int has_obj = 1;
698
699 121174
        (void)vl;
700 121174
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
701 121174
        ONLY_CLIENT(hp, av);
702 121174
        AZ(strcmp(av[0], "rxresp"));
703 121174
        av++;
704
705 122214
        for (; *av != NULL; av++)
706 2080
                if (!strcmp(*av, "-no_obj"))
707 1040
                        has_obj = 0;
708
                else
709 0
                        vtc_fatal(hp->vl,
710 0
                            "Unknown http rxresp spec: %s\n", *av);
711 121174
        http_rxhdr(hp);
712 121174
        http_splitheader(hp, 0);
713 121174
        if (http_count_header(hp->resp, "Content-Length") > 1)
714 0
                vtc_fatal(hp->vl,
715
                    "Multiple Content-Length headers.\n");
716 121174
        if (!has_obj)
717 1040
                return;
718 120134
        if (!hp->resp[0] || !hp->resp[1])
719 2480
                return;
720 117676
        if (hp->head_method)
721 2800
                return;
722 114876
        if (!strcmp(hp->resp[1], "200"))
723 86236
                http_swallow_body(hp, hp->resp, 1, 0);
724
        else
725 28640
                http_swallow_body(hp, hp->resp, 0, 0);
726 114876
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
727 121196
}
728
729
/* SECTION: client-server.spec.rxresphdrs
730
 *
731
 * rxresphdrs (client only)
732
 *         Receive and parse a response's headers.
733
 */
734
735
static void
736 1840
cmd_http_rxresphdrs(CMD_ARGS)
737
{
738
        struct http *hp;
739
740 1840
        (void)vl;
741 1840
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
742 1840
        ONLY_CLIENT(hp, av);
743 1840
        AZ(strcmp(av[0], "rxresphdrs"));
744 1840
        av++;
745
746 1840
        for (; *av != NULL; av++)
747 0
                vtc_fatal(hp->vl, "Unknown http rxresp spec: %s\n", *av);
748 1840
        http_rxhdr(hp);
749 1840
        http_splitheader(hp, 0);
750 1840
        if (http_count_header(hp->resp, "Content-Length") > 1)
751 0
                vtc_fatal(hp->vl,
752
                    "Multiple Content-Length headers.\n");
753 1840
}
754
755
/* SECTION: client-server.spec.gunzip
756
 *
757
 * gunzip
758
 *         Gunzip the body in place.
759
 */
760
static void
761 1480
cmd_http_gunzip(CMD_ARGS)
762
{
763
        struct http *hp;
764
765 1480
        (void)av;
766 1480
        (void)vl;
767
768 1480
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
769 1480
        vtc_gunzip(hp, hp->body, &hp->bodyl);
770 1480
}
771
772
/**********************************************************************
773
 * Handle common arguments of a transmited request or response
774
 */
775
776
static char* const *
777 206455
http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
778
    char *body, unsigned nohost, unsigned nodate, unsigned noserver, unsigned nouseragent)
779
{
780 206455
        long bodylen = 0;
781
        char *b, *c;
782
        char *nullbody;
783
        ssize_t len;
784 206455
        int nolen = 0;
785
        int l;
786
787 206455
        nullbody = body;
788
789 291183
        for (; *av != NULL; av++) {
790 129168
                if (!strcmp(*av, "-nolen")) {
791 5400
                        nolen = 1;
792 129168
                } else if (!strcmp(*av, "-nohost")) {
793 80
                        nohost = 1;
794 123768
                } else if (!strcmp(*av, "-nodate")) {
795 320
                        nodate = 1;
796 123688
                } else if (!strcmp(*av, "-hdr")) {
797 78768
                        if (!strncasecmp(av[1], "Content-Length:", 15) ||
798 76208
                            !strncasecmp(av[1], "Transfer-Encoding:", 18))
799 5999
                                nolen = 1;
800 78768
                        if (!strncasecmp(av[1], "Host:", 5))
801 1440
                                nohost = 1;
802 78768
                        if (!strncasecmp(av[1], "Date:", 5))
803 320
                                nodate = 1;
804 78768
                        if (!strncasecmp(av[1], "Server:", 7))
805 600
                                noserver = 1;
806 78768
                        if (!strncasecmp(av[1], "User-Agent:", 11))
807 80
                                nouseragent = 1;
808 78768
                        VSB_printf(hp->vsb, "%s%s", av[1], nl);
809 78768
                        av++;
810 123368
                } else if (!strcmp(*av, "-hdrlen")) {
811 160
                        VSB_printf(hp->vsb, "%s: ", av[1]);
812 160
                        l = atoi(av[2]);
813 30160
                        while (l-- > 0)
814 30000
                                VSB_putc(hp->vsb, '0' + (l % 10));
815 160
                        VSB_printf(hp->vsb, "%s", nl);
816 160
                        av+=2;
817 160
                } else
818 44440
                        break;
819 84728
        }
820 251534
        for (; *av != NULL; av++) {
821 45079
                if (!strcmp(*av, "-body")) {
822 29159
                        assert(body == nullbody);
823 29159
                        REPLACE(body, av[1]);
824
825 29159
                        AN(body);
826 29159
                        av++;
827 29159
                        bodylen = strlen(body);
828 907395
                        for (b = body; *b != '\0'; b++) {
829 878236
                                if (*b == '\\' && b[1] == '0') {
830 160
                                        *b = '\0';
831 1640
                                        for (c = b+1; *c != '\0'; c++) {
832 1480
                                                *c = c[1];
833 1480
                                        }
834 160
                                        b++;
835 160
                                        bodylen--;
836 160
                                }
837 878236
                        }
838 45079
                } else if (!strcmp(*av, "-bodyfrom")) {
839 80
                        assert(body == nullbody);
840 80
                        free(body);
841 80
                        body = VFIL_readfile(NULL, av[1], &len);
842 80
                        AN(body);
843 80
                        assert(len < INT_MAX);
844 80
                        bodylen = len;
845 80
                        av++;
846 15920
                } else if (!strcmp(*av, "-bodylen")) {
847 8680
                        assert(body == nullbody);
848 8680
                        free(body);
849 8680
                        body = synth_body(av[1], 0);
850 8680
                        bodylen = strlen(body);
851 8680
                        av++;
852 15840
                } else if (!strncmp(*av, "-gzip", 5)) {
853 7160
                        l = vtc_gzip_cmd(hp, av, &body, &bodylen);
854 7160
                        if (l == 0)
855 0
                                break;
856 7160
                        av += l;
857 7160
                        if (l > 1)
858 6520
                                VSB_printf(hp->vsb, "Content-Encoding: gzip%s", nl);
859 7160
                } else
860 0
                        break;
861 45079
        }
862 206455
        if (!nohost) {
863 118317
                VSB_cat(hp->vsb, "Host: ");
864 118317
                macro_cat(vl, hp->vsb, "localhost", NULL);
865 118317
                VSB_cat(hp->vsb, nl);
866 118317
        }
867 206455
        if (!nodate) {
868 84293
                VSB_cat(hp->vsb, "Date: ");
869 84293
                macro_cat(vl, hp->vsb, "date", NULL);
870 84293
                VSB_cat(hp->vsb, nl);
871 84293
        }
872 206455
        if (!noserver)
873 83776
                VSB_printf(hp->vsb, "Server: %s%s", hp->sess->name, nl);
874 206455
        if (!nouseragent)
875 121280
                VSB_printf(hp->vsb, "User-Agent: %s%s", hp->sess->name, nl);
876 206455
        if (body != NULL && !nolen)
877 81656
                VSB_printf(hp->vsb, "Content-Length: %ld%s", bodylen, nl);
878 206455
        VSB_cat(hp->vsb, nl);
879 206455
        if (body != NULL) {
880 86816
                VSB_bcat(hp->vsb, body, bodylen);
881 86816
                free(body);
882 86816
        }
883 206455
        return (av);
884
}
885
886
/* SECTION: client-server.spec.txreq
887
 *
888
 * txreq|txresp [...]
889
 *         Send a minimal request or response, but overload it if necessary.
890
 *
891
 *         txreq is client-specific and txresp is server-specific.
892
 *
893
 *         The only thing different between a request and a response, apart
894
 *         from who can send them is that the first line (request line vs
895
 *         status line), so all the options are prety much the same.
896
 *
897
 *         \-method STRING (txreq only)
898
 *                 What method to use (default: "GET").
899
 *
900
 *         \-req STRING (txreq only)
901
 *                 Alias for -method.
902
 *
903
 *         \-url STRING (txreq only)
904
 *                 What location to use (default "/").
905
 *
906
 *         \-proto STRING
907
 *                 What protocol use in the status line.
908
 *                 (default: "HTTP/1.1").
909
 *
910
 *         \-status NUMBER (txresp only)
911
 *                 What status code to return (default 200).
912
 *
913
 *         \-reason STRING (txresp only)
914
 *                 What message to put in the status line (default: "OK").
915
 *
916
 *         \-noserver (txresp only)
917
 *                 Don't include a Server header with the id of the server.
918
 *
919
 *         \-nouseragent (txreq only)
920
 *                 Don't include a User-Agent header with the id of the client.
921
 *
922
 *         These three switches can appear in any order but must come before the
923
 *         following ones.
924
 *
925
 *         \-nohost
926
 *                 Don't include a Host header in the request. Also Implied
927
 *                 by the addition of a Host header with ``-hdr``.
928
 *
929
 *         \-nolen
930
 *                 Don't include a Content-Length header. Also implied by the
931
 *                 addition of a Content-Length or Transfer-Encoding header
932
 *                 with ``-hdr``.
933
 *
934
 *         \-nodate
935
 *                 Don't include a Date header in the response. Also implied
936
 *                 by the addition of a Date header with ``-hdr``.
937
 *
938
 *         \-hdr STRING
939
 *                 Add STRING as a header, it must follow this format:
940
 *                 "name: value". It can be called multiple times.
941
 *
942
 *         \-hdrlen STRING NUMBER
943
 *                 Add STRING as a header with NUMBER bytes of content.
944
 *
945
 *         You can then use the arguments related to the body:
946
 *
947
 *         \-body STRING
948
 *                 Input STRING as body.
949
 *
950
 *         \-bodyfrom FILE
951
 *                 Same as -body but content is read from FILE.
952
 *
953
 *         \-bodylen NUMBER
954
 *                 Generate and input a body that is NUMBER bytes-long.
955
 *
956
 *         \-gziplevel NUMBER
957
 *                 Set the gzip level (call it before any of the other gzip
958
 *                 switches).
959
 *
960
 *         \-gzipresidual NUMBER
961
 *                 Add extra gzip bits. You should never need it.
962
 *
963
 *         \-gzipbody STRING
964
 *                 Gzip STRING and send it as body.
965
 *
966
 *         \-gziplen NUMBER
967
 *                 Combine -bodylen and -gzipbody: generate a string of length
968
 *                 NUMBER, gzip it and send as body.
969
 */
970
971
/**********************************************************************
972
 * Transmit a response
973
 */
974
975
static void
976 84935
cmd_http_txresp(CMD_ARGS)
977
{
978
        struct http *hp;
979 84935
        const char *proto = "HTTP/1.1";
980 84935
        const char *status = "200";
981 84935
        const char *reason = "OK";
982 84935
        char* body = NULL;
983 84935
        unsigned noserver = 0;
984
985 84935
        (void)vl;
986 84935
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
987 84935
        ONLY_SERVER(hp, av);
988 84935
        AZ(strcmp(av[0], "txresp"));
989 84935
        av++;
990
991 84935
        VSB_clear(hp->vsb);
992
993 94415
        for (; *av != NULL; av++) {
994 65760
                if (!strcmp(*av, "-proto")) {
995 1640
                        proto = av[1];
996 1640
                        av++;
997 65760
                } else if (!strcmp(*av, "-status")) {
998 6160
                        status = av[1];
999 6160
                        av++;
1000 64120
                } else if (!strcmp(*av, "-reason")) {
1001 1120
                        reason = av[1];
1002 1120
                        av++;
1003 1120
                        continue;
1004 56840
                } else if (!strcmp(*av, "-noserver")) {
1005 560
                        noserver = 1;
1006 560
                        continue;
1007
                } else
1008 56280
                        break;
1009 7800
        }
1010
1011 84935
        VSB_printf(hp->vsb, "%s %s %s%s", proto, status, reason, nl);
1012
1013
        /* send a "Content-Length: 0" header unless something else happens */
1014 84935
        REPLACE(body, "");
1015
1016 84935
        av = http_tx_parse_args(av, vl, hp, body, 1, 0, noserver, 1);
1017 84935
        if (*av != NULL)
1018 0
                vtc_fatal(hp->vl, "Unknown http txresp spec: %s\n", *av);
1019
1020 84935
        http_write(hp, 4, "txresp");
1021 84935
}
1022
1023
static void
1024 40
cmd_http_upgrade(CMD_ARGS)
1025
{
1026
        char *h;
1027
        struct http *hp;
1028
1029 40
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1030 40
        ONLY_SERVER(hp, av);
1031 40
        AN(hp->sfd);
1032
1033 40
        h = http_find_header(hp->req, "Upgrade");
1034 40
        if (!h || strcmp(h, "h2c"))
1035 0
                vtc_fatal(vl, "Req misses \"Upgrade: h2c\" header");
1036
1037 40
        h = http_find_header(hp->req, "Connection");
1038 40
        if (!h || strcmp(h, "Upgrade, HTTP2-Settings"))
1039 0
                vtc_fatal(vl, "Req misses \"Connection: "
1040
                        "Upgrade, HTTP2-Settings\" header");
1041
1042 40
        h = http_find_header(hp->req, "HTTP2-Settings");
1043 40
        if (!h)
1044 0
                vtc_fatal(vl, "Req misses \"HTTP2-Settings\" header");
1045
1046 40
        parse_string(vl, hp,
1047
            "txresp -status 101"
1048
            " -hdr \"Connection: Upgrade\""
1049
            " -hdr \"Upgrade: h2c\"\n"
1050
        );
1051
1052 40
        b64_settings(hp, h);
1053
1054 40
        parse_string(vl, hp,
1055
            "rxpri\n"
1056
            "stream 0 {\n"
1057
            "    txsettings\n"
1058
            "    rxsettings\n"
1059
            "    txsettings -ack\n"
1060
            "    rxsettings\n"
1061
            "    expect settings.ack == true\n"
1062
            "} -start\n"
1063
        );
1064 40
}
1065
1066
/**********************************************************************
1067
 * Receive a request
1068
 */
1069
1070
/* SECTION: client-server.spec.rxreq
1071
 *
1072
 * rxreq (server only)
1073
 *         Receive and parse a request's headers and body.
1074
 */
1075
static void
1076 91548
cmd_http_rxreq(CMD_ARGS)
1077
{
1078
        struct http *hp;
1079
1080 91548
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1081 91548
        ONLY_SERVER(hp, av);
1082 91548
        AZ(strcmp(av[0], "rxreq"));
1083 91548
        av++;
1084
1085 91548
        for (; *av != NULL; av++)
1086 0
                vtc_fatal(vl, "Unknown http rxreq spec: %s\n", *av);
1087 91548
        http_rxhdr(hp);
1088 91548
        http_splitheader(hp, 1);
1089 91548
        if (http_count_header(hp->req, "Content-Length") > 1)
1090 0
                vtc_fatal(vl, "Multiple Content-Length headers.\n");
1091 91548
        http_swallow_body(hp, hp->req, 0, 0);
1092 91548
        vtc_log(vl, 4, "bodylen = %s", hp->bodylen);
1093 91548
}
1094
1095
/* SECTION: client-server.spec.rxreqhdrs
1096
 *
1097
 * rxreqhdrs (server only)
1098
 *         Receive and parse a request's headers (but not the body).
1099
 */
1100
1101
static void
1102 200
cmd_http_rxreqhdrs(CMD_ARGS)
1103
{
1104
        struct http *hp;
1105
1106 200
        (void)vl;
1107 200
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1108 200
        AZ(strcmp(av[0], "rxreqhdrs"));
1109 200
        av++;
1110
1111 200
        for (; *av != NULL; av++)
1112 0
                vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av);
1113 200
        http_rxhdr(hp);
1114 200
        http_splitheader(hp, 1);
1115 200
        if (http_count_header(hp->req, "Content-Length") > 1)
1116 0
                vtc_fatal(hp->vl, "Multiple Content-Length headers.\n");
1117 200
}
1118
1119
/* SECTION: client-server.spec.rxreqbody
1120
 *
1121
 * rxreqbody (server only)
1122
 *         Receive a request's body.
1123
 */
1124
1125
static void
1126 160
cmd_http_rxreqbody(CMD_ARGS)
1127
{
1128
        struct http *hp;
1129
1130 160
        (void)vl;
1131 160
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1132 160
        ONLY_SERVER(hp, av);
1133 160
        AZ(strcmp(av[0], "rxreqbody"));
1134 160
        av++;
1135
1136 160
        for (; *av != NULL; av++)
1137 0
                vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av);
1138 160
        http_swallow_body(hp, hp->req, 0, 0);
1139 160
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
1140 160
}
1141
1142
/* SECTION: client-server.spec.rxrespbody
1143
 *
1144
 * rxrespbody (client only)
1145
 *         Receive (part of) a response's body.
1146
 *
1147
 * -max : max length of this receive, 0 for all
1148
 */
1149
1150
static void
1151 1320
cmd_http_rxrespbody(CMD_ARGS)
1152
{
1153
        struct http *hp;
1154 1320
        int max = 0;
1155
1156 1320
        (void)vl;
1157 1320
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1158 1320
        ONLY_CLIENT(hp, av);
1159 1320
        AZ(strcmp(av[0], "rxrespbody"));
1160 1320
        av++;
1161
1162 1720
        for (; *av != NULL; av++)
1163 800
                if (!strcmp(*av, "-max")) {
1164 400
                        max = atoi(av[1]);
1165 400
                        av++;
1166 400
                } else
1167 0
                        vtc_fatal(hp->vl,
1168 0
                            "Unknown http rxrespbody spec: %s\n", *av);
1169
1170 1320
        http_swallow_body(hp, hp->resp, 1, max);
1171 1320
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
1172 1320
}
1173
1174
/* SECTION: client-server.spec.rxchunk
1175
 *
1176
 * rxchunk
1177
 *         Receive an HTTP chunk.
1178
 */
1179
1180
static void
1181 520
cmd_http_rxchunk(CMD_ARGS)
1182
{
1183
        struct http *hp;
1184
        int ll, i;
1185
1186 520
        (void)vl;
1187 520
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1188 520
        ONLY_CLIENT(hp, av);
1189
1190 520
        i = http_rxchunk(hp);
1191 520
        if (i == 0) {
1192 0
                ll = hp->rx_p - hp->body;
1193 0
                hp->bodyl = ll;
1194 0
                bprintf(hp->bodylen, "%d", ll);
1195 0
                vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
1196 0
        }
1197 520
}
1198
1199
/**********************************************************************
1200
 * Transmit a request
1201
 */
1202
1203
static void
1204 121517
cmd_http_txreq(CMD_ARGS)
1205
{
1206
        struct http *hp;
1207 121517
        const char *req = "GET";
1208 121517
        const char *url = "/";
1209 121517
        const char *proto = "HTTP/1.1";
1210 121517
        const char *up = NULL;
1211
        unsigned nohost;
1212 121517
        unsigned nouseragent = 0;
1213
1214 121517
        (void)vl;
1215 121517
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1216 121517
        ONLY_CLIENT(hp, av);
1217 121517
        AZ(strcmp(av[0], "txreq"));
1218 121517
        av++;
1219
1220 121517
        VSB_clear(hp->vsb);
1221
1222 121517
        hp->head_method = 0;
1223 199154
        for (; *av != NULL; av++) {
1224 111037
                if (!strcmp(*av, "-url")) {
1225 65397
                        url = av[1];
1226 65397
                        av++;
1227 111037
                } else if (!strcmp(*av, "-proto")) {
1228 2000
                        proto = av[1];
1229 2000
                        av++;
1230 45640
                } else if (!strcmp(*av, "-method") ||
1231 38799
                    !strcmp(*av, "-req")) {
1232 10080
                        req = av[1];
1233 10080
                        hp->head_method = !strcmp(av[1], "HEAD") ;
1234 10080
                        av++;
1235 43640
                } else if (!hp->sfd && !strcmp(*av, "-up")) {
1236 0
                        up = av[1];
1237 0
                        av++;
1238 33560
                } else if (!strcmp(*av, "-nouseragent")) {
1239 160
                        nouseragent = 1;
1240 160
                } else
1241 33400
                        break;
1242 77637
        }
1243 121517
        VSB_printf(hp->vsb, "%s %s %s%s", req, url, proto, nl);
1244
1245 121517
        if (up)
1246 0
                VSB_printf(hp->vsb, "Connection: Upgrade, HTTP2-Settings%s"
1247
                                "Upgrade: h2c%s"
1248 0
                                "HTTP2-Settings: %s%s", nl, nl, up, nl);
1249
1250 121517
        nohost = strcmp(proto, "HTTP/1.1") != 0;
1251 121517
        av = http_tx_parse_args(av, vl, hp, NULL, nohost, 1, 1, nouseragent);
1252 121517
        if (*av != NULL)
1253 0
                vtc_fatal(hp->vl, "Unknown http txreq spec: %s\n", *av);
1254 121517
        http_write(hp, 4, "txreq");
1255
1256 121517
        if (up) {
1257 0
                parse_string(vl, hp,
1258
                    "rxresp\n"
1259
                    "expect resp.status == 101\n"
1260
                    "expect resp.http.connection == Upgrade\n"
1261
                    "expect resp.http.upgrade == h2c\n"
1262
                    "txpri\n"
1263
                );
1264 0
                b64_settings(hp, up);
1265 0
                parse_string(vl, hp,
1266
                    "stream 0 {\n"
1267
                    "    txsettings\n"
1268
                    "    rxsettings\n"
1269
                    "    txsettings -ack\n"
1270
                    "    rxsettings\n"
1271
                    "    expect settings.ack == true"
1272
                    "} -start\n"
1273
                );
1274 0
        }
1275 121517
}
1276
1277
/* SECTION: client-server.spec.recv
1278
 *
1279
 * recv NUMBER
1280
 *         Read NUMBER bytes from the connection.
1281
 */
1282
1283
static void
1284 360
cmd_http_recv(CMD_ARGS)
1285
{
1286
        struct http *hp;
1287
        int i, n;
1288
        char u[32];
1289
1290 360
        (void)vl;
1291 360
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1292 360
        AN(av[1]);
1293 360
        AZ(av[2]);
1294 360
        n = strtoul(av[1], NULL, 0);
1295 1160
        while (n > 0) {
1296 800
                i = read(hp->sess->fd, u, n > 32 ? 32 : n);
1297 800
                if (i > 0)
1298 800
                        vtc_dump(hp->vl, 4, "recv", u, i);
1299
                else
1300 0
                        vtc_log(hp->vl, hp->fatal, "recv() got %d (%s)", i,
1301 0
                            strerror(errno));
1302 800
                n -= i;
1303
        }
1304 360
}
1305
1306
/* SECTION: client-server.spec.send
1307
 *
1308
 * send STRING
1309
 *         Push STRING on the connection.
1310
 */
1311
1312
static void
1313 34969
cmd_http_send(CMD_ARGS)
1314
{
1315
        struct http *hp;
1316
        int i;
1317
1318 34969
        (void)vl;
1319 34969
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1320 34969
        AN(av[1]);
1321 34969
        AZ(av[2]);
1322 34969
        vtc_dump(hp->vl, 4, "send", av[1], -1);
1323 34969
        i = write(hp->sess->fd, av[1], strlen(av[1]));
1324 34969
        if (i != strlen(av[1]))
1325 6
                vtc_log(hp->vl, hp->fatal, "Write error in http_send(): %s",
1326 3
                    strerror(errno));
1327 34969
}
1328
1329
/* SECTION: client-server.spec.send_n
1330
 *
1331
 * send_n NUMBER STRING
1332
 *         Write STRING on the socket NUMBER times.
1333
 */
1334
1335
static void
1336 80
cmd_http_send_n(CMD_ARGS)
1337
{
1338
        struct http *hp;
1339
        int i, n, l;
1340
1341 80
        (void)vl;
1342 80
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1343 80
        AN(av[1]);
1344 80
        AN(av[2]);
1345 80
        AZ(av[3]);
1346 80
        n = strtoul(av[1], NULL, 0);
1347 80
                vtc_dump(hp->vl, 4, "send_n", av[2], -1);
1348 80
        l = strlen(av[2]);
1349 82400
        while (n--) {
1350 82320
                i = write(hp->sess->fd, av[2], l);
1351 82320
                if (i != l)
1352 5236
                        vtc_log(hp->vl, hp->fatal,
1353
                            "Write error in http_send(): %s",
1354 2618
                            strerror(errno));
1355
        }
1356 80
}
1357
1358
/* SECTION: client-server.spec.send_urgent
1359
 *
1360
 * send_urgent STRING
1361
 *         Send string as TCP OOB urgent data. You will never need this.
1362
 */
1363
1364
static void
1365 400
cmd_http_send_urgent(CMD_ARGS)
1366
{
1367
        struct http *hp;
1368
        int i;
1369
1370 400
        (void)vl;
1371 400
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1372 400
        AN(av[1]);
1373 400
        AZ(av[2]);
1374 400
        vtc_dump(hp->vl, 4, "send_urgent", av[1], -1);
1375 400
        i = send(hp->sess->fd, av[1], strlen(av[1]), MSG_OOB);
1376 400
        if (i != strlen(av[1]))
1377 0
                vtc_log(hp->vl, hp->fatal,
1378 0
                    "Write error in http_send_urgent(): %s", strerror(errno));
1379 400
}
1380
1381
/* SECTION: client-server.spec.sendhex
1382
 *
1383
 * sendhex STRING
1384
 *         Send bytes as described by STRING. STRING should consist of hex pairs
1385
 *         possibly separated by whitespace or newlines. For example:
1386
 *         "0F EE a5    3df2".
1387
 */
1388
1389
static void
1390 6439
cmd_http_sendhex(CMD_ARGS)
1391
{
1392
        struct vsb *vsb;
1393
        struct http *hp;
1394
1395 6439
        (void)vl;
1396 6439
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1397 6439
        AN(av[1]);
1398 6439
        AZ(av[2]);
1399 6439
        vsb = vtc_hex_to_bin(hp->vl, av[1]);
1400 6439
        assert(VSB_len(vsb) >= 0);
1401 6439
        vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
1402 6439
        if (VSB_tofile(vsb, hp->sess->fd))
1403 0
                vtc_log(hp->vl, hp->fatal, "Write failed: %s",
1404 0
                    strerror(errno));
1405 6439
        VSB_destroy(&vsb);
1406 6439
}
1407
1408
/* SECTION: client-server.spec.chunked
1409
 *
1410
 * chunked STRING
1411
 *         Send STRING as chunked encoding.
1412
 */
1413
1414
static void
1415 2440
cmd_http_chunked(CMD_ARGS)
1416
{
1417
        struct http *hp;
1418
1419 2440
        (void)vl;
1420 2440
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1421 2440
        AN(av[1]);
1422 2440
        AZ(av[2]);
1423 2440
        VSB_clear(hp->vsb);
1424 4880
        VSB_printf(hp->vsb, "%jx%s%s%s",
1425 2440
            (uintmax_t)strlen(av[1]), nl, av[1], nl);
1426 2440
        http_write(hp, 4, "chunked");
1427 2440
}
1428
1429
/* SECTION: client-server.spec.chunkedlen
1430
 *
1431
 * chunkedlen NUMBER
1432
 *         Do as ``chunked`` except that the string will be generated
1433
 *         for you, with a length of NUMBER characters.
1434
 */
1435
1436
static void
1437 7399
cmd_http_chunkedlen(CMD_ARGS)
1438
{
1439
        unsigned len;
1440
        unsigned u, v;
1441
        char buf[16384];
1442
        struct http *hp;
1443
1444 7399
        (void)vl;
1445 7399
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1446 7399
        AN(av[1]);
1447 7399
        AZ(av[2]);
1448 7399
        VSB_clear(hp->vsb);
1449
1450 7399
        len = atoi(av[1]);
1451
1452 7399
        if (len == 0) {
1453 2800
                VSB_printf(hp->vsb, "0%s%s", nl, nl);
1454 2800
        } else {
1455 67516888
                for (u = 0; u < sizeof buf; u++)
1456 67512289
                        buf[u] = (u & 7) + '0';
1457
1458 4599
                VSB_printf(hp->vsb, "%x%s", len, nl);
1459 16158
                for (u = 0; u < len; u += v) {
1460 11559
                        v = vmin_t(unsigned, len - u, sizeof buf);
1461 11559
                        VSB_bcat(hp->vsb, buf, v);
1462 11559
                }
1463 4599
                VSB_printf(hp->vsb, "%s", nl);
1464
        }
1465 7399
        http_write(hp, 4, "chunked");
1466 7399
}
1467
1468
1469
/* SECTION: client-server.spec.timeout
1470
 *
1471
 * timeout NUMBER
1472
 *         Set the TCP timeout for this entity.
1473
 */
1474
1475
static void
1476 1320
cmd_http_timeout(CMD_ARGS)
1477
{
1478
        struct http *hp;
1479
        double d;
1480
1481 1320
        (void)vl;
1482 1320
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1483 1320
        AN(av[1]);
1484 1320
        AZ(av[2]);
1485 1320
        d = VNUM(av[1]);
1486 1320
        if (isnan(d))
1487 0
                vtc_fatal(vl, "timeout is not a number (%s)", av[1]);
1488 1320
        hp->timeout = d;
1489 1320
}
1490
1491
/* SECTION: client-server.spec.expect_close
1492
 *
1493
 * expect_close
1494
 *      Reads from the connection, expecting nothing to read but an EOF.
1495
 */
1496
static void
1497 6462
cmd_http_expect_close(CMD_ARGS)
1498
{
1499
        struct http *hp;
1500
        struct pollfd fds[1];
1501
        char c;
1502
        int i;
1503
1504 6462
        (void)vl;
1505 6462
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1506 6462
        AZ(av[1]);
1507
1508 6462
        vtc_log(vl, 4, "Expecting close (fd = %d)", hp->sess->fd);
1509 6462
        if (hp->h2)
1510 560
                stop_h2(hp);
1511 6462
        while (1) {
1512 6462
                fds[0].fd = hp->sess->fd;
1513 6462
                fds[0].events = POLLIN;
1514 6462
                fds[0].revents = 0;
1515 6462
                i = poll(fds, 1, (int)(hp->timeout * 1000));
1516 6462
                if (i < 0 && errno == EINTR)
1517 0
                        continue;
1518 6462
                if (i == 0)
1519 0
                        vtc_log(vl, hp->fatal, "Expected close: timeout");
1520 6462
                if (i != 1 || !(fds[0].revents & (POLLIN|POLLERR|POLLHUP)))
1521 412
                        vtc_log(vl, hp->fatal,
1522
                            "Expected close: poll = %d, revents = 0x%x",
1523 206
                            i, fds[0].revents);
1524 6256
                i = read(hp->sess->fd, &c, 1);
1525 6256
                if (i <= 0 && VTCP_Check(i))
1526 6256
                        break;
1527 206
                if (i == 1 && vct_islws(c))
1528 0
                        continue;
1529 0
                vtc_log(vl, hp->fatal,
1530 0
                    "Expecting close: read = %d, c = 0x%02x", i, c);
1531
        }
1532 6256
        vtc_log(vl, 4, "fd=%d EOF, as expected", hp->sess->fd);
1533 6256
}
1534
1535
/* SECTION: client-server.spec.close
1536
 *
1537
 * close (server only)
1538
 *      Close the connection. Note that if operating in HTTP/2 mode no
1539
 *      extra (GOAWAY) frame is sent, it's simply a TCP close.
1540
 */
1541
static void
1542 760
cmd_http_close(CMD_ARGS)
1543
{
1544
        struct http *hp;
1545
1546 760
        (void)vl;
1547 760
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1548 760
        ONLY_SERVER(hp, av);
1549 760
        AZ(av[1]);
1550 760
        assert(hp->sfd != NULL);
1551 760
        assert(*hp->sfd >= 0);
1552 760
        if (hp->h2)
1553 0
                stop_h2(hp);
1554 760
        VTCP_close(&hp->sess->fd);
1555 760
        vtc_log(vl, 4, "Closed");
1556 760
}
1557
1558
/* SECTION: client-server.spec.accept
1559
 *
1560
 * accept (server only)
1561
 *      Close the current connection, if any, and accept a new one. Note
1562
 *      that this new connection is HTTP/1.x.
1563
 */
1564
static void
1565 6449
cmd_http_accept(CMD_ARGS)
1566
{
1567
        struct http *hp;
1568
1569 6449
        (void)vl;
1570 6449
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1571 6449
        ONLY_SERVER(hp, av);
1572 6449
        AZ(av[1]);
1573 6449
        assert(hp->sfd != NULL);
1574 6449
        assert(*hp->sfd >= 0);
1575 6449
        if (hp->h2)
1576 0
                stop_h2(hp);
1577 6449
        if (hp->sess->fd >= 0)
1578 5729
                VTCP_close(&hp->sess->fd);
1579 6449
        vtc_log(vl, 4, "Accepting");
1580 6449
        hp->sess->fd = accept(*hp->sfd, NULL, NULL);
1581 6449
        if (hp->sess->fd < 0)
1582 0
                vtc_log(vl, hp->fatal, "Accepted failed: %s", strerror(errno));
1583 6449
        vtc_log(vl, 3, "Accepted socket fd is %d", hp->sess->fd);
1584 6449
}
1585
1586
/* SECTION: client-server.spec.fatal
1587
 *
1588
 * fatal|non_fatal
1589
 *         Control whether a failure of this entity should stop the test.
1590
 */
1591
1592
static void
1593 11633
cmd_http_fatal(CMD_ARGS)
1594
{
1595
        struct http *hp;
1596 11633
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1597
1598 11633
        (void)vl;
1599 11633
        AZ(av[1]);
1600 11633
        if (!strcmp(av[0], "fatal")) {
1601 600
                hp->fatal = 0;
1602 600
        } else {
1603 11033
                assert(!strcmp(av[0], "non_fatal"));
1604 11033
                hp->fatal = -1;
1605
        }
1606 11633
}
1607
1608
#define cmd_http_non_fatal cmd_http_fatal
1609
1610
static const char PREFACE[24] = {
1611
        0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54,
1612
        0x54, 0x50, 0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a,
1613
        0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a, 0x0d, 0x0a
1614
};
1615
1616
/* SECTION: client-server.spec.txpri
1617
 *
1618
 * txpri (client only)
1619
 *      Send an HTTP/2 preface ("PRI * HTTP/2.0\\r\\n\\r\\nSM\\r\\n\\r\\n")
1620
 *      and set client to HTTP/2.
1621
 */
1622
static void
1623 6760
cmd_http_txpri(CMD_ARGS)
1624
{
1625
        size_t l;
1626
        struct http *hp;
1627
1628 6760
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1629 6760
        ONLY_CLIENT(hp, av);
1630
1631 6760
        vtc_dump(hp->vl, 4, "txpri", PREFACE, sizeof(PREFACE));
1632
        /* Dribble out the preface */
1633 6760
        l = write(hp->sess->fd, PREFACE, 18);
1634 6760
        if (l != 18)
1635 0
                vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
1636 0
                    l, sizeof(PREFACE), strerror(errno));
1637 6760
        usleep(10000);
1638 6760
        l = write(hp->sess->fd, PREFACE + 18, sizeof(PREFACE) - 18);
1639 6760
        if (l != sizeof(PREFACE) - 18)
1640 0
                vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
1641 0
                    l, sizeof(PREFACE), strerror(errno));
1642
1643 6760
        start_h2(hp);
1644 6760
        AN(hp->h2);
1645 6760
}
1646
1647
/* SECTION: client-server.spec.rxpri
1648
 *
1649
 * rxpri (server only)
1650
 *      Receive a preface. If valid set the server to HTTP/2, abort
1651
 *      otherwise.
1652
 */
1653
static void
1654 1160
cmd_http_rxpri(CMD_ARGS)
1655
{
1656
        struct http *hp;
1657
1658 1160
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1659 1160
        ONLY_SERVER(hp, av);
1660
1661 1160
        hp->rx_p = hp->rx_b;
1662 1160
        if (!http_rxchar(hp, sizeof(PREFACE), 0))
1663 0
                vtc_fatal(vl, "Couldn't retrieve connection preface");
1664 1160
        if (memcmp(hp->rx_b, PREFACE, sizeof(PREFACE)))
1665 0
                vtc_fatal(vl, "Received invalid preface\n");
1666 1160
        start_h2(hp);
1667 1160
        AN(hp->h2);
1668 1160
}
1669
1670
/* SECTION: client-server.spec.settings
1671
 *
1672
 * settings -dectbl INT
1673
 *      Force internal HTTP/2 settings to certain values. Currently only
1674
 *      support setting the decoding table size.
1675
 */
1676
static void
1677 0
cmd_http_settings(CMD_ARGS)
1678
{
1679
        uint32_t n;
1680
        char *p;
1681
        struct http *hp;
1682 0
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1683
1684 0
        if (!hp->h2)
1685 0
                vtc_fatal(hp->vl, "Only possible in H/2 mode");
1686
1687 0
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1688
1689 0
        for (; *av != NULL; av++) {
1690 0
                if (!strcmp(*av, "-dectbl")) {
1691 0
                        n = strtoul(av[1], &p, 0);
1692 0
                        if (*p != '\0')
1693 0
                                vtc_fatal(hp->vl, "-dectbl takes an integer as "
1694 0
                                    "argument (found %s)", av[1]);
1695 0
                        assert(HPK_ResizeTbl(hp->decctx, n) != hpk_err);
1696 0
                        av++;
1697 0
                } else
1698 0
                        vtc_fatal(vl, "Unknown settings spec: %s\n", *av);
1699 0
        }
1700 0
}
1701
1702
static void
1703 22359
cmd_http_stream(CMD_ARGS)
1704
{
1705
        struct http *hp;
1706 22359
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1707 22359
        if (!hp->h2) {
1708 7080
                vtc_log(hp->vl, 4, "Not in H/2 mode, do what's needed");
1709 7080
                if (hp->sfd)
1710 1040
                        parse_string(vl, hp, "rxpri");
1711
                else
1712 6040
                        parse_string(vl, hp, "txpri");
1713 7080
                parse_string(vl, hp,
1714
                    "stream 0 {\n"
1715
                    "    txsettings\n"
1716
                    "    rxsettings\n"
1717
                    "    txsettings -ack\n"
1718
                    "    rxsettings\n"
1719
                    "    expect settings.ack == true"
1720
                    "} -run\n"
1721
                );
1722 7080
        }
1723 22359
        cmd_stream(av, hp, vl);
1724 22359
}
1725
1726
/* SECTION: client-server.spec.write_body
1727
 *
1728
 * write_body STRING
1729
 *      Write the body of a request or a response to a file. By using the
1730
 *      shell command, higher-level checks on the body can be performed
1731
 *      (eg. XML, JSON, ...) provided that such checks can be delegated
1732
 *      to an external program.
1733
 */
1734
static void
1735 80
cmd_http_write_body(CMD_ARGS)
1736
{
1737
        struct http *hp;
1738
1739 80
        (void)vl;
1740 80
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1741 80
        AN(av[0]);
1742 80
        AN(av[1]);
1743 80
        AZ(av[2]);
1744 80
        AZ(strcmp(av[0], "write_body"));
1745 80
        if (VFIL_writefile(NULL, av[1], hp->body, hp->bodyl) != 0)
1746 0
                vtc_fatal(hp->vl, "failed to write body: %s (%d)",
1747 0
                    strerror(errno), errno);
1748 80
}
1749
1750
/**********************************************************************
1751
 * Execute HTTP specifications
1752
 */
1753
1754
const struct cmds http_cmds[] = {
1755
#define CMD_HTTP(n) { #n, cmd_http_##n },
1756
        /* session */
1757
        CMD_HTTP(accept)
1758
        CMD_HTTP(close)
1759
        CMD_HTTP(recv)
1760
        CMD_HTTP(send)
1761
        CMD_HTTP(send_n)
1762
        CMD_HTTP(send_urgent)
1763
        CMD_HTTP(sendhex)
1764
        CMD_HTTP(timeout)
1765
1766
        /* spec */
1767
        CMD_HTTP(fatal)
1768
        CMD_HTTP(non_fatal)
1769
1770
        /* body */
1771
        CMD_HTTP(gunzip)
1772
        CMD_HTTP(write_body)
1773
1774
        /* HTTP/1.x */
1775
        CMD_HTTP(chunked)
1776
        CMD_HTTP(chunkedlen)
1777
        CMD_HTTP(rxchunk)
1778
1779
        /* HTTP/2 */
1780
        CMD_HTTP(stream)
1781
        CMD_HTTP(settings)
1782
1783
        /* client */
1784
        CMD_HTTP(rxresp)
1785
        CMD_HTTP(rxrespbody)
1786
        CMD_HTTP(rxresphdrs)
1787
        CMD_HTTP(txpri)
1788
        CMD_HTTP(txreq)
1789
1790
        /* server */
1791
        CMD_HTTP(rxpri)
1792
        CMD_HTTP(rxreq)
1793
        CMD_HTTP(rxreqbody)
1794
        CMD_HTTP(rxreqhdrs)
1795
        CMD_HTTP(txresp)
1796
        CMD_HTTP(upgrade)
1797
1798
        /* expect */
1799
        CMD_HTTP(expect)
1800
        CMD_HTTP(expect_close)
1801
        CMD_HTTP(expect_pattern)
1802
#undef CMD_HTTP
1803
        { NULL, NULL }
1804
};
1805
1806
static void
1807 150129
http_process_cleanup(void *arg)
1808
{
1809
        struct http *hp;
1810
1811 150129
        CAST_OBJ_NOTNULL(hp, arg, HTTP_MAGIC);
1812
1813 150129
        if (hp->h2)
1814 7360
                stop_h2(hp);
1815 150129
        VSB_destroy(&hp->vsb);
1816 150129
        free(hp->rx_b);
1817 150129
        free(hp->rem_ip);
1818 150129
        free(hp->rem_port);
1819 150129
        free(hp->rem_path);
1820 150129
        FREE_OBJ(hp);
1821 150129
}
1822
1823
int
1824 150148
http_process(struct vtclog *vl, struct vtc_sess *vsp, const char *spec,
1825
    int sock, int *sfd, const char *addr, int rcvbuf)
1826
{
1827
        struct http *hp;
1828
        int retval, oldbuf;
1829 150148
        socklen_t intlen = sizeof(int);
1830
1831 150148
        (void)sfd;
1832 150148
        ALLOC_OBJ(hp, HTTP_MAGIC);
1833 150148
        AN(hp);
1834 150148
        hp->sess = vsp;
1835 150148
        hp->sess->fd = sock;
1836 150148
        hp->timeout = vtc_maxdur * .5;
1837
1838 150148
        if (rcvbuf) {
1839
                // XXX setsockopt() too late on SunOS
1840
                // https://github.com/varnishcache/varnish-cache/pull/2980#issuecomment-486214661
1841 360
                hp->rcvbuf = rcvbuf;
1842
1843 360
                oldbuf = 0;
1844 360
                AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &oldbuf, &intlen));
1845 360
                AZ(setsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, intlen));
1846 360
                AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &intlen));
1847
1848 720
                vtc_log(vl, 3, "-rcvbuf fd=%d old=%d new=%d actual=%d",
1849 360
                    hp->sess->fd, oldbuf, hp->rcvbuf, rcvbuf);
1850 360
        }
1851
1852 150148
        hp->nrxbuf = 2048*1024;
1853 150148
        hp->rx_b = malloc(hp->nrxbuf);
1854 150148
        AN(hp->rx_b);
1855 150148
        hp->rx_e = hp->rx_b + hp->nrxbuf;
1856 150148
        hp->rx_p = hp->rx_b;
1857 150148
        *hp->rx_p = '\0';
1858
1859 150148
        hp->vsb = VSB_new_auto();
1860 150148
        AN(hp->vsb);
1861
1862 150148
        hp->sfd = sfd;
1863
1864 150148
        hp->rem_ip = malloc(VTCP_ADDRBUFSIZE);
1865 150148
        AN(hp->rem_ip);
1866
1867 150148
        hp->rem_port = malloc(VTCP_PORTBUFSIZE);
1868 150148
        AN(hp->rem_port);
1869
1870 150148
        hp->vl = vl;
1871 150148
        vtc_log_set_cmd(hp->vl, http_cmds);
1872 150148
        hp->gziplevel = 0;
1873 150148
        hp->gzipresidual = -1;
1874
1875 150148
        if (*addr != '/') {
1876 137105
                VTCP_hisname(sock, hp->rem_ip, VTCP_ADDRBUFSIZE, hp->rem_port,
1877
                             VTCP_PORTBUFSIZE);
1878 137105
                hp->rem_path = NULL;
1879 137105
        } else {
1880 13043
                strcpy(hp->rem_ip, "0.0.0.0");
1881 13043
                strcpy(hp->rem_port, "0");
1882 13043
                hp->rem_path = strdup(addr);
1883
        }
1884
        /* XXX: After an upgrade to HTTP/2 the cleanup of a server that is
1885
         * not -wait'ed before the test resets is subject to a race where the
1886
         * cleanup does not happen, so ASAN reports leaks despite the push
1887
         * of a cleanup handler. To easily reproduce, remove the server wait
1888
         * from a02022.vtc and run with ASAN enabled.
1889
         */
1890 150148
        pthread_cleanup_push(http_process_cleanup, hp);
1891 150148
        parse_string(vl, hp, spec);
1892 150148
        retval = hp->sess->fd;
1893 150148
        pthread_cleanup_pop(0);
1894 150148
        http_process_cleanup(hp);
1895 150148
        return (retval);
1896
}
1897
1898
/**********************************************************************
1899
 * Magic test routine
1900
 *
1901
 * This function brute-forces some short strings through gzip(9) to
1902
 * find candidates for all possible 8 bit positions of the stopbit.
1903
 *
1904
 * Here is some good short output strings:
1905
 *
1906
 *      0 184 <e04c8d0fd604c>
1907
 *      1 257 <1ea86e6cf31bf4ec3d7a86>
1908
 *      2 106 <10>
1909
 *      3 163 <a5e2e2e1c2e2>
1910
 *      4 180 <71c5d18ec5d5d1>
1911
 *      5 189 <39886d28a6d2988>
1912
 *      6 118 <80000>
1913
 *      7 151 <386811868>
1914
 *
1915
 */
1916
1917
#if 0
1918
void xxx(void);
1919
1920
void
1921
xxx(void)
1922
{
1923
        z_stream vz;
1924
        int n;
1925
        char ibuf[200];
1926
        char obuf[200];
1927
        int fl[8];
1928
        int i, j;
1929
1930
        for (n = 0; n < 8; n++)
1931
                fl[n] = 9999;
1932
1933
        memset(&vz, 0, sizeof vz);
1934
1935
        for (n = 0;  n < 999999999; n++) {
1936
                *ibuf = 0;
1937
                for (j = 0; j < 7; j++) {
1938
                        snprintf(strchr(ibuf, 0), 5, "%x",
1939
                            (unsigned)VRND_RandomTestable() & 0xffff);
1940
                        vz.next_in = TRUST_ME(ibuf);
1941
                        vz.avail_in = strlen(ibuf);
1942
                        vz.next_out = TRUST_ME(obuf);
1943
                        vz.avail_out = sizeof obuf;
1944
                        assert(Z_OK == deflateInit2(&vz,
1945
                            9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
1946
                        assert(Z_STREAM_END == deflate(&vz, Z_FINISH));
1947
                        i = vz.stop_bit & 7;
1948
                        if (fl[i] > strlen(ibuf)) {
1949
                                printf("%d %jd <%s>\n", i, vz.stop_bit, ibuf);
1950
                                fl[i] = strlen(ibuf);
1951
                        }
1952
                        assert(Z_OK == deflateEnd(&vz));
1953
                }
1954
        }
1955
1956
        printf("FOO\n");
1957
}
1958
#endif