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 9576
synth_body(const char *len, int rnd)
178
{
179
        int i, j, k, l;
180
        char *b;
181
182
183 9576
        AN(len);
184 9576
        i = strtoul(len, NULL, 0);
185 9576
        assert(i > 0);
186 9576
        b = malloc(i + 1L);
187 9576
        AN(b);
188 9576
        l = k = '!';
189 649264734
        for (j = 0; j < i; j++) {
190 649255158
                if ((j % 64) == 63) {
191 10138770
                        b[j] = '\n';
192 10138770
                        k++;
193 10138770
                        if (k == '~')
194 108090
                                k = '!';
195 10138770
                        l = k;
196 649255158
                } else if (rnd) {
197 74304
                        b[j] = (VRND_RandomTestable() % 95) + ' ';
198 74304
                } else {
199 639042084
                        b[j] = (char)l;
200 639042084
                        if (++l == '~')
201 6844734
                                l = '!';
202
                }
203 649255158
        }
204 9576
        b[i - 1] = '\n';
205 9576
        b[i] = '\0';
206 9576
        return (b);
207
}
208
209
/**********************************************************************
210
 * Finish and write the vsb to the fd
211
 */
212
213
static void
214 100319
http_write(const struct http *hp, int lvl, const char *pfx)
215
{
216
217 100319
        AZ(VSB_finish(hp->vsb));
218 100319
        vtc_dump(hp->vl, lvl, pfx, VSB_data(hp->vsb), VSB_len(hp->vsb));
219 100319
        if (VSB_tofile(hp->vsb, hp->sess->fd))
220 252
                vtc_log(hp->vl, hp->fatal, "Write failed: %s",
221 126
                    strerror(errno));
222 100319
}
223
224
/**********************************************************************
225
 * find header
226
 */
227
228
static char *
229 253156
http_find_header(char * const *hh, const char *hdr)
230
{
231
        int n, l;
232
        char *r;
233
234 253156
        l = strlen(hdr);
235
236 1847832
        for (n = 3; hh[n] != NULL; n++) {
237 1704886
                if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
238 1594676
                        continue;
239 220600
                for (r = hh[n] + l + 1; vct_issp(*r); r++)
240 110390
                        continue;
241 110210
                return (r);
242
        }
243 142946
        return (NULL);
244 253156
}
245
246
/**********************************************************************
247
 * count header
248
 */
