Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_http2.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2016 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Guillaume Quintard <guillaume.quintard@gmail.com>
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
#include <netinet/in.h>
41
42
#include "vtc.h"
43
#include "vtc_http.h"
44
45
#include "vfil.h"
46
#include "hpack.h"
47
#include "vend.h"
48
49 106
#define ERR_MAX         13
50 2604
#define BUF_SIZE        (1024*2048)
51
52
static const char *const h2_errs[] = {
53
#define H2_ERROR(n,v,sc,g,r,t) [v] = #n,
54
#include <tbl/h2_error.h>
55
        NULL
56
};
57
58
static const char *const h2_types[] = {
59
#define H2_FRAME(l,u,t,f,...) [t] = #u,
60
#include <tbl/h2_frames.h>
61
        NULL
62
};
63
64
static const char * const h2_settings[] = {
65
        [0] = "unknown",
66
#define H2_SETTING(U,l,v,...) [v] = #U,
67
#include <tbl/h2_settings.h>
68
        NULL
69
};
70
71
enum h2_settings_e {
72
#define H2_SETTING(U,l,v,...) SETTINGS_##U = v,
73
#include <tbl/h2_settings.h>
74
        SETTINGS_MAX
75
};
76
77
78
enum h2_type_e {
79
#define H2_FRAME(l,u,t,f,...) TYPE_##u = t,
80
#include <tbl/h2_frames.h>
81
        TYPE_MAX
82
};
83
84
//lint -save -e849      Same enum value
85
enum {
86
        ACK = 0x1,
87
        END_STREAM = 0x1,
88
        PADDED = 0x8,
89
        END_HEADERS = 0x4,
90
        PRIORITY = 0x20,
91
};
92
//lint -restore
93
94
struct stream {
95
        unsigned                magic;
96
#define STREAM_MAGIC            0x63f1fac2
97
        uint32_t                id;
98
        struct vtclog           *vl;
99
        char                    *spec;
100
        char                    *name;
101
        VTAILQ_ENTRY(stream)    list;
102
        unsigned                running;
103
        pthread_cond_t          cond;
104
        struct frame            *frame;
105
        pthread_t               tp;
106
        struct http             *hp;
107
        int64_t                 win_self;
108
        int64_t                 win_peer;
109
        int                     wf;
110
111
        VTAILQ_HEAD(, frame)   fq;
112
113
        char                    *body;
114
        long                    bodylen;
115
        struct hpk_hdr          req[MAX_HDR];
116
        struct hpk_hdr          resp[MAX_HDR];
117
118
        int                     dependency;
119
        int                     weight;
120
};
121
122
static void
123
clean_headers(struct hpk_hdr *h)
124 1222
{
125 1222
        unsigned n = MAX_HDR;
126
127 2393
        while (h->t && n > 0) {
128 1171
                if (h->key.len)
129 1169
                        free(h->key.ptr);
130 1171
                free(h->value.ptr);
131 1171
                memset(h, 0, sizeof(*h));
132 1171
                h++;
133 1171
                n--;
134 1171
        }
135 1222
}
136
137
#define ONLY_H2_CLIENT(hp, av)                                          \
138 374
        do {                                                            \
139 374
                if (hp->sfd != NULL)                                    \
140 374
                        vtc_fatal(s->vl,                                \
141 0
                            "\"%s\" only possible in client", av[0]);   \
142 374
        } while (0)
143
144
#define ONLY_H2_SERVER(hp, av)                                          \
145 96
        do {                                                            \
146 96
                if (hp->sfd == NULL)                                    \
147 96
                        vtc_fatal(s->vl,                                \
148 0
                            "\"%s\" only possible in server", av[0]);   \
149 96
        } while (0)
150
151
static void
152
http_write(const struct http *hp, int lvl,
153
    const char *buf, int s, const char *pfx)
154 57
{
155 57
        ssize_t l;
156
157 57
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
158 57
        AN(buf);
159 57
        AN(pfx);
160
161 57
        vtc_dump(hp->vl, lvl, pfx, buf, s);
162 57
        l = write(*hp->sess->fd, buf, s);
163 57
        if (l != s)
164 0
                vtc_log(hp->vl, hp->fatal, "Write failed: (%zd vs %d) %s",
165 0
                    l, s, strerror(errno));
166 57
}
167
168
static int
169
get_bytes(const struct http *hp, char *buf, size_t n)
170 1743
{
171 1743
        int i;
172 1743
        struct pollfd pfd[1];
173
174 1743
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
175 1743
        AN(buf);
176
177 3486
        while (n > 0) {
178 1743
                pfd[0].fd = *hp->sess->fd;
179 1743
                pfd[0].events = POLLIN;
180 1743
                pfd[0].revents = 0;
181 1743
                i = poll(pfd, 1, (int)(hp->timeout * 1000));
182 1743
                if (i < 0 && errno == EINTR)
183 0
                        continue;
184 1743
                if (i == 0)
185 0
                        vtc_log(hp->vl, 3,
186 0
                            "HTTP2 rx timeout (fd:%d %.3fs)",
187 0
                            *hp->sess->fd, hp->timeout);
188 1743
                if (i < 0)
189 0
                        vtc_log(hp->vl, 3,
190 0
                            "HTTP2 rx failed (fd:%d poll: %s)",
191 0
                            *hp->sess->fd, strerror(errno));
192 1743
                if (i <= 0)
193 0
                        return (i);
194 1743
                i = read(*hp->sess->fd, buf, n);
195 1743
                if (!(pfd[0].revents & POLLIN))
196 0
                        vtc_log(hp->vl, 4,
197 0
                            "HTTP2 rx poll (fd:%d revents: %x n=%zu, i=%d)",
198 0
                            *hp->sess->fd, pfd[0].revents, n, i);
199 1743
                if (i == 0)
200 0
                        vtc_log(hp->vl, 3,
201 0
                            "HTTP2 rx EOF (fd:%d read: %s)",
202 0
                            *hp->sess->fd, strerror(errno));
203 1743
                if (i < 0)
204 0
                        vtc_log(hp->vl, 3,
205 0
                            "HTTP2 rx failed (fd:%d read: %s)",
206 0
                            *hp->sess->fd, strerror(errno));
207 1743
                if (i <= 0)
208 0
                        return (i);
209 1743
                n -= i;
210 1743
        }
211 1743
        return (1);
212
213 1743
}
214
215
VTAILQ_HEAD(fq_head, frame);
216
217
struct frame {
218
        unsigned        magic;
219 2131
#define FRAME_MAGIC     0x5dd3ec4
220
        uint32_t        size;
221
        uint32_t        stid;
222
        uint8_t         type;
223
        uint8_t         flags;
224
        char            *data;
225
226
        VTAILQ_ENTRY(frame)    list;
227
228
        union {
229
                struct {
230
                        uint32_t stream;
231
                        uint8_t  exclusive;
232
                        uint8_t  weight;
233
                }               prio;
234
                uint32_t        rst_err;
235
                double settings[SETTINGS_MAX+1];
236
                struct {
237
                        char data[9];
238
                        int ack;
239
                }               ping;
240
                struct {
241
                        uint32_t err;
242
                        uint32_t stream;
243
                        char     *debug;
244
                }               goaway;
245
                uint32_t        winup_size;
246
                uint32_t        promised;
247
                uint8_t         padded;
248
        } md;
249
};
250
251
static void
252
readFrameHeader(struct frame *f, const char *buf)
253 1017
{
254 1017
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
255 1017
        AN(buf);
256
257 1017
        f->size  = (unsigned char)buf[0] << 16;
258 1017
        f->size += (unsigned char)buf[1] << 8;
259 1017
        f->size += (unsigned char)buf[2];
260
261 1017
        f->type = (unsigned char)buf[3];
262
263 1017
        f->flags = (unsigned char)buf[4];
264
265 1017
        f->stid  = vbe32dec(buf+5);
266 1017
}
267
268
static void
269
writeFrameHeader(char *buf, const struct frame *f)
270 2131
{
271 2131
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
272 2131
        AN(buf);
273 2131
        buf[0] = (f->size >> 16) & 0xff;
274 2131
        buf[1] = (f->size >>  8) & 0xff;
275 2131
        buf[2] = (f->size      ) & 0xff;
276
277 2131
        buf[3] = f->type;
278
279 2131
        buf[4] = f->flags;
280
281 2131
        vbe32enc(buf + 5, f->stid);
282 2131
}
283
284 2131
#define INIT_FRAME(f, ty, sz, id, fl) \
285 2131
do { \
286 2131
        f.magic = FRAME_MAGIC; \
287 2131
        f.type = TYPE_ ## ty; \
288 2131
        f.size = sz; \
289 2131
        f.stid = id; \
290 2131
        f.flags = fl; \
291 2131
        f.data = NULL; \
292 2131
} while(0)
293
294
static void
295
replace_frame(struct frame **fp, struct frame *new)
296 2251
{
297 2251
        struct frame *old;
298
299 2251
        AN(fp);
300 2251
        CHECK_OBJ_ORNULL(new, FRAME_MAGIC);
301
302 2251
        old = *fp;
303 2251
        *fp = new;
304 2251
        if (old == NULL)
305 1232
                return;
306
307 1019
        CHECK_OBJ(old, FRAME_MAGIC);
308 1019
        if (old->type == TYPE_GOAWAY)
309 41
                free(old->md.goaway.debug);
310 1019
        free(old->data);
311 1019
        FREE_OBJ(old);
312 1019
}
313
314
static void
315
clean_frame(struct frame **fp)
316 1613
{
317
318 1613
        replace_frame(fp, NULL);
319 1613
}
320
321
static void
322
write_frame(struct stream *sp, const struct frame *f, const unsigned lock)
323 2131
{
324 2131
        struct http *hp;
325 2131
        ssize_t l;
326 2131
        char hdr[9];
327
328 2131
        CHECK_OBJ_NOTNULL(sp, STREAM_MAGIC);
329 2131
        hp = sp->hp;
330 2131
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
331 2131
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
332
333 2131
        writeFrameHeader(hdr, f);
334
335 2131
        vtc_log(sp->vl, 3,
336 2131
            "tx: stream: %d, type: %s (%d), flags: 0x%02x, size: %d",
337 2131
            f->stid,
338 2131
            f->type < TYPE_MAX ? h2_types[f->type] : "?",
339 2131
            f->type, f->flags, f->size);
340
341 2131
        if (f->type == TYPE_DATA) {
342 337
                sp->win_peer -= f->size;
343 337
                hp->h2_win_peer->size -= f->size;
344 337
        }
345
346 2131
        if (lock)
347 2131
                PTOK(pthread_mutex_lock(&hp->mtx));
348 2131
        l = write(*hp->sess->fd, hdr, sizeof(hdr));
349 2131
        if (l != sizeof(hdr))
350 987
                vtc_log(sp->vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
351 987
                    l, sizeof(hdr), strerror(errno));
352
353 2131
        if (f->size) {
354 1720
                AN(f->data);
355 1720
                l = write(*hp->sess->fd, f->data, f->size);
356 1720
                if (l != f->size)
357 987
                        vtc_log(sp->vl, hp->fatal,
358 987
                                        "Write failed: (%zd vs %d) %s",
359 987
                                        l, f->size, strerror(errno));
360 1720
        }
361 2131
        if (lock)
362 2131
                PTOK(pthread_mutex_unlock(&hp->mtx));
363 2131
}
364
365
static void
366
exclusive_stream_dependency(const struct stream *s)
367 6
{
368 6
        struct stream *target;
369 6
        struct http *hp = s->hp;
370
371 6
        if (s->id == 0)
372 0
                return;
373
374 22
        VTAILQ_FOREACH(target, &hp->streams, list) {
375 22
                if (target->id != s->id && target->dependency == s->dependency)
376 8
                        target->dependency = s->id;
377 22
        }
378 6
}
379
380
static void
381
explain_flags(uint8_t flags, uint8_t type, struct vtclog *vl)
382 1019
{
383 1019
        if (flags & ACK && (type == TYPE_PING || type == TYPE_SETTINGS)) {
384 216
                vtc_log(vl, 3, "flag: ACK");
385 803
        } else if (flags & END_STREAM && (type == TYPE_HEADERS ||
386 196
            type == TYPE_PUSH_PROMISE || type == TYPE_DATA)) {
387 196
                vtc_log(vl, 3, "flag: END_STREAM");
388 607
        } else if (flags & END_HEADERS && (type == TYPE_HEADERS ||
389 61
            type == TYPE_PUSH_PROMISE || type == TYPE_CONTINUATION)) {
390 61
                vtc_log(vl, 3, "flag: END_TYPE_HEADERS");
391 546
        } else if (flags & PRIORITY && (type == TYPE_HEADERS ||
392 0
            type == TYPE_PUSH_PROMISE)) {
393 0
                vtc_log(vl, 3, "flag: END_PRIORITY");
394 546
        } else if (flags & PADDED && (type == TYPE_DATA || type ==
395 1
            TYPE_HEADERS || type == TYPE_PUSH_PROMISE)) {
396 1
                vtc_log(vl, 3, "flag: PADDED");
397 545
        } else if (flags)
398 0
                vtc_log(vl, 3, "UNKNOWN FLAG(S): 0x%02x", flags);
399 1019
}
400
401
static void
402
parse_data(struct stream *s, struct frame *f)
403 199
{
404 199
        struct http *hp;
405 199
        uint32_t size = f->size;
406 199
        char *data = f->data;
407
408 199
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
409 199
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
410 199
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
411
412 199
        if (f->flags & PADDED) {
413 4
                f->md.padded = *((uint8_t *)data);
414 4
                if (f->md.padded >= size) {
415 0
                        vtc_log(s->vl, hp->fatal,
416 0
                                        "invalid padding: %d reported,"
417 0
                                        "but size is only %d",
418 0
                                        f->md.padded, size);
419 0
                        size = 0;
420 0
                        f->md.padded = 0;
421 0
                }
422 4
                data++;
423 4
                size -= f->md.padded + 1;
424 4
                vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
425 4
        }
426
427 199
        if (s->id)
428 197
                s->win_self -= size;
429
430 199
        s->hp->h2_win_self->size -= size;
431
432 199
        if (!size) {
433 29
                AZ(data);
434 29
                vtc_log(s->vl, 4, "s%u - no data", s->id);
435 29
                return;
436 29
        }
437
438 170
        s->body = realloc(s->body, s->bodylen + size + 1L);
439 170
        AN(s->body);
440 170
        vmemcpy(s->body + s->bodylen, data, size);
441 170
        s->bodylen += size;
442 170
        s->body[s->bodylen] = '\0';
443 170
}
444
445
static void
446
decode_hdr(struct http *hp, struct hpk_hdr *h, const struct vsb *vsb)
447 202
{
448 202
        struct hpk_iter *iter;
449 202
        enum hpk_result r = hpk_err;
450 202
        int n;
451
452 202
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
453 202
        CAST_OBJ_NOTNULL(hp, hp, HTTP_MAGIC);;
454
455 202
        if (VSB_len(vsb) == 0)
456 0
                return;
457
458 202
        iter = HPK_NewIter(hp->decctx, VSB_data(vsb), VSB_len(vsb));
459
460 202
        n = 0;
461 202
        while (n < MAX_HDR && h[n].t)
462 0
                n++;
463 1171
        while (n < MAX_HDR) {
464 1171
                r = HPK_DecHdr(iter, h + n);
465 1171
                if (r == hpk_err )
466 2
                        break;
467 1169
                vtc_log(hp->vl, 4, "header[%2d]: %s: %s",
468 1169
                    n, h[n].key.ptr, h[n].value.ptr);
469 1169
                n++;
470 1169
                if (r == hpk_done)
471 200
                        break;
472 1169
        }
473
474 202
        if (r != hpk_done) {
475 2
                vtc_log(hp->vl, hp->fatal ? 4 : 0,
476 2
                    "Header decoding failed (%d) %d", r, hp->fatal);
477 200
        } else if (n == MAX_HDR) {
478 0
                vtc_log(hp->vl, hp->fatal,
479 0
                    "Max number of headers reached (%d)", MAX_HDR);
480 0
        }
481
482 202
        HPK_FreeIter(iter);
483 202
}
484
485
static void
486
parse_hdr(struct stream *s, struct frame *f, struct vsb *vsb)
487 216
{
488 216
        int shift = 0;
489 216
        int exclusive = 0;
490 216
        uint32_t size = f->size;
491 216
        char *data = f->data;
492 216
        struct http *hp;
493 216
        uint32_t n;
494
495 216
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
496 216
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
497 216
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
498 216
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
499
500 216
        if (f->flags & PADDED && f->type != TYPE_CONTINUATION) {
501 2
                f->md.padded = *((uint8_t *)data);
502 2
                if (f->md.padded >= size) {
503 0
                        vtc_log(s->vl, hp->fatal,
504 0
                                        "invalid padding: %d reported,"
505 0
                                        "but size is only %d",
506 0
                                        f->md.padded, size);
507 0
                        size = 0;
508 0
                        f->md.padded = 0;
509 0
                }
510 2
                shift += 1;
511 2
                size -= f->md.padded;
512 2
                vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
513 2
        }
514
515 216
        if (f->type == TYPE_HEADERS && f->flags & PRIORITY){
516 2
                shift += 5;
517 2
                n = vbe32dec(f->data);
518 2
                s->dependency = n & ~(1U << 31);
519 2
                exclusive = n >> 31;
520
521 2
                s->weight = f->data[4];
522 2
                if (exclusive)
523 2
                        exclusive_stream_dependency(s);
524
525 2
                vtc_log(s->vl, 4, "stream->dependency: %u", s->dependency);
526 2
                vtc_log(s->vl, 4, "stream->weight: %u", s->weight);
527 214
        } else if (f->type == TYPE_PUSH_PROMISE){
528 1
                shift += 4;
529 1
                n = vbe32dec(f->data);
530 1
                f->md.promised = n & ~(1U << 31);
531 1
        }
532
533 216
        AZ(VSB_bcat(vsb, data + shift, size - shift));
534 216
}
535
536
static void
537
parse_prio(struct stream *s, struct frame *f)
538 3
{
539 3
        struct http *hp;
540 3
        char *buf;
541 3
        uint32_t n;
542
543 3
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
544 3
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
545 3
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
546
547 3
        if (f->size != 5)
548 0
                vtc_fatal(s->vl, "Size should be 5, but isn't (%d)", f->size);
549
550 3
        buf = f->data;
551 3
        AN(buf);
552
553 3
        n = vbe32dec(f->data);
554 3
        f->md.prio.stream = n & ~(1U << 31);
555
556 3
        s->dependency = f->md.prio.stream;
557 3
        if (n >> 31){
558 1
                f->md.prio.exclusive = 1;
559 1
                exclusive_stream_dependency(s);
560 1
        }
561
562 3
        buf += 4;
563 3
        f->md.prio.weight = *buf;
564 3
        s->weight = f->md.prio.weight;
565
566 3
        vtc_log(s->vl, 3, "prio->stream: %u", f->md.prio.stream);
567 3
        vtc_log(s->vl, 3, "prio->weight: %u", f->md.prio.weight);
568 3
}
569
570
static void
571
parse_rst(const struct stream *s, struct frame *f)
572 65
{
573 65
        struct http *hp;
574 65
        uint32_t err;
575 65
        const char *buf;
576 65
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
577 65
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
578 65
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
579
580 65
        if (f->size != 4)
581 0
                vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
582
583 65
        err = vbe32dec(f->data);
584 65
        f->md.rst_err = err;
585
586 65
        vtc_log(s->vl, 2, "ouch");
587 65
        if (err <= ERR_MAX)
588 65
                buf = h2_errs[err];
589 0
        else
590 0
                buf = "unknown";
591 65
        vtc_log(s->vl, 4, "rst->err: %s (%d)", buf, err);
592
593 65
}
594
595
static void
596
parse_settings(const struct stream *s, struct frame *f)
597 423
{
598 423
        struct http *hp;
599 423
        int v;
600 423
        unsigned u, t;
601 423
        const char *buf;
602 423
        enum hpk_result r;
603 423
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
604 423
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
605 423
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
606
607 423
        if (f->size % 6)
608 0
                vtc_fatal(s->vl,
609 0
                    "Size should be a multiple of 6, but isn't (%d)", f->size);
610
611 423
        if (s->id != 0)
612 0
                vtc_fatal(s->vl,
613 0
                    "Setting frames should only be on stream 0, but received on stream: %d", s->id);
614
615 8037
        for (u = 0; u <= SETTINGS_MAX; u++)
616 7614
                f->md.settings[u] = NAN;
617
618 876
        for (u = 0; u < f->size;) {
619 453
                t = vbe16dec(f->data + u);
620 453
                u += 2;
621 453
                v = vbe32dec(f->data + u);
622 453
                if (t <= SETTINGS_MAX) {
623 453
                        buf = h2_settings[t];
624 453
                        f->md.settings[t] = v;
625 453
                } else
626 0
                        buf = "unknown";
627 453
                u += 4;
628
629 453
                if (t == SETTINGS_HEADER_TABLE_SIZE) {
630 5
                        r = HPK_ResizeTbl(s->hp->encctx, v);
631 5
                        assert(r == hpk_done);
632 5
                }
633
634 453
                vtc_log(s->vl, 4, "settings->%s (%u): %d", buf, t, v);
635 453
        }
636
637 423
}
638
639
static void
640
parse_ping(const struct stream *s, struct frame *f)
641 6
{
642 6
        struct http *hp;
643 6
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
644 6
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
645 6
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
646 6
        if (f->size != 8)
647 0
                vtc_fatal(s->vl, "Size should be 8, but isn't (%d)", f->size);
648 6
        f->md.ping.ack = f->flags & ACK;
649 6
        vmemcpy(f->md.ping.data, f->data, 8);
650 6
        f->md.ping.data[8] = '\0';
651
652 6
        vtc_log(s->vl, 4, "ping->data: %s", f->md.ping.data);
653
654 6
}
655
656
static void
657
parse_goaway(const struct stream *s, struct frame *f)
658 41
{
659 41
        struct http *hp;
660 41
        const char *err_buf;
661 41
        uint32_t err, stid;
662 41
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
663 41
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
664 41
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
665
666 41
        if (f->size < 8)
667 0
                vtc_fatal(s->vl,
668 0
                    "Size should be at least 8, but isn't (%d)", f->size);
669 41
        if (f->data[0] & (1<<7))
670 0
                vtc_fatal(s->vl,
671 0
                    "First bit of data is reserved and should be 0");
672
673 41
        stid = vbe32dec(f->data);
674 41
        err = vbe32dec(f->data + 4);
675 41
        f->md.goaway.err = err;
676 41
        f->md.goaway.stream = stid;
677
678 41
        if (err <= ERR_MAX)
679 41
                err_buf = h2_errs[err];
680 0
        else
681 0
                err_buf = "unknown";
682
683 41
        if (f->size > 8) {
684 2
                f->md.goaway.debug = malloc((f->size - 8) + 1L);
685 2
                AN(f->md.goaway.debug);
686 2
                f->md.goaway.debug[f->size - 8] = '\0';
687
688 2
                vmemcpy(f->md.goaway.debug, f->data + 8, f->size - 8);
689 2
        }
690
691 41
        vtc_log(s->vl, 3, "goaway->laststream: %d", stid);
692 41
        vtc_log(s->vl, 3, "goaway->err: %s (%d)", err_buf, err);
693 41
        if (f->md.goaway.debug)
694 2
                vtc_log(s->vl, 3, "goaway->debug: %s", f->md.goaway.debug);
695 41
}
696
697
static void
698
parse_winup(const struct stream *s, struct frame *f)
699 66
{
700 66
        struct http *hp;
701 66
        uint32_t size;
702 66
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
703 66
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
704 66
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
705
706 66
        if (f->size != 4)
707 0
                vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
708 66
        if (f->data[0] & (1<<7))
709 0
                vtc_log(s->vl, s->hp->fatal,
710 0
                    "First bit of data is reserved and should be 0");
711
712 66
        size = vbe32dec(f->data);
713 66
        f->md.winup_size = size;
714
715 66
        vtc_log(s->vl, 3, "winup->size: %d", size);
716 66
}
717
718
/* read a frame and queue it in the relevant stream, wait if not present yet.
719
 */
