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