249
250
static int
251 99437
http_count_header(char * const *hh, const char *hdr)
252
{
253 99437
        int n, l, r = 0;
254
255 99437
        l = strlen(hdr);
256
257 794628
        for (n = 3; hh[n] != NULL; n++) {
258 695191
                if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
259 645832
                        continue;
260 49359
                r++;
261 49359
        }
262 99437
        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 240451
cmd_var_resolve(struct http *hp, char *spec)
304
{
305
        char **hh, *hdr;
306 240451
        if (!strcmp(spec, "remote.ip"))
307 72
                return (hp->rem_ip);
308 240379
        if (!strcmp(spec, "remote.port"))
309 72
                return (hp->rem_port);
310 240307
        if (!strcmp(spec, "remote.path"))
311 72
                return (hp->rem_path);
312 240235
        if (!strcmp(spec, "req.method"))
313 882
                return (hp->req[0]);
314 239353
        if (!strcmp(spec, "req.url"))
315 11214
                return (hp->req[1]);
316 228139
        if (!strcmp(spec, "req.proto"))
317 810
                return (hp->req[2]);
318 227329
        if (!strcmp(spec, "resp.proto"))
319 612
                return (hp->resp[0]);
320 226717
        if (!strcmp(spec, "resp.status"))
321 28908
                return (hp->resp[1]);
322 197809
        if (!strcmp(spec, "resp.reason"))
323 4086
                return (hp->resp[2]);
324 193723
        if (!strcmp(spec, "resp.chunklen"))
325 0
                return (hp->chunklen);
326 193723
        if (!strcmp(spec, "req.bodylen"))
327 522
                return (hp->bodylen);
328 193201
        if (!strcmp(spec, "req.body"))
329 360
                return (hp->body != NULL ? hp->body : spec);
330 192841
        if (!strcmp(spec, "resp.bodylen"))
331 11700
                return (hp->bodylen);
332 181141
        if (!strcmp(spec, "resp.body"))
333 3779
                return (hp->body != NULL ? hp->body : spec);
334 177362
        if (!strncmp(spec, "req.http.", 9)) {
335 6480
                hh = hp->req;
336 6480
                hdr = spec + 9;
337 177362
        } else if (!strncmp(spec, "resp.http.", 10)) {
338 53980
                hh = hp->resp;
339 53980
                hdr = spec + 10;
340 170882
        } else if (!strcmp(spec, "h2.state")) {
341 135
                if (hp->h2)
342 63
                        return ("true");
343
                else
344 72
                        return ("false");
345
        } else
346 116767
                return (spec);
347 60460
        hdr = http_find_header(hh, hdr);
348 60460
        return (hdr);
349 240451
}
350
351
static void
352 120227
cmd_http_expect(CMD_ARGS)
353
{
354
        struct http *hp;
355
        const char *lhs;
356
        char *cmp;
357
        const char *rhs;
358
359 120227
        (void)vl;
360 120227
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
361 120227
        AZ(strcmp(av[0], "expect"));
362 120227
        av++;
363
364 120227
        AN(av[0]);
365 120227
        AN(av[1]);
366 120227
        AN(av[2]);
367 120227
        AZ(av[3]);
368 120227
        lhs = cmd_var_resolve(hp, av[0]);
369 120227
        cmp = av[1];
370 120227
        rhs = cmd_var_resolve(hp, av[2]);
371
372 120227
        vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
373 120227
}
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 72
cmd_http_expect_pattern(CMD_ARGS)
384
{
385
        char *p;
386
        struct http *hp;
387 72
        char t = '0';
388
389 72
        (void)vl;
390 72
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
391 72
        AZ(strcmp(av[0], "expect_pattern"));
392 72
        av++;
393 72
        AZ(av[0]);
394 4718664
        for (p = hp->body; *p != '\0'; p++) {
395 4718592
                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 4718592
                t += 1;
400 4718592
                t &= ~0x08;
401 4718592
        }
402 72
        vtc_log(hp->vl, 4, "EXPECT PATTERN SUCCESS");
403 72
}
404
405
/**********************************************************************
406
 * Split a HTTP protocol header
407
 */
408
409
static void
410 99434
http_splitheader(struct http *hp, int req)
411
{
412
        char *p, *q, **hh;
413
        int n;
414
        char buf[20];
415
416 99434
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
417 99434
        if (req) {
418 41636
                memset(hp->req, 0, sizeof hp->req);
419 41636
                hh = hp->req;
420 41636
        } else {
421 57798
                memset(hp->resp, 0, sizeof hp->resp);
422 57798
                hh = hp->resp;
423
        }
424
425 99434
        n = 0;
426 99434
        p = hp->rx_b;
427 99434
        if (*p == '\0') {
428 1440
                vtc_log(hp->vl, 4, "No headers");
429 1440
                return;
430
        }
431
432
        /* REQ/PROTO */
433 98012
        while (vct_islws(*p))
434 18
                p++;
435 97994
        hh[n++] = p;
436 677128
        while (!vct_islws(*p))
437 579134
                p++;
438 97994
        AZ(vct_iscrlf(p, hp->rx_e));
439 97994
        *p++ = '\0';
440
441
        /* URL/STATUS */
442 97994
        while (vct_issp(*p))            /* XXX: H space only */
443 0
                p++;
444 97994
        AZ(vct_iscrlf(p, hp->rx_e));
445 97994
        hh[n++] = p;
446 480957
        while (!vct_islws(*p))
447 382963
                p++;
448 97994
        if (vct_iscrlf(p, hp->rx_e)) {
449 18
                hh[n++] = NULL;
450 18
                q = p;
451 18
                p = vct_skipcrlf(p, hp->rx_e);
452 18
                *q = '\0';
453 18
        } else {
454 97976
                *p++ = '\0';
455
                /* PROTO/MSG */
456 97976
                while (vct_issp(*p))            /* XXX: H space only */
457 0
                        p++;
458 97976
                hh[n++] = p;
459 763625
                while (!vct_iscrlf(p, hp->rx_e))
460 665649
                        p++;
461 97976
                q = p;
462 97976
                p = vct_skipcrlf(p, hp->rx_e);
463 97976
                *q = '\0';
464
        }
465 97994
        assert(n == 3);
466
467 793121
        while (*p != '\0') {
468 793157
                assert(n < MAX_HDR);
469 793157
                if (vct_iscrlf(p, hp->rx_e))
470 97992
                        break;
471 695165
                hh[n++] = p++;
472 15220541
                while (*p != '\0' && !vct_iscrlf(p, hp->rx_e))
473 14525376
                        p++;
474 695127
                if (*p == '\0') {
475 0
                        break;
476
                }
477 695127
                q = p;
478 695127
                p = vct_skipcrlf(p, hp->rx_e);
479 695127
                *q = '\0';
480
        }
481 98036
        p = vct_skipcrlf(p, hp->rx_e);
482 98036
        assert(*p == '\0');
483
484 1087229
        for (n = 0; n < 3 || hh[n] != NULL; n++) {
485 989193
                bprintf(buf, "http[%2d] ", n);
486 989193
                vtc_dump(hp->vl, 4, buf, hh[n], -1);
487 989193
        }
488 99476
}
489
490
491
/**********************************************************************
492
 * Receive another character
493
 */
494
495
static int
496 22587262
http_rxchar(struct http *hp, int n, int eof)
497
{
498
        int i;
499
        struct pollfd pfd[1];
500
501 45196723
        while (n > 0) {
502 22611333
                pfd[0].fd = hp->sess->fd;
503 22611333
                pfd[0].events = POLLIN;
504 22611333
                pfd[0].revents = 0;
505 22611333
                i = poll(pfd, 1, (int)(hp->timeout * 1000));
506 22611333
                if (i < 0 && errno == EINTR)
507 0
                        continue;
508 22611333
                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 22611333
                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 22611333
                assert(i > 0);
521 22611333
                assert(hp->rx_p + n < hp->rx_e);
522 22611333
                i = read(hp->sess->fd, hp->rx_p, n);
523 22611333
                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 22611333
                if (i == 0 && eof)
528 1530
                        return (i);
529 22609803
                if (i == 0) {
530 684
                        vtc_log(hp->vl, hp->fatal,
531
                            "HTTP rx EOF (fd:%d read: %s) %d",
532 342
                            hp->sess->fd, strerror(errno), n);
533 342
                        return (-1);
534
                }
535 22609461
                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 22609461
                hp->rx_p += i;
542 22609461
                *hp->rx_p = '\0';
543 22609461
                n -= i;
544
        }
545 22585390
        return (1);
546 22587262
}
547
548
static int
549 24227
http_rxchunk(struct http *hp)
550
{
551
        char *q, *old;
552
        int i;
553
554 24227
        old = hp->rx_p;
555 24227
        do {
556 122291
                if (http_rxchar(hp, 1, 0) < 0)
557 184
                        return (-1);
558 122107
        } while (hp->rx_p[-1] != '\n');
559 24043
        vtc_dump(hp->vl, 4, "len", old, -1);
560 24043
        i = strtoul(old, &q, 16);
561 24043
        bprintf(hp->chunklen, "%d", i);
562 24043
        if ((q == old) || (q == hp->rx_p) || (*q != '\0' && !vct_islws(*q))) {
563 4
                vtc_log(hp->vl, hp->fatal, "Chunklen fail (%02x @ %td)",
564 2
                    (*q & 0xff), q - old);
565 2
                return (-1);
566
        }
567 24043
        assert(*q == '\0' || vct_islws(*q));
568 24041
        hp->rx_p = old;
569 24041
        if (i > 0) {
570 19365
                if (http_rxchar(hp, i, 0) < 0)
571 32
                        return (-1);
572 19333
                vtc_dump(hp->vl, 4, "chunk", old, i);
573 19333
        }
574 24009
        old = hp->rx_p;
575 24009
        if (http_rxchar(hp, 2, 0) < 0)
576 0
                return (-1);
577 24009
        if (!vct_iscrlf(old, hp->rx_e)) {
578 0
                vtc_log(hp->vl, hp->fatal, "Chunklen without CRLF");
579 0
                return (-1);
580
        }
581 24009
        hp->rx_p = old;
582 24009
        *hp->rx_p = '\0';
583 24009
        return (i);
584 24225
}
585
586
/**********************************************************************
587
 * Swallow a HTTP message body
588
 *
589
 * max: 0 is all
590
 */
591
592
static void
593 96322
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 96322
        l = hp->rx_p - hp->body;
599
600 96322
        p = http_find_header(hh, "transfer-encoding");
601 96322
        q = http_find_header(hh, "content-length");
602 96322
        if (p != NULL && !strcasecmp(p, "chunked")) {
603 4947
                if (q != NULL) {
604 0
                        vtc_log(hp->vl, hp->fatal, "Both C-E: Chunked and C-L");
605 0
                        return;
606
                }
607 4947
                ll = 0;
608 23991
                while (http_rxchunk(hp) > 0) {
609 19152
                        ll = (hp->rx_p - hp->body) - l;
610 19152
                        if (max && ll >= max)
611 108
                                break;
612
                }
613 4947
                p = "chunked";
614 96322
        } else if (q != NULL) {
615 48441
                ll = strtoul(q, NULL, 10);
616 48441
                if (max && ll > l + max)
617 36
                        ll = max;
618
                else
619 48405
                        ll -= l;
620 48441
                i = http_rxchar(hp, ll, 0);
621 48441
                if (i < 0)
622 126
                        return;
623 48315
                p = "c-l";
624 91249
        } else if (body) {
625 126
                ll = 0;
626 126
                do  {
627 3541050
                        i = http_rxchar(hp, 1, 1);
628 3541050
                        if (i < 0)
629 0
                                return;
630 3541050
                        ll += i;
631 3541050
                        if (max && ll >= max)
632 36
                                break;
633 3541014
                } while (i > 0);
634 126
                p = "eof";
635 126
        } else {
636 42808
                p = "none";
637 42808
                ll = l = 0;
638
        }
639 96196
        vtc_dump(hp->vl, 4, p, hp->body + l, ll);
640 96196
        l += ll;
641 96196
        hp->bodyl = l;
642 96196
        bprintf(hp->bodylen, "%d", l);
643 96322
}
644
645
/**********************************************************************
646
 * Receive a HTTP protocol header
647
 */