720
static void *
721
receive_frame(void *priv)
722 215
{
723 215
        struct http *hp;
724 215
        char hdr[9];
725 215
        struct frame *f;
726 215
        struct stream *s;
727 215
        int expect_cont = 0;
728 215
        struct vsb *vsb = NULL;
729 215
        struct hpk_hdr *hdrs = NULL;
730
731 215
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
732
733 215
        PTOK(pthread_mutex_lock(&hp->mtx));
734 2386
        while (hp->h2) {
735
                /*no wanted frames? */
736 2171
                assert(hp->wf >= 0);
737 2171
                if (hp->wf == 0) {
738 1152
                        PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
739 1152
                        continue;
740 1152
                }
741 1019
                PTOK(pthread_mutex_unlock(&hp->mtx));
742
743 1019
                if (get_bytes(hp, hdr, sizeof hdr) <= 0) {
744 0
                        PTOK(pthread_mutex_lock(&hp->mtx));
745 0
                        VTAILQ_FOREACH(s, &hp->streams, list)
746 0
                                PTOK(pthread_cond_signal(&s->cond));
747 0
                        PTOK(pthread_mutex_unlock(&hp->mtx));
748 0
                        vtc_log(hp->vl, hp->fatal,
749 0
                            "could not get frame header");
750 0
                        return (NULL);
751 0
                }
752 1019
                ALLOC_OBJ(f, FRAME_MAGIC);
753 1019
                AN(f);
754 1019
                readFrameHeader(f, hdr);
755
756 1019
                vtc_log(hp->vl, 3, "rx: stream: %d, type: %s (%d), "
757 1019
                                "flags: 0x%02x, size: %d",
758 1019
                                f->stid,
759 1019
                                f->type < TYPE_MAX ? h2_types[f->type] : "?",
760 1019
                                f->type, f->flags, f->size);
761 1019
                explain_flags(f->flags, f->type, hp->vl);
762
763 1019
                if (f->size) {
764 725
                        f->data = malloc(f->size + 1L);
765 725
                        AN(f->data);
766 725
                        f->data[f->size] = '\0';
767 725
                        if (get_bytes(hp, f->data, f->size) <= 0) {
768 0
                                PTOK(pthread_mutex_lock(&hp->mtx));
769 0
                                VTAILQ_FOREACH(s, &hp->streams, list)
770 0
                                        PTOK(pthread_cond_signal(&s->cond));
771 0
                                clean_frame(&f);
772 0
                                PTOK(pthread_mutex_unlock(&hp->mtx));
773 0
                                vtc_log(hp->vl, hp->fatal,
774 0
                                    "could not get frame body");
775 0
                                return (NULL);
776 0
                        }
777 725
                }
778
779
                /* is the corresponding stream waiting? */
780 1019
                PTOK(pthread_mutex_lock(&hp->mtx));
781 1019
                s = NULL;
782 2038
                while (!s) {
783 1019
                        VTAILQ_FOREACH(s, &hp->streams, list)
784 1166
                                if (s->id == f->stid)
785 1019
                                        break;
786 1019
                        if (!s)
787 1019
                                PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
788 1019
                        if (!hp->h2) {
789 0
                                clean_frame(&f);
790 0
                                PTOK(pthread_mutex_unlock(&hp->mtx));
791 0
                                return (NULL);
792 0
                        }
793 1019
                }
794 1019
                PTOK(pthread_mutex_unlock(&hp->mtx));
795
796 1019
                AN(s);
797 1019
                if (expect_cont &&
798 1019
                    (f->type != TYPE_CONTINUATION || expect_cont != s->id))
799 0
                        vtc_fatal(s->vl, "Expected CONTINUATION frame for "
800 0
                            "stream %u", expect_cont);
801
802
                /* parse the frame according to it type, and fill the metadata */
803 1019
                switch (f->type) {
804 199
                        case TYPE_DATA:
805 199
                                parse_data(s, f);
806 199
                                break;
807 1
                        case TYPE_PUSH_PROMISE:
808 1
                                hdrs = s->req;
809
                                /*FALLTHROUGH*/
810 202
                        case TYPE_HEADERS:
811 202
                                if (!hdrs) {
812 201
                                        if (hp->sfd)
813 47
                                                hdrs = s->req;
814 154
                                        else
815 154
                                                hdrs = s->resp;
816 201
                                }
817 202
                                clean_headers(hdrs);
818 202
                                hdrs[0].t = hpk_unset;
819 202
                                AZ(vsb);
820 202
                                vsb = VSB_new_auto();
821
                                /*FALLTHROUGH*/
822 216
                        case TYPE_CONTINUATION:
823 216
                                AN(hdrs);
824 216
                                expect_cont = s->id;
825 216
                                parse_hdr(s, f, vsb);
826 216
                                if (f->flags & END_HEADERS) {
827 202
                                        expect_cont = 0;
828 202
                                        AZ(VSB_finish(vsb));
829 202
                                        decode_hdr(hp, hdrs, vsb);
830 202
                                        VSB_destroy(&vsb);
831 202
                                        hdrs = NULL;
832 202
                                }
833 216
                                break;
834 216
                        case TYPE_PRIORITY:
835 3
                                parse_prio(s, f);
836 3
                                break;
837 65
                        case TYPE_RST_STREAM:
838 65
                                parse_rst(s, f);
839 65
                                break;
840 423
                        case TYPE_SETTINGS:
841 423
                                parse_settings(s, f);
842 423
                                break;
843 6
                        case TYPE_PING:
844 6
                                parse_ping(s, f);
845 6
                                break;
846 41
                        case TYPE_GOAWAY:
847 41
                                parse_goaway(s, f);
848 41
                                break;
849 66
                        case TYPE_WINDOW_UPDATE:
850 66
                                parse_winup(s, f);
851 66
                                break;
852 0
                        default:
853 0
                                WRONG("wrong frame type");
854 1019
                }
855
856 1019
                PTOK(pthread_mutex_lock(&hp->mtx));
857 1019
                VTAILQ_INSERT_HEAD(&s->fq, f, list);
858 1019
                if (s->wf) {
859 973
                        assert(hp->wf > 0);
860 973
                        hp->wf--;
861 973
                        s->wf = 0;
862 973
                        PTOK(pthread_cond_signal(&s->cond));
863 973
                }
864 1019
                continue;
865 1019
        }
866 215
        PTOK(pthread_mutex_unlock(&hp->mtx));
867 215
        if (vsb != NULL)
868 0
                VSB_destroy(&vsb);
869 215
        return (NULL);
870 215
}
871
872
#define STRTOU32(n, ss, p, v, c)                                        \
873 654
        do {                                                            \
874 654
                n = strtoul(ss, &p, 0);                                 \
875 654
                if (*p != '\0')                                         \
876 654
                        vtc_fatal(v, "%s takes an integer as argument " \
877 0
                                "(found %s)", c, ss);                   \
878 654
        } while (0)
