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