648
649
static void
650 99437
http_rxhdr(struct http *hp)
651
{
652 99437
        int i, s = 0;
653
        char *p;
654
        ssize_t l;
655
656 99437
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
657 99437
        hp->rx_p = hp->rx_b;
658 99437
        *hp->rx_p = '\0';
659 99437
        hp->body = NULL;
660 99437
        bprintf(hp->bodylen, "%s", "<undef>");
661 18828409
        while (1) {
662 18828409
                p = hp->rx_p;
663 18828409
                i = http_rxchar(hp, 1, 1);
664 18828409
                if (i < 1)
665 1440
                        break;
666 18826969
                if (s == 0 && *p == '\r')
667 793139
                        s = 1;
668 18033830
                else if ((s == 0 || s == 1) && *p == '\n')
669 793212
                        s = 2;
670 17240618
                else if (s == 2 && *p == '\r')
671 97979
                        s = 3;
672 17142639
                else if ((s == 2 || s == 3) && *p == '\n')
673 97997
                        break;
674
                else
675 17044642
                        s = 0;
676
        }
677 99437
        l = hp->rx_p - hp->rx_b;
678 99437
        vtc_dump(hp->vl, 4, "rxhdr", hp->rx_b, l);
679 99437
        vtc_log(hp->vl, 4, "rxhdrlen = %zd", l);
680 99437
        if (i < 1)
681 1440
                vtc_log(hp->vl, hp->fatal, "HTTP header is incomplete");
682 99437
        *hp->rx_p = '\0';
683 99437
        hp->body = hp->rx_p;
684 99437
}
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 56967
cmd_http_rxresp(CMD_ARGS)
695
{
696
        struct http *hp;
697 56967
        int has_obj = 1;
698
699 56967
        (void)vl;
700 56967
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
701 56967
        ONLY_CLIENT(hp, av);
702 56967
        AZ(strcmp(av[0], "rxresp"));
703 56967
        av++;
704
705 57435
        for (; *av != NULL; av++)
706 936
                if (!strcmp(*av, "-no_obj"))
707 468
                        has_obj = 0;
708
                else
709 0
                        vtc_fatal(hp->vl,
710 0
                            "Unknown http rxresp spec: %s\n", *av);
711 56967
        http_rxhdr(hp);
712 56967
        http_splitheader(hp, 0);
713 56967
        if (http_count_header(hp->resp, "Content-Length") > 1)
714 0
                vtc_fatal(hp->vl,
715
                    "Multiple Content-Length headers.\n");
716 56967
        if (!has_obj)
717 468
                return;
718 56499
        if (!hp->resp[0] || !hp->resp[1])
719 1134
                return;
720 55367
        if (hp->head_method)
721 1260
                return;
722 54107
        if (!strcmp(hp->resp[1], "200"))
723 40679
                http_swallow_body(hp, hp->resp, 1, 0);
724
        else
725 13428
                http_swallow_body(hp, hp->resp, 0, 0);
726 54107
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
727 56969
}
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 828
cmd_http_rxresphdrs(CMD_ARGS)
737
{
738
        struct http *hp;
739
740 828
        (void)vl;
741 828
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
742 828
        ONLY_CLIENT(hp, av);
743 828
        AZ(strcmp(av[0], "rxresphdrs"));
744 828
        av++;
745
746 828
        for (; *av != NULL; av++)
747 0
                vtc_fatal(hp->vl, "Unknown http rxresp spec: %s\n", *av);
748 828
        http_rxhdr(hp);
749 828
        http_splitheader(hp, 0);
750 828
        if (http_count_header(hp->resp, "Content-Length") > 1)
751 0
                vtc_fatal(hp->vl,
752
                    "Multiple Content-Length headers.\n");
753 828
}
754
755
/* SECTION: client-server.spec.gunzip
756
 *
757
 * gunzip
758
 *         Gunzip the body in place.
759
 */
760
static void
761 666
cmd_http_gunzip(CMD_ARGS)
762
{
763
        struct http *hp;
764
765 666
        (void)av;
766 666
        (void)vl;
767
768 666
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
769 666
        vtc_gunzip(hp, hp->body, &hp->bodyl);
770 666
}
771
772
/**********************************************************************
773
 * Handle common arguments of a transmitted request or response
774
 */
775
776
static char* const *
777 95951
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 95951
        long bodylen = 0;
781
        char *b, *c;
782
        char *nullbody;
783
        ssize_t len;
784 95951
        int nolen = 0;
785
        int l;
786
787 95951
        nullbody = body;
788
789 136075
        for (; *av != NULL; av++) {
790 60644
                if (!strcmp(*av, "-nolen")) {
791 2394
                        nolen = 1;
792 60644
                } else if (!strcmp(*av, "-nohost")) {
793 36
                        nohost = 1;
794 58250
                } else if (!strcmp(*av, "-nodate")) {
795 144
                        nodate = 1;
796 58214
                } else if (!strcmp(*av, "-hdr")) {
797 37478
                        if (!strncasecmp(av[1], "Content-Length:", 15) ||
798 36326
                            !strncasecmp(av[1], "Transfer-Encoding:", 18))
799 2664
                                nolen = 1;
800 37478
                        if (!strncasecmp(av[1], "Host:", 5))
801 648
                                nohost = 1;
802 37478
                        if (!strncasecmp(av[1], "Date:", 5))
803 144
                                nodate = 1;
804 37478
                        if (!strncasecmp(av[1], "Server:", 7))
805 270
                                noserver = 1;
806 37478
                        if (!strncasecmp(av[1], "User-Agent:", 11))
807 36
                                nouseragent = 1;
808 37478
                        VSB_printf(hp->vsb, "%s%s", av[1], nl);
809 37478
                        av++;
810 58070
                } else if (!strcmp(*av, "-hdrlen")) {
811 72
                        VSB_printf(hp->vsb, "%s: ", av[1]);
812 72
                        l = atoi(av[2]);
813 13572
                        while (l-- > 0)
814 13500
                                VSB_putc(hp->vsb, '0' + (l % 10));
815 72
                        VSB_printf(hp->vsb, "%s", nl);
816 72
                        av+=2;
817 72
                } else
818 20520
                        break;
819 40124
        }
820 116759
        for (; *av != NULL; av++) {
821 20808
                if (!strcmp(*av, "-body")) {
822 13572
                        assert(body == nullbody);
823 13572
                        REPLACE(body, av[1]);
824
825 13572
                        AN(body);
826 13572
                        av++;
827 13572
                        bodylen = strlen(body);
828 417884
                        for (b = body; *b != '\0'; b++) {
829 404312
                                if (*b == '\\' && b[1] == '0') {
830 72
                                        *b = '\0';
831 738
                                        for (c = b+1; *c != '\0'; c++) {
832 666
                                                *c = c[1];
833 666
                                        }
834 72
                                        b++;
835 72
                                        bodylen--;
836 72
                                }
837 404312
                        }
838 20808
                } else if (!strcmp(*av, "-bodyfrom")) {
839 36
                        assert(body == nullbody);
840 36
                        free(body);
841 36
                        body = VFIL_readfile(NULL, av[1], &len);
842 36
                        AN(body);
843 36
                        assert(len < INT_MAX);
844 36
                        bodylen = len;
845 36
                        av++;
846 7236
                } else if (!strcmp(*av, "-bodylen")) {
847 3978
                        assert(body == nullbody);
848 3978
                        free(body);
849 3978
                        body = synth_body(av[1], 0);
850 3978
                        bodylen = strlen(body);
851 3978
                        av++;
852 7200
                } else if (!strncmp(*av, "-gzip", 5)) {
853 3222
                        l = vtc_gzip_cmd(hp, av, &body, &bodylen);
854 3222
                        if (l == 0)
855 0
                                break;
856 3222
                        av += l;
857 3222
                        if (l > 1)
858 2934
                                VSB_printf(hp->vsb, "Content-Encoding: gzip%s", nl);
859 3222
                } else
860 0
                        break;
861 20808
        }