879
880 130
#define STRTOU32_CHECK(n, sp, p, v, c, l)                               \
881 130
do {                                                                    \
882 130
        sp++;                                                           \
883 130
        AN(*sp);                                                        \
884 130
        STRTOU32(n, *sp, p, v, c);                                      \
885 130
        if (l && n >= (1U << l))                                        \
886 130
                vtc_fatal(v,                                            \
887 0
                    c " must be a %d-bits integer (found %s)", l, *sp); \
888 130
} while (0)
889
890
#define CHECK_LAST_FRAME(TYPE) \
891 362
        if (!f || f->type != TYPE_ ## TYPE) {                              \
892 0
                vtc_fatal(s->vl, "Last frame was not of type " #TYPE); \
893 0
        }
894
895 17
#define RETURN_SETTING(U) \
896 17
do { \
897 17
        if (isnan(f->md.settings[SETTINGS_##U])) { \
898 9
                return (NULL); \
899 9
        } \
900 17
        snprintf(buf, 20, "%.0f", f->md.settings[SETTINGS_##U]); \
901 8
        return (buf); \
902 17
} while (0)
903
904 7
#define RETURN_SETTING_BOOL(U)                                  \
905 7
do {                                                            \
906 7
        if (isnan(f->md.settings[SETTINGS_##U])) {              \
907 4
                return (NULL);                                  \
908 4
        }                                                       \
909 7
        else if (f->md.settings[SETTINGS_##U] == 1)             \
910 3
                snprintf(buf, 20, "true");                      \
911 3
        else                                                    \
912 3
                snprintf(buf, 20, "false");                     \
913 7
        return (buf);                                           \
914 7
} while (0)
915
916 170
#define RETURN_BUFFED(val) \
917 170
do { \
918 170
        snprintf(buf, 20, "%ld", (long)val); \
919 170
        return (buf); \
920 170
} while (0)
921
922
static char *
923
find_header(const struct hpk_hdr *h, const char *k)
924 201
{
925 201
        AN(k);
926
927 201
        int kl = vstrlen(k);
928 1065
        while (h->t) {
929 1055
                if (kl == h->key.len  && !strncasecmp(h->key.ptr, k, kl))
930 191
                        return (h->value.ptr);
931 864
                h++;
932 864
        }
933 10
        return (NULL);
934 201
}
935
/* SECTION: stream.spec.zexpect expect
936
 *
937
 * expect in stream works as it does in client or server, except that the
938
 * elements compared will be different.
939
 *
940
 * Most of these elements will be frame specific, meaning that the last frame
941
 * received on that stream must of the correct type.
942
 *
943
 * Here the list of keywords you can look at.
944
 */
945
static const char *
946
cmd_var_resolve(const struct stream *s, const char *spec, char *buf)
947 1564
{
948 1564
        uint32_t idx;
949 1564
        int n;
950 1564
        const struct hpk_hdr *h;
951 1564
        struct hpk_ctx *ctx;
952 1564
        struct frame *f = s->frame;
953
954 1564
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
955 1564
        CHECK_OBJ_NOTNULL(s->hp, HTTP_MAGIC);
956 1564
        AN(spec);
957 1564
        AN(buf);
958
959 1564
        n = 0;
960
        /* SECTION: stream.spec.zexpect.ping PING specific
961
         *
962
         * ping.data
963
         *      The 8-bytes string of the PING frame payload.
964
         * ping.ack (PING)
965
         *      "true" if the ACK flag was set, "false" otherwise.
966
         */
967 1564
        if (!vstrcmp(spec, "ping.data")) {
968 6
                CHECK_LAST_FRAME(PING);
969 6
                return (f->md.ping.data);
970 6
        }
971 1558
        if (!vstrcmp(spec, "ping.ack")) {
972 4
                CHECK_LAST_FRAME(PING);
973 4
                snprintf(buf, 20, (f->flags & ACK) ? "true" : "false");
974 4
                return (buf);
975 4
        }
976
        /* SECTION: stream.spec.zexpect.winup WINDOW_UPDATE specific
977
         *
978
         * winup.size
979
         *      The size of the upgrade given by the WINDOW_UPDATE frame.
980
         */
981 1554
        if (!vstrcmp(spec, "winup.size")) {
982 1
                CHECK_LAST_FRAME(WINDOW_UPDATE);
983 1
                RETURN_BUFFED(f->md.winup_size);
984 1
        }
985
        /* SECTION: stream.spec.zexpect.prio PRIORITY specific
986
         *
987
         * prio.stream
988
         *      The stream ID announced.
989
         *
990
         * prio.exclusive
991
         *      "true" if the priority is exclusive, else "false".
992
         *
993
         * prio.weight
994
         *      The dependency weight.
995
         */
996 1553
        if (!vstrcmp(spec, "prio.stream")) {
997 2
                CHECK_LAST_FRAME(PRIORITY);
998 2
                RETURN_BUFFED(f->md.prio.stream);
999 2
        }
1000 1551
        if (!vstrcmp(spec, "prio.exclusive")) {
1001 0
                CHECK_LAST_FRAME(PRIORITY);
1002 0
                snprintf(buf, 20, f->md.prio.exclusive ? "true" : "false");
1003 0
                return (buf);
1004 0
        }
1005 1551
        if (!vstrcmp(spec, "prio.weight")) {
1006 2
                CHECK_LAST_FRAME(PRIORITY);
1007 2
                RETURN_BUFFED(f->md.prio.weight);
1008 2
        }
1009
        /* SECTION: stream.spec.zexpect.rst RESET_STREAM specific
1010
         *
1011
         * rst.err
1012
         *      The error code (as integer) of the RESET_STREAM frame.
1013
         */
1014 1549
        if (!vstrcmp(spec, "rst.err")) {
1015 44
                CHECK_LAST_FRAME(RST_STREAM);
1016 44
                RETURN_BUFFED(f->md.rst_err);
1017 44
        }
1018
        /* SECTION: stream.spec.zexpect.settings SETTINGS specific
1019
         *
1020
         * settings.ack
1021
         *      "true" if the ACK flag was set, else "false".
1022
         *
1023
         * settings.push
1024
         *      "true" if the push settings was set to yes, "false" if set to
1025
         *      no, and <undef> if not present.
1026
         *
1027
         * settings.hdrtbl
1028
         *      Value of HEADER_TABLE_SIZE if set, <undef> otherwise.
1029
         *
1030
         * settings.maxstreams
1031
         *      Value of MAX_CONCURRENT_STREAMS if set, <undef> otherwise.
1032
         *
1033
         * settings.winsize
1034
         *      Value of INITIAL_WINDOW_SIZE if set, <undef> otherwise.
1035
         *
1036
         * setting.framesize
1037
         *      Value of MAX_FRAME_SIZE if set, <undef> otherwise.
1038
         *
1039
         * settings.hdrsize
1040
         *      Value of MAX_HEADER_LIST_SIZE if set, <undef> otherwise.
1041
         *
1042
         * settings.connect
1043
         *      Value of ENABLE_CONNECT_PROTOCOL "true" if yes, "false if no,
1044
         *      <undef> if not present
1045
         *
1046
         * settings.no_prio
1047
         *      Value of NO_RFC7540_PRIORITIES "true" if yes, "false if no,
1048
         *      <undef> if not present
1049
         *
1050
         * settings.tls_reneg
1051
         *      Value of TLS_RENEG_PERMITTED if set, <undef> otherwise.
1052
         */
1053 1505
        if (!vstrncmp(spec, "settings.", 9)) {
1054 234
                CHECK_LAST_FRAME(SETTINGS);
1055 234
                spec += 9;
1056 234
                if (!vstrcmp(spec, "ack")) {
1057 210
                        snprintf(buf, 20, (f->flags & ACK) ? "true" : "false");
1058 210
                        return (buf);
1059 210
                }
1060 24
                if (!vstrcmp(spec, "hdrtbl"))   RETURN_SETTING(HEADER_TABLE_SIZE);
1061 21
                if (!vstrcmp(spec, "push"))     RETURN_SETTING_BOOL(ENABLE_PUSH);
1062 18
                if (!vstrcmp(spec, "maxstreams"))RETURN_SETTING(MAX_CONCURRENT_STREAMS);
1063 15
                if (!vstrcmp(spec, "winsize"))  RETURN_SETTING(INITIAL_WINDOW_SIZE);
1064 12
                if (!vstrcmp(spec, "framesize"))        RETURN_SETTING(MAX_FRAME_SIZE);
1065 9
                if (!vstrcmp(spec, "hdrsize"))  RETURN_SETTING(MAX_HEADER_LIST_SIZE);
1066 6
                if (!vstrcmp(spec, "connect"))  RETURN_SETTING_BOOL(ENABLE_CONNECT_PROTOCOL);
1067 4
                if (!vstrcmp(spec, "no_prio"))  RETURN_SETTING_BOOL(NO_RFC7540_PRIORITIES);
1068 2
                if (!vstrcmp(spec, "tls_reneg"))        RETURN_SETTING(TLS_RENEG_PERMITTED);
1069 2
        }
1070
        /* SECTION: stream.spec.zexpect.push PUSH_PROMISE specific
1071
         *
1072
         * push.id
1073
         *      The id of the promised stream.
1074
         */
1075 1271
        if (!vstrcmp(spec, "push.id")) {
1076 1
                CHECK_LAST_FRAME(PUSH_PROMISE);
1077 1
                RETURN_BUFFED(f->md.promised);
1078 1
        }
1079
        /* SECTION: stream.spec.zexpect.goaway GOAWAY specific
1080
         *
1081
         * goaway.err
1082
         *      The error code (as integer) of the GOAWAY frame.
1083
         *
1084
         * goaway.laststream
1085
         *      Last-Stream-ID
1086
         *
1087
         * goaway.debug
1088
         *      Debug data, if any.
1089
         */
1090 1270
        if (!vstrncmp(spec, "goaway.", 7)) {
1091 68
                spec += 7;
1092 68
                CHECK_LAST_FRAME(GOAWAY);
1093
1094 68
                if (!vstrcmp(spec, "err"))
1095 37
                        RETURN_BUFFED(f->md.goaway.err);
1096 31
                else if (!vstrcmp(spec, "laststream"))
1097 29
                        RETURN_BUFFED(f->md.goaway.stream);
1098 2
                else if (!vstrcmp(spec, "debug"))
1099 2
                        return (f->md.goaway.debug);
1100 68
        }
1101
        /* SECTION: stream.spec.zexpect.zframe Generic frame
1102
         *
1103
         * frame.data
1104
         *      Payload of the last frame
1105
         *
1106
         * frame.type
1107
         *      Type of the frame, as integer.
1108
         *
1109
         * frame.size
1110
         *      Size of the frame.
1111
         *
1112
         * frame.stream
1113
         *      Stream of the frame (correspond to the one you are executing
1114
         *      this from, obviously).
1115
         *
1116
         * frame.padding (for DATA, HEADERS, PUSH_PROMISE frames)
1117
         *      Number of padded bytes.
1118
         */
1119 1202
        if (!vstrncmp(spec, "frame.", 6)) {
1120 8
                spec += 6;
1121 8
                if (!f)
1122 0
                        vtc_fatal(s->vl, "No frame received yet.");
1123 8
                if (!vstrcmp(spec, "data"))   { return (f->data); }
1124 7
                else if (!vstrcmp(spec, "type"))   { RETURN_BUFFED(f->type); }
1125 6
                else if (!vstrcmp(spec, "size"))          { RETURN_BUFFED(f->size); }
1126 2
                else if (!vstrcmp(spec, "stream")) { RETURN_BUFFED(f->stid); }
1127 1
                else if (!vstrcmp(spec, "padding")) {
1128 1
                        if (f->type != TYPE_DATA &&
1129 1
                                        f->type != TYPE_HEADERS &&
1130 1
                                        f->type != TYPE_PUSH_PROMISE)
1131 0
                                vtc_fatal(s->vl,
1132 0
                                                "Last frame was not of type "
1133 0
                                                "DATA, HEADERS or PUSH");
1134 1
                        RETURN_BUFFED(f->md.padded);
1135 1
                }
1136 8
        }
1137
        /* SECTION: stream.spec.zexpect.zstream Stream
1138
         *
1139
         * stream.window
1140
         *      The current local window size of the stream, or, if on stream 0,
1141
         *      of the connection.
1142
         *
1143
         * stream.peer_window
1144
         *      The current peer window size of the stream, or, if on stream 0,
1145
         *      of the connection.
1146
         *
1147
         * stream.weight
1148
         *      Weight of the stream
1149
         *
1150
         * stream.dependency
1151
         *      Id of the stream this one depends on.
1152
         */
1153 1194
        if (!vstrcmp(spec, "stream.window")) {
1154 13
                snprintf(buf, 20, "%jd",
1155 13
                    (intmax_t)(s->id ? s->win_self : s->hp->h2_win_self->size));
1156 13
                return (buf);
1157 13
        }
1158 1181
        if (!vstrcmp(spec, "stream.peer_window")) {
1159 16
                snprintf(buf, 20, "%jd",
1160 16
                    (intmax_t)(s->id ? s->win_peer : s->hp->h2_win_peer->size));
1161 16
                return (buf);
1162 16
        }
1163 1165
        if (!vstrcmp(spec, "stream.weight")) {
1164 8
                if (s->id) {
1165 7
                        snprintf(buf, 20, "%d", s->weight);
1166 7
                        return (buf);
1167 7
                } else
1168 1
                        return (NULL);
1169 8
        }
1170 1157
        if (!vstrcmp(spec, "stream.dependency")) {
1171 16
                if (s->id) {
1172 15
                        snprintf(buf, 20, "%d", s->dependency);
1173 15
                        return (buf);
1174 15
                } else
1175 1
                        return (NULL);
1176 16
        }
1177
        /* SECTION: stream.spec.zexpect.ztable Index tables
1178
         *
1179
         * tbl.dec.size / tbl.enc.size
1180
         *      Size (bytes) of the decoding/encoding table.
1181
         *
1182
         * tbl.dec.size / tbl.enc.maxsize
1183
         *      Maximum size (bytes) of the decoding/encoding table.
1184
         *
1185
         * tbl.dec.length / tbl.enc.length
1186
         *      Number of headers in decoding/encoding table.
1187
         *
1188
         * tbl.dec[INT].key / tbl.enc[INT].key
1189
         *      Name of the header at index INT of the decoding/encoding
1190
         *      table.
1191
         *
1192
         * tbl.dec[INT].value / tbl.enc[INT].value
1193
         *      Value of the header at index INT of the decoding/encoding
1194
         *      table.
1195
         */
1196 1141
        if (!vstrncmp(spec, "tbl.dec", 7) || !vstrncmp(spec, "tbl.enc", 7)) {
1197 123
                if (spec[4] == 'd')
1198 63
                        ctx = s->hp->decctx;
1199 60
                else
1200 60
                        ctx = s->hp->encctx;
1201 123
                spec += 7;
1202
1203 123
                if (1 == sscanf(spec, "[%u].key%n", &idx, &n) &&
1204 123
                                spec[n] == '\0') {
1205 48
                        h = HPK_GetHdr(ctx, idx + 61);
1206 48
                        return (h ? h->key.ptr : NULL);
1207 48
                }
1208 75
                else if (1 == sscanf(spec, "[%u].value%n", &idx, &n) &&
1209 75
                                spec[n] == '\0') {
1210 48
                        h = HPK_GetHdr(ctx, idx + 61);
1211 48
                        return (h ? h->value.ptr : NULL);
1212 48
                }
1213 27
                else if (!vstrcmp(spec, ".size"))
1214 26
                        RETURN_BUFFED(HPK_GetTblSize(ctx));
1215 1
                else if (!vstrcmp(spec, ".maxsize"))
1216 0
                        RETURN_BUFFED(HPK_GetTblMaxSize(ctx));
1217 1
                else if (!vstrcmp(spec, ".length"))
1218 1
                        RETURN_BUFFED(HPK_GetTblLength(ctx));
1219 123
        }
1220
        /* SECTION: stream.spec.zexpect.zre Request and response
1221
         *
1222
         * Note: it's possible to inspect a request or response while it is
1223
         * still being construct (in-between two frames for example).
1224
         *
1225
         * req.bodylen / resp.bodylen
1226
         *      Length in bytes of the request/response so far.
1227
         *
1228
         * req.body / resp.body
1229
         *      Body of the request/response so far.
1230
         *
1231
         * req.http.STRING / resp.http.STRING
1232
         *      Value of the header STRING in the request/response.
1233
         *
1234
         * req.status / resp.status
1235
         *      :status pseudo-header's value.
1236
         *
1237
         * req.url / resp.url
1238
         *      :path pseudo-header's value.
1239
         *
1240
         * req.method / resp.method
1241
         *      :method pseudo-header's value.
1242
         *
1243
         * req.authority / resp.authority
1244
         *      :method pseudo-header's value.
1245
         *
1246
         * req.scheme / resp.scheme
1247
         *      :method pseudo-header's value.
1248
         */
1249 1018
        if (!vstrncmp(spec, "req.", 4) || !vstrncmp(spec, "resp.", 5)) {
1250 238
                if (spec[2] == 'q') {
1251 15
                        h = s->req;
1252 15
                        spec += 4;
1253 223
                } else {
1254 223
                        h = s->resp;
1255 223
                        spec += 5;
1256 223
                }
1257 238
                if (!vstrcmp(spec, "body"))
1258 17
                        return (s->body);
1259 221
                else if (!vstrcmp(spec, "bodylen"))
1260 20
                        RETURN_BUFFED(s->bodylen);
1261 201
                else if (!vstrcmp(spec, "status"))
1262 85
                        return (find_header(h, ":status"));
1263 116
                else if (!vstrcmp(spec, "url"))
1264 2
                        return (find_header(h, ":path"));
1265 114
                else if (!vstrcmp(spec, "method"))
1266 2
                        return (find_header(h, ":method"));
1267 112
                else if (!vstrcmp(spec, "authority"))
1268 1
                        return (find_header(h, ":authority"));
1269 111
                else if (!vstrcmp(spec, "scheme"))
1270 1
                        return (find_header(h, ":scheme"));
1271 110
                else if (!vstrncmp(spec, "http.", 5))
1272 110
                        return (find_header(h, spec + 5));
1273 0
                else
1274 0
                        return (NULL);
1275 238
        }
1276 780
#define H2_ERROR(U,v,sc,g,r,t) \
1277 10153
        if (!vstrcmp(spec, #U)) { return (#v); }
1278 1018
#include "tbl/h2_error.h"
1279 703
        return (spec);
1280 10153
}
1281
1282
/* SECTION: stream.spec.frame_sendhex sendhex
1283
 *
1284
 * Push bytes directly on the wire. sendhex takes exactly one argument: a string
1285
 * describing the bytes, in hex notation, with possible whitespaces between
1286
 * them. Here's an example::
1287
 *
1288
 *      sendhex "00 00 08 00 0900       8d"
1289
 */
1290
static void
1291
cmd_sendhex(CMD_ARGS)
1292 57
{
1293 57
        struct http *hp;
1294 57
        struct stream *s;
1295 57
        struct vsb *vsb;
1296
1297 57
        (void)vl;
1298 57
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1299 57
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
1300 57
        ARGN(vl, av, 1);
1301 57
        ARGZ(vl, av, 2);
1302 57
        vsb = vtc_hex_to_bin(hp->vl, av[1]);
1303 57
        assert(VSB_len(vsb) >= 0);
1304 57
        vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
1305 57
        PTOK(pthread_mutex_lock(&hp->mtx));
1306 57
        http_write(hp, 4, VSB_data(vsb), VSB_len(vsb), "sendhex");
1307 57
        PTOK(pthread_mutex_unlock(&hp->mtx));
1308 57
        VSB_destroy(&vsb);
1309 57
}
1310
1311 1932
#define ENC(hdr, k, v)                                  \
1312 1932
{                                                       \
1313 1932
        AN(k);                                          \
1314 1932
        hdr.key.ptr = TRUST_ME(k);                      \
1315 1932
        hdr.key.len = vstrlen(k);                       \
1316 1932
        AN(v);                                          \
1317 1932
        hdr.value.ptr = TRUST_ME(v);                    \
1318 1932
        hdr.value.len = vstrlen(v);                     \
1319 1932
        assert(HPK_EncHdr(iter, &hdr) != hpk_err);      \
1320 1932
}
1321
1322 30
#define STR_ENC(av, field, str)                                                \
1323 30
{                                                                              \
1324 30
        av++;                                                                  \
1325 30
             if (AV_IS("plain")) { hdr.field.huff = 0; }                       \
1326 30
        else if (AV_IS("huf"))   { hdr.field.huff = 1; }                       \
1327 14
        else                                                                   \
1328 14
                vtc_fatal(vl, str " arg can be huf or plain (got: %s)", *av); \
1329 30
        av++;                                                                  \
1330 30
        AN(*av);                                                               \
1331 30
        hdr.field.ptr = *av;                                                   \
1332 30
        hdr.field.len = vstrlen(*av);                                          \
1333 30
}
1334
1335
1336
/* SECTION: stream.spec.data_0 txreq, txresp, txcont, txpush
1337
 *
1338
 * These four commands are about sending headers. txreq and txresp
1339
 * will send HEADER frames; txcont will send CONTINUATION frames; txpush
1340
 * PUSH frames.
1341
 * The only difference between txreq and txresp are the default headers
1342
 * set by each of them.
1343
 *
1344
 * \-noadd
1345
 *      Do not add default headers. Useful to avoid duplicates when sending
1346
 *      default headers using ``-hdr``, ``-idxHdr`` and ``-litIdxHdr``.
1347
 *
1348
 * \-status INT (txresp)
1349
 *      Set the :status pseudo-header.
1350
 *
1351
 * \-url STRING (txreq, txpush)
1352
 *      Set the :path pseudo-header.
1353
 *
1354
 * \-method STRING (txreq, txpush)
1355
 *      Set the :method pseudo-header.
1356
 *
1357
 * \-req STRING (txreq, txpush)
1358
 *      Alias for -method.
1359
 *
1360
 * \-scheme STRING (txreq, txpush)
1361
 *      Set the :scheme pseudo-header.
1362
 *
1363
 * \-hdr STRING1 STRING2
1364
 *      Insert a header, STRING1 being the name, and STRING2 the value.
1365
 *
1366
 * \-idxHdr INT
1367
 *      Insert an indexed header, using INT as index.
1368
 *
1369
 * \-litIdxHdr inc|not|never INT huf|plain STRING
1370
 *      Insert an literal, indexed header. The first argument specify if the
1371
 *      header should be added to the table, shouldn't, or mustn't be
1372
 *      compressed if/when retransmitted.
1373
 *
1374
 *      INT is the index of the header name to use.
1375
 *
1376
 *      The third argument informs about the Huffman encoding: yes (huf) or
1377
 *      no (plain).
1378
 *
1379
 *      The last term is the literal value of the header.
1380
 *
1381
 * \-litHdr inc|not|never huf|plain STRING1 huf|plain STRING2
1382
 *      Insert a literal header, with the same first argument as
1383
 *      ``-litIdxHdr``.
1384
 *
1385
 *      The second and third terms tell what the name of the header is and if
1386
 *      it should be Huffman-encoded, while the last two do the same
1387
 *      regarding the value.
1388
 *
1389
 * \-body STRING (txreq, txresp)
1390
 *      Specify a body, effectively putting STRING into a DATA frame after
1391
 *      the HEADER frame is sent.
1392
 *
1393
 * \-bodyfrom FILE (txreq, txresp)
1394
 *      Same as ``-body`` but content is read from FILE.
1395
 *
1396
 * \-bodylen INT (txreq, txresp)
1397
 *      Do the same thing as ``-body`` but generate a string of INT length
1398
 *      for you.
1399
 *
1400
 * \-gzipbody STRING (txreq, txresp)
1401
 *      Gzip STRING and send it as body.
1402
 *
1403
 * \-gziplen NUMBER (txreq, txresp)
1404
 *      Combine -bodylen and -gzipbody: generate a string of length NUMBER,
1405
 *      gzip it and send as body.
1406
 *
1407
 * \-nostrend (txreq, txresp)
1408
 *      Don't set the END_STREAM flag automatically, making the peer expect
1409
 *      a body after the headers.
1410
 *
1411
 * \-nohdrend
1412
 *      Don't set the END_HEADERS flag automatically, making the peer expect
1413
 *      more HEADER frames.
1414
 *
1415
 * \-dep INT (txreq, txresp)
1416
 *      Tell the peer that this content depends on the stream with the INT
1417
 *      id.
1418
 *
1419
 * \-ex (txreq, txresp)
1420
 *      Make the dependency exclusive (``-dep`` is still needed).
1421
 *
1422
 * \-weight (txreq, txresp)
1423
 *      Set the weight for the dependency.
1424
 *
1425
 * \-promised INT (txpush)
1426
 *      The id of the promised stream.
1427
 *
1428
 * \-pad STRING / -padlen INT (txreq, txresp, txpush)
1429
 *      Add string as padding to the frame, either the one you provided with
1430
 *      \-pad, or one that is generated for you, of length INT is -padlen
1431
 *      case.
1432
 */
1433
1434
#define cmd_txreq       cmd_tx11obj
1435
#define cmd_txresp      cmd_tx11obj
1436
#define cmd_txpush      cmd_tx11obj
1437
#define cmd_txcont      cmd_tx11obj
1438
1439
static void
1440
cmd_tx11obj(CMD_ARGS)
1441 1302
{
1442 1302
        struct stream *s;
1443 1302
        int i;
1444 1302
        int status_done = 1;
1445 1302
        int method_done = 1;
1446 1302
        int path_done = 1;
1447 1302
        int scheme_done = 1;
1448 1302
        long bodylen = 0;
1449 1302
        ssize_t len;
1450 1302
        uint32_t stid = 0, pstid;
1451 1302
        uint32_t weight = 16;
1452 1302
        uint32_t exclusive = 0;
1453 1302
        char *buf;
1454 1302
        struct hpk_iter *iter;
1455 1302
        struct frame f;
1456 1302
        char *body = NULL, *pad = NULL;
1457
        /*XXX: do we need a better api? yes we do */
1458 1302
        struct hpk_hdr hdr;
1459 1302
        char *cmd_str = *av;
1460 1302
        char *p;
1461
1462 1302
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1463 1302
        INIT_FRAME(f, CONTINUATION, 0, s->id, END_HEADERS);
1464 1302
        buf = malloc(BUF_SIZE);
1465 1302
        AN(buf);
1466
1467 1302
        if (!vstrcmp(cmd_str, "txreq")) {
1468 236
                ONLY_H2_CLIENT(s->hp, av);
1469 236
                f.type = TYPE_HEADERS;
1470 236
                f.flags |= END_STREAM;
1471 236
                method_done = 0;
1472 236
                path_done = 0;
1473 236
                scheme_done = 0;
1474 1066
        } else if (!vstrcmp(cmd_str, "txresp")) {
1475 48
                ONLY_H2_SERVER(s->hp, av);
1476 48
                f.type = TYPE_HEADERS;
1477 48
                f.flags |= END_STREAM;
1478 48
                status_done = 0;
1479 1018
        } else if (!vstrcmp(cmd_str, "txpush")) {
1480 1
                ONLY_H2_SERVER(s->hp, av);
1481 1
                f.type = TYPE_PUSH_PROMISE;
1482 1
                method_done = 0;
1483 1
                path_done = 0;
1484 1
                scheme_done = 0;
1485 1
        }
1486
1487 1302
        if (f.type == TYPE_PUSH_PROMISE) {
1488 1
                *buf = 0;
1489 1
                iter = HPK_NewIter(s->hp->encctx, buf + 4, BUF_SIZE - 4);
1490 1
        } else
1491 1301
                iter = HPK_NewIter(s->hp->encctx, buf, BUF_SIZE);
1492
1493 22625
#define AV_IS(str) !vstrcmp(*av, str)
1494 1302
#define CMD_IS(str) !vstrcmp(cmd_str, str)
1495 3783
        while (*++av) {
1496 2481
                memset(&hdr, 0, sizeof(hdr));
1497 2481
                hdr.t = hpk_not;
1498 2481
                if (AV_IS("-noadd")) {
1499 14
                        path_done = 1;
1500 14
                        status_done = 1;
1501 14
                        method_done = 1;
1502 14
                        scheme_done = 1;
1503 14
                }
1504 2467
                else if (AV_IS("-status") && CMD_IS("txresp")) {
1505 2
                        ENC(hdr, ":status", av[1]);
1506 1
                        av++;
1507 1
                        status_done = 1;
1508 1
                }
1509 2466
                else if (AV_IS("-url") &&
1510 2466
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1511 126
                        ENC(hdr, ":path", av[1]);
1512 63
                        av++;
1513 63
                        path_done = 1;
1514 63
                }
1515 2403
                else if ((AV_IS("-method") || AV_IS("-req")) &&
1516 2403
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1517 74
                        ENC(hdr, ":method", av[1]);
1518 37
                        av++;
1519 37
                        method_done = 1;
1520 37
                }
1521 2366
                else if (AV_IS("-scheme") &&
1522 2366
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1523 14
                        ENC(hdr, ":scheme", av[1]);
1524 7
                        av++;
1525 7
                        scheme_done = 1;
1526 7
                }
1527 2359
                else if (AV_IS("-hdr")) {
1528 1210
                        if (av[2] == NULL)
1529 0
                                vtc_fatal(vl, "-hdr takes two arguments in http2");
1530 3630
                        ENC(hdr, av[1], av[2]);
1531 1210
                        av += 2;
1532 1210
                }
1533 1149
                else if (AV_IS("-idxHdr")) {
1534 42
                        hdr.t = hpk_idx;
1535 42
                        STRTOU32_CHECK(hdr.i, av, p, vl, "-idxHdr", 0);
1536 42
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1537 42
                }
1538 1107
                else if (AV_IS("-litIdxHdr")) {
1539 16
                        av++;
1540 16
                        if      (AV_IS("inc"))   { hdr.t = hpk_inc;   }
1541 1
                        else if (AV_IS("not"))   { hdr.t = hpk_not;   }
1542 1
                        else if (AV_IS("never")) { hdr.t = hpk_never; }
1543 0
                        else
1544 0
                                vtc_fatal(vl, "first -litidxHdr arg can be "
1545 0
                                    "inc, not, never (got: %s)", *av);
1546
1547 16
                        STRTOU32_CHECK(hdr.i, av, p, vl,
1548 16
                            "second -litidxHdr arg", 0);
1549
1550 16
                        hdr.key.ptr = NULL;
1551 16
                        hdr.key.len = 0;
1552 32
                        STR_ENC(av, value,   "third -litHdr");
1553 32
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1554 32
                }
1555 1091
                else if (AV_IS("-litHdr")) {
1556 7
                        av++;
1557 7
                        if      (AV_IS("inc"))   { hdr.t = hpk_inc;   }
1558 0
                        else if (AV_IS("not"))   { hdr.t = hpk_not;   }
1559 0
                        else if (AV_IS("never")) { hdr.t = hpk_never; }
1560 0
                        else
1561 0
                                vtc_fatal(vl, "first -litHdr arg can be inc, "
1562 0
                                    "not, never (got: %s)", *av);
1563
1564 21
                        STR_ENC(av, key,   "second -litHdr");
1565 21
                        STR_ENC(av, value, "fourth -litHdr");
1566 14
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1567 14
                }
1568 1084
                else if (AV_IS("-nostrend")) {
1569 39
                        f.flags &= ~END_STREAM;
1570 39
                }
1571 1045
                else if (AV_IS("-nohdrend")) {
1572 1016
                        f.flags &= ~END_HEADERS;
1573 1016
                }
1574 29
                else if (AV_IS("-promised") && CMD_IS("txpush")) {
1575 1
                        STRTOU32_CHECK(pstid, av, p, vl, "-promised", 31);
1576 1
                        vbe32enc(buf, pstid);
1577 1
                }
1578 28
                else if (AV_IS("-pad") && !CMD_IS("txcont")) {
1579 6
                        AZ(pad);
1580 6
                        av++;
1581 6
                        AN(*av);
1582 6
                        pad = strdup(*av);
1583 6
                }
1584 22
                else if (AV_IS("-padlen") && !CMD_IS("txcont")) {
1585 0
                        AZ(pad);
1586 0
                        av++;
1587 0
                        pad = synth_body(*av, 0);
1588 0
                }
1589 22
                else if (CMD_IS("txreq") || CMD_IS("txresp")) {
1590 22
                        if (AV_IS("-body")) {
1591 8
                                AZ(body);
1592 8
                                REPLACE(body, av[1]);
1593 8
                                AN(body);
1594 8
                                bodylen = vstrlen(body);
1595 8
                                f.flags &= ~END_STREAM;
1596 8
                                av++;
1597 8
                        }
1598 14
                        else if (AV_IS("-bodyfrom")) {
1599 2
                                AZ(body);
1600 2
                                body = VFIL_readfile(NULL, av[1], &len);
1601 2
                                AN(body);
1602 2
                                assert(len < INT_MAX);
1603 2
                                bodylen = len;
1604 2
                                f.flags &= ~END_STREAM;
1605 2
                                av++;
1606 2
                        }
1607 12
                        else if (AV_IS("-bodylen")) {
1608 4
                                AZ(body);
1609 4
                                body = synth_body(av[1], 0);
1610 4
                                bodylen = vstrlen(body);
1611 4
                                f.flags &= ~END_STREAM;
1612 4
                                av++;
1613 4
                        }
1614 8
                        else if (!vstrncmp(*av, "-gzip", 5)) {
1615 2
                                i = vtc_gzip_cmd(s->hp, av, &body, &bodylen);
1616 2
                                if (i == 0)
1617 0
                                        break;
1618 2
                                av++;
1619 2
                                if (i > 1) {
1620 4
                                        ENC(hdr, ":content-encoding", "gzip");
1621 2
                                        f.flags &= ~END_STREAM;
1622 2
                                }
1623 2
                        }
1624 6
                        else if (AV_IS("-dep")) {
1625 3
                                STRTOU32_CHECK(stid, av, p, vl, "-dep", 0);
1626 3
                                f.flags |= PRIORITY;
1627 3
                        }
1628 3
                        else if (AV_IS("-ex")) {
1629 2
                                exclusive = 1U << 31;
1630 2
                                f.flags |= PRIORITY;
1631 2
                        }
1632 1
                        else if (AV_IS("-weight")) {
1633 1
                                STRTOU32_CHECK(weight, av, p, vl, "-weight", 8);
1634 1
                                f.flags |= PRIORITY;
1635 1
                        } else
1636 0
                                break;
1637 22
                } else
1638 0
                        break;
1639 2481
        }
1640 1302
#undef CMD_IS
1641 1302
#undef AV_IS
1642 1302
        if (*av != NULL)
1643 0
                vtc_fatal(vl, "Unknown %s spec: %s\n", cmd_str, *av);
1644
1645 1302
        memset(&hdr, 0, sizeof(hdr));
1646 1302
        hdr.t = hpk_not;
1647
1648 1302
        if (!status_done) { ENC(hdr, ":status", "200"); }
1649 1302
        if (!path_done)   { ENC(hdr, ":path",   "/"); }
1650 1302
        if (!method_done) { ENC(hdr, ":method", "GET"); }
1651 1302
        if (!scheme_done) { ENC(hdr, ":scheme", "http"); }
1652
1653 1302
        f.size = gethpk_iterLen(iter);
1654 1302
        if (f.flags & PRIORITY) {
1655 4
                s->weight = weight & 0xff;
1656 4
                s->dependency = stid;
1657
1658 4
                assert(f.size + 5 < BUF_SIZE);
1659 4
                vmemmove(buf + 5, buf, f.size);
1660 4
                vbe32enc(buf, (stid | exclusive));
1661 4
                buf[4] = s->weight;
1662 4
                f.size += 5;
1663
1664 4
                vtc_log(vl, 4, "stream->dependency: %u", s->dependency);
1665 4
                vtc_log(vl, 4, "stream->weight: %u", s->weight);
1666 4
                if (exclusive)
1667 2
                        exclusive_stream_dependency(s);
1668 4
        }
1669 1302
        if (pad) {
1670 6
                if (vstrlen(pad) > 255)
1671 0
                        vtc_fatal(vl, "Padding is limited to 255 bytes");
1672 6
                f.flags |= PADDED;
1673 6
                assert(f.size + vstrlen(pad) < BUF_SIZE);
1674 6
                vmemmove(buf + 1, buf, f.size);
1675 6
                buf[0] = vstrlen(pad);
1676 6
                f.size += 1;
1677 6
                vmemcpy(buf + f.size, pad, vstrlen(pad));
1678 6
                f.size += vstrlen(pad);
1679 6
                free(pad);
1680 6
        }
1681 1302
        if (f.type == TYPE_PUSH_PROMISE)
1682 1
                f.size += 4;
1683 1302
        f.data = buf;
1684 1302
        HPK_FreeIter(iter);
1685 1302
        write_frame(s, &f, 1);
1686 1302
        free(buf);
1687
1688 1302
        if (!body)
1689 1286
                return;
1690
1691 16
        INIT_FRAME(f, DATA, bodylen, s->id, END_STREAM);
1692 16
        f.data = body;
1693
1694 16
        write_frame(s, &f, 1);
1695 16
        free(body);
1696 16
}
1697
1698
/* SECTION: stream.spec.data_1 txdata
1699
 *
1700
 * By default, data frames are empty. The receiving end will know the whole body
1701
 * has been delivered thanks to the END_STREAM flag set in the last DATA frame,
1702
 * and txdata automatically set it.
1703
 *
1704
 * \-data STRING
1705
 *      Data to be embedded into the frame.
1706
 *
1707
 * \-datalen INT
1708
 *      Generate and INT-bytes long string to be sent in the frame.
1709
 *
1710
 * \-pad STRING / -padlen INT
1711
 *      Add string as padding to the frame, either the one you provided with
1712
 *      \-pad, or one that is generated for you, of length INT is -padlen
1713
 *      case.
1714
 *
1715
 * \-nostrend
1716
 *      Don't set the END_STREAM flag, allowing to send more data on this
1717
 *      stream.
1718
 */
1719
static void
1720
cmd_txdata(CMD_ARGS)
1721 321
{
1722 321
        struct stream *s;
1723 321
        char *pad = NULL;
1724 321
        struct frame f;
1725 321
        char *body = NULL;
1726 321
        char *data = NULL;
1727
1728 321
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1729
1730 321
        INIT_FRAME(f, DATA, 0, s->id, END_STREAM);
1731
1732 940
        while (*++av) {
1733 619
                if (!vstrcmp(*av, "-data")) {
1734 26
                        AZ(body);
1735 26
                        av++;
1736 26
                        body = strdup(*av);
1737 593
                } else if (!vstrcmp(*av, "-datalen")) {
1738 35
                        AZ(body);
1739 35
                        av++;
1740 35
                        body = synth_body(*av, 0);
1741 558
                } else if (!vstrcmp(*av, "-pad")) {
1742 1
                        AZ(pad);
1743 1
                        av++;
1744 1
                        AN(*av);
1745 1
                        pad = strdup(*av);
1746 557
                } else if (!vstrcmp(*av, "-padlen")) {
1747 268
                        AZ(pad);
1748 268
                        av++;
1749 268
                        pad = synth_body(*av, 0);
1750 289
                } else if (!vstrcmp(*av, "-nostrend"))
1751 289
                        f.flags &= ~END_STREAM;
1752 0
                else
1753 0
                        break;
1754 619
        }
1755 321
        if (*av != NULL)
1756 0
                vtc_fatal(vl, "Unknown txdata spec: %s\n", *av);
1757
1758 321
        if (!body)
1759 260
                body = strdup("");
1760
1761 321
        if (pad) {
1762 269
                f.flags |= PADDED;
1763 269
                if (vstrlen(pad) > 255)
1764 0
                        vtc_fatal(vl, "Padding is limited to 255 bytes");
1765 269
                data = malloc( 1 + vstrlen(body) + vstrlen(pad));
1766 269
                AN(data);
1767 269
                *((uint8_t *)data) = vstrlen(pad);
1768 269
                f.size = 1;
1769 269
                vmemcpy(data + f.size, body, vstrlen(body));
1770 269
                f.size += vstrlen(body);
1771 269
                vmemcpy(data + f.size, pad, vstrlen(pad));
1772 269
                f.size += vstrlen(pad);
1773 269
                f.data = data;
1774 269
        } else {
1775 52
                f.size = vstrlen(body);
1776 52
                f.data = body;
1777 52
        }
1778 321
        write_frame(s, &f, 1);
1779 321
        free(body);
1780 321
        free(pad);
1781 321
        free(data);
1782 321
}
1783
1784
/* SECTION: stream.spec.reset_txrst txrst
1785
 *
1786
 * Send a RST_STREAM frame. By default, txrst will send a 0 error code
1787
 * (NO_ERROR).
1788
 *
1789
 * \-err STRING|INT
1790
 *      Sets the error code to be sent. The argument can be an integer or a
1791
 *      string describing the error, such as NO_ERROR, or CANCEL (see
1792
 *      rfc7540#11.4 for more strings).
1793
 */
1794
static void
1795
cmd_txrst(CMD_ARGS)
1796 19
{
1797 19
        struct stream *s;
1798 19
        char *p;
1799 19
        uint32_t err = 0;
1800 19
        struct frame f;
1801
1802 19
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1803
1804 19
        INIT_FRAME(f, RST_STREAM, 4, s->id, 0);
1805
1806 27
        while (*++av) {
1807 8
                if (!vstrcmp(*av, "-err")) {
1808 8
                        ++av;
1809 120
                        for (err = 0; h2_errs[err]; err++) {
1810 112
                                if (!vstrcmp(h2_errs[err], *av))
1811 0
                                        break;
1812 112
                        }
1813
1814 8
                        if (h2_errs[err])
1815 0
                                continue;
1816
1817 8
                        STRTOU32(err, *av, p, vl, "-err");
1818 8
                } else
1819 0
                        break;
1820 8
        }
1821 19
        if (*av != NULL)
1822 0
                vtc_fatal(vl, "Unknown txrst spec: %s\n", *av);
1823
1824 19
        err = htonl(err);
1825 19
        f.data = (void *)&err;
1826 19
        write_frame(s, &f, 1);
1827 19
}
1828
1829
/* SECTION: stream.spec.prio_txprio txprio
1830
 *
1831
 * Send a PRIORITY frame
1832
 *
1833
 * \-stream INT
1834
 *      indicate the id of the stream the sender stream depends on.
1835
 *
1836
 * \-ex
1837
 *      the dependency should be made exclusive (only this streams depends on
1838
 *      the parent stream).
1839
 *
1840
 * \-weight INT
1841
 *      an 8-bits integer is used to balance priority between streams
1842
 *      depending on the same streams.
1843
 */
1844
static void
1845
cmd_txprio(CMD_ARGS)
1846 13
{
1847 13
        struct stream *s;
1848 13
        char *p;
1849 13
        uint32_t stid = 0;
1850 13
        struct frame f;
1851 13
        uint32_t weight = 0;
1852 13
        uint32_t exclusive = 0;
1853 13
        uint8_t buf[5];
1854
1855 13
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1856
1857 13
        INIT_FRAME(f, PRIORITY, 5, s->id, 0);
1858 13
        f.data = (void *)buf;
1859
1860 31
        while (*++av) {
1861 18
                if (!vstrcmp(*av, "-stream")) {
1862 9
                        STRTOU32_CHECK(stid, av, p, vl, "-stream", 0);
1863 9
                } else if (!vstrcmp(*av, "-ex")) {
1864 1
                        exclusive = 1U << 31;
1865 8
                } else if (!vstrcmp(*av, "-weight")) {
1866 8
                        STRTOU32_CHECK(weight, av, p, vl, "-weight", 8);
1867 8
                } else
1868 0
                        break;
1869 18
        }
1870 13
        if (*av != NULL)
1871 0
                vtc_fatal(vl, "Unknown txprio spec: %s\n", *av);
1872 13
        s->weight = weight & 0xff;
1873 13
        s->dependency = stid;
1874
1875 13
        if (exclusive)
1876 1
                exclusive_stream_dependency(s);
1877
1878 13
        vbe32enc(buf, (stid | exclusive));
1879 13
        buf[4] = s->weight;
1880 13
        write_frame(s, &f, 1);
1881 13
}
1882
1883
#define PUT(val, code)                                          \
1884 22
        do {                                                    \
1885 22
                vbe16enc(cursor, code);                         \
1886 22
                cursor += sizeof(uint16_t);                     \
1887 22
                vbe32enc(cursor, val);                          \
1888 22
                cursor += sizeof(uint32_t);                     \
1889 22
                f.size += 6;                                    \
1890 22
        } while(0)
1891
1892
#define PUT_KV(av, vl, name, val, code)                         \
1893 19
        do {                                                    \
1894 19
                STRTOU32_CHECK(val, av, p, vl, #name, 0);       \
1895 19
                PUT(val, code);                                 \
1896 19
        } while(0)
1897
1898
#define PUT_BOOL(av, vl, name, code)                                    \
1899 3
        do {                                                            \
1900 3
                ++av;                                                   \
1901 3
                if (!vstrcmp(*av, "false"))                             \
1902 3
                        PUT(0, code);                                   \
1903 3
                else if (!vstrcmp(*av, "true"))                         \
1904 3
                        PUT(1, code);                                   \
1905 3
                else {                                                  \
1906 0
                        vtc_fatal(vl, "Push parameter is either "       \
1907 0
                            "\"true\" or \"false\", not %s", *av);      \
1908 0
                }                                                       \
1909 3
        } while(0)
1910
1911
1912
/* SECTION: stream.spec.settings_txsettings txsettings
1913
 *
1914
 * SETTINGS frames must be acknowledge, arguments are as follow (most of them
1915
 * are from  rfc7540#6.5.2):
1916
 *
1917
 * \-hdrtbl INT
1918
 *      headers table size
1919
 *
1920
 * \-push BOOL
1921
 *      whether push frames are accepted or not
1922
 *
1923
 * \-maxstreams INT
1924
 *      maximum concurrent streams allowed
1925
 *
1926
 * \-winsize INT
1927
 *      sender's initial window size
1928
 *
1929
 * \-framesize INT
1930
 *      largest frame size authorized
1931
 *
1932
 * \-hdrsize INT
1933
 *      maximum size of the header list authorized
1934
 *
1935
 * \-connect BOOL
1936
 *      whether connect protocol is enabled
1937
 *
1938
 * \-no_prio BOOL
1939
 *      whether priorities are disabled
1940
 *
1941
 * \-tls_reneg INT
1942
 *      value for TLS_RENEG_PERMITTED
1943
 *
1944
 * \-0xHH[HH] INT
1945
 *      tx arbitraty settings with tag xx
1946
 *
1947
 * \-ack
1948
 *      set the ack bit
1949
 */
1950
static void
1951
cmd_txsettings(CMD_ARGS)
1952 420
{
1953 420
        struct stream *s, *s2;
1954 420
        struct http *hp;
1955 420
        char *p, *e;
1956 420
        unsigned long u;
1957 420
        uint32_t val = 0;
1958 420
        struct frame f;
1959
        //TODO dynamic alloc
1960 420
        char buf[512];
1961 420
        char *cursor = buf;
1962
1963 420
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1964 420
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
1965
1966 420
        memset(buf, 0, 512);
1967 420
        INIT_FRAME(f, SETTINGS, 0, s->id, 0);
1968 420
        f.data = buf;
1969
1970 420
        PTOK(pthread_mutex_lock(&hp->mtx));
1971 650
        while (*++av) {
1972 230
                if (!vstrcmp(*av, "-push"))
1973 1
                        PUT_BOOL(av, vl, push, SETTINGS_ENABLE_PUSH);
1974 229
                else if (!vstrcmp(*av, "-hdrtbl")) {
1975 5
                        PUT_KV(av, vl, hdrtbl, val, SETTINGS_HEADER_TABLE_SIZE);
1976 5
                        assert(HPK_ResizeTbl(s->hp->decctx, val) != hpk_err);
1977 5
                }
1978 224
                else if (!vstrcmp(*av, "-maxstreams"))
1979 1
                        PUT_KV(av, vl, maxstreams, val, SETTINGS_MAX_CONCURRENT_STREAMS);
1980 223
                else if (!vstrcmp(*av, "-winsize"))     {
1981 8
                        PUT_KV(av, vl, winsize, val, SETTINGS_INITIAL_WINDOW_SIZE);
1982 8
                        VTAILQ_FOREACH(s2, &hp->streams, list)
1983 9
                                s2->win_self += (val - hp->h2_win_self->init);
1984 8
                        hp->h2_win_self->init = val;
1985 8
                }
1986 215
                else if (!vstrcmp(*av, "-framesize"))
1987 3
                        PUT_KV(av, vl, framesize, val, SETTINGS_MAX_FRAME_SIZE);
1988 212
                else if (!vstrcmp(*av, "-hdrsize"))
1989 1
                        PUT_KV(av, vl, hdrsize, val, SETTINGS_MAX_HEADER_LIST_SIZE);
1990 211
                else if (!vstrcmp(*av, "-connect"))
1991 1
                        PUT_BOOL(av, vl, connect, SETTINGS_ENABLE_CONNECT_PROTOCOL);
1992 210
                else if (!vstrcmp(*av, "-no_prio"))
1993 1
                        PUT_BOOL(av, vl, no_prio, SETTINGS_NO_RFC7540_PRIORITIES);
1994 209
                else if (!vstrcmp(*av, "-tls_reneg"))
1995 1
                        PUT_KV(av, vl, tls_reneg, val, SETTINGS_TLS_RENEG_PERMITTED);
1996 208
                else if (!vstrncmp(*av, "-0x", 3)) {
1997 0
                        p = *av + 3;
1998 0
                        errno = 0;
1999 0
                        u = strtoul(p, &e, 16);
2000 0
                        if (*p == '\0' || *e != '\0' || u > 0xffff || errno != 0)
2001 0
                                vtc_fatal(vl, "Invalid settings tag %s", p);
2002 0
                        assert(u <= 0xffff);
2003 0
                        PUT_KV(av, vl, hdrtbl, val, (uint16_t)u);
2004 0
                }
2005 208
                else if (!vstrcmp(*av, "-ack"))
2006 208
                        f.flags |= 1;
2007 0
                else
2008 0
                        break;
2009 230
        }
2010 420
        if (*av != NULL)
2011 0
                vtc_fatal(vl, "Unknown txsettings spec: %s\n", *av);
2012
2013 420
        AN(s->hp);
2014 420
        write_frame(s, &f, 0);
2015 420
        PTOK(pthread_mutex_unlock(&hp->mtx));
2016 420
}
2017
2018
/* SECTION: stream.spec.ping_txping txping
2019
 *
2020
 * Send PING frame.
2021
 *
2022
 * \-data STRING
2023
 *      specify the payload of the frame, with STRING being an 8-char string.
2024
 *
2025
 * \-ack
2026
 *      set the ACK flag.
2027
 */
2028
static void
2029
cmd_txping(CMD_ARGS)
2030 7
{
2031 7
        struct stream *s;
2032 7
        struct frame f;
2033 7
        char buf[8];
2034
2035 7
        memset(buf, 0, 8);
2036 7
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2037 7
        INIT_FRAME(f, PING, 8, s->id, 0);
2038
2039 14
        while (*++av) {
2040 7
                if (!vstrcmp(*av, "-data")) {
2041 5
                        av++;
2042 5
                        if (f.data)
2043 0
                                vtc_fatal(vl, "this frame already has data");
2044 5
                        if (vstrlen(*av) != 8)
2045 0
                                vtc_fatal(vl, "data must be a 8-char string, found  (%s)", *av);
2046 5
                        f.data = *av;
2047 5
                } else if (!vstrcmp(*av, "-ack"))
2048 2
                        f.flags |= 1;
2049 0
                else
2050 0
                        break;
2051 7
        }
2052 7
        if (*av != NULL)
2053 0
                vtc_fatal(vl, "Unknown txping spec: %s\n", *av);
2054 7
        if (!f.data)
2055 2
                f.data = buf;
2056 7
        write_frame(s, &f, 1);
2057 7
}
2058
2059
/*
2060
 * SECTION: stream.spec.goaway_txgoaway txgoaway
2061
 *
2062
 * Possible options include:
2063
 *
2064
 * \-err STRING|INT
2065
 *      set the error code to explain the termination. The second argument
2066
 *      can be a integer or the string version of the error code as found
2067
 *      in rfc7540#7.
2068
 *
2069
 * \-laststream INT
2070
 *      the id of the "highest-numbered stream identifier for which the
2071
 *      sender of the GOAWAY frame might have taken some action on or might
2072
 *      yet take action on".
2073
 *
2074
 * \-debug
2075
 *      specify the debug data, if any to append to the frame.
2076
 */
2077
static void
2078
cmd_txgoaway(CMD_ARGS)
2079 5
{
2080 5
        struct stream *s;
2081 5
        char *p;
2082 5
        uint32_t err = 0;
2083 5
        uint32_t ls = 0;
2084 5
        struct frame f;
2085
2086 5
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2087
2088 5
        INIT_FRAME(f, GOAWAY, 8, s->id, 0);
2089
2090 14
        while (*++av) {
2091 9
                if (!vstrcmp(*av, "-err")) {
2092 5
                        ++av;
2093 75
                        for (err = 0; h2_errs[err]; err++)
2094 70
                                if (!vstrcmp(h2_errs[err], *av))
2095 0
                                        break;
2096
2097 5
                        if (h2_errs[err])
2098 0
                                continue;
2099
2100 5
                        STRTOU32(err, *av, p, vl, "-err");
2101 5
                } else if (!vstrcmp(*av, "-laststream")) {
2102 2
                        STRTOU32_CHECK(ls, av, p, vl, "-laststream", 31);
2103 2
                } else if (!vstrcmp(*av, "-debug")) {
2104 2
                        ++av;
2105 2
                        if (f.data)
2106 0
                                vtc_fatal(vl, "this frame already has debug data");
2107 2
                        f.size = 8 + vstrlen(*av);
2108 2
                        f.data = malloc(f.size);
2109 2
                        AN(f.data);
2110 2
                        vmemcpy(f.data + 8, *av, f.size - 8);
2111 2
                } else
2112 0
                        break;
2113 9
        }
2114 5
        if (*av != NULL)
2115 0
                vtc_fatal(vl, "Unknown txgoaway spec: %s\n", *av);
2116
2117 5
        if (!f.data) {
2118 3
                f.data = malloc(8);
2119 3
                AN(f.data);
2120 3
        }
2121 5
        vbe32enc(f.data, ls);
2122 5
        vbe32enc(f.data + 4, err);
2123 5
        write_frame(s, &f, 1);
2124 5
        free(f.data);
2125 5
}
2126
2127
/* SECTION: stream.spec.winup_txwinup txwinup
2128
 *
2129
 * Transmit a WINDOW_UPDATE frame, increasing the amount of credit of the
2130
 * connection (from stream 0) or of the stream (any other stream).
2131
 *
2132
 * \-size INT
2133
 *      give INT credits to the peer.
2134
 */
2135
static void
2136
cmd_txwinup(CMD_ARGS)
2137 28
{
2138 28
        struct http *hp;
2139 28
        struct stream *s;
2140 28
        char *p;
2141 28
        struct frame f;
2142 28
        char buf[8];
2143 28
        uint32_t size = 0;
2144
2145 28
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2146 28
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
2147 28
        memset(buf, 0, 8);
2148
2149 28
        ARGN(vl, av, 1);
2150 28
        ARGN(vl, av, 2);
2151
2152 28
        INIT_FRAME(f, WINDOW_UPDATE, 4, s->id, 0);
2153 28
        f.data = buf;
2154
2155 56
        while (*++av)
2156 28
                if (!vstrcmp(*av, "-size")) {
2157 28
                        STRTOU32_CHECK(size, av, p, vl, "-size", 0);
2158 28
                } else
2159 0
                        break;
2160 28
        if (*av != NULL)
2161 0
                vtc_fatal(vl, "Unknown txwinup spec: %s\n", *av);
2162
2163 28
        PTOK(pthread_mutex_lock(&hp->mtx));
2164 28
        if (s->id == 0)
2165 10
                hp->h2_win_self->size += size;
2166 28
        s->win_self += size;
2167 28
        PTOK(pthread_mutex_unlock(&hp->mtx));
2168
2169 28
        size = htonl(size);
2170 28
        f.data = (void *)&size;
2171 28
        write_frame(s, &f, 1);
2172 28
}
2173
2174
static struct frame *
2175
rxstuff(struct stream *s)
2176 976
{
2177 976
        struct frame *f;
2178
2179 976
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2180
2181 976
        PTOK(pthread_mutex_lock(&s->hp->mtx));
2182 976
        if (VTAILQ_EMPTY(&s->fq)) {
2183 973
                assert(s->hp->wf >= 0);
2184 973
                s->hp->wf++;
2185 973
                s->wf = 1;
2186 973
                PTOK(pthread_cond_signal(&s->hp->cond));
2187 973
                PTOK(pthread_cond_wait(&s->cond, &s->hp->mtx));
2188 973
        }
2189 976
        if (VTAILQ_EMPTY(&s->fq)) {
2190 0
                PTOK(pthread_mutex_unlock(&s->hp->mtx));
2191 0
                return (NULL);
2192 0
        }
2193 976
        clean_frame(&s->frame);
2194 976
        f = VTAILQ_LAST(&s->fq, fq_head);
2195 976
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
2196 976
        VTAILQ_REMOVE(&s->fq, f, list);
2197 976
        PTOK(pthread_mutex_unlock(&s->hp->mtx));
2198 976
        return (f);
2199 976
}
2200
2201
#define CHKFRAME(rt, wt, rcv, func) \
2202 863
        do { \
2203 863
        if (rt != wt) \
2204 863
                vtc_fatal(vl, "Frame #%d for %s was of type %s (%d) " \
2205 0
                    "instead of %s (%d)", \
2206 0
                    rcv, func, \
2207 0
                    rt < TYPE_MAX ? h2_types[rt] : "?", rt, \
2208 0
                    wt < TYPE_MAX ? h2_types[wt] : "?", wt); \
2209 863
        } while (0);
2210
2211
/* SECTION: stream.spec.data_11 rxhdrs
2212
 *
2213
 * ``rxhdrs`` will expect one HEADER frame, then, depending on the arguments,
2214
 * zero or more CONTINUATION frame.
2215
 *
2216
 * \-all
2217
 *      Keep waiting for CONTINUATION frames until END_HEADERS flag is seen.
2218
 *
2219
 * \-some INT
2220
 *      Retrieve INT - 1 CONTINUATION frames after the HEADER frame.
2221
 *
2222
 */
2223
static void
2224
cmd_rxhdrs(CMD_ARGS)
2225 16
{
2226 16
        struct stream *s;
2227 16
        struct frame *f = NULL;
2228 16
        char *p;
2229 16
        int loop = 0;
2230 16
        unsigned long int times = 1;
2231 16
        unsigned rcv = 0;
2232 16
        enum h2_type_e expect = TYPE_HEADERS;
2233
2234 16
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2235
2236 18
        while (*++av) {
2237 2
                if (!vstrcmp(*av, "-some")) {
2238 1
                        STRTOU32_CHECK(times, av, p, vl, "-some", 0);
2239 1
                        if (!times)
2240 0
                                vtc_fatal(vl, "-some argument must be more"
2241 0
                                               "than 0 (found \"%s\")\n", *av);
2242 1
                } else if (!vstrcmp(*av, "-all"))
2243 1
                        loop = 1;
2244 0
                else
2245 0
                        break;
2246 2
        }
2247 16
        if (*av != NULL)
2248 0
                vtc_fatal(vl, "Unknown rxhdrs spec: %s\n", *av);
2249
2250 19
        do {
2251 19
                replace_frame(&f, rxstuff(s));
2252 19
                if (f == NULL)
2253 0
                        break;
2254 19
                rcv++;
2255 19
                CHKFRAME(f->type, expect, rcv, "rxhdrs");
2256 19
                expect = TYPE_CONTINUATION;
2257 19
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2258 16
        replace_frame(&s->frame, f);
2259 16
}
2260
2261
static void
2262
cmd_rxcont(CMD_ARGS)
2263 5
{
2264 5
        struct stream *s;
2265 5
        struct frame *f = NULL;
2266 5
        char *p;
2267 5
        int loop = 0;
2268 5
        unsigned long int times = 1;
2269 5
        unsigned rcv = 0;
2270
2271 5
        (void)av;
2272 5
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2273
2274 5
        while (*++av)
2275 0
                if (!vstrcmp(*av, "-some")) {
2276 0
                        STRTOU32(times, *av, p, vl, "-some");
2277 0
                        if (!times)
2278 0
                                vtc_fatal(vl, "-some argument must be more"
2279 0
                                               "than 0 (found \"%s\")\n", *av);
2280 0
                } else if (!vstrcmp(*av, "-all"))
2281 0
                        loop = 1;
2282 0
                else
2283 0
                        break;
2284 5
        if (*av != NULL)
2285 0
                vtc_fatal(vl, "Unknown rxcont spec: %s\n", *av);
2286
2287 5
        do {
2288 5
                replace_frame(&f, rxstuff(s));
2289 5
                if (f == NULL)
2290 0
                        break;
2291 5
                rcv++;
2292 5
                CHKFRAME(f->type, TYPE_CONTINUATION, rcv, "rxcont");
2293 5
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2294 5
        replace_frame(&s->frame, f);
2295 5
}
2296
2297
2298
/* SECTION: stream.spec.data_13 rxdata
2299
 *
2300
 * Receiving data is done using the ``rxdata`` keywords and will retrieve one
2301
 * DATA frame, if you wish to receive more, you can use these two convenience
2302
 * arguments:
2303
 *
2304
 * \-all
2305
 *      keep waiting for DATA frame until one sets the END_STREAM flag
2306
 *
2307
 * \-some INT
2308
 *      retrieve INT DATA frames.
2309
 *
2310
 */
2311
static void
2312
cmd_rxdata(CMD_ARGS)
2313 16
{
2314 16
        struct stream *s;
2315 16
        struct frame *f = NULL;
2316 16
        char *p;
2317 16
        int loop = 0;
2318 16
        unsigned long int times = 1;
2319 16
        unsigned rcv = 0;
2320
2321 16
        (void)av;
2322 16
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2323
2324 17
        while (*++av)
2325 1
                if (!vstrcmp(*av, "-some")) {
2326 1
                        av++;
2327 1
                        STRTOU32(times, *av, p, vl, "-some");
2328 1
                        if (!times)
2329 0
                                vtc_fatal(vl, "-some argument must be more"
2330 0
                                               "than 0 (found \"%s\")\n", *av);
2331 1
                } else if (!vstrcmp(*av, "-all"))
2332 0
                        loop = 1;
2333 0
                else
2334 0
                        break;
2335 16
        if (*av != NULL)
2336 0
                vtc_fatal(vl, "Unknown rxdata spec: %s\n", *av);
2337
2338 18
        do {
2339 18
                replace_frame(&f, rxstuff(s));
2340 18
                if (f == NULL)
2341 0
                        break;
2342 18
                rcv++;
2343 18
                CHKFRAME(f->type, TYPE_DATA, rcv, "rxhdata");
2344 18
        } while (rcv < times || (loop && !(f->flags & END_STREAM)));
2345 16
        replace_frame(&s->frame, f);
2346 16
}
2347
2348
/* SECTION: stream.spec.data_10 rxreq, rxresp
2349
 *
2350
 * These are two convenience functions to receive headers and body of an
2351
 * incoming request or response. The only difference is that rxreq can only be
2352
 * by a server, and rxresp by a client.
2353
 *
2354
 */
2355
2356
#define cmd_rxreq       cmd_rxmsg
2357
#define cmd_rxresp      cmd_rxmsg
2358
2359
static void
2360
cmd_rxmsg(CMD_ARGS)
2361 185
{
2362 185
        struct stream *s;
2363 185
        struct frame *f = NULL;
2364 185
        int end_stream;
2365 185
        int rcv = 0;
2366
2367 185
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2368
2369 185
        if (!vstrcmp(av[0], "rxreq"))
2370 47
                ONLY_H2_SERVER(s->hp, av);
2371 138
        else
2372 138
                ONLY_H2_CLIENT(s->hp, av);
2373
2374 187
        do {
2375 187
                replace_frame(&f, rxstuff(s));
2376 187
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2377 187
                if (f == NULL)
2378 0
                        return;
2379 187
        } while (f->type == TYPE_WINDOW_UPDATE);
2380
2381 185
        rcv++;
2382 185
        CHKFRAME(f->type, TYPE_HEADERS, rcv, *av);
2383
2384 185
        end_stream = f->flags & END_STREAM;
2385
2386 191
        while (!(f->flags & END_HEADERS)) {
2387 6
                replace_frame(&f, rxstuff(s));
2388 6
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2389 6
                if (f == NULL)
2390 0
                        return;
2391 6
                rcv++;
2392 6
                CHKFRAME(f->type, TYPE_CONTINUATION, rcv, *av);
2393 6
        }
2394
2395 366
        while (!end_stream) {
2396 181
                replace_frame(&f, rxstuff(s));
2397 181
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2398 181
                if (f == NULL)
2399 0
                        break;
2400 181
                rcv++;
2401 181
                CHKFRAME(f->type, TYPE_DATA, rcv, *av);
2402 181
                end_stream = f->flags & END_STREAM;
2403 181
        }
2404 185
        replace_frame(&s->frame, f);
2405 185
}
2406
2407
/* SECTION: stream.spec.data_12 rxpush
2408
 *
2409
 * This works like ``rxhdrs``, expecting a PUSH frame and then zero or more
2410
 * CONTINUATION frames.
2411
 *
2412
 * \-all
2413
 *      Keep waiting for CONTINUATION frames until END_HEADERS flag is seen.
2414
 *
2415
 * \-some INT
2416
 *      Retrieve INT - 1 CONTINUATION frames after the PUSH frame.
2417
 *
2418
 */
2419
static void
2420
cmd_rxpush(CMD_ARGS)
2421 1
{
2422 1
        struct stream *s;
2423 1
        struct frame *f = NULL;
2424 1
        char *p;
2425 1
        int loop = 0;
2426 1
        unsigned long int times = 1;
2427 1
        unsigned rcv = 0;
2428 1
        enum h2_type_e expect = TYPE_PUSH_PROMISE;
2429
2430 1
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2431
2432 1
        while (*++av) {
2433 0
                if (!vstrcmp(*av, "-some")) {
2434 0
                        STRTOU32_CHECK(times, av, p, vl, "-some", 0);
2435 0
                        if (!times)
2436 0
                                vtc_fatal(vl, "-some argument must be more"
2437 0
                                               "than 0 (found \"%s\")\n", *av);
2438 0
                } else if (!vstrcmp(*av, "-all")) {
2439 0
                        loop = 1;
2440 0
                } else
2441 0
                        break;
2442 0
        }
2443 1
        if (*av != NULL)
2444 0
                vtc_fatal(vl, "Unknown rxpush spec: %s\n", *av);
2445
2446 1
        do {
2447 1
                f = rxstuff(s);
2448 1
                if (!f)
2449 0
                        return;
2450 1
                rcv++;
2451 1
                CHKFRAME(f->type, expect, rcv, "rxpush");
2452 1
                expect = TYPE_CONTINUATION;
2453 1
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2454 1
        s->frame = f;
2455 1
}
2456
2457
/* SECTION: stream.spec.winup_rxwinup rxwinup
2458
 *
2459
 * Receive a WINDOW_UPDATE frame.
2460
 */
2461
static void
2462
cmd_rxwinup(CMD_ARGS)
2463 25
{
2464 25
        struct stream *s;
2465 25
        struct frame *f;
2466
2467 25
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2468 25
        s->frame = rxstuff(s);
2469 25
        CAST_OBJ_NOTNULL(f, s->frame, FRAME_MAGIC);
2470 25
        CHKFRAME(f->type, TYPE_WINDOW_UPDATE, 0, *av);
2471 25
        if (s->id == 0)
2472 6
                s->hp->h2_win_peer->size += s->frame->md.winup_size;
2473 25
        s->win_peer += s->frame->md.winup_size;
2474 25
}
2475
2476
/* SECTION: stream.spec.settings_rxsettings rxsettings
2477
 *
2478
 * Receive a SETTINGS frame.
2479
 */
2480
static void
2481
cmd_rxsettings(CMD_ARGS)
2482 423
{
2483 423
        struct stream *s, *s2;
2484 423
        uint32_t val = 0;
2485 423
        struct http *hp;
2486 423
        struct frame *f;
2487
2488 423
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2489 423
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
2490 423
        s->frame = rxstuff(s);
2491 423
        CAST_OBJ_NOTNULL(f, s->frame, FRAME_MAGIC);
2492 423
        CHKFRAME(f->type, TYPE_SETTINGS, 0, *av);
2493 423
        if (! isnan(f->md.settings[SETTINGS_INITIAL_WINDOW_SIZE])) {
2494 139
                val = (uint32_t)f->md.settings[SETTINGS_INITIAL_WINDOW_SIZE];
2495 139
                VTAILQ_FOREACH(s2, &hp->streams, list)
2496 146
                        s2->win_peer += (val - hp->h2_win_peer->init);
2497 139
                hp->h2_win_peer->init = val;
2498 139
        }
2499 423
}
2500
/* SECTION: stream.spec.prio_rxprio rxprio
2501
 *
2502
 * Receive a PRIORITY frame.
2503
 */
2504
static void
2505
cmd_rxprio (CMD_ARGS)
2506 9
{
2507 9
        struct stream *s;
2508 9
        (void)av;
2509 9
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2510 9
        if (s->hp->no_rfc7540_priorities) {
2511 6
                vtc_log(vl, 4, "skipping rxprio: no_rfc7540_priorities is set");
2512 6
                return;
2513 6
        }
2514 3
        s->frame = rxstuff(s);
2515 3
        if (s->frame != NULL && s->frame->type != TYPE_PRIORITY) {
2516 0
                vtc_fatal(vl,
2517 0
                    "Wrong frame type %s (%d) wanted %s",
2518 0
                    s->frame->type < TYPE_MAX ?
2519 0
                    h2_types[s->frame->type] : "?",
2520 0
                    s->frame->type, "PRIORITY");
2521 0
        }
2522 3
}
2523
2524
#define RXFUNC(lctype, upctype) \
2525
        static void \
2526
        cmd_rx ## lctype(CMD_ARGS) \
2527 108
        { \
2528 108
                struct stream *s; \
2529 108
                (void)av; \
2530 108
                CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC); \
2531 108
                s->frame = rxstuff(s); \
2532 108
                if (s->frame != NULL && s->frame->type != TYPE_ ## upctype) { \
2533 0
                        vtc_fatal(vl, \
2534 0
                            "Wrong frame type %s (%d) wanted %s", \
2535 0
                            s->frame->type < TYPE_MAX ? \
2536 0
                            h2_types[s->frame->type] : "?", \
2537 0
                            s->frame->type, #upctype); \
2538 0
                } \
2539 108
        }
2540
2541
/* SECTION: stream.spec.reset_rxrst rxrst
2542
 *
2543
 * Receive a RST_STREAM frame.
2544
 */
2545 61
RXFUNC(rst,     RST_STREAM)
2546
2547
/* SECTION: stream.spec.ping_rxping rxping
2548
 *
2549
 * Receive a PING frame.
2550
 */
2551 6
RXFUNC(ping,    PING)
2552
2553
/* SECTION: stream.spec.goaway_rxgoaway rxgoaway
2554
 *
2555
 * Receive a GOAWAY frame.
2556
 */
2557 41
RXFUNC(goaway,  GOAWAY)
2558
2559
/* SECTION: stream.spec.frame_rxframe
2560
 *
2561
 * Receive a frame, any frame.
2562
 */
2563
static void
2564
cmd_rxframe(CMD_ARGS)
2565 0
{
2566 0
        struct stream *s;
2567
2568 0
        (void)vl;
2569 0
        (void)av;
2570 0
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2571 0
        if (rxstuff(s) == NULL)
2572 0
                vtc_fatal(s->vl, "No frame received");
2573 0
}
2574
2575
static void
2576
cmd_expect(CMD_ARGS)
2577 782
{
2578 782
        struct http *hp;
2579 782
        struct stream *s;
2580 782
        const char *lhs;
2581 782
        char *cmp;
2582 782
        const char *rhs;
2583 782
        char buf[20];
2584
2585 782
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2586 782
        hp = s->hp;
2587 782
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2588
2589 782
        AZ(vstrcmp(av[0], "expect"));
2590 782
        ARGN(vl, av, 1);
2591 782
        ARGN(vl, av, 2);
2592 782
        ARGN(vl, av, 3);
2593 782
        ARGZ(vl, av, 4);
2594
2595 782
        av++;
2596 782
        PTOK(pthread_mutex_lock(&hp->mtx));
2597 782
        lhs = cmd_var_resolve(s, av[0], buf);
2598 782
        cmp = av[1];
2599 782
        rhs = cmd_var_resolve(s, av[2], buf);
2600 782
        vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
2601 782
        PTOK(pthread_mutex_unlock(&hp->mtx));
2602 782
}
2603
2604
/* SECTION: stream.spec.gunzip gunzip
2605
 *
2606
 * Same as the ``gunzip`` command for HTTP/1.
2607
 */
2608
static void
2609
cmd_gunzip(CMD_ARGS)
2610 2
{
2611 2
        struct http *hp;
2612 2
        struct stream *s;
2613
2614 2
        (void)av;
2615 2
        (void)vl;
2616
2617 2
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2618 2
        hp = s->hp;
2619 2
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2620
2621 2
        vtc_gunzip(hp, s->body, &s->bodylen);
2622 2
}
2623
2624
/* SECTION: stream.spec.write_body
2625
 *
2626
 * write_body STRING
2627
 *      Same as the ``write_body`` command for HTTP/1.
2628
 */
2629
static void
2630
cmd_write_body(CMD_ARGS)
2631 2
{
2632 2
        struct stream *s;
2633
2634 2
        (void)vl;
2635 2
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2636 2
        AN(av[0]);
2637 2
        ARGN(vl, av, 1);
2638 2
        ARGZ(vl, av, 2);
2639 2
        AZ(vstrcmp(av[0], "write_body"));
2640 2
        if (VFIL_writefile(NULL, av[1], s->body, s->bodylen) != 0)
2641 0
                vtc_fatal(s->vl, "failed to write body: %s (%d)",
2642 0
                    strerror(errno), errno);
2643 2
}
2644
2645
/* SECTION: stream.spec Specification
2646
 *
2647
 * The specification of a stream follows the exact same rules as one for a
2648
 * client or a server.
2649
 */
2650
static const struct cmds stream_cmds[] = {
2651
#define CMD_STREAM(n) { CMDS_MAGIC, #n, cmd_##n },
2652
        /* spec */
2653
        CMD_STREAM(expect)
2654
        CMD_STREAM(gunzip)
2655
        CMD_STREAM(rxcont)
2656
        CMD_STREAM(rxdata)
2657
        CMD_STREAM(rxframe)
2658
        CMD_STREAM(rxgoaway)
2659
        CMD_STREAM(rxhdrs)
2660
        CMD_STREAM(rxping)
2661
        CMD_STREAM(rxprio)
2662
        CMD_STREAM(rxpush)
2663
        CMD_STREAM(rxreq)
2664
        CMD_STREAM(rxresp)
2665
        CMD_STREAM(rxrst)
2666
        CMD_STREAM(rxsettings)
2667
        CMD_STREAM(rxwinup)
2668
        CMD_STREAM(sendhex)
2669
        CMD_STREAM(txcont)
2670
        CMD_STREAM(txdata)
2671
        CMD_STREAM(txgoaway)
2672
        CMD_STREAM(txping)
2673
        CMD_STREAM(txprio)
2674
        CMD_STREAM(txpush)
2675
        CMD_STREAM(txreq)
2676
        CMD_STREAM(txresp)
2677
        CMD_STREAM(txrst)
2678
        CMD_STREAM(txsettings)
2679
        CMD_STREAM(txwinup)
2680
        CMD_STREAM(write_body)
2681
        { CMDS_MAGIC, NULL, NULL }
2682
#undef CMD_STREAM
2683
};
2684
2685
static void *
2686
stream_thread(void *priv)
2687 594
{
2688 594
        struct stream *s;
2689
2690 594
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2691 594
        parse_string(s->vl, s, s->spec);
2692 594
        vtc_log(s->vl, 2, "Ending stream %u", s->id);
2693 594
        return (NULL);
2694 594
}
2695
/**********************************************************************
2696
 * Allocate and initialize a stream
2697
 */
2698
2699
static struct stream *
2700
stream_new(const char *name, struct http *h)
2701 510
{
2702 510
        char *p, buf[20];
2703 510
        struct stream *s;
2704
2705 510
        if (!vstrcmp("next", name)) {
2706 21
                if (h->last_stream > 0)
2707 14
                        bprintf(buf, "%d", h->last_stream + 2);
2708 7
                else
2709 7
                        bprintf(buf, "%d", 1);
2710 21
                name = buf;
2711 21
        }
2712
2713 510
        ALLOC_OBJ(s, STREAM_MAGIC);
2714 510
        AN(s);
2715 510
        PTOK(pthread_cond_init(&s->cond, NULL));
2716 510
        REPLACE(s->name, name);
2717 510
        AN(s->name);
2718 510
        VTAILQ_INIT(&s->fq);
2719 510
        s->win_self = h->h2_win_self->init;
2720 510
        s->win_peer = h->h2_win_peer->init;
2721 510
        s->vl = vtc_logopen("%s.%s", h->sess->name, name);
2722 510
        vtc_log_set_cmd(s->vl, stream_cmds);
2723
2724 510
        s->weight = 16;
2725 510
        s->dependency = 0;
2726
2727 510
        STRTOU32(s->id, name, p, s->vl, "stream");
2728 510
        if (s->id & (1U << 31))
2729 0
                vtc_fatal(s->vl, "Stream id must be a 31-bits integer "
2730 0
                    "(found %s)", name);
2731
2732 510
        CHECK_OBJ_NOTNULL(h, HTTP_MAGIC);
2733 510
        s->hp = h;
2734 510
        h->last_stream = s->id;
2735
2736
        //bprintf(s->connect, "%s", "${v1_sock}");
2737 510
        PTOK(pthread_mutex_lock(&h->mtx));
2738 510
        VTAILQ_INSERT_HEAD(&h->streams, s, list);
2739 510
        PTOK(pthread_mutex_unlock(&h->mtx));
2740 510
        return (s);
2741 510
}
2742
2743
/**********************************************************************
2744
 * Clean up stream
2745
 */
2746
2747
static void
2748
stream_delete(struct stream *s)
2749 510
{
2750 510
        struct frame *f, *f2;
2751
2752 510
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2753
2754 510
        VTAILQ_FOREACH_SAFE(f, &s->fq, list, f2) {
2755 35
                VTAILQ_REMOVE(&s->fq, f, list);
2756 35
                clean_frame(&f);
2757 35
        }
2758 510
        vtc_logclose(s->vl);
2759 510
        clean_headers(s->req);
2760 510
        clean_headers(s->resp);
2761 510
        AZ(s->frame);
2762 510
        free(s->body);
2763 510
        free(s->spec);
2764 510
        free(s->name);
2765 510
        FREE_OBJ(s);
2766 510
}
2767
2768
/**********************************************************************
2769
 * Start the stream thread
2770
 */
2771
2772
static void
2773
stream_start(struct stream *s)
2774 594
{
2775 594
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2776 594
        vtc_log(s->hp->vl, 2, "Starting stream %s (%p)", s->name, s);
2777 594
        PTOK(pthread_create(&s->tp, NULL, stream_thread, s));
2778 594
        s->running = 1;
2779 594
}
2780
2781
/**********************************************************************
2782
 * Wait for stream thread to stop
2783
 */
2784
static void
2785
stream_wait(struct stream *s)
2786 594
{
2787 594
        void *res;
2788 594
        struct frame *f, *f2;
2789
2790 594
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2791 594
        vtc_log(s->hp->vl, 2, "Waiting for stream %u", s->id);
2792 594
        PTOK(pthread_join(s->tp, &res));
2793 594
        if (res != NULL)
2794 0
                vtc_fatal(s->hp->vl, "Stream %u returned \"%s\"", s->id,
2795 0
                    (char *)res);
2796
2797 594
        VTAILQ_FOREACH_SAFE(f, &s->fq, list, f2) {
2798 8
                VTAILQ_REMOVE(&s->fq, f, list);
2799 8
                clean_frame(&f);
2800 8
        }
2801 594
        clean_frame(&s->frame);
2802 594
        s->tp = 0;
2803 594
        s->running = 0;
2804 594
}
2805
2806
/**********************************************************************
2807
 * Run the stream thread
2808
 */
2809
2810
static void
2811
stream_run(struct stream *s)
2812 542
{
2813 542
        stream_start(s);
2814 542
        stream_wait(s);
2815 542
}
2816
2817
2818
2819
/* SECTION: client-server.spec.stream
2820
 *
2821
 * stream
2822
 *      HTTP/2 introduces the concept of streams, and these come with
2823
 *      their own specification, and as it's quite big, have been moved
2824
 *      to their own chapter.
2825
 *
2826
 * SECTION: stream stream
2827
 *
2828
 * (note: this section is at the top-level for easier navigation, but
2829
 * it's part of the client/server specification)
2830
 *
2831
 * Streams map roughly to a request in HTTP/2, a request is sent on
2832
 * stream N, the response too, then the stream is discarded. The main
2833
 * exception is the first stream, 0, that serves as coordinator.
2834
 *
2835
 * Stream syntax follow the client/server one::
2836
 *
2837
 *      stream ID [SPEC] [ACTION]
2838
 *
2839
 * ID is the HTTP/2 stream number, while SPEC describes what will be
2840
 * done in that stream. If ID has the value ``next``, the actual stream
2841
 * number is computed based on the last one.
2842
 *
2843
 * Note that, when parsing a stream action, if the entity isn't operating
2844
 * in HTTP/2 mode, these spec is ran before::
2845
 *
2846
 *      txpri/rxpri # client/server
2847
 *      stream 0 {
2848
 *          txsettings
2849
 *          rxsettings
2850
 *          txsettings -ack
2851
 *          rxsettings
2852
 *          expect settings.ack == true
2853
 *      } -run
2854
 *
2855
 * And HTTP/2 mode is then activated before parsing the specification.
2856
 *
2857
 * SECTION: stream.actions Actions
2858
 *
2859
 * \-start
2860
 *      Run the specification in a thread, giving back control immediately.
2861
 *
2862
 * \-wait
2863
 *      Wait for the started thread to finish running the spec.
2864
 *
2865
 * \-run
2866
 *      equivalent to calling ``-start`` then ``-wait``.
2867
 */
2868
2869
void
2870
cmd_stream(CMD_ARGS)
2871 618
{
2872 618
        struct stream *s;
2873 618
        struct http *h;
2874
2875 618
        (void)vl;
2876 618
        CAST_OBJ_NOTNULL(h, priv, HTTP_MAGIC);
2877
2878 618
        AZ(vstrcmp(av[0], "stream"));
2879 618
        av++;
2880
2881 618
        VTAILQ_FOREACH(s, &h->streams, list)
2882 710
                if (!vstrcmp(s->name, av[0]))
2883 107
                        break;
2884 618
        if (s == NULL)
2885 510
                s = stream_new(av[0], h);
2886 618
        av++;
2887
2888 1828
        for (; *av != NULL; av++) {
2889 1210
                if (vtc_error)
2890 0
                        break;
2891
2892 1210
                if (!vstrcmp(*av, "-wait")) {
2893 25
                        stream_wait(s);
2894 25
                        continue;
2895 25
                }
2896
2897
                /* Don't muck about with a running client */
2898 1185
                if (s->running)
2899 0
                        stream_wait(s);
2900
2901 1185
                if (!vstrcmp(*av, "-start")) {
2902 52
                        stream_start(s);
2903 52
                        continue;
2904 52
                }
2905 1133
                if (!vstrcmp(*av, "-run")) {
2906 542
                        stream_run(s);
2907 542
                        continue;
2908 542
                }
2909 591
                if (**av == '-')
2910 0
                        vtc_fatal(vl, "Unknown stream argument: %s", *av);
2911 591
                REPLACE(s->spec, *av);
2912 591
        }
2913 618
}
2914
2915
void
2916
b64_settings(struct http *hp, const char *s)
2917 3
{
2918 3
        uint16_t i;
2919 3
        uint64_t v, vv;
2920 3
        const char *buf;
2921 3
        int shift;
2922
2923 12
        while (*s) {
2924 9
                v = 0;
2925 81
                for (shift = 42; shift >= 0; shift -= 6) {
2926 72
                        if (*s >= 'A' && *s <= 'Z')
2927 60
                                vv = (*s - 'A');
2928 12
                        else if (*s >= 'a' && *s <= 'z')
2929 6
                                vv = (*s - 'a') + 26;
2930 6
                        else if (*s >= '0' && *s <= '9')
2931 0
                                vv = (*s - '0') + 52;
2932 6
                        else if (*s == '-')
2933 0
                                vv = 62;
2934 6
                        else if (*s == '_')
2935 6
                                vv = 63;
2936 0
                        else
2937 0
                                vtc_fatal(hp->vl,
2938 0
                                    "Bad \"HTTP2-Settings\" header");
2939 72
                        v |= vv << shift;
2940 72
                        s++;
2941 72
                }
2942 9
                i = v >> 32;
2943 9
                v &= 0xffff;
2944
2945 9
                if (i <= SETTINGS_MAX)
2946 9
                        buf = h2_settings[i];
2947 0
                else
2948 0
                        buf = "unknown";
2949
2950 9
                if (i == SETTINGS_NO_RFC7540_PRIORITIES) {
2951 3
                        hp->no_rfc7540_priorities = v;
2952 3
                }
2953 9
                if (i == SETTINGS_HEADER_TABLE_SIZE) {
2954 0
                        enum hpk_result hrs;
2955 0
                        if (hp->sfd) {
2956 0
                                AN(hp->encctx);
2957 0
                                hrs = HPK_ResizeTbl(hp->encctx, v);
2958 0
                        } else {
2959 0
                                AN(hp->decctx);
2960 0
                                hrs = HPK_ResizeTbl(hp->decctx, v);
2961 0
                        }
2962 0
                        if (hrs != hpk_done)
2963 0
                                vtc_fatal(hp->vl, "HPK resize failed %d\n", hrs);
2964 0
                }
2965
2966 9
                vtc_log(hp->vl, 4, "Upgrade: %s (%d): %ju",
2967 9
                    buf, i, (intmax_t)v);
2968 9
        }
2969 3
}
2970
2971
void
2972
start_h2(struct http *hp)
2973 215
{
2974 215
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2975 215
        PTOK(pthread_mutex_init(&hp->mtx, NULL));
2976 215
        PTOK(pthread_cond_init(&hp->cond, NULL));
2977 215
        VTAILQ_INIT(&hp->streams);
2978 215
        hp->h2_win_self->init = 0xffff;
2979 215
        hp->h2_win_self->size = 0xffff;
2980 215
        hp->h2_win_peer->init = 0xffff;
2981 215
        hp->h2_win_peer->size = 0xffff;
2982 215
        hp->h2 = 1;
2983
2984 215
        hp->decctx = HPK_NewCtx(4096);
2985 215
        hp->encctx = HPK_NewCtx(4096);
2986 215
        PTOK(pthread_create(&hp->tp, NULL, receive_frame, hp));
2987 215
}
2988
2989
void
2990
stop_h2(struct http *hp)
2991 215
{
2992 215
        struct stream *s, *s2;
2993
2994 215
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2995 510
        VTAILQ_FOREACH_SAFE(s, &hp->streams, list, s2) {
2996 510
                if (s->running)
2997 27
                        stream_wait(s);
2998 510
                PTOK(pthread_mutex_lock(&hp->mtx));
2999 510
                VTAILQ_REMOVE(&hp->streams, s, list);
3000 510
                PTOK(pthread_mutex_unlock(&hp->mtx));
3001 510
                stream_delete(s);
3002 510
        }
3003
3004 215
        PTOK(pthread_mutex_lock(&hp->mtx));
3005 215
        hp->h2 = 0;
3006 215
        PTOK(pthread_cond_signal(&hp->cond));
3007 215
        PTOK(pthread_mutex_unlock(&hp->mtx));
3008 215
        PTOK(pthread_join(hp->tp, NULL));
3009
3010 215
        HPK_FreeCtx(hp->decctx);
3011 215
        HPK_FreeCtx(hp->encctx);
3012
3013 215
        PTOK(pthread_mutex_destroy(&hp->mtx));
3014 215
        PTOK(pthread_cond_destroy(&hp->cond));
3015 215
}