862 95951
        if (!nohost) {
863 55671
                VSB_cat(hp->vsb, "Host: ");
864 55671
                macro_cat(vl, hp->vsb, "localhost", NULL);
865 55671
                VSB_cat(hp->vsb, nl);
866 55671
        }
867 95951
        if (!nodate) {
868 38549
                VSB_cat(hp->vsb, "Date: ");
869 38549
                macro_cat(vl, hp->vsb, "date", NULL);
870 38549
                VSB_cat(hp->vsb, nl);
871 38549
        }
872 95951
        if (!noserver)
873 38316
                VSB_printf(hp->vsb, "Server: %s%s", hp->sess->name, nl);
874 95951
        if (!nouseragent)
875 57004
                VSB_printf(hp->vsb, "User-Agent: %s%s", hp->sess->name, nl);
876 95951
        if (body != NULL && !nolen)
877 37650
                VSB_printf(hp->vsb, "Content-Length: %ld%s", bodylen, nl);
878 95951
        VSB_cat(hp->vsb, nl);
879 95951
        if (body != NULL) {
880 39990
                VSB_bcat(hp->vsb, body, bodylen);
881 39990
                free(body);
882 39990
        }
883 95951
        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 pretty 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 38838
cmd_http_txresp(CMD_ARGS)
977
{
978
        struct http *hp;
979 38838
        const char *proto = "HTTP/1.1";
980 38838
        const char *status = "200";
981 38838
        const char *reason = "OK";
982 38838
        char* body = NULL;
983 38838
        unsigned noserver = 0;
984
985 38838
        (void)vl;
986 38838
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
987 38838
        ONLY_SERVER(hp, av);
988 38838
        AZ(strcmp(av[0], "txresp"));
989 38838
        av++;
990
991 38838
        VSB_clear(hp->vsb);
992
993 43158
        for (; *av != NULL; av++) {
994 29915
                if (!strcmp(*av, "-proto")) {
995 738
                        proto = av[1];
996 738
                        av++;
997 29915
                } else if (!strcmp(*av, "-status")) {
998 2826
                        status = av[1];
999 2826
                        av++;
1000 29177
                } else if (!strcmp(*av, "-reason")) {
1001 504
                        reason = av[1];
1002 504
                        av++;
1003 504
                        continue;
1004 25847
                } else if (!strcmp(*av, "-noserver")) {
1005 252
                        noserver = 1;
1006 252
                        continue;
1007
                } else
1008 25595
                        break;
1009 3564
        }
1010
1011 38838
        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 38838
        REPLACE(body, "");
1015
1016 38838
        av = http_tx_parse_args(av, vl, hp, body, 1, 0, noserver, 1);
1017 38838
        if (*av != NULL)
1018 0
                vtc_fatal(hp->vl, "Unknown http txresp spec: %s\n", *av);
1019
1020 38838
        http_write(hp, 4, "txresp");
1021 38838
}
1022
1023
static void
1024 18
cmd_http_upgrade(CMD_ARGS)
1025
{
1026
        char *h;
1027
        struct http *hp;
1028
1029 18
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1030 18
        ONLY_SERVER(hp, av);
1031 18
        AN(hp->sfd);
1032
1033 18
        h = http_find_header(hp->req, "Upgrade");
1034 18
        if (!h || strcmp(h, "h2c"))
1035 0
                vtc_fatal(vl, "Req misses \"Upgrade: h2c\" header");
1036
1037 18
        h = http_find_header(hp->req, "Connection");
1038 18
        if (!h || strcmp(h, "Upgrade, HTTP2-Settings"))
1039 0
                vtc_fatal(vl, "Req misses \"Connection: "
1040
                        "Upgrade, HTTP2-Settings\" header");
1041
1042 18
        h = http_find_header(hp->req, "HTTP2-Settings");
1043 18
        if (!h)
1044 0
                vtc_fatal(vl, "Req misses \"HTTP2-Settings\" header");
1045
1046 18
        parse_string(vl, hp,
1047
            "txresp -status 101"
1048
            " -hdr \"Connection: Upgrade\""
1049
            " -hdr \"Upgrade: h2c\"\n"
1050
        );
1051
1052 18
        b64_settings(hp, h);
1053
1054 18
        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 18
}
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 41872
cmd_http_rxreq(CMD_ARGS)
1077
{
1078
        struct http *hp;
1079
1080 41872
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1081 41872
        ONLY_SERVER(hp, av);
1082 41872
        AZ(strcmp(av[0], "rxreq"));
1083 41872
        av++;
1084
1085 41872
        for (; *av != NULL; av++)
1086 0
                vtc_fatal(vl, "Unknown http rxreq spec: %s\n", *av);
1087 41872
        http_rxhdr(hp);
1088 41872
        http_splitheader(hp, 1);
1089 41872
        if (http_count_header(hp->req, "Content-Length") > 1)
1090 0
                vtc_fatal(vl, "Multiple Content-Length headers.\n");
1091 41872
        http_swallow_body(hp, hp->req, 0, 0);
1092 41872
        vtc_log(vl, 4, "bodylen = %s", hp->bodylen);
1093 41872
}
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 90
cmd_http_rxreqhdrs(CMD_ARGS)
1103
{
1104
        struct http *hp;
1105
1106 90
        (void)vl;
1107 90
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1108 90
        AZ(strcmp(av[0], "rxreqhdrs"));
1109 90
        av++;
1110
1111 90
        for (; *av != NULL; av++)
1112 0
                vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av);
1113 90
        http_rxhdr(hp);
1114 90
        http_splitheader(hp, 1);
1115 90
        if (http_count_header(hp->req, "Content-Length") > 1)
1116 0
                vtc_fatal(hp->vl, "Multiple Content-Length headers.\n");
1117 90
}
1118
1119
/* SECTION: client-server.spec.rxreqbody
1120
 *
1121
 * rxreqbody (server only)
1122
 *         Receive a request's body.
1123
 */
1124
1125
static void
1126 72
cmd_http_rxreqbody(CMD_ARGS)
1127
{
1128
        struct http *hp;
1129
1130 72
        (void)vl;
1131 72
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1132 72
        ONLY_SERVER(hp, av);
1133 72
        AZ(strcmp(av[0], "rxreqbody"));
1134 72
        av++;
1135
1136 72
        for (; *av != NULL; av++)
1137 0
                vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av);
1138 72
        http_swallow_body(hp, hp->req, 0, 0);
1139 72
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
1140 72
}
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 594
cmd_http_rxrespbody(CMD_ARGS)
1152
{
1153
        struct http *hp;
1154 594
        int max = 0;
1155
1156 594
        (void)vl;
1157 594
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1158 594
        ONLY_CLIENT(hp, av);
1159 594
        AZ(strcmp(av[0], "rxrespbody"));
1160 594
        av++;
1161
1162 774
        for (; *av != NULL; av++)
1163 360
                if (!strcmp(*av, "-max")) {
1164 180
                        max = atoi(av[1]);
1165 180
                        av++;
1166 180
                } else
1167 0
                        vtc_fatal(hp->vl,
1168 0
                            "Unknown http rxrespbody spec: %s\n", *av);
1169
1170 594
        http_swallow_body(hp, hp->resp, 1, max);
1171 594
        vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
1172 594
}
1173
1174
/* SECTION: client-server.spec.rxchunk
1175
 *
1176
 * rxchunk
1177
 *         Receive an HTTP chunk.
1178
 */
1179
1180
static void
1181 234
cmd_http_rxchunk(CMD_ARGS)
1182
{
1183
        struct http *hp;
1184
        int ll, i;
1185
1186 234
        (void)vl;
1187 234
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1188 234
        ONLY_CLIENT(hp, av);
1189
1190 234
        i = http_rxchunk(hp);
1191 234
        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 234
}
1198
1199
/**********************************************************************
1200
 * Transmit a request
1201
 */
1202
1203
static void
1204 57107
cmd_http_txreq(CMD_ARGS)
1205
{
1206
        struct http *hp;
1207 57107
        const char *req = "GET";
1208 57107
        const char *url = "/";
1209 57107
        const char *proto = "HTTP/1.1";
1210 57107
        const char *up = NULL;
1211
        unsigned nohost;
1212 57107
        unsigned nouseragent = 0;
1213
1214 57107
        (void)vl;
1215 57107
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1216 57107
        ONLY_CLIENT(hp, av);
1217 57107
        AZ(strcmp(av[0], "txreq"));
1218 57107
        av++;
1219
1220 57107
        VSB_clear(hp->vsb);
1221
1222 57107
        hp->head_method = 0;
1223 92855
        for (; *av != NULL; av++) {
1224 52110
                if (!strcmp(*av, "-url")) {
1225 29970
                        url = av[1];
1226 29970
                        av++;
1227 52110
                } else if (!strcmp(*av, "-proto")) {
1228 900
                        proto = av[1];
1229 900
                        av++;
1230 22140
                } else if (!strcmp(*av, "-method") ||
1231 19062
                    !strcmp(*av, "-req")) {
1232 4806
                        req = av[1];
1233 4806
                        hp->head_method = !strcmp(av[1], "HEAD") ;
1234 4806
                        av++;
1235 21240
                } else if (!hp->sfd && !strcmp(*av, "-up")) {
1236 0
                        up = av[1];
1237 0
                        av++;
1238 16434
                } else if (!strcmp(*av, "-nouseragent")) {
1239 72
                        nouseragent = 1;
1240 72
                } else
1241 16362
                        break;
1242 35748
        }
1243 57107
        VSB_printf(hp->vsb, "%s %s %s%s", req, url, proto, nl);
1244
1245 57107
        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 57107
        nohost = strcmp(proto, "HTTP/1.1") != 0;
1251 57107
        av = http_tx_parse_args(av, vl, hp, NULL, nohost, 1, 1, nouseragent);
1252 57107
        if (*av != NULL)
1253 0
                vtc_fatal(hp->vl, "Unknown http txreq spec: %s\n", *av);
1254 57107
        http_write(hp, 4, "txreq");
1255
1256 57107
        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 57107
}
1276
1277
/* SECTION: client-server.spec.recv
1278
 *
1279
 * recv NUMBER
1280
 *         Read NUMBER bytes from the connection.
1281
 */
1282
1283
static void
1284 162
cmd_http_recv(CMD_ARGS)
1285
{
1286
        struct http *hp;
1287
        int i, n;
1288
        char u[32];
1289
1290 162
        (void)vl;
1291 162
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1292 162
        AN(av[1]);
1293 162
        AZ(av[2]);
1294 162
        n = strtoul(av[1], NULL, 0);
1295 522
        while (n > 0) {
1296 360
                i = read(hp->sess->fd, u, n > 32 ? 32 : n);
1297 360
                if (i > 0)
1298 360
                        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 360
                n -= i;
1303
        }
1304 162
}
1305
1306
/* SECTION: client-server.spec.send
1307
 *
1308
 * send STRING
1309
 *         Push STRING on the connection.
1310
 */
1311
1312
static void
1313 15743
cmd_http_send(CMD_ARGS)
1314
{
1315
        struct http *hp;
1316
        int i;
1317
1318 15743
        (void)vl;
1319 15743
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1320 15743
        AN(av[1]);
1321 15743
        AZ(av[2]);
1322 15743
        vtc_dump(hp->vl, 4, "send", av[1], -1);
1323 15743
        i = write(hp->sess->fd, av[1], strlen(av[1]));
1324 15743
        if (i != strlen(av[1]))
1325 2
                vtc_log(hp->vl, hp->fatal, "Write error in http_send(): %s",
1326 1
                    strerror(errno));
1327 15743
}
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 36
cmd_http_send_n(CMD_ARGS)
1337
{
1338
        struct http *hp;
1339
        int i, n, l;
1340
1341 36
        (void)vl;
1342 36
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1343 36
        AN(av[1]);
1344 36
        AN(av[2]);
1345 36
        AZ(av[3]);
1346 36
        n = strtoul(av[1], NULL, 0);
1347 36
                vtc_dump(hp->vl, 4, "send_n", av[2], -1);
1348 36
        l = strlen(av[2]);
1349 37080
        while (n--) {
1350 37044
                i = write(hp->sess->fd, av[2], l);
1351 37044
                if (i != l)
1352 1680
                        vtc_log(hp->vl, hp->fatal,
1353
                            "Write error in http_send(): %s",
1354 840
                            strerror(errno));
1355
        }
1356 36
}
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 180
cmd_http_send_urgent(CMD_ARGS)
1366
{
1367
        struct http *hp;
1368
        int i;
1369
1370 180
        (void)vl;
1371 180
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1372 180
        AN(av[1]);
1373 180
        AZ(av[2]);
1374 180
        vtc_dump(hp->vl, 4, "send_urgent", av[1], -1);
1375 180
        i = send(hp->sess->fd, av[1], strlen(av[1]), MSG_OOB);
1376 180
        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 180
}
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 2898
cmd_http_sendhex(CMD_ARGS)
1391
{
1392
        struct vsb *vsb;
1393
        struct http *hp;
1394
1395 2898
        (void)vl;
1396 2898
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1397 2898
        AN(av[1]);
1398 2898
        AZ(av[2]);
1399 2898
        vsb = vtc_hex_to_bin(hp->vl, av[1]);
1400 2898
        assert(VSB_len(vsb) >= 0);
1401 2898
        vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
1402 2898
        if (VSB_tofile(vsb, hp->sess->fd))
1403 0
                vtc_log(hp->vl, hp->fatal, "Write failed: %s",
1404 0
                    strerror(errno));
1405 2898
        VSB_destroy(&vsb);
1406 2898
}
1407
1408
/* SECTION: client-server.spec.chunked
1409
 *
1410
 * chunked STRING
1411
 *         Send STRING as chunked encoding.
1412
 */
1413
1414
static void
1415 1080
cmd_http_chunked(CMD_ARGS)
1416
{
1417
        struct http *hp;
1418
1419 1080
        (void)vl;
1420 1080
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1421 1080
        AN(av[1]);
1422 1080
        AZ(av[2]);
1423 1080
        VSB_clear(hp->vsb);
1424 2160
        VSB_printf(hp->vsb, "%jx%s%s%s",
1425 1080
            (uintmax_t)strlen(av[1]), nl, av[1], nl);
1426 1080
        http_write(hp, 4, "chunked");
1427 1080
}
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 3293
cmd_http_chunkedlen(CMD_ARGS)
1438
{
1439
        unsigned len;
1440
        unsigned u, v;
1441
        char buf[16384];
1442
        struct http *hp;
1443
1444 3293
        (void)vl;
1445 3293
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1446 3293
        AN(av[1]);
1447 3293
        AZ(av[2]);
1448 3293
        VSB_clear(hp->vsb);
1449
1450 3293
        len = atoi(av[1]);
1451
1452 3293
        if (len == 0) {
1453 1224
                VSB_printf(hp->vsb, "0%s%s", nl, nl);
1454 1224
        } else {
1455 30314525
                for (u = 0; u < sizeof buf; u++)
1456 30312456
                        buf[u] = (u & 7) + '0';
1457
1458 2069
                VSB_printf(hp->vsb, "%x%s", len, nl);
1459 7271
                for (u = 0; u < len; u += v) {
1460 5202
                        v = vmin_t(unsigned, len - u, sizeof buf);
1461 5202
                        VSB_bcat(hp->vsb, buf, v);
1462 5202
                }
1463 2069
                VSB_printf(hp->vsb, "%s", nl);
1464
        }
1465 3293
        http_write(hp, 4, "chunked");
1466 3293
}
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 594
cmd_http_timeout(CMD_ARGS)
1477
{
1478
        struct http *hp;
1479
        double d;
1480
1481 594
        (void)vl;
1482 594
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1483 594
        AN(av[1]);
1484 594
        AZ(av[2]);
1485 594
        d = VNUM(av[1]);
1486 594
        if (isnan(d))
1487 0
                vtc_fatal(vl, "timeout is not a number (%s)", av[1]);
1488 594
        hp->timeout = d;
1489 594
}
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 2957
cmd_http_expect_close(CMD_ARGS)
1498
{
1499
        struct http *hp;
1500
        struct pollfd fds[1];
1501
        char c;
1502
        int i;
1503
1504 2957
        (void)vl;
1505 2957
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1506 2957
        AZ(av[1]);
1507
1508 2957
        vtc_log(vl, 4, "Expecting close (fd = %d)", hp->sess->fd);
1509 2957
        if (hp->h2)
1510 252
                stop_h2(hp);
1511 2957
        while (1) {
1512 2957
                fds[0].fd = hp->sess->fd;
1513 2957
                fds[0].events = POLLIN;
1514 2957
                fds[0].revents = 0;
1515 2957
                i = poll(fds, 1, (int)(hp->timeout * 1000));
1516 2957
                if (i < 0 && errno == EINTR)
1517 0
                        continue;
1518 2957
                if (i == 0)
1519 0
                        vtc_log(vl, hp->fatal, "Expected close: timeout");
1520 2957
                if (i != 1 || !(fds[0].revents & (POLLIN|POLLERR|POLLHUP)))
1521 172
                        vtc_log(vl, hp->fatal,
1522
                            "Expected close: poll = %d, revents = 0x%x",
1523 86
                            i, fds[0].revents);
1524 2871
                i = read(hp->sess->fd, &c, 1);
1525 2871
                if (i <= 0 && VTCP_Check(i))
1526 2871
                        break;
1527 86
                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 2871
        vtc_log(vl, 4, "fd=%d EOF, as expected", hp->sess->fd);
1533 2871
}
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 342
cmd_http_close(CMD_ARGS)
1543
{
1544
        struct http *hp;
1545
1546 342
        (void)vl;
1547 342
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1548 342
        ONLY_SERVER(hp, av);
1549 342
        AZ(av[1]);
1550 342
        assert(hp->sfd != NULL);
1551 342
        assert(*hp->sfd >= 0);
1552 342
        if (hp->h2)
1553 0
                stop_h2(hp);
1554 342
        VTCP_close(&hp->sess->fd);
1555 342
        vtc_log(vl, 4, "Closed");
1556 342
}
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 2919
cmd_http_accept(CMD_ARGS)
1566
{
1567
        struct http *hp;
1568
1569 2919
        (void)vl;
1570 2919
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1571 2919
        ONLY_SERVER(hp, av);
1572 2919
        AZ(av[1]);
1573 2919
        assert(hp->sfd != NULL);
1574 2919
        assert(*hp->sfd >= 0);
1575 2919
        if (hp->h2)
1576 0
                stop_h2(hp);
1577 2919
        if (hp->sess->fd >= 0)
1578 2595
                VTCP_close(&hp->sess->fd);
1579 2919
        vtc_log(vl, 4, "Accepting");
1580 2919
        hp->sess->fd = accept(*hp->sfd, NULL, NULL);
1581 2919
        if (hp->sess->fd < 0)
1582 0
                vtc_log(vl, hp->fatal, "Accepted failed: %s", strerror(errno));
1583 2919
        vtc_log(vl, 3, "Accepted socket fd is %d", hp->sess->fd);
1584 2919
}
1585
1586
/* SECTION: client-server.spec.shutdown
1587
 *
1588
 * shutdown
1589
 *      Initiate shutdown.
1590
 *
1591
 *      \-read
1592
 *              Shutdown the read direction.
1593
 *      \-write
1594
 *              Shutdown the write direction.
1595
 *
1596
 *      The default is both direction.
1597
 */
1598
static void
1599 108
cmd_http_shutdown(CMD_ARGS)
1600
{
1601
        struct http *hp;
1602 108
        int how = SHUT_RDWR;
1603 108
        const char *str[] = {
1604
                [SHUT_RD]       = "RD",
1605
                [SHUT_WR]       = "WR",
1606
                [SHUT_RDWR]     = "RDWR",
1607
        };
1608
1609 108
        (void)vl;
1610 108
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1611 108
        AZ(strcmp(av[0], "shutdown"));
1612 108
        av++;
1613
1614 108
        if (*av != NULL) {
1615 72
                if (!strcmp(*av, "-read")) {
1616 36
                        how = SHUT_RD;
1617 36
                        av++;
1618 72
                } else if (!strcmp(*av, "-write")) {
1619 36
                        how = SHUT_WR;
1620 36
                        av++;
1621 36
                }
1622 72
        }
1623
1624 108
        if (*av != NULL)
1625 0
                vtc_fatal(hp->vl, "Unknown http shutdown spec: %s\n", *av);
1626
1627 108
        vtc_log(vl, 4, "Shutting down fd (%s): %d", str[how], hp->sess->fd);
1628 108
        if (shutdown(hp->sess->fd, how) < 0)
1629 0
                vtc_log(vl, hp->fatal, "Shutdown failed: %s", strerror(errno));
1630 108
        vtc_log(vl, 3, "Shutdown socket fd (%d): %d", how, hp->sess->fd);
1631 108
}
1632
1633
/* SECTION: client-server.spec.fatal
1634
 *
1635
 * fatal|non_fatal
1636
 *         Control whether a failure of this entity should stop the test.
1637
 */
1638
1639
static void
1640 7296
cmd_http_fatal(CMD_ARGS)
1641
{
1642
        struct http *hp;
1643 7296
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1644
1645 7296
        (void)vl;
1646 7296
        AZ(av[1]);
1647 7296
        if (!strcmp(av[0], "fatal")) {
1648 288
                hp->fatal = 0;
1649 288
        } else {
1650 7008
                assert(!strcmp(av[0], "non_fatal"));
1651 7008
                hp->fatal = -1;
1652
        }
1653 7296
}
1654
1655
#define cmd_http_non_fatal cmd_http_fatal
1656
1657
static const char PREFACE[24] = {
1658
        0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54,
1659
        0x54, 0x50, 0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a,
1660
        0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a, 0x0d, 0x0a
1661
};
1662
1663
/* SECTION: client-server.spec.txpri
1664
 *
1665
 * txpri (client only)
1666
 *      Send an HTTP/2 preface ("PRI * HTTP/2.0\\r\\n\\r\\nSM\\r\\n\\r\\n")
1667
 *      and set client to HTTP/2.
1668
 */
1669
static void
1670 3042
cmd_http_txpri(CMD_ARGS)
1671
{
1672
        size_t l;
1673
        struct http *hp;
1674
1675 3042
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1676 3042
        ONLY_CLIENT(hp, av);
1677
1678 3042
        vtc_dump(hp->vl, 4, "txpri", PREFACE, sizeof(PREFACE));
1679
        /* Dribble out the preface */
1680 3042
        l = write(hp->sess->fd, PREFACE, 18);
1681 3042
        if (l != 18)
1682 0
                vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
1683 0
                    l, sizeof(PREFACE), strerror(errno));
1684 3042
        usleep(10000);
1685 3042
        l = write(hp->sess->fd, PREFACE + 18, sizeof(PREFACE) - 18);
1686 3042
        if (l != sizeof(PREFACE) - 18)
1687 0
                vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
1688 0
                    l, sizeof(PREFACE), strerror(errno));
1689
1690 3042
        start_h2(hp);
1691 3042
        AN(hp->h2);
1692 3042
}
1693
1694
/* SECTION: client-server.spec.rxpri
1695
 *
1696
 * rxpri (server only)
1697
 *      Receive a preface. If valid set the server to HTTP/2, abort
1698
 *      otherwise.
1699
 */
1700
static void
1701 522
cmd_http_rxpri(CMD_ARGS)
1702
{
1703
        struct http *hp;
1704
1705 522
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1706 522
        ONLY_SERVER(hp, av);
1707
1708 522
        hp->rx_p = hp->rx_b;
1709 522
        if (!http_rxchar(hp, sizeof(PREFACE), 0))
1710 0
                vtc_fatal(vl, "Couldn't retrieve connection preface");
1711 522
        if (memcmp(hp->rx_b, PREFACE, sizeof(PREFACE)))
1712 0
                vtc_fatal(vl, "Received invalid preface\n");
1713 522
        start_h2(hp);
1714 522
        AN(hp->h2);
1715 522
}
1716
1717
/* SECTION: client-server.spec.settings
1718
 *
1719
 * settings -dectbl INT
1720
 *      Force internal HTTP/2 settings to certain values. Currently only
1721
 *      support setting the decoding table size.
1722
 */
1723
static void
1724 0
cmd_http_settings(CMD_ARGS)
1725
{
1726
        uint32_t n;
1727
        char *p;
1728
        struct http *hp;
1729 0
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1730
1731 0
        if (!hp->h2)
1732 0
                vtc_fatal(hp->vl, "Only possible in H/2 mode");
1733
1734 0
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1735
1736 0
        for (; *av != NULL; av++) {
1737 0
                if (!strcmp(*av, "-dectbl")) {
1738 0
                        n = strtoul(av[1], &p, 0);
1739 0
                        if (*p != '\0')
1740 0
                                vtc_fatal(hp->vl, "-dectbl takes an integer as "
1741 0
                                    "argument (found %s)", av[1]);
1742 0
                        assert(HPK_ResizeTbl(hp->decctx, n) != hpk_err);
1743 0
                        av++;
1744 0
                } else
1745 0
                        vtc_fatal(vl, "Unknown settings spec: %s\n", *av);
1746 0
        }
1747 0
}
1748
1749
static void
1750 10061
cmd_http_stream(CMD_ARGS)
1751
{
1752
        struct http *hp;
1753 10061
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1754 10061
        if (!hp->h2) {
1755 3186
                vtc_log(hp->vl, 4, "Not in H/2 mode, do what's needed");
1756 3186
                if (hp->sfd)
1757 468
                        parse_string(vl, hp, "rxpri");
1758
                else
1759 2718
                        parse_string(vl, hp, "txpri");
1760 3186
                parse_string(vl, hp,
1761
                    "stream 0 {\n"
1762
                    "    txsettings\n"
1763
                    "    rxsettings\n"
1764
                    "    txsettings -ack\n"
1765
                    "    rxsettings\n"
1766
                    "    expect settings.ack == true"
1767
                    "} -run\n"
1768
                );
1769 3186
        }
1770 10061
        cmd_stream(av, hp, vl);
1771 10061
}
1772
1773
/* SECTION: client-server.spec.write_body
1774
 *
1775
 * write_body STRING
1776
 *      Write the body of a request or a response to a file. By using the
1777
 *      shell command, higher-level checks on the body can be performed
1778
 *      (eg. XML, JSON, ...) provided that such checks can be delegated
1779
 *      to an external program.
1780
 */
1781
static void
1782 36
cmd_http_write_body(CMD_ARGS)
1783
{
1784
        struct http *hp;
1785
1786 36
        (void)vl;
1787 36
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
1788 36
        AN(av[0]);
1789 36
        AN(av[1]);
1790 36
        AZ(av[2]);
1791 36
        AZ(strcmp(av[0], "write_body"));
1792 36
        if (VFIL_writefile(NULL, av[1], hp->body, hp->bodyl) != 0)
1793 0
                vtc_fatal(hp->vl, "failed to write body: %s (%d)",
1794 0
                    strerror(errno), errno);
1795 36
}
1796
1797
/**********************************************************************
1798
 * Execute HTTP specifications
1799
 */
1800
1801
const struct cmds http_cmds[] = {
1802
#define CMD_HTTP(n) { #n, cmd_http_##n },
1803
        /* session */
1804
        CMD_HTTP(accept)
1805
        CMD_HTTP(close)
1806
        CMD_HTTP(recv)
1807
        CMD_HTTP(send)
1808
        CMD_HTTP(send_n)
1809
        CMD_HTTP(send_urgent)
1810
        CMD_HTTP(sendhex)
1811
        CMD_HTTP(shutdown)
1812
        CMD_HTTP(timeout)
1813
1814
        /* spec */
1815
        CMD_HTTP(fatal)
1816
        CMD_HTTP(non_fatal)
1817
1818
        /* body */
1819
        CMD_HTTP(gunzip)
1820
        CMD_HTTP(write_body)
1821
1822
        /* HTTP/1.x */
1823
        CMD_HTTP(chunked)
1824
        CMD_HTTP(chunkedlen)
1825
        CMD_HTTP(rxchunk)
1826
1827
        /* HTTP/2 */
1828
        CMD_HTTP(stream)
1829
        CMD_HTTP(settings)
1830
1831
        /* client */
1832
        CMD_HTTP(rxresp)
1833
        CMD_HTTP(rxrespbody)
1834
        CMD_HTTP(rxresphdrs)
1835
        CMD_HTTP(txpri)
1836
        CMD_HTTP(txreq)
1837
1838
        /* server */
1839
        CMD_HTTP(rxpri)
1840
        CMD_HTTP(rxreq)
1841
        CMD_HTTP(rxreqbody)
1842
        CMD_HTTP(rxreqhdrs)
1843
        CMD_HTTP(txresp)
1844
        CMD_HTTP(upgrade)
1845
1846
        /* expect */
1847
        CMD_HTTP(expect)
1848
        CMD_HTTP(expect_close)
1849
        CMD_HTTP(expect_pattern)
1850
#undef CMD_HTTP
1851
        { NULL, NULL }
1852
};
1853
1854
static void
1855 69905
http_process_cleanup(void *arg)
1856
{
1857
        struct http *hp;
1858
1859 69905
        CAST_OBJ_NOTNULL(hp, arg, HTTP_MAGIC);
1860
1861 69905
        if (hp->h2)
1862 3312
                stop_h2(hp);
1863 69905
        VSB_destroy(&hp->vsb);
1864 69905
        free(hp->rx_b);
1865 69905
        free(hp->rem_ip);
1866 69905
        free(hp->rem_port);
1867 69905
        free(hp->rem_path);
1868 69905
        FREE_OBJ(hp);
1869 69905
}
1870
1871
int
1872 69912
http_process(struct vtclog *vl, struct vtc_sess *vsp, const char *spec,
1873
    int sock, int *sfd, const char *addr, int rcvbuf)
1874
{
1875
        struct http *hp;
1876
        int retval, oldbuf;
1877 69912
        socklen_t intlen = sizeof(int);
1878
1879 69912
        (void)sfd;
1880 69912
        ALLOC_OBJ(hp, HTTP_MAGIC);
1881 69912
        AN(hp);
1882 69912
        hp->sess = vsp;
1883 69912
        hp->sess->fd = sock;
1884 69912
        hp->timeout = vtc_maxdur * .5;
1885
1886 69912
        if (rcvbuf) {
1887
                // XXX setsockopt() too late on SunOS
1888
                // https://github.com/varnishcache/varnish-cache/pull/2980#issuecomment-486214661
1889 162
                hp->rcvbuf = rcvbuf;
1890
1891 162
                oldbuf = 0;
1892 162
                AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &oldbuf, &intlen));
1893 162
                AZ(setsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, intlen));
1894 162
                AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &intlen));
1895
1896 324
                vtc_log(vl, 3, "-rcvbuf fd=%d old=%d new=%d actual=%d",
1897 162
                    hp->sess->fd, oldbuf, hp->rcvbuf, rcvbuf);
1898 162
        }
1899
1900 69912
        hp->nrxbuf = 2048*1024;
1901 69912
        hp->rx_b = malloc(hp->nrxbuf);
1902 69912
        AN(hp->rx_b);
1903 69912
        hp->rx_e = hp->rx_b + hp->nrxbuf;
1904 69912
        hp->rx_p = hp->rx_b;
1905 69912
        *hp->rx_p = '\0';
1906
1907 69912
        hp->vsb = VSB_new_auto();
1908 69912
        AN(hp->vsb);
1909
1910 69912
        hp->sfd = sfd;
1911
1912 69912
        hp->rem_ip = malloc(VTCP_ADDRBUFSIZE);
1913 69912
        AN(hp->rem_ip);
1914
1915 69912
        hp->rem_port = malloc(VTCP_PORTBUFSIZE);
1916 69912
        AN(hp->rem_port);
1917
1918 69912
        hp->vl = vl;
1919 69912
        vtc_log_set_cmd(hp->vl, http_cmds);
1920 69912
        hp->gziplevel = 0;
1921 69912
        hp->gzipresidual = -1;
1922
1923 69912
        if (*addr != '/') {
1924 64045
                VTCP_hisname(sock, hp->rem_ip, VTCP_ADDRBUFSIZE, hp->rem_port,
1925
                             VTCP_PORTBUFSIZE);
1926 64045
                hp->rem_path = NULL;
1927 64045
        } else {
1928 5867
                strcpy(hp->rem_ip, "0.0.0.0");
1929 5867
                strcpy(hp->rem_port, "0");
1930 5867
                hp->rem_path = strdup(addr);
1931
        }
1932
        /* XXX: After an upgrade to HTTP/2 the cleanup of a server that is
1933
         * not -wait'ed before the test resets is subject to a race where the
1934
         * cleanup does not happen, so ASAN reports leaks despite the push
1935
         * of a cleanup handler. To easily reproduce, remove the server wait
1936
         * from a02022.vtc and run with ASAN enabled.
1937
         */
1938 69912
        pthread_cleanup_push(http_process_cleanup, hp);
1939 69912
        parse_string(vl, hp, spec);
1940 69912
        retval = hp->sess->fd;
1941 69912
        pthread_cleanup_pop(0);
1942 69912
        http_process_cleanup(hp);
1943 69912
        return (retval);
1944
}
1945
1946
/**********************************************************************
1947
 * Magic test routine
1948
 *
1949
 * This function brute-forces some short strings through gzip(9) to
1950
 * find candidates for all possible 8 bit positions of the stopbit.
1951
 *
1952
 * Here is some good short output strings:
1953
 *
1954
 *      0 184 <e04c8d0fd604c>
1955
 *      1 257 <1ea86e6cf31bf4ec3d7a86>
1956
 *      2 106 <10>
1957
 *      3 163 <a5e2e2e1c2e2>
1958
 *      4 180 <71c5d18ec5d5d1>
1959
 *      5 189 <39886d28a6d2988>
1960
 *      6 118 <80000>
1961
 *      7 151 <386811868>
1962
 *
1963
 */
1964
1965
#if 0
1966
void xxx(void);
1967
1968
void
1969
xxx(void)
1970
{
1971
        z_stream vz;
1972
        int n;
1973
        char ibuf[200];
1974
        char obuf[200];
1975
        int fl[8];
1976
        int i, j;
1977
1978
        for (n = 0; n < 8; n++)
1979
                fl[n] = 9999;
1980
1981
        memset(&vz, 0, sizeof vz);
1982
1983
        for (n = 0;  n < 999999999; n++) {
1984
                *ibuf = 0;
1985
                for (j = 0; j < 7; j++) {
1986
                        snprintf(strchr(ibuf, 0), 5, "%x",
1987
                            (unsigned)VRND_RandomTestable() & 0xffff);
1988
                        vz.next_in = TRUST_ME(ibuf);
1989
                        vz.avail_in = strlen(ibuf);
1990
                        vz.next_out = TRUST_ME(obuf);
1991
                        vz.avail_out = sizeof obuf;
1992
                        assert(Z_OK == deflateInit2(&vz,
1993
                            9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
1994
                        assert(Z_STREAM_END == deflate(&vz, Z_FINISH));
1995
                        i = vz.stop_bit & 7;
1996
                        if (fl[i] > strlen(ibuf)) {
1997
                                printf("%d %jd <%s>\n", i, vz.stop_bit, ibuf);
1998
                                fl[i] = strlen(ibuf);
1999
                        }
2000
                        assert(Z_OK == deflateEnd(&vz));
2001
                }
2002
        }
2003
2004
        printf("FOO\n");
2005
}
2006
#endif