varnish-cache/bin/varnishtest/vtest2/src/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 759
#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 752
#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 710
#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 709
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 706
#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 706
        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 704
        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 700
        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 698
        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 695
119
        int                     dependency;
120
        int                     weight;
121
};
122
123
static void
124 1193
clean_headers(struct hpk_hdr *h)
125
{
126 1193
        unsigned n = MAX_HDR;
127 692
128 2343
        while (h->t && n > 0) {
129 1150
                if (h->key.len)
130 1148
                        free(h->key.ptr);
131 1150
                if (h->value.len)
132 1150
                        free(h->value.ptr);
133 1150
                memset(h, 0, sizeof(*h));
134 1150
                h++;
135
                n--;
136 1885
        }
137
}
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 687
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 57
static void
154 687
http_write(const struct http *hp, int lvl,
155
    const char *buf, int s, const char *pfx)
156
{
157
        ssize_t l;
158 57
159 57
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
160 57
        AN(buf);
161
        AN(pfx);
162 57
163 57
        vtc_dump(hp->vl, lvl, pfx, buf, s);
164 57
        l = write(hp->sess->fd, buf, s);
165 0
        if (l != s)
166 0
                vtc_log(hp->vl, hp->fatal, "Write failed: (%zd vs %d) %s",
167 57
                    l, s, strerror(errno));
168
}
169
170 1502
static int
171
get_bytes(const struct http *hp, char *buf, size_t n)
172
{
173
        int i;
174
        struct pollfd pfd[1];
175 1502
176 1502
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
177
        AN(buf);
178 3005
179 1503
        while (n > 0) {
180 1503
                pfd[0].fd = hp->sess->fd;
181 1503
                pfd[0].events = POLLIN;
182 1503
                pfd[0].revents = 0;
183 1503
                i = poll(pfd, 1, (int)(hp->timeout * 1000));
184 0
                if (i < 0 && errno == EINTR)
185 1503
                        continue;
186 0
                if (i == 0)
187
                        vtc_log(hp->vl, 3,
188 0
                            "HTTP2 rx timeout (fd:%d %.3fs)",
189 1503
                            hp->sess->fd, hp->timeout);
190 0
                if (i < 0)
191
                        vtc_log(hp->vl, 3,
192 0
                            "HTTP2 rx failed (fd:%d poll: %s)",
193 1503
                            hp->sess->fd, strerror(errno));
194 0
                if (i <= 0)
195 1503
                        return (i);
196 1503
                i = read(hp->sess->fd, buf, n);
197 0
                if (!(pfd[0].revents & POLLIN))
198
                        vtc_log(hp->vl, 4,
199 0
                            "HTTP2 rx poll (fd:%d revents: %x n=%zu, i=%d)",
200 1503
                            hp->sess->fd, pfd[0].revents, n, i);
201 0
                if (i == 0)
202
                        vtc_log(hp->vl, 3,
203 0
                            "HTTP2 rx EOF (fd:%d read: %s)",
204 1503
                            hp->sess->fd, strerror(errno));
205 0
                if (i < 0)
206
                        vtc_log(hp->vl, 3,
207 0
                            "HTTP2 rx failed (fd:%d read: %s)",
208 1503
                            hp->sess->fd, strerror(errno));
209 0
                if (i <= 0)
210 1503
                        return (i);
211
                n -= i;
212 1502
        }
213
        return (1);
214 1502
215
}
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 893
static void
254
readFrameHeader(struct frame *f, const char *buf)
255 893
{
256 893
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
257
        AN(buf);
258 893
259 893
        f->size  = (unsigned char)buf[0] << 16;
260 893
        f->size += (unsigned char)buf[1] << 8;
261
        f->size += (unsigned char)buf[2];
262 893
263
        f->type = (unsigned char)buf[3];
264 893
265
        f->flags = (unsigned char)buf[4];
266 893
267 893
        f->stid  = vbe32dec(buf+5);
268
}
269
270 2109
static void
271
writeFrameHeader(char *buf, const struct frame *f)
272 2109
{
273 2109
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
274 2109
        AN(buf);
275 2109
        buf[0] = (f->size >> 16) & 0xff;
276 2109
        buf[1] = (f->size >>  8) & 0xff;
277
        buf[2] = (f->size      ) & 0xff;
278 2109
279
        buf[3] = f->type;
280 2109
281
        buf[4] = f->flags;
282 2109
283 2109
        vbe32enc(buf + 5, f->stid);
284
}
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 2007
static void
297
replace_frame(struct frame **fp, struct frame *new)
298
{
299
        struct frame *old;
300 2007
301 2007
        AN(fp);
302
        CHECK_OBJ_ORNULL(new, FRAME_MAGIC);
303 2007
304 2007
        old = *fp;
305 2007
        *fp = new;
306 1111
        if (old == NULL)
307
                return;
308 896
309 896
        CHECK_OBJ(old, FRAME_MAGIC);
310 39
        if (old->type == TYPE_GOAWAY)
311 896
                free(old->md.goaway.debug);
312 896
        free(old->data);
313 2007
        FREE_OBJ(old);
314
}
315
316 1475
static void
317
clean_frame(struct frame **fp)
318
{
319 1475
320 1475
        replace_frame(fp, NULL);
321
}
322
323 2109
static void
324
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 2109
330 2109
        CHECK_OBJ_NOTNULL(sp, STREAM_MAGIC);
331 2109
        hp = sp->hp;
332 2109
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
333
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
334 2109
335
        writeFrameHeader(hdr, f);
336 4218
337
        vtc_log(sp->vl, 3,
338 2109
            "tx: stream: %d, type: %s (%d), flags: 0x%02x, size: %d",
339 2109
            f->stid,
340 2109
            f->type < TYPE_MAX ? h2_types[f->type] : "?",
341
            f->type, f->flags, f->size);
342 2109
343 336
        if (f->type == TYPE_DATA) {
344 336
                sp->win_peer -= f->size;
345 336
                hp->h2_win_peer->size -= f->size;
346
        }
347 2109
348 1697
        if (lock)
349 2109
                PTOK(pthread_mutex_lock(&hp->mtx));
350 2109
        l = write(hp->sess->fd, hdr, sizeof(hdr));
351 1974
        if (l != sizeof(hdr))
352 987
                vtc_log(sp->vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
353
                    l, sizeof(hdr), strerror(errno));
354 2109
355 1706
        if (f->size) {
356 1706
                AN(f->data);
357 1706
                l = write(hp->sess->fd, f->data, f->size);
358 1974
                if (l != f->size)
359
                        vtc_log(sp->vl, hp->fatal,
360 987
                                        "Write failed: (%zd vs %d) %s",
361 1706
                                        l, f->size, strerror(errno));
362 2109
        }
363 1697
        if (lock)
364 2109
                PTOK(pthread_mutex_unlock(&hp->mtx));
365
}
366
367 6
static void
368
exclusive_stream_dependency(const struct stream *s)
369
{
370 6
        struct stream *target;
371
        struct http *hp = s->hp;
372 6
373 0
        if (s->id == 0)
374
                return;
375 28
376 22
        VTAILQ_FOREACH(target, &hp->streams, list) {
377 8
                if (target->id != s->id && target->dependency == s->dependency)
378 22
                        target->dependency = s->id;
379 6
        }
380
}
381
382 896
static void
383
explain_flags(uint8_t flags, uint8_t type, struct vtclog *vl)
384 896
{
385 212
        if (flags & ACK && (type == TYPE_PING || type == TYPE_SETTINGS)) {
386 945
                vtc_log(vl, 3, "flag: ACK");
387 49
        } else if (flags & END_STREAM && (type == TYPE_HEADERS ||
388 195
            type == TYPE_PUSH_PROMISE || type == TYPE_DATA)) {
389 690
                vtc_log(vl, 3, "flag: END_STREAM");
390 7
        } else if (flags & END_HEADERS && (type == TYPE_HEADERS ||
391 58
            type == TYPE_PUSH_PROMISE || type == TYPE_CONTINUATION)) {
392 489
                vtc_log(vl, 3, "flag: END_TYPE_HEADERS");
393 0
        } else if (flags & PRIORITY && (type == TYPE_HEADERS ||
394 0
            type == TYPE_PUSH_PROMISE)) {
395 431
                vtc_log(vl, 3, "flag: END_PRIORITY");
396 0
        } else if (flags & PADDED && (type == TYPE_DATA || type ==
397 1
            TYPE_HEADERS || type == TYPE_PUSH_PROMISE)) {
398 431
                vtc_log(vl, 3, "flag: PADDED");
399 0
        } else if (flags)
400 896
                vtc_log(vl, 3, "UNKNOWN FLAG(S): 0x%02x", flags);
401
}
402
403 100
static void
404
parse_data(struct stream *s, struct frame *f)
405
{
406 100
        struct http *hp;
407 100
        uint32_t size = f->size;
408
        char *data = f->data;
409 100
410 100
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
411 100
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
412
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
413 100
414 4
        if (f->flags & PADDED) {
415 4
                f->md.padded = *((uint8_t *)data);
416 0
                if (f->md.padded >= size) {
417
                        vtc_log(s->vl, hp->fatal,
418
                                        "invalid padding: %d reported,"
419 0
                                        "but size is only %d",
420 0
                                        f->md.padded, size);
421 0
                        size = 0;
422 0
                        f->md.padded = 0;
423 4
                }
424 4
                data++;
425 4
                size -= f->md.padded + 1;
426 4
                vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
427
        }
428 100
429 98
        if (s->id)
430
                s->win_self -= size;
431 100
432
        s->hp->h2_win_self->size -= size;
433 100
434 28
        if (!size) {
435 28
                AZ(data);
436 28
                vtc_log(s->vl, 4, "s%u - no data", s->id);
437
                return;
438
        }
439 72
440 72
        s->body = realloc(s->body, s->bodylen + size + 1L);
441 72
        AN(s->body);
442 72
        memcpy(s->body + s->bodylen, data, size);
443 72
        s->bodylen += size;
444 100
        s->body[s->bodylen] = '\0';
445
}
446
447 199
static void
448
decode_hdr(struct http *hp, struct hpk_hdr *h, const struct vsb *vsb)
449
{
450 199
        struct hpk_iter *iter;
451
        enum hpk_result r = hpk_err;
452
        int n;
453 199
454 199
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
455
        CAST_OBJ_NOTNULL(hp, hp, HTTP_MAGIC);;
456 199
457 0
        if (VSB_len(vsb) == 0)
458
                return;
459 199
460
        iter = HPK_NewIter(hp->decctx, VSB_data(vsb), VSB_len(vsb));
461 199
462 199
        n = 0;
463 0
        while (n < MAX_HDR && h[n].t)
464 1150
                n++;
465 1150
        while (n < MAX_HDR) {
466 1150
                r = HPK_DecHdr(iter, h + n);
467 2
                if (r == hpk_err )
468 2296
                        break;
469 1148
                vtc_log(hp->vl, 4, "header[%2d]: %s: %s",
470 1148
                    n, h[n].key.ptr, h[n].value.ptr);
471 1148
                n++;
472 197
                if (r == hpk_done)
473
                        break;
474
        }
475 199
476 4
        if (r != hpk_done) {
477 2
                vtc_log(hp->vl, hp->fatal ? 4 : 0,
478 199
                    "Header decoding failed (%d) %d", r, hp->fatal);
479 0
        } else if (n == MAX_HDR) {
480
                vtc_log(hp->vl, hp->fatal,
481 0
                    "Max number of headers reached (%d)", MAX_HDR);
482
        }
483 199
484 199
        HPK_FreeIter(iter);
485
}
486
487 213
static void
488
parse_hdr(struct stream *s, struct frame *f, struct vsb *vsb)
489 213
{
490 213
        int shift = 0;
491 213
        int exclusive = 0;
492 213
        uint32_t size = f->size;
493
        char *data = f->data;
494
        struct http *hp;
495
        uint32_t n;
496 213
497 213
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
498 213
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
499 213
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
500
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
501 213
502 2
        if (f->flags & PADDED && f->type != TYPE_CONTINUATION) {
503 2
                f->md.padded = *((uint8_t *)data);
504 0
                if (f->md.padded >= size) {
505
                        vtc_log(s->vl, hp->fatal,
506
                                        "invalid padding: %d reported,"
507 0
                                        "but size is only %d",
508 0
                                        f->md.padded, size);
509 0
                        size = 0;
510 0
                        f->md.padded = 0;
511 2
                }
512 2
                shift += 1;
513 2
                size -= f->md.padded;
514 2
                vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
515
        }
516 213
517 2
        if (f->type == TYPE_HEADERS && f->flags & PRIORITY){
518 2
                shift += 5;
519 2
                n = vbe32dec(f->data);
520 2
                s->dependency = n & ~(1U << 31);
521
                exclusive = n >> 31;
522 2
523 2
                s->weight = f->data[4];
524 2
                if (exclusive)
525
                        exclusive_stream_dependency(s);
526 2
527 2
                vtc_log(s->vl, 4, "stream->dependency: %u", s->dependency);
528 213
                vtc_log(s->vl, 4, "stream->weight: %u", s->weight);
529 1
        } else if (f->type == TYPE_PUSH_PROMISE){
530 1
                shift += 4;
531 1
                n = vbe32dec(f->data);
532 1
                f->md.promised = n & ~(1U << 31);
533
        }
534 213
535 213
        AZ(VSB_bcat(vsb, data + shift, size - shift));
536
}
537
538 3
static void
539
parse_prio(struct stream *s, struct frame *f)
540
{
541
        struct http *hp;
542
        char *buf;
543
        uint32_t n;
544 3
545 3
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
546 3
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
547
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
548 3
549 0
        if (f->size != 5)
550
                vtc_fatal(s->vl, "Size should be 5, but isn't (%d)", f->size);
551 3
552 3
        buf = f->data;
553
        AN(buf);
554 3
555 3
        n = vbe32dec(f->data);
556
        f->md.prio.stream = n & ~(1U << 31);
557 3
558 3
        s->dependency = f->md.prio.stream;
559 1
        if (n >> 31){
560 1
                f->md.prio.exclusive = 1;
561 1
                exclusive_stream_dependency(s);
562
        }
563 3
564 3
        buf += 4;
565 3
        f->md.prio.weight = *buf;
566
        s->weight = f->md.prio.weight;
567 3
568 3
        vtc_log(s->vl, 3, "prio->stream: %u", f->md.prio.stream);
569 3
        vtc_log(s->vl, 3, "prio->weight: %u", f->md.prio.weight);
570
}
571
572 54
static void
573
parse_rst(const struct stream *s, struct frame *f)
574
{
575
        struct http *hp;
576
        uint32_t err;
577 54
        const char *buf;
578 54
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
579 54
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
580
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
581 54
582 0
        if (f->size != 4)
583
                vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
584 54
585 54
        err = vbe32dec(f->data);
586
        f->md.rst_err = err;
587 54
588 54
        vtc_log(s->vl, 2, "ouch");
589 54
        if (err <= ERR_MAX)
590
                buf = h2_errs[err];
591 0
        else
592 54
                buf = "unknown";
593
        vtc_log(s->vl, 4, "rst->err: %s (%d)", buf, err);
594 54
595
}
596
597 415
static void
598
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 415
        enum hpk_result r;
605 415
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
606 415
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
607
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
608 415
609 0
        if (f->size % 6)
610 0
                vtc_fatal(s->vl,
611
                    "Size should be a multiple of 6, but isn't (%d)", f->size);
612 415
613 0
        if (s->id != 0)
614 0
                vtc_fatal(s->vl,
615
                    "Setting frames should only be on stream 0, but received on stream: %d", s->id);
616 7885
617 7470
        for (u = 0; u <= SETTINGS_MAX; u++)
618
                f->md.settings[u] = NAN;
619 853
620 438
        for (u = 0; u < f->size;) {
621 438
                t = vbe16dec(f->data + u);
622 438
                u += 2;
623 438
                v = vbe32dec(f->data + u);
624 438
                if (t <= SETTINGS_MAX) {
625 438
                        buf = h2_settings[t];
626 438
                        f->md.settings[t] = v;
627 0
                } else
628 438
                        buf = "unknown";
629
                u += 4;
630 438
631 5
                if (t == SETTINGS_HEADER_TABLE_SIZE) {
632 5
                        r = HPK_ResizeTbl(s->hp->encctx, v);
633 5
                        assert(r == hpk_done);
634
                }
635 438
636
                vtc_log(s->vl, 4, "settings->%s (%u): %d", buf, t, v);
637
        }
638 415
639
}
640
641 6
static void
642
parse_ping(const struct stream *s, struct frame *f)
643
{
644 6
        struct http *hp;
645 6
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
646 6
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
647 6
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
648 0
        if (f->size != 8)
649 6
                vtc_fatal(s->vl, "Size should be 8, but isn't (%d)", f->size);
650 6
        f->md.ping.ack = f->flags & ACK;
651 6
        memcpy(f->md.ping.data, f->data, 8);
652
        f->md.ping.data[8] = '\0';
653 6
654
        vtc_log(s->vl, 4, "ping->data: %s", f->md.ping.data);
655 6
656
}
657
658 39
static void
659
parse_goaway(const struct stream *s, struct frame *f)
660
{
661
        struct http *hp;
662
        const char *err_buf;
663 39
        uint32_t err, stid;
664 39
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
665 39
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
666
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
667 39
668 0
        if (f->size < 8)
669 0
                vtc_fatal(s->vl,
670 39
                    "Size should be at least 8, but isn't (%d)", f->size);
671 0
        if (f->data[0] & (1<<7))
672
                vtc_fatal(s->vl,
673
                    "First bit of data is reserved and should be 0");
674 39
675 39
        stid = vbe32dec(f->data);
676 39
        err = vbe32dec(f->data + 4);
677 39
        f->md.goaway.err = err;
678
        f->md.goaway.stream = stid;
679 39
680 39
        if (err <= ERR_MAX)
681
                err_buf = h2_errs[err];
682 0
        else
683
                err_buf = "unknown";
684 39
685 2
        if (f->size > 8) {
686 2
                f->md.goaway.debug = malloc((f->size - 8) + 1L);
687 2
                AN(f->md.goaway.debug);
688
                f->md.goaway.debug[f->size - 8] = '\0';
689 2
690 2
                memcpy(f->md.goaway.debug, f->data + 8, f->size - 8);
691
        }
692 39
693 39
        vtc_log(s->vl, 3, "goaway->laststream: %d", stid);
694 39
        vtc_log(s->vl, 3, "goaway->err: %s (%d)", err_buf, err);
695 2
        if (f->md.goaway.debug)
696 39
                vtc_log(s->vl, 3, "goaway->debug: %s", f->md.goaway.debug);
697
}
698
699 66
static void
700
parse_winup(const struct stream *s, struct frame *f)
701
{
702
        struct http *hp;
703 66
        uint32_t size;
704 66
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
705 66
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
706
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
707 66
708 0
        if (f->size != 4)
709 66
                vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
710 0
        if (f->data[0] & (1<<7))
711
                vtc_log(s->vl, s->hp->fatal,
712
                    "First bit of data is reserved and should be 0");
713 66
714 66
        size = vbe32dec(f->data);
715
        f->md.winup_size = size;
716 66
717 66
        vtc_log(s->vl, 3, "winup->size: %d", size);
718
}
719
720
/* read a frame and queue it in the relevant stream, wait if not present yet.
721
 */
722 211
static void *
723
receive_frame(void *priv)
724
{
725
        struct http *hp;
726
        char hdr[9];
727
        struct frame *f;
728 211
        struct stream *s;
729 211
        int expect_cont = 0;
730 211
        struct vsb *vsb = NULL;
731
        struct hpk_hdr *hdrs = NULL;
732 211
733
        CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
734 211
735 2151
        PTOK(pthread_mutex_lock(&hp->mtx));
736
        while (hp->h2) {
737 1940
                /*no wanted frames? */
738 1940
                assert(hp->wf >= 0);
739 1044
                if (hp->wf == 0) {
740 1044
                        PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
741
                        continue;
742 896
                }
743
                PTOK(pthread_mutex_unlock(&hp->mtx));
744 896
745 0
                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
                        vtc_log(hp->vl, hp->fatal,
751 0
                            "could not get frame header");
752
                        return (NULL);
753 896
                }
754 896
                ALLOC_OBJ(f, FRAME_MAGIC);
755 896
                AN(f);
756
                readFrameHeader(f, hdr);
757 1792
758
                vtc_log(hp->vl, 3, "rx: stream: %d, type: %s (%d), "
759 896
                                "flags: 0x%02x, size: %d",
760 896
                                f->stid,
761 896
                                f->type < TYPE_MAX ? h2_types[f->type] : "?",
762 896
                                f->type, f->flags, f->size);
763
                explain_flags(f->flags, f->type, hp->vl);
764 896
765 607
                if (f->size) {
766 607
                        f->data = malloc(f->size + 1L);
767 607
                        AN(f->data);
768 607
                        f->data[f->size] = '\0';
769 0
                        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
                                vtc_log(hp->vl, hp->fatal,
776 0
                                    "could not get frame body");
777
                                return (NULL);
778 607
                        }
779
                }
780
781 896
                /* is the corresponding stream waiting? */
782 896
                PTOK(pthread_mutex_lock(&hp->mtx));
783 1792
                s = NULL;
784 1027
                while (!s) {
785 1027
                        VTAILQ_FOREACH(s, &hp->streams, list)
786 896
                                if (s->id == f->stid)
787 896
                                        break;
788 0
                        if (!s)
789 896
                                PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
790 0
                        if (!hp->h2) {
791 0
                                clean_frame(&f);
792 0
                                PTOK(pthread_mutex_unlock(&hp->mtx));
793
                                return (NULL);
794
                        }
795 896
                }
796
                PTOK(pthread_mutex_unlock(&hp->mtx));
797 896
798 910
                AN(s);
799 14
                if (expect_cont &&
800 0
                    (f->type != TYPE_CONTINUATION || expect_cont != s->id))
801 0
                        vtc_fatal(s->vl, "Expected CONTINUATION frame for "
802
                            "stream %u", expect_cont);
803
804 896
                /* parse the frame according to it type, and fill the metadata */
805
                switch (f->type) {
806 100
                        case TYPE_DATA:
807 100
                                parse_data(s, f);
808
                                break;
809 1
                        case TYPE_PUSH_PROMISE:
810
                                hdrs = s->req;
811
                                /*FALLTHROUGH*/
812 199
                        case TYPE_HEADERS:
813 198
                                if (!hdrs) {
814 47
                                        if (hp->sfd)
815
                                                hdrs = s->req;
816 151
                                        else
817 198
                                                hdrs = s->resp;
818 199
                                }
819 199
                                clean_headers(hdrs);
820 199
                                hdrs[0].t = hpk_unset;
821 199
                                AZ(vsb);
822
                                vsb = VSB_new_auto();
823
                                /*FALLTHROUGH*/
824 213
                        case TYPE_CONTINUATION:
825 213
                                AN(hdrs);
826 213
                                expect_cont = s->id;
827 213
                                parse_hdr(s, f, vsb);
828 199
                                if (f->flags & END_HEADERS) {
829 199
                                        expect_cont = 0;
830 199
                                        AZ(VSB_finish(vsb));
831 199
                                        decode_hdr(hp, hdrs, vsb);
832 199
                                        VSB_destroy(&vsb);
833 199
                                        hdrs = NULL;
834 213
                                }
835
                                break;
836 3
                        case TYPE_PRIORITY:
837 3
                                parse_prio(s, f);
838
                                break;
839 54
                        case TYPE_RST_STREAM:
840 54
                                parse_rst(s, f);
841
                                break;
842 415
                        case TYPE_SETTINGS:
843 415
                                parse_settings(s, f);
844
                                break;
845 6
                        case TYPE_PING:
846 6
                                parse_ping(s, f);
847
                                break;
848 39
                        case TYPE_GOAWAY:
849 39
                                parse_goaway(s, f);
850
                                break;
851 66
                        case TYPE_WINDOW_UPDATE:
852 66
                                parse_winup(s, f);
853
                                break;
854 0
                        default:
855 0
                                WRONG("wrong frame type");
856
                }
857 896
858 896
                PTOK(pthread_mutex_lock(&hp->mtx));
859 896
                VTAILQ_INSERT_HEAD(&s->fq, f, list);
860 854
                if (s->wf) {
861 854
                        assert(hp->wf > 0);
862 854
                        hp->wf--;
863 854
                        s->wf = 0;
864 854
                        PTOK(pthread_cond_signal(&s->cond));
865 896
                }
866
                continue;
867 211
        }
868 211
        PTOK(pthread_mutex_unlock(&hp->mtx));
869 0
        if (vsb != NULL)
870 211
                VSB_destroy(&vsb);
871 211
        return (NULL);
872
}
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 198
static char *
913
find_header(const struct hpk_hdr *h, const char *k)
914 198
{
915
        AN(k);
916 198
917 1062
        int kl = strlen(k);
918 1052
        while (h->t) {
919 188
                if (kl == h->key.len  && !strncasecmp(h->key.ptr, k, kl))
920 864
                        return (h->value.ptr);
921
                h++;
922 10
        }
923 198
        return (NULL);
924
}
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 1522
static const char *
936
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 1522
        struct hpk_ctx *ctx;
942
        struct frame *f = s->frame;
943 1522
944 1522
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
945 1522
        CHECK_OBJ_NOTNULL(s->hp, HTTP_MAGIC);
946 1522
        AN(spec);
947
        AN(buf);
948 1522
949
        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 1522
         */
957 6
        if (!strcmp(spec, "ping.data")) {
958 6
                CHECK_LAST_FRAME(PING);
959
                return (f->md.ping.data);
960 1516
        }
961 4
        if (!strcmp(spec, "ping.ack")) {
962 4
                CHECK_LAST_FRAME(PING);
963 4
                snprintf(buf, 20, (f->flags & ACK) ? "true" : "false");
964
                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 1512
         */
971 1
        if (!strcmp(spec, "winup.size")) {
972 1
                CHECK_LAST_FRAME(WINDOW_UPDATE);
973 0
                RETURN_BUFFED(f->md.winup_size);
974
        }
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 1511
         */
986 2
        if (!strcmp(spec, "prio.stream")) {
987 2
                CHECK_LAST_FRAME(PRIORITY);
988 0
                RETURN_BUFFED(f->md.prio.stream);
989 1509
        }
990 0
        if (!strcmp(spec, "prio.exclusive")) {
991 0
                CHECK_LAST_FRAME(PRIORITY);
992 0
                snprintf(buf, 20, f->md.prio.exclusive ? "true" : "false");
993
                return (buf);
994 1509
        }
995 2
        if (!strcmp(spec, "prio.weight")) {
996 2
                CHECK_LAST_FRAME(PRIORITY);
997 0
                RETURN_BUFFED(f->md.prio.weight);
998
        }
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 1507
         */
1004 41
        if (!strcmp(spec, "rst.err")) {
1005 41
                CHECK_LAST_FRAME(RST_STREAM);
1006 0
                RETURN_BUFFED(f->md.rst_err);
1007
        }
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 1466
         */
1032 222
        if (!strncmp(spec, "settings.", 9)) {
1033 222
                CHECK_LAST_FRAME(SETTINGS);
1034 222
                spec += 9;
1035 206
                if (!strcmp(spec, "ack")) {
1036 206
                        snprintf(buf, 20, (f->flags & ACK) ? "true" : "false");
1037
                        return (buf);
1038 16
                }
1039 1
                if (!strcmp(spec, "push")) {
1040 1
                        if (isnan(f->md.settings[SETTINGS_ENABLE_PUSH]))
1041 0
                                return (NULL);
1042 0
                        else if (f->md.settings[SETTINGS_ENABLE_PUSH] == 1)
1043
                                snprintf(buf, 20, "true");
1044 0
                        else
1045 0
                                snprintf(buf, 20, "false");
1046
                        return (buf);
1047 15
                }
1048 12
                if (!strcmp(spec, "hdrtbl"))     { RETURN_SETTINGS(1); }
1049 9
                if (!strcmp(spec, "maxstreams")) { RETURN_SETTINGS(3); }
1050 6
                if (!strcmp(spec, "winsize"))    { RETURN_SETTINGS(4); }
1051 3
                if (!strcmp(spec, "framesize"))  { RETURN_SETTINGS(5); }
1052 0
                if (!strcmp(spec, "hdrsize"))    { RETURN_SETTINGS(6); }
1053
        }
1054
        /* SECTION: stream.spec.zexpect.push PUSH_PROMISE specific
1055
         *
1056
         * push.id
1057
         *      The id of the promised stream.
1058 1244
         */
1059 1
        if (!strcmp(spec, "push.id")) {
1060 1
                CHECK_LAST_FRAME(PUSH_PROMISE);
1061 0
                RETURN_BUFFED(f->md.promised);
1062
        }
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 1243
         */
1074 66
        if (!strncmp(spec, "goaway.", 7)) {
1075 66
                spec += 7;
1076
                CHECK_LAST_FRAME(GOAWAY);
1077 66
1078 35
                if (!strcmp(spec, "err"))
1079 31
                        RETURN_BUFFED(f->md.goaway.err);
1080 29
                else if (!strcmp(spec, "laststream"))
1081 2
                        RETURN_BUFFED(f->md.goaway.stream);
1082 2
                else if (!strcmp(spec, "debug"))
1083 0
                        return (f->md.goaway.debug);
1084
        }
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 1177
         */
1103 8
        if (!strncmp(spec, "frame.", 6)) {
1104 8
                spec += 6;
1105 0
                if (!f)
1106 8
                        vtc_fatal(s->vl, "No frame received yet.");
1107 7
                if (!strcmp(spec, "data"))   { return (f->data); }
1108 6
                else if (!strcmp(spec, "type"))   { RETURN_BUFFED(f->type); }
1109 2
                else if (!strcmp(spec, "size"))   { RETURN_BUFFED(f->size); }
1110 1
                else if (!strcmp(spec, "stream")) { RETURN_BUFFED(f->stid); }
1111 1
                else if (!strcmp(spec, "padding")) {
1112 1
                        if (f->type != TYPE_DATA &&
1113 0
                                        f->type != TYPE_HEADERS &&
1114 0
                                        f->type != TYPE_PUSH_PROMISE)
1115
                                vtc_fatal(s->vl,
1116
                                                "Last frame was not of type "
1117 1
                                                "DATA, HEADERS or PUSH");
1118 0
                        RETURN_BUFFED(f->md.padded);
1119 0
                }
1120
        }
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 1169
         */
1137 26
        if (!strcmp(spec, "stream.window")) {
1138 13
                snprintf(buf, 20, "%jd",
1139 13
                    (intmax_t)(s->id ? s->win_self : s->hp->h2_win_self->size));
1140
                return (buf);
1141 1156
        }
1142 32
        if (!strcmp(spec, "stream.peer_window")) {
1143 16
                snprintf(buf, 20, "%jd",
1144 16
                    (intmax_t)(s->id ? s->win_peer : s->hp->h2_win_peer->size));
1145
                return (buf);
1146 1140
        }
1147 8
        if (!strcmp(spec, "stream.weight")) {
1148 7
                if (s->id) {
1149 7
                        snprintf(buf, 20, "%d", s->weight);
1150
                        return (buf);
1151 1
                } else
1152
                        return (NULL);
1153 1132
        }
1154 16
        if (!strcmp(spec, "stream.dependency")) {
1155 15
                if (s->id) {
1156 15
                        snprintf(buf, 20, "%d", s->dependency);
1157
                        return (buf);
1158 1
                } else
1159
                        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 1116
         */
1180 123
        if (!strncmp(spec, "tbl.dec", 7) || !strncmp(spec, "tbl.enc", 7)) {
1181 63
                if (spec[4] == 'd')
1182
                        ctx = s->hp->decctx;
1183 60
                else
1184 123
                        ctx = s->hp->encctx;
1185
                spec += 7;
1186 123
1187 96
                if (1 == sscanf(spec, "[%u].key%n", &idx, &n) &&
1188 48
                                spec[n] == '\0') {
1189 48
                        h = HPK_GetHdr(ctx, idx + 61);
1190
                        return (h ? h->key.ptr : NULL);
1191 75
                }
1192 48
                else if (1 == sscanf(spec, "[%u].value%n", &idx, &n) &&
1193 48
                                spec[n] == '\0') {
1194 48
                        h = HPK_GetHdr(ctx, idx + 61);
1195
                        return (h ? h->value.ptr : NULL);
1196 27
                }
1197 26
                else if (!strcmp(spec, ".size"))
1198 1
                        RETURN_BUFFED(HPK_GetTblSize(ctx));
1199 0
                else if (!strcmp(spec, ".maxsize"))
1200 1
                        RETURN_BUFFED(HPK_GetTblMaxSize(ctx));
1201 1
                else if (!strcmp(spec, ".length"))
1202 0
                        RETURN_BUFFED(HPK_GetTblLength(ctx));
1203
        }
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 993
         */
1233 234
        if (!strncmp(spec, "req.", 4) || !strncmp(spec, "resp.", 5)) {
1234 15
                if (spec[2] == 'q') {
1235 15
                        h = s->req;
1236 15
                        spec += 4;
1237 219
                } else {
1238 219
                        h = s->resp;
1239
                        spec += 5;
1240 234
                }
1241 17
                if (!strcmp(spec, "body"))
1242 217
                        return (s->body);
1243 19
                else if (!strcmp(spec, "bodylen"))
1244 198
                        RETURN_BUFFED(s->bodylen);
1245 82
                else if (!strcmp(spec, "status"))
1246 116
                        return (find_header(h, ":status"));
1247 2
                else if (!strcmp(spec, "url"))
1248 114
                        return (find_header(h, ":path"));
1249 2
                else if (!strcmp(spec, "method"))
1250 112
                        return (find_header(h, ":method"));
1251 1
                else if (!strcmp(spec, "authority"))
1252 111
                        return (find_header(h, ":authority"));
1253 1
                else if (!strcmp(spec, "scheme"))
1254 110
                        return (find_header(h, ":scheme"));
1255 110
                else if (!strncmp(spec, "http.", 5))
1256
                        return (find_header(h, spec + 5));
1257 0
                else
1258 0
                        return (NULL);
1259
        }
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 57
static void
1275
cmd_sendhex(CMD_ARGS)
1276
{
1277
        struct http *hp;
1278
        struct stream *s;
1279
        struct vsb *vsb;
1280 57
1281 57
        (void)vl;
1282 57
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1283 57
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
1284 57
        AN(av[1]);
1285 57
        AZ(av[2]);
1286 57
        vsb = vtc_hex_to_bin(hp->vl, av[1]);
1287 57
        assert(VSB_len(vsb) >= 0);
1288 57
        vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
1289 57
        PTOK(pthread_mutex_lock(&hp->mtx));
1290 57
        http_write(hp, 4, VSB_data(vsb), VSB_len(vsb), "sendhex");
1291 57
        PTOK(pthread_mutex_unlock(&hp->mtx));
1292 57
        VSB_destroy(&vsb);
1293
}
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 1293
static void
1424
cmd_tx11obj(CMD_ARGS)
1425
{
1426
        struct stream *s;
1427 1293
        int i;
1428 1293
        int status_done = 1;
1429 1293
        int method_done = 1;
1430 1293
        int path_done = 1;
1431 1293
        int scheme_done = 1;
1432
        long bodylen = 0;
1433 1293
        ssize_t len;
1434 1293
        uint32_t stid = 0, pstid;
1435 1293
        uint32_t weight = 16;
1436
        uint32_t exclusive = 0;
1437
        char *buf;
1438
        struct hpk_iter *iter;
1439 1293
        struct frame f;
1440
        char *body = NULL, *pad = NULL;
1441
        /*XXX: do we need a better api? yes we do */
1442 1293
        struct hpk_hdr hdr;
1443
        char *cmd_str = *av;
1444
        char *p;
1445 1293
1446 1293
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1447 1293
        INIT_FRAME(f, CONTINUATION, 0, s->id, END_HEADERS);
1448 1293
        buf = malloc(BUF_SIZE);
1449
        AN(buf);
1450 1293
1451 227
        if (!strcmp(cmd_str, "txreq")) {
1452 227
                ONLY_H2_CLIENT(s->hp, av);
1453 227
                f.type = TYPE_HEADERS;
1454 227
                f.flags |= END_STREAM;
1455 227
                method_done = 0;
1456 227
                path_done = 0;
1457 1293
                scheme_done = 0;
1458 48
        } else if (!strcmp(cmd_str, "txresp")) {
1459 48
                ONLY_H2_SERVER(s->hp, av);
1460 48
                f.type = TYPE_HEADERS;
1461 48
                f.flags |= END_STREAM;
1462 1066
                status_done = 0;
1463 1
        } else if (!strcmp(cmd_str, "txpush")) {
1464 1
                ONLY_H2_SERVER(s->hp, av);
1465 1
                f.type = TYPE_PUSH_PROMISE;
1466 1
                method_done = 0;
1467 1
                path_done = 0;
1468 1
                scheme_done = 0;
1469
        }
1470 1293
1471 1
        if (f.type == TYPE_PUSH_PROMISE) {
1472 1
                *buf = 0;
1473 1
                iter = HPK_NewIter(s->hp->encctx, buf + 4, BUF_SIZE - 4);
1474 1292
        } else
1475
                iter = HPK_NewIter(s->hp->encctx, buf, BUF_SIZE);
1476
1477
#define AV_IS(str) !strcmp(*av, str)
1478 3745
#define CMD_IS(str) !strcmp(cmd_str, str)
1479 2452
        while (*++av) {
1480 2452
                memset(&hdr, 0, sizeof(hdr));
1481 2452
                hdr.t = hpk_not;
1482 12
                if (AV_IS("-noadd")) {
1483 12
                        path_done = 1;
1484 12
                        status_done = 1;
1485 12
                        method_done = 1;
1486 12
                        scheme_done = 1;
1487 2440
                }
1488 1
                else if (AV_IS("-status") && CMD_IS("txresp")) {
1489 1
                        ENC(hdr, ":status", av[1]);
1490 1
                        av++;
1491 1
                        status_done = 1;
1492 2440
                }
1493 62
                else if (AV_IS("-url") &&
1494 62
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1495 62
                        ENC(hdr, ":path", av[1]);
1496 62
                        av++;
1497 62
                        path_done = 1;
1498 2377
                }
1499 37
                else if ((AV_IS("-method") || AV_IS("-req")) &&
1500 37
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1501 37
                        ENC(hdr, ":method", av[1]);
1502 37
                        av++;
1503 37
                        method_done = 1;
1504 2340
                }
1505 7
                else if (AV_IS("-scheme") &&
1506 7
                                (CMD_IS("txreq") || CMD_IS("txpush"))) {
1507 7
                        ENC(hdr, ":scheme", av[1]);
1508 7
                        av++;
1509 7
                        scheme_done = 1;
1510 2333
                }
1511 1184
                else if (AV_IS("-hdr")) {
1512 0
                        if (av[2] == NULL)
1513 1184
                                vtc_fatal(vl, "-hdr takes two arguments in http2");
1514 1184
                        ENC(hdr, av[1], av[2]);
1515 1184
                        av += 2;
1516 1149
                }
1517 42
                else if (AV_IS("-idxHdr")) {
1518 42
                        hdr.t = hpk_idx;
1519 42
                        STRTOU32_CHECK(hdr.i, av, p, vl, "-idxHdr", 0);
1520 42
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1521 1107
                }
1522 16
                else if (AV_IS("-litIdxHdr")) {
1523 16
                        av++;
1524 1
                        if      (AV_IS("inc"))   { hdr.t = hpk_inc;   }
1525 1
                        else if (AV_IS("not"))   { hdr.t = hpk_not;   }
1526
                        else if (AV_IS("never")) { hdr.t = hpk_never; }
1527 0
                        else
1528 0
                                vtc_fatal(vl, "first -litidxHdr arg can be "
1529
                                    "inc, not, never (got: %s)", *av);
1530 16
1531
                        STRTOU32_CHECK(hdr.i, av, p, vl,
1532
                            "second -litidxHdr arg", 0);
1533 16
1534 16
                        hdr.key.ptr = NULL;
1535 24
                        hdr.key.len = 0;
1536 16
                        STR_ENC(av, value,   "third -litHdr");
1537 16
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1538 1091
                }
1539 7
                else if (AV_IS("-litHdr")) {
1540 7
                        av++;
1541 0
                        if      (AV_IS("inc"))   { hdr.t = hpk_inc;   }
1542 0
                        else if (AV_IS("not"))   { hdr.t = hpk_not;   }
1543
                        else if (AV_IS("never")) { hdr.t = hpk_never; }
1544 0
                        else
1545 0
                                vtc_fatal(vl, "first -litHdr arg can be inc, "
1546
                                    "not, never (got: %s)", *av);
1547 10
1548 10
                        STR_ENC(av, key,   "second -litHdr");
1549 7
                        STR_ENC(av, value, "fourth -litHdr");
1550 7
                        assert(HPK_EncHdr(iter, &hdr) != hpk_err);
1551 1084
                }
1552 39
                else if (AV_IS("-nostrend")) {
1553 39
                        f.flags &= ~END_STREAM;
1554 1045
                }
1555 1016
                else if (AV_IS("-nohdrend")) {
1556 1016
                        f.flags &= ~END_HEADERS;
1557 29
                }
1558 1
                else if (AV_IS("-promised") && CMD_IS("txpush")) {
1559 1
                        STRTOU32_CHECK(pstid, av, p, vl, "-promised", 31);
1560 1
                        vbe32enc(buf, pstid);
1561 28
                }
1562 6
                else if (AV_IS("-pad") && !CMD_IS("txcont")) {
1563 6
                        AZ(pad);
1564 6
                        av++;
1565 6
                        AN(*av);
1566 6
                        pad = strdup(*av);
1567 22
                }
1568 0
                else if (AV_IS("-padlen") && !CMD_IS("txcont")) {
1569 0
                        AZ(pad);
1570 0
                        av++;
1571 0
                        pad = synth_body(*av, 0);
1572 22
                }
1573 22
                else if (CMD_IS("txreq") || CMD_IS("txresp")) {
1574 8
                        if (AV_IS("-body")) {
1575 8
                                AZ(body);
1576 8
                                REPLACE(body, av[1]);
1577 8
                                AN(body);
1578 8
                                bodylen = strlen(body);
1579 8
                                f.flags &= ~END_STREAM;
1580 8
                                av++;
1581 14
                        }
1582 2
                        else if (AV_IS("-bodyfrom")) {
1583 2
                                AZ(body);
1584 2
                                body = VFIL_readfile(NULL, av[1], &len);
1585 2
                                AN(body);
1586 2
                                assert(len < INT_MAX);
1587 2
                                bodylen = len;
1588 2
                                f.flags &= ~END_STREAM;
1589 2
                                av++;
1590 12
                        }
1591 4
                        else if (AV_IS("-bodylen")) {
1592 4
                                AZ(body);
1593 4
                                body = synth_body(av[1], 0);
1594 4
                                bodylen = strlen(body);
1595 4
                                f.flags &= ~END_STREAM;
1596 4
                                av++;
1597 8
                        }
1598 2
                        else if (!strncmp(*av, "-gzip", 5)) {
1599 2
                                i = vtc_gzip_cmd(s->hp, av, &body, &bodylen);
1600 0
                                if (i == 0)
1601 2
                                        break;
1602 2
                                av++;
1603 2
                                if (i > 1) {
1604 2
                                        ENC(hdr, ":content-encoding", "gzip");
1605 2
                                        f.flags &= ~END_STREAM;
1606 2
                                }
1607 6
                        }
1608 3
                        else if (AV_IS("-dep")) {
1609 3
                                STRTOU32_CHECK(stid, av, p, vl, "-dep", 0);
1610 3
                                f.flags |= PRIORITY;
1611 3
                        }
1612 2
                        else if (AV_IS("-ex")) {
1613 2
                                exclusive = 1U << 31;
1614 2
                                f.flags |= PRIORITY;
1615 1
                        }
1616 1
                        else if (AV_IS("-weight")) {
1617 1
                                STRTOU32_CHECK(weight, av, p, vl, "-weight", 8);
1618 1
                                f.flags |= PRIORITY;
1619 0
                        } else
1620 22
                                break;
1621 0
                } else
1622
                        break;
1623
        }
1624
#undef CMD_IS
1625 1293
#undef AV_IS
1626 0
        if (*av != NULL)
1627
                vtc_fatal(vl, "Unknown %s spec: %s\n", cmd_str, *av);
1628 1293
1629 1293
        memset(&hdr, 0, sizeof(hdr));
1630
        hdr.t = hpk_not;
1631 1293
1632 1293
        if (!status_done) { ENC(hdr, ":status", "200"); }
1633 1293
        if (!path_done)   { ENC(hdr, ":path",   "/"); }
1634 1293
        if (!method_done) { ENC(hdr, ":method", "GET"); }
1635
        if (!scheme_done) { ENC(hdr, ":scheme", "http"); }
1636 1293
1637 1293
        f.size = gethpk_iterLen(iter);
1638 4
        if (f.flags & PRIORITY) {
1639 4
                s->weight = weight & 0xff;
1640
                s->dependency = stid;
1641 4
1642 4
                assert(f.size + 5 < BUF_SIZE);
1643 4
                memmove(buf + 5, buf, f.size);
1644 4
                vbe32enc(buf, (stid | exclusive));
1645 4
                buf[4] = s->weight;
1646
                f.size += 5;
1647 4
1648 4
                vtc_log(vl, 4, "stream->dependency: %u", s->dependency);
1649 4
                vtc_log(vl, 4, "stream->weight: %u", s->weight);
1650 2
                if (exclusive)
1651 4
                        exclusive_stream_dependency(s);
1652 1293
        }
1653 6
        if (pad) {
1654 0
                if (strlen(pad) > 255)
1655 6
                        vtc_fatal(vl, "Padding is limited to 255 bytes");
1656 6
                f.flags |= PADDED;
1657 6
                assert(f.size + strlen(pad) < BUF_SIZE);
1658 6
                memmove(buf + 1, buf, f.size);
1659 6
                buf[0] = strlen(pad);
1660 6
                f.size += 1;
1661 6
                memcpy(buf + f.size, pad, strlen(pad));
1662 6
                f.size += strlen(pad);
1663 6
                free(pad);
1664 1293
        }
1665 1
        if (f.type == TYPE_PUSH_PROMISE)
1666 1293
                f.size += 4;
1667 1293
        f.data = buf;
1668 1293
        HPK_FreeIter(iter);
1669 1293
        write_frame(s, &f, 1);
1670
        free(buf);
1671 1293
1672 1277
        if (!body)
1673
                return;
1674 16
1675 16
        INIT_FRAME(f, DATA, bodylen, s->id, END_STREAM);
1676
        f.data = body;
1677 16
1678 16
        write_frame(s, &f, 1);
1679 1293
        free(body);
1680
}
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 320
static void
1704
cmd_txdata(CMD_ARGS)
1705
{
1706 320
        struct stream *s;
1707
        char *pad = NULL;
1708 320
        struct frame f;
1709 320
        char *body = NULL;
1710
        char *data = NULL;
1711 320
1712
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1713 320
1714
        INIT_FRAME(f, DATA, 0, s->id, END_STREAM);
1715 937
1716 617
        while (*++av) {
1717 25
                if (!strcmp(*av, "-data")) {
1718 25
                        AZ(body);
1719 25
                        av++;
1720 617
                        body = strdup(*av);
1721 35
                } else if (!strcmp(*av, "-datalen")) {
1722 35
                        AZ(body);
1723 35
                        av++;
1724 592
                        body = synth_body(*av, 0);
1725 1
                } else if (!strcmp(*av, "-pad")) {
1726 1
                        AZ(pad);
1727 1
                        av++;
1728 1
                        AN(*av);
1729 557
                        pad = strdup(*av);
1730 268
                } else if (!strcmp(*av, "-padlen")) {
1731 268
                        AZ(pad);
1732 268
                        av++;
1733 556
                        pad = synth_body(*av, 0);
1734 288
                } else if (!strcmp(*av, "-nostrend"))
1735
                        f.flags &= ~END_STREAM;
1736 0
                else
1737
                        break;
1738 320
        }
1739 0
        if (*av != NULL)
1740
                vtc_fatal(vl, "Unknown txdata spec: %s\n", *av);
1741 320
1742 260
        if (!body)
1743
                body = strdup("");
1744 320
1745 269
        if (pad) {
1746 269
                f.flags |= PADDED;
1747 0
                if (strlen(pad) > 255)
1748 269
                        vtc_fatal(vl, "Padding is limited to 255 bytes");
1749 269
                data = malloc( 1 + strlen(body) + strlen(pad));
1750 269
                AN(data);
1751 269
                *((uint8_t *)data) = strlen(pad);
1752 269
                f.size = 1;
1753 269
                memcpy(data + f.size, body, strlen(body));
1754 269
                f.size += strlen(body);
1755 269
                memcpy(data + f.size, pad, strlen(pad));
1756 269
                f.size += strlen(pad);
1757 269
                f.data = data;
1758 51
        } else {
1759 51
                f.size = strlen(body);
1760
                f.data = body;
1761 320
        }
1762 320
        write_frame(s, &f, 1);
1763 320
        free(body);
1764 320
        free(pad);
1765 320
        free(data);
1766
}
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 19
static void
1779
cmd_txrst(CMD_ARGS)
1780
{
1781
        struct stream *s;
1782 19
        char *p;
1783
        uint32_t err = 0;
1784
        struct frame f;
1785 19
1786
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1787 19
1788
        INIT_FRAME(f, RST_STREAM, 4, s->id, 0);
1789 27
1790 8
        while (*++av) {
1791 8
                if (!strcmp(*av, "-err")) {
1792 120
                        ++av;
1793 112
                        for (err = 0; h2_errs[err]; err++) {
1794 0
                                if (!strcmp(h2_errs[err], *av))
1795 112
                                        break;
1796
                        }
1797 8
1798 0
                        if (h2_errs[err])
1799
                                continue;
1800 8
1801 8
                        STRTOU32(err, *av, p, vl, "-err");
1802 0
                } else
1803
                        break;
1804 19
        }
1805 0
        if (*av != NULL)
1806
                vtc_fatal(vl, "Unknown txrst spec: %s\n", *av);
1807 19
1808 19
        err = htonl(err);
1809 19
        f.data = (void *)&err;
1810 19
        write_frame(s, &f, 1);
1811
}
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 13
static void
1829
cmd_txprio(CMD_ARGS)
1830
{
1831
        struct stream *s;
1832 13
        char *p;
1833
        uint32_t stid = 0;
1834 13
        struct frame f;
1835 13
        uint32_t weight = 0;
1836
        uint32_t exclusive = 0;
1837
        uint8_t buf[5];
1838 13
1839
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1840 13
1841 13
        INIT_FRAME(f, PRIORITY, 5, s->id, 0);
1842
        f.data = (void *)buf;
1843 31
1844 18
        while (*++av) {
1845 9
                if (!strcmp(*av, "-stream")) {
1846 18
                        STRTOU32_CHECK(stid, av, p, vl, "-stream", 0);
1847 1
                } else if (!strcmp(*av, "-ex")) {
1848 9
                        exclusive = 1U << 31;
1849 8
                } else if (!strcmp(*av, "-weight")) {
1850 8
                        STRTOU32_CHECK(weight, av, p, vl, "-weight", 8);
1851 0
                } else
1852
                        break;
1853 13
        }
1854 0
        if (*av != NULL)
1855 13
                vtc_fatal(vl, "Unknown txprio spec: %s\n", *av);
1856 13
        s->weight = weight & 0xff;
1857
        s->dependency = stid;
1858 13
1859 1
        if (exclusive)
1860
                exclusive_stream_dependency(s);
1861 13
1862 13
        vbe32enc(buf, (stid | exclusive));
1863 13
        buf[4] = s->weight;
1864 13
        write_frame(s, &f, 1);
1865
}
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
 * \-0xHH[HH] INT
1901
 *      tx arbitraty settings with tag xx
1902
 *
1903
 * \-ack
1904
 *      set the ack bit
1905
 */
1906 412
static void
1907
cmd_txsettings(CMD_ARGS)
1908
{
1909
        struct stream *s, *s2;
1910
        struct http *hp;
1911
        char *p, *e;
1912 412
        unsigned long u;
1913
        uint32_t val = 0;
1914
        struct frame f;
1915
        //TODO dynamic alloc
1916 412
        char buf[512];
1917
        char *cursor = buf;
1918 412
1919 412
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1920
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
1921 412
1922 412
        memset(buf, 0, 512);
1923 412
        INIT_FRAME(f, SETTINGS, 0, s->id, 0);
1924
        f.data = buf;
1925 412
1926 635
        PTOK(pthread_mutex_lock(&hp->mtx));
1927 223
        while (*++av) {
1928 1
                if (!strcmp(*av, "-push")) {
1929 1
                        ++av;
1930 1
                        vbe16enc(cursor, 0x2);
1931 1
                        cursor += sizeof(uint16_t);
1932 0
                        if (!strcmp(*av, "false"))
1933 1
                                vbe32enc(cursor, 0);
1934 1
                        else if (!strcmp(*av, "true"))
1935
                                vbe32enc(cursor, 1);
1936 0
                        else
1937 0
                                vtc_fatal(vl, "Push parameter is either "
1938 1
                                    "\"true\" or \"false\", not %s", *av);
1939 1
                        cursor += sizeof(uint32_t);
1940 1
                        f.size += 6;
1941 222
                }
1942 5
                else if (!strcmp(*av, "-hdrtbl")) {
1943 5
                        PUT_KV(av, vl, hdrtbl, val, 0x1);
1944 5
                        assert(HPK_ResizeTbl(s->hp->decctx, val) != hpk_err);
1945 217
                }
1946 1
                else if (!strcmp(*av, "-maxstreams"))
1947 216
                        PUT_KV(av, vl, maxstreams, val, 0x3);
1948 8
                else if (!strcmp(*av, "-winsize"))      {
1949 17
                        PUT_KV(av, vl, winsize, val, 0x4);
1950 9
                        VTAILQ_FOREACH(s2, &hp->streams, list)
1951 8
                                s2->win_self += (val - hp->h2_win_self->init);
1952 8
                        hp->h2_win_self->init = val;
1953 208
                }
1954 3
                else if (!strcmp(*av, "-framesize"))
1955 205
                        PUT_KV(av, vl, framesize, val, 0x5);
1956 1
                else if (!strcmp(*av, "-hdrsize"))
1957 204
                        PUT_KV(av, vl, hdrsize, val, 0x6);
1958 0
                else if (!strncmp(*av, "-0x", 3)) {
1959 0
                        p = *av + 3;
1960 0
                        errno = 0;
1961 0
                        u = strtoul(p, &e, 16);
1962 0
                        if (*p == '\0' || *e != '\0' || u > 0xffff || errno != 0)
1963 0
                                vtc_fatal(vl, "Invalid settings tag %s", p);
1964 0
                        assert(u <= 0xffff);
1965 0
                        PUT_KV(av, vl, hdrtbl, val, (uint16_t)u);
1966 204
                }
1967 204
                else if (!strcmp(*av, "-ack"))
1968
                        f.flags |= 1;
1969 0
                else
1970
                        break;
1971 412
        }
1972 0
        if (*av != NULL)
1973
                vtc_fatal(vl, "Unknown txsettings spec: %s\n", *av);
1974 412
1975 412
        AN(s->hp);
1976 412
        write_frame(s, &f, 0);
1977 412
        PTOK(pthread_mutex_unlock(&hp->mtx));
1978
}
1979
1980
/* SECTION: stream.spec.ping_txping txping
1981
 *
1982
 * Send PING frame.
1983
 *
1984
 * \-data STRING
1985
 *      specify the payload of the frame, with STRING being an 8-char string.
1986
 *
1987
 * \-ack
1988
 *      set the ACK flag.
1989
 */
1990 7
static void
1991
cmd_txping(CMD_ARGS)
1992
{
1993
        struct stream *s;
1994
        struct frame f;
1995
        char buf[8];
1996 7
1997 7
        memset(buf, 0, 8);
1998 7
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
1999
        INIT_FRAME(f, PING, 8, s->id, 0);
2000 14
2001 7
        while (*++av) {
2002 5
                if (!strcmp(*av, "-data")) {
2003 5
                        av++;
2004 0
                        if (f.data)
2005 5
                                vtc_fatal(vl, "this frame already has data");
2006 0
                        if (strlen(*av) != 8)
2007 5
                                vtc_fatal(vl, "data must be a 8-char string, found  (%s)", *av);
2008 7
                        f.data = *av;
2009 2
                } else if (!strcmp(*av, "-ack"))
2010
                        f.flags |= 1;
2011 0
                else
2012
                        break;
2013 7
        }
2014 0
        if (*av != NULL)
2015 7
                vtc_fatal(vl, "Unknown txping spec: %s\n", *av);
2016 2
        if (!f.data)
2017 7
                f.data = buf;
2018 7
        write_frame(s, &f, 1);
2019
}
2020
2021
/*
2022
 * SECTION: stream.spec.goaway_txgoaway txgoaway
2023
 *
2024
 * Possible options include:
2025
 *
2026
 * \-err STRING|INT
2027
 *      set the error code to explain the termination. The second argument
2028
 *      can be a integer or the string version of the error code as found
2029
 *      in rfc7540#7.
2030
 *
2031
 * \-laststream INT
2032
 *      the id of the "highest-numbered stream identifier for which the
2033
 *      sender of the GOAWAY frame might have taken some action on or might
2034
 *      yet take action on".
2035
 *
2036
 * \-debug
2037
 *      specify the debug data, if any to append to the frame.
2038
 */
2039 5
static void
2040
cmd_txgoaway(CMD_ARGS)
2041
{
2042
        struct stream *s;
2043 5
        char *p;
2044 5
        uint32_t err = 0;
2045
        uint32_t ls = 0;
2046
        struct frame f;
2047 5
2048
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2049 5
2050
        INIT_FRAME(f, GOAWAY, 8, s->id, 0);
2051 14
2052 9
        while (*++av) {
2053 5
                if (!strcmp(*av, "-err")) {
2054 75
                        ++av;
2055 70
                        for (err = 0; h2_errs[err]; err++)
2056 0
                                if (!strcmp(h2_errs[err], *av))
2057
                                        break;
2058 5
2059 0
                        if (h2_errs[err])
2060
                                continue;
2061 5
2062 9
                        STRTOU32(err, *av, p, vl, "-err");
2063 2
                } else if (!strcmp(*av, "-laststream")) {
2064 4
                        STRTOU32_CHECK(ls, av, p, vl, "-laststream", 31);
2065 2
                } else if (!strcmp(*av, "-debug")) {
2066 2
                        ++av;
2067 0
                        if (f.data)
2068 2
                                vtc_fatal(vl, "this frame already has debug data");
2069 2
                        f.size = 8 + strlen(*av);
2070 2
                        f.data = malloc(f.size);
2071 2
                        AN(f.data);
2072 2
                        memcpy(f.data + 8, *av, f.size - 8);
2073 0
                } else
2074
                        break;
2075 5
        }
2076 0
        if (*av != NULL)
2077
                vtc_fatal(vl, "Unknown txgoaway spec: %s\n", *av);
2078 5
2079 3
        if (!f.data) {
2080 3
                f.data = malloc(8);
2081 3
                AN(f.data);
2082 5
        }
2083 5
        vbe32enc(f.data, ls);
2084 5
        vbe32enc(f.data + 4, err);
2085 5
        write_frame(s, &f, 1);
2086 5
        free(f.data);
2087
}
2088
2089
/* SECTION: stream.spec.winup_txwinup txwinup
2090
 *
2091
 * Transmit a WINDOW_UPDATE frame, increasing the amount of credit of the
2092
 * connection (from stream 0) or of the stream (any other stream).
2093
 *
2094
 * \-size INT
2095
 *      give INT credits to the peer.
2096
 */
2097 24
static void
2098
cmd_txwinup(CMD_ARGS)
2099
{
2100
        struct http *hp;
2101
        struct stream *s;
2102
        char *p;
2103
        struct frame f;
2104 24
        char buf[8];
2105
        uint32_t size = 0;
2106 24
2107 24
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2108 24
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
2109
        memset(buf, 0, 8);
2110 24
2111 24
        AN(av[1]);
2112
        AN(av[2]);
2113 24
2114 24
        INIT_FRAME(f, WINDOW_UPDATE, 4, s->id, 0);
2115
        f.data = buf;
2116 48
2117 24
        while (*++av)
2118 24
                if (!strcmp(*av, "-size")) {
2119 24
                        STRTOU32_CHECK(size, av, p, vl, "-size", 0);
2120 0
                } else
2121 24
                        break;
2122 0
        if (*av != NULL)
2123
                vtc_fatal(vl, "Unknown txwinup spec: %s\n", *av);
2124 24
2125 24
        PTOK(pthread_mutex_lock(&hp->mtx));
2126 10
        if (s->id == 0)
2127 24
                hp->h2_win_self->size += size;
2128 24
        s->win_self += size;
2129
        PTOK(pthread_mutex_unlock(&hp->mtx));
2130 24
2131 24
        size = htonl(size);
2132 24
        f.data = (void *)&size;
2133 24
        write_frame(s, &f, 1);
2134
}
2135
2136 856
static struct frame *
2137
rxstuff(struct stream *s)
2138
{
2139
        struct frame *f;
2140 856
2141
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2142 856
2143 856
        PTOK(pthread_mutex_lock(&s->hp->mtx));
2144 854
        if (VTAILQ_EMPTY(&s->fq)) {
2145 854
                assert(s->hp->wf >= 0);
2146 854
                s->hp->wf++;
2147 854
                s->wf = 1;
2148 854
                PTOK(pthread_cond_signal(&s->hp->cond));
2149 854
                PTOK(pthread_cond_wait(&s->cond, &s->hp->mtx));
2150 856
        }
2151 0
        if (VTAILQ_EMPTY(&s->fq)) {
2152 0
                PTOK(pthread_mutex_unlock(&s->hp->mtx));
2153
                return (NULL);
2154 856
        }
2155 856
        clean_frame(&s->frame);
2156 856
        f = VTAILQ_LAST(&s->fq, fq_head);
2157 856
        CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
2158 856
        VTAILQ_REMOVE(&s->fq, f, list);
2159 856
        PTOK(pthread_mutex_unlock(&s->hp->mtx));
2160 856
        return (f);
2161
}
2162
2163
#define CHKFRAME(rt, wt, rcv, func) \
2164
        do { \
2165
        if (rt != wt) \
2166
                vtc_fatal(vl, "Frame #%d for %s was of type %s (%d) " \
2167
                    "instead of %s (%d)", \
2168
                    rcv, func, \
2169
                    rt < TYPE_MAX ? h2_types[rt] : "?", rt, \
2170
                    wt < TYPE_MAX ? h2_types[wt] : "?", wt); \
2171
        } while (0);
2172
2173
/* SECTION: stream.spec.data_11 rxhdrs
2174
 *
2175
 * ``rxhdrs`` will expect one HEADER frame, then, depending on the arguments,
2176
 * zero or more CONTINUATION frame.
2177
 *
2178
 * \-all
2179
 *      Keep waiting for CONTINUATION frames until END_HEADERS flag is seen.
2180
 *
2181
 * \-some INT
2182
 *      Retrieve INT - 1 CONTINUATION frames after the HEADER frame.
2183
 *
2184
 */
2185 14
static void
2186
cmd_rxhdrs(CMD_ARGS)
2187
{
2188 14
        struct stream *s;
2189
        struct frame *f = NULL;
2190 14
        char *p;
2191 14
        int loop = 0;
2192 14
        unsigned long int times = 1;
2193 14
        unsigned rcv = 0;
2194
        enum h2_type_e expect = TYPE_HEADERS;
2195 14
2196
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2197 16
2198 2
        while (*++av) {
2199 1
                if (!strcmp(*av, "-some")) {
2200 1
                        STRTOU32_CHECK(times, av, p, vl, "-some", 0);
2201 0
                        if (!times)
2202 0
                                vtc_fatal(vl, "-some argument must be more"
2203 2
                                               "than 0 (found \"%s\")\n", *av);
2204 1
                } else if (!strcmp(*av, "-all"))
2205
                        loop = 1;
2206 0
                else
2207
                        break;
2208 14
        }
2209 0
        if (*av != NULL)
2210
                vtc_fatal(vl, "Unknown rxhdrs spec: %s\n", *av);
2211 14
2212 17
        do {
2213 17
                replace_frame(&f, rxstuff(s));
2214 0
                if (f == NULL)
2215 17
                        break;
2216 17
                rcv++;
2217 17
                CHKFRAME(f->type, expect, rcv, "rxhdrs");
2218 17
                expect = TYPE_CONTINUATION;
2219 14
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2220 14
        replace_frame(&s->frame, f);
2221
}
2222
2223 5
static void
2224
cmd_rxcont(CMD_ARGS)
2225
{
2226 5
        struct stream *s;
2227
        struct frame *f = NULL;
2228 5
        char *p;
2229 5
        int loop = 0;
2230 5
        unsigned long int times = 1;
2231
        unsigned rcv = 0;
2232 5
2233 5
        (void)av;
2234
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2235 5
2236 0
        while (*++av)
2237 0
                if (!strcmp(*av, "-some")) {
2238 0
                        STRTOU32(times, *av, p, vl, "-some");
2239 0
                        if (!times)
2240 0
                                vtc_fatal(vl, "-some argument must be more"
2241 0
                                               "than 0 (found \"%s\")\n", *av);
2242 0
                } else if (!strcmp(*av, "-all"))
2243
                        loop = 1;
2244 0
                else
2245 5
                        break;
2246 0
        if (*av != NULL)
2247
                vtc_fatal(vl, "Unknown rxcont spec: %s\n", *av);
2248 5
2249 5
        do {
2250 5
                replace_frame(&f, rxstuff(s));
2251 0
                if (f == NULL)
2252 5
                        break;
2253 5
                rcv++;
2254 5
                CHKFRAME(f->type, TYPE_CONTINUATION, rcv, "rxcont");
2255 5
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2256 5
        replace_frame(&s->frame, f);
2257
}
2258
2259
2260
/* SECTION: stream.spec.data_13 rxdata
2261
 *
2262
 * Receiving data is done using the ``rxdata`` keywords and will retrieve one
2263
 * DATA frame, if you wish to receive more, you can use these two convenience
2264
 * arguments:
2265
 *
2266
 * \-all
2267
 *      keep waiting for DATA frame until one sets the END_STREAM flag
2268
 *
2269
 * \-some INT
2270
 *      retrieve INT DATA frames.
2271
 *
2272
 */
2273 15
static void
2274
cmd_rxdata(CMD_ARGS)
2275
{
2276 15
        struct stream *s;
2277
        struct frame *f = NULL;
2278 15
        char *p;
2279 15
        int loop = 0;
2280 15
        unsigned long int times = 1;
2281
        unsigned rcv = 0;
2282 15
2283 15
        (void)av;
2284
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2285 16
2286 1
        while (*++av)
2287 1
                if (!strcmp(*av, "-some")) {
2288 1
                        av++;
2289 1
                        STRTOU32(times, *av, p, vl, "-some");
2290 0
                        if (!times)
2291 0
                                vtc_fatal(vl, "-some argument must be more"
2292 1
                                               "than 0 (found \"%s\")\n", *av);
2293 0
                } else if (!strcmp(*av, "-all"))
2294
                        loop = 1;
2295 0
                else
2296 15
                        break;
2297 0
        if (*av != NULL)
2298
                vtc_fatal(vl, "Unknown rxdata spec: %s\n", *av);
2299 15
2300 17
        do {
2301 17
                replace_frame(&f, rxstuff(s));
2302 0
                if (f == NULL)
2303 17
                        break;
2304 17
                rcv++;
2305 17
                CHKFRAME(f->type, TYPE_DATA, rcv, "rxhdata");
2306 15
        } while (rcv < times || (loop && !(f->flags & END_STREAM)));
2307 15
        replace_frame(&s->frame, f);
2308
}
2309
2310
/* SECTION: stream.spec.data_10 rxreq, rxresp
2311
 *
2312
 * These are two convenience functions to receive headers and body of an
2313
 * incoming request or response. The only difference is that rxreq can only be
2314
 * by a server, and rxresp by a client.
2315
 *
2316
 */
2317
2318
#define cmd_rxreq       cmd_rxmsg
2319
#define cmd_rxresp      cmd_rxmsg
2320
2321 184
static void
2322
cmd_rxmsg(CMD_ARGS)
2323
{
2324 184
        struct stream *s;
2325
        struct frame *f = NULL;
2326 184
        int end_stream;
2327
        int rcv = 0;
2328 184
2329
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2330 184
2331 47
        if (!strcmp(av[0], "rxreq"))
2332
                ONLY_H2_SERVER(s->hp, av);
2333 137
        else
2334
                ONLY_H2_CLIENT(s->hp, av);
2335 184
2336 186
        do {
2337 186
                replace_frame(&f, rxstuff(s));
2338 186
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2339 0
                if (f == NULL)
2340 186
                        return;
2341
        } while (f->type == TYPE_WINDOW_UPDATE);
2342 184
2343 184
        rcv++;
2344
        CHKFRAME(f->type, TYPE_HEADERS, rcv, *av);
2345 184
2346
        end_stream = f->flags & END_STREAM;
2347 190
2348 6
        while (!(f->flags & END_HEADERS)) {
2349 6
                replace_frame(&f, rxstuff(s));
2350 6
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2351 0
                if (f == NULL)
2352 6
                        return;
2353 6
                rcv++;
2354
                CHKFRAME(f->type, TYPE_CONTINUATION, rcv, *av);
2355
        }
2356 267
2357 83
        while (!end_stream) {
2358 83
                replace_frame(&f, rxstuff(s));
2359 83
                CHECK_OBJ_ORNULL(f, FRAME_MAGIC);
2360 0
                if (f == NULL)
2361 83
                        break;
2362 83
                rcv++;
2363 83
                CHKFRAME(f->type, TYPE_DATA, rcv, *av);
2364
                end_stream = f->flags & END_STREAM;
2365 184
        }
2366 184
        replace_frame(&s->frame, f);
2367
}
2368
2369
/* SECTION: stream.spec.data_12 rxpush
2370
 *
2371
 * This works like ``rxhdrs``, expecting a PUSH frame and then zero or more
2372
 * CONTINUATION frames.
2373
 *
2374
 * \-all
2375
 *      Keep waiting for CONTINUATION frames until END_HEADERS flag is seen.
2376
 *
2377
 * \-some INT
2378
 *      Retrieve INT - 1 CONTINUATION frames after the PUSH frame.
2379
 *
2380
 */
2381 1
static void
2382
cmd_rxpush(CMD_ARGS)
2383
{
2384 1
        struct stream *s;
2385
        struct frame *f = NULL;
2386 1
        char *p;
2387 1
        int loop = 0;
2388 1
        unsigned long int times = 1;
2389 1
        unsigned rcv = 0;
2390
        enum h2_type_e expect = TYPE_PUSH_PROMISE;
2391 1
2392
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2393 1
2394 0
        while (*++av) {
2395 0
                if (!strcmp(*av, "-some")) {
2396 0
                        STRTOU32_CHECK(times, av, p, vl, "-some", 0);
2397 0
                        if (!times)
2398 0
                                vtc_fatal(vl, "-some argument must be more"
2399 0
                                               "than 0 (found \"%s\")\n", *av);
2400 0
                } else if (!strcmp(*av, "-all")) {
2401 0
                        loop = 1;
2402 0
                } else
2403
                        break;
2404 1
        }
2405 0
        if (*av != NULL)
2406
                vtc_fatal(vl, "Unknown rxpush spec: %s\n", *av);
2407 1
2408 1
        do {
2409 1
                f = rxstuff(s);
2410 0
                if (!f)
2411 1
                        return;
2412 1
                rcv++;
2413 1
                CHKFRAME(f->type, expect, rcv, "rxpush");
2414 1
                expect = TYPE_CONTINUATION;
2415 1
        } while (rcv < times || (loop && !(f->flags & END_HEADERS)));
2416 1
        s->frame = f;
2417
}
2418
2419
/* SECTION: stream.spec.winup_rxwinup rxwinup
2420
 *
2421
 * Receive a WINDOW_UPDATE frame.
2422
 */
2423 25
static void
2424
cmd_rxwinup(CMD_ARGS)
2425
{
2426
        struct stream *s;
2427
        struct frame *f;
2428 25
2429 25
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2430 25
        s->frame = rxstuff(s);
2431 25
        CAST_OBJ_NOTNULL(f, s->frame, FRAME_MAGIC);
2432 25
        CHKFRAME(f->type, TYPE_WINDOW_UPDATE, 0, *av);
2433 6
        if (s->id == 0)
2434 25
                s->hp->h2_win_peer->size += s->frame->md.winup_size;
2435 25
        s->win_peer += s->frame->md.winup_size;
2436
}
2437
2438
/* SECTION: stream.spec.settings_rxsettings rxsettings
2439
 *
2440
 * Receive a SETTINGS frame.
2441
 */
2442 414
static void
2443
cmd_rxsettings(CMD_ARGS)
2444
{
2445 414
        struct stream *s, *s2;
2446
        uint32_t val = 0;
2447
        struct http *hp;
2448
        struct frame *f;
2449 414
2450 414
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2451 414
        CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
2452 414
        s->frame = rxstuff(s);
2453 414
        CAST_OBJ_NOTNULL(f, s->frame, FRAME_MAGIC);
2454 414
        CHKFRAME(f->type, TYPE_SETTINGS, 0, *av);
2455 135
        if (! isnan(f->md.settings[SETTINGS_INITIAL_WINDOW_SIZE])) {
2456 277
                val = (uint32_t)f->md.settings[SETTINGS_INITIAL_WINDOW_SIZE];
2457 142
                VTAILQ_FOREACH(s2, &hp->streams, list)
2458 135
                        s2->win_peer += (val - hp->h2_win_peer->init);
2459 135
                hp->h2_win_peer->init = val;
2460 414
        }
2461
}
2462
/* SECTION: stream.spec.prio_rxprio rxprio
2463
 *
2464
 * Receive a PRIORITY frame.
2465
 */
2466 9
static void
2467
cmd_rxprio (CMD_ARGS)
2468
{
2469 9
        struct stream *s;
2470 9
        (void)av;
2471 9
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2472 6
        if (s->hp->no_rfc7540_priorities) {
2473 6
                vtc_log(vl, 4, "skipping rxprio: no_rfc7540_priorities is set");
2474
                return;
2475 3
        }
2476 3
        s->frame = rxstuff(s);
2477 0
        if (s->frame != NULL && s->frame->type != TYPE_PRIORITY) {
2478
                vtc_fatal(vl,
2479 0
                    "Wrong frame type %s (%d) wanted %s",
2480 0
                    s->frame->type < TYPE_MAX ?
2481 0
                    h2_types[s->frame->type] : "?",
2482
                    s->frame->type, "PRIORITY");
2483 9
        }
2484
}
2485
2486
#define RXFUNC(lctype, upctype) \
2487
        static void \
2488
        cmd_rx ## lctype(CMD_ARGS) \
2489
        { \
2490
                struct stream *s; \
2491
                (void)av; \
2492
                CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC); \
2493
                s->frame = rxstuff(s); \
2494
                if (s->frame != NULL && s->frame->type != TYPE_ ## upctype) { \
2495
                        vtc_fatal(vl, \
2496
                            "Wrong frame type %s (%d) wanted %s", \
2497
                            s->frame->type < TYPE_MAX ? \
2498
                            h2_types[s->frame->type] : "?", \
2499
                            s->frame->type, #upctype); \
2500
                } \
2501
        }
2502
2503
/* SECTION: stream.spec.reset_rxrst rxrst
2504
 *
2505
 * Receive a RST_STREAM frame.
2506 54
 */
2507
RXFUNC(rst,     RST_STREAM)
2508
2509
/* SECTION: stream.spec.ping_rxping rxping
2510
 *
2511
 * Receive a PING frame.
2512 6
 */
2513
RXFUNC(ping,    PING)
2514
2515
/* SECTION: stream.spec.goaway_rxgoaway rxgoaway
2516
 *
2517
 * Receive a GOAWAY frame.
2518 39
 */
2519
RXFUNC(goaway,  GOAWAY)
2520
2521
/* SECTION: stream.spec.frame_rxframe
2522
 *
2523
 * Receive a frame, any frame.
2524
 */
2525 0
static void
2526
cmd_rxframe(CMD_ARGS)
2527
{
2528
        struct stream *s;
2529 0
2530 0
        (void)vl;
2531 0
        (void)av;
2532 0
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2533 0
        if (rxstuff(s) == NULL)
2534 0
                vtc_fatal(s->vl, "No frame received");
2535
}
2536
2537 761
static void
2538
cmd_expect(CMD_ARGS)
2539
{
2540
        struct http *hp;
2541
        struct stream *s;
2542
        const char *lhs;
2543
        char *cmp;
2544
        const char *rhs;
2545
        char buf[20];
2546 761
2547 761
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2548 761
        hp = s->hp;
2549
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2550 761
2551 761
        AZ(strcmp(av[0], "expect"));
2552
        av++;
2553 761
2554 761
        AN(av[0]);
2555 761
        AN(av[1]);
2556 761
        AN(av[2]);
2557 761
        AZ(av[3]);
2558 761
        PTOK(pthread_mutex_lock(&s->hp->mtx));
2559 761
        lhs = cmd_var_resolve(s, av[0], buf);
2560 761
        cmp = av[1];
2561 761
        rhs = cmd_var_resolve(s, av[2], buf);
2562 761
        vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
2563 761
        PTOK(pthread_mutex_unlock(&s->hp->mtx));
2564
}
2565
2566
/* SECTION: stream.spec.gunzip gunzip
2567
 *
2568
 * Same as the ``gunzip`` command for HTTP/1.
2569
 */
2570 2
static void
2571
cmd_gunzip(CMD_ARGS)
2572
{
2573
        struct http *hp;
2574
        struct stream *s;
2575 2
2576 2
        (void)av;
2577
        (void)vl;
2578 2
2579 2
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2580 2
        hp = s->hp;
2581
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2582 2
2583 2
        vtc_gunzip(s->hp, s->body, &s->bodylen);
2584
}
2585
2586
/* SECTION: stream.spec.write_body
2587
 *
2588
 * write_body STRING
2589
 *      Same as the ``write_body`` command for HTTP/1.
2590
 */
2591 2
static void
2592
cmd_write_body(CMD_ARGS)
2593
{
2594
        struct stream *s;
2595 2
2596 2
        (void)vl;
2597 2
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2598 2
        AN(av[0]);
2599 2
        AN(av[1]);
2600 2
        AZ(av[2]);
2601 2
        AZ(strcmp(av[0], "write_body"));
2602 0
        if (VFIL_writefile(NULL, av[1], s->body, s->bodylen) != 0)
2603 0
                vtc_fatal(s->vl, "failed to write body: %s (%d)",
2604 2
                    strerror(errno), errno);
2605
}
2606
2607
/* SECTION: stream.spec Specification
2608
 *
2609
 * The specification of a stream follows the exact same rules as one for a
2610
 * client or a server.
2611
 */
2612
static const struct cmds stream_cmds[] = {
2613
#define CMD_STREAM(n) { #n, cmd_##n },
2614
        /* spec */
2615
        CMD_STREAM(expect)
2616
        CMD_STREAM(gunzip)
2617
        CMD_STREAM(rxcont)
2618
        CMD_STREAM(rxdata)
2619
        CMD_STREAM(rxframe)
2620
        CMD_STREAM(rxgoaway)
2621
        CMD_STREAM(rxhdrs)
2622
        CMD_STREAM(rxping)
2623
        CMD_STREAM(rxprio)
2624
        CMD_STREAM(rxpush)
2625
        CMD_STREAM(rxreq)
2626
        CMD_STREAM(rxresp)
2627
        CMD_STREAM(rxrst)
2628
        CMD_STREAM(rxsettings)
2629
        CMD_STREAM(rxwinup)
2630
        CMD_STREAM(sendhex)
2631
        CMD_STREAM(txcont)
2632
        CMD_STREAM(txdata)
2633
        CMD_STREAM(txgoaway)
2634
        CMD_STREAM(txping)
2635
        CMD_STREAM(txprio)
2636
        CMD_STREAM(txpush)
2637
        CMD_STREAM(txreq)
2638
        CMD_STREAM(txresp)
2639
        CMD_STREAM(txrst)
2640
        CMD_STREAM(txsettings)
2641
        CMD_STREAM(txwinup)
2642
        CMD_STREAM(write_body)
2643
        { NULL, NULL }
2644
#undef CMD_STREAM
2645
};
2646
2647 579
static void *
2648
stream_thread(void *priv)
2649
{
2650
        struct stream *s;
2651 579
2652 579
        CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
2653 579
        parse_string(s->vl, s, s->spec);
2654 579
        vtc_log(s->vl, 2, "Ending stream %u", s->id);
2655
        return (NULL);
2656
}
2657
/**********************************************************************
2658
 * Allocate and initialize a stream
2659
 */
2660
2661 497
static struct stream *
2662
stream_new(const char *name, struct http *h)
2663
{
2664
        char *p, buf[20];
2665
        struct stream *s;
2666 497
2667 20
        if (!strcmp("next", name)) {
2668 13
                if (h->last_stream > 0)
2669
                        bprintf(buf, "%d", h->last_stream + 2);
2670 7
                else
2671 20
                        bprintf(buf, "%d", 1);
2672 20
                name = buf;
2673
        }
2674 497
2675 497
        ALLOC_OBJ(s, STREAM_MAGIC);
2676 497
        AN(s);
2677 497
        PTOK(pthread_cond_init(&s->cond, NULL));
2678 497
        REPLACE(s->name, name);
2679 497
        AN(s->name);
2680 497
        VTAILQ_INIT(&s->fq);
2681 497
        s->win_self = h->h2_win_self->init;
2682 497
        s->win_peer = h->h2_win_peer->init;
2683 497
        s->vl = vtc_logopen("%s.%s", h->sess->name, name);
2684
        vtc_log_set_cmd(s->vl, stream_cmds);
2685 497
2686 497
        s->weight = 16;
2687
        s->dependency = 0;
2688 497
2689 497
        STRTOU32(s->id, name, p, s->vl, "stream");
2690 0
        if (s->id & (1U << 31))
2691 0
                vtc_fatal(s->vl, "Stream id must be a 31-bits integer "
2692
                    "(found %s)", name);
2693 497
2694 497
        CHECK_OBJ_NOTNULL(h, HTTP_MAGIC);
2695 497
        s->hp = h;
2696
        h->last_stream = s->id;
2697
2698 497
        //bprintf(s->connect, "%s", "${v1_sock}");
2699 497
        PTOK(pthread_mutex_lock(&h->mtx));
2700 497
        VTAILQ_INSERT_HEAD(&h->streams, s, list);
2701 497
        PTOK(pthread_mutex_unlock(&h->mtx));
2702
        return (s);
2703
}
2704
2705
/**********************************************************************
2706
 * Clean up stream
2707
 */
2708
2709 497
static void
2710
stream_delete(struct stream *s)
2711
{
2712
        struct frame *f, *f2;
2713 497
2714
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2715 530
2716 33
        VTAILQ_FOREACH_SAFE(f, &s->fq, list, f2) {
2717 33
                VTAILQ_REMOVE(&s->fq, f, list);
2718 33
                clean_frame(&f);
2719 497
        }
2720 497
        vtc_logclose(s->vl);
2721 497
        clean_headers(s->req);
2722 497
        clean_headers(s->resp);
2723 497
        AZ(s->frame);
2724 497
        free(s->body);
2725 497
        free(s->spec);
2726 497
        free(s->name);
2727 497
        FREE_OBJ(s);
2728
}
2729
2730
/**********************************************************************
2731
 * Start the stream thread
2732
 */
2733
2734 579
static void
2735
stream_start(struct stream *s)
2736 579
{
2737 579
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2738 579
        vtc_log(s->hp->vl, 2, "Starting stream %s (%p)", s->name, s);
2739 579
        PTOK(pthread_create(&s->tp, NULL, stream_thread, s));
2740 579
        s->running = 1;
2741
}
2742
2743
/**********************************************************************
2744
 * Wait for stream thread to stop
2745
 */
2746 579
static void
2747
stream_wait(struct stream *s)
2748
{
2749
        void *res;
2750
        struct frame *f, *f2;
2751 579
2752 579
        CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
2753 579
        vtc_log(s->hp->vl, 2, "Waiting for stream %u", s->id);
2754 579
        PTOK(pthread_join(s->tp, &res));
2755 0
        if (res != NULL)
2756 0
                vtc_fatal(s->hp->vl, "Stream %u returned \"%s\"", s->id,
2757
                    (char *)res);
2758 585
2759 6
        VTAILQ_FOREACH_SAFE(f, &s->fq, list, f2) {
2760 6
                VTAILQ_REMOVE(&s->fq, f, list);
2761 6
                clean_frame(&f);
2762 579
        }
2763 579
        clean_frame(&s->frame);
2764 579
        s->tp = 0;
2765 579
        s->running = 0;
2766
}
2767
2768
/**********************************************************************
2769
 * Run the stream thread
2770
 */
2771
2772 529
static void
2773
stream_run(struct stream *s)
2774 529
{
2775 529
        stream_start(s);
2776 529
        stream_wait(s);
2777
}
2778
2779
2780
2781
/* SECTION: client-server.spec.stream
2782
 *
2783
 * stream
2784
 *      HTTP/2 introduces the concept of streams, and these come with
2785
 *      their own specification, and as it's quite big, have been moved
2786
 *      to their own chapter.
2787
 *
2788
 * SECTION: stream stream
2789
 *
2790
 * (note: this section is at the top-level for easier navigation, but
2791
 * it's part of the client/server specification)
2792
 *
2793
 * Streams map roughly to a request in HTTP/2, a request is sent on
2794
 * stream N, the response too, then the stream is discarded. The main
2795
 * exception is the first stream, 0, that serves as coordinator.
2796
 *
2797
 * Stream syntax follow the client/server one::
2798
 *
2799
 *      stream ID [SPEC] [ACTION]
2800
 *
2801
 * ID is the HTTP/2 stream number, while SPEC describes what will be
2802
 * done in that stream. If ID has the value ``next``, the actual stream
2803
 * number is computed based on the last one.
2804
 *
2805
 * Note that, when parsing a stream action, if the entity isn't operating
2806
 * in HTTP/2 mode, these spec is ran before::
2807
 *
2808
 *      txpri/rxpri # client/server
2809
 *      stream 0 {
2810
 *          txsettings
2811
 *          rxsettings
2812
 *          txsettings -ack
2813
 *          rxsettings
2814
 *          expect settings.ack == true
2815
 *      } -run
2816
 *
2817
 * And HTTP/2 mode is then activated before parsing the specification.
2818
 *
2819
 * SECTION: stream.actions Actions
2820
 *
2821
 * \-start
2822
 *      Run the specification in a thread, giving back control immediately.
2823
 *
2824
 * \-wait
2825
 *      Wait for the started thread to finish running the spec.
2826
 *
2827
 * \-run
2828
 *      equivalent to calling ``-start`` then ``-wait``.
2829
 */
2830
2831 600
void
2832
cmd_stream(CMD_ARGS)
2833
{
2834
        struct stream *s;
2835
        struct http *h;
2836 600
2837 600
        (void)vl;
2838
        CAST_OBJ_NOTNULL(h, priv, HTTP_MAGIC);
2839 600
2840 600
        AZ(strcmp(av[0], "stream"));
2841
        av++;
2842 1175
2843 678
        VTAILQ_FOREACH(s, &h->streams, list)
2844 103
                if (!strcmp(s->name, av[0]))
2845 600
                        break;
2846 497
        if (s == NULL)
2847 600
                s = stream_new(av[0], h);
2848
        av++;
2849 1780
2850 1180
        for (; *av != NULL; av++) {
2851 0
                if (vtc_error)
2852
                        break;
2853 1180
2854 22
                if (!strcmp(*av, "-wait")) {
2855 22
                        stream_wait(s);
2856
                        continue;
2857
                }
2858
2859 1158
                /* Don't muck about with a running client */
2860 0
                if (s->running)
2861
                        stream_wait(s);
2862 1158
2863 50
                if (!strcmp(*av, "-start")) {
2864 50
                        stream_start(s);
2865
                        continue;
2866 1108
                }
2867 529
                if (!strcmp(*av, "-run")) {
2868 529
                        stream_run(s);
2869
                        continue;
2870 579
                }
2871 0
                if (**av == '-')
2872 579
                        vtc_fatal(vl, "Unknown stream argument: %s", *av);
2873 579
                REPLACE(s->spec, *av);
2874 600
        }
2875
}
2876
2877 3
void
2878
b64_settings(struct http *hp, const char *s)
2879
{
2880
        uint16_t i;
2881
        uint64_t v, vv;
2882
        const char *buf;
2883
        int shift;
2884 12
2885 9
        while (*s) {
2886 81
                v = 0;
2887 72
                for (shift = 42; shift >= 0; shift -= 6) {
2888 60
                        if (*s >= 'A' && *s <= 'Z')
2889 12
                                vv = (*s - 'A');
2890 6
                        else if (*s >= 'a' && *s <= 'z')
2891 6
                                vv = (*s - 'a') + 26;
2892 0
                        else if (*s >= '0' && *s <= '9')
2893 6
                                vv = (*s - '0') + 52;
2894 0
                        else if (*s == '-')
2895 6
                                vv = 62;
2896 6
                        else if (*s == '_')
2897
                                vv = 63;
2898 0
                        else
2899
                                vtc_fatal(hp->vl,
2900 72
                                    "Bad \"HTTP2-Settings\" header");
2901 72
                        v |= vv << shift;
2902 72
                        s++;
2903 9
                }
2904 9
                i = v >> 32;
2905
                v &= 0xffff;
2906 9
2907 9
                if (i <= SETTINGS_MAX)
2908
                        buf = h2_settings[i];
2909 0
                else
2910
                        buf = "unknown";
2911 9
2912 3
                if (i == SETTINGS_NO_RFC7540_PRIORITIES) {
2913 3
                        hp->no_rfc7540_priorities = v;
2914 9
                }
2915
                if (i == SETTINGS_HEADER_TABLE_SIZE) {
2916 0
                        enum hpk_result hrs;
2917 0
                        if (hp->sfd) {
2918 0
                                AN(hp->encctx);
2919 0
                                hrs = HPK_ResizeTbl(hp->encctx, v);
2920 0
                        } else {
2921 0
                                AN(hp->decctx);
2922
                                hrs = HPK_ResizeTbl(hp->decctx, v);
2923 0
                        }
2924 0
                        if (hrs != hpk_done)
2925 0
                                vtc_fatal(hp->vl, "HPK resize failed %d\n", hrs);
2926
                }
2927 18
2928 9
                vtc_log(hp->vl, 4, "Upgrade: %s (%d): %ju",
2929
                    buf, i, (intmax_t)v);
2930 3
        }
2931
}
2932
2933 211
void
2934
start_h2(struct http *hp)
2935 211
{
2936 211
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2937 211
        PTOK(pthread_mutex_init(&hp->mtx, NULL));
2938 211
        PTOK(pthread_cond_init(&hp->cond, NULL));
2939 211
        VTAILQ_INIT(&hp->streams);
2940 211
        hp->h2_win_self->init = 0xffff;
2941 211
        hp->h2_win_self->size = 0xffff;
2942 211
        hp->h2_win_peer->init = 0xffff;
2943 211
        hp->h2_win_peer->size = 0xffff;
2944
        hp->h2 = 1;
2945 211
2946 211
        hp->decctx = HPK_NewCtx(4096);
2947 211
        hp->encctx = HPK_NewCtx(4096);
2948 211
        PTOK(pthread_create(&hp->tp, NULL, receive_frame, hp));
2949
}
2950
2951 211
void
2952
stop_h2(struct http *hp)
2953
{
2954
        struct stream *s, *s2;
2955 211
2956 708
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
2957 497
        VTAILQ_FOREACH_SAFE(s, &hp->streams, list, s2) {
2958 28
                if (s->running)
2959 497
                        stream_wait(s);
2960 497
                PTOK(pthread_mutex_lock(&hp->mtx));
2961 497
                VTAILQ_REMOVE(&hp->streams, s, list);
2962 497
                PTOK(pthread_mutex_unlock(&hp->mtx));
2963 497
                stream_delete(s);
2964
        }
2965 211
2966 211
        PTOK(pthread_mutex_lock(&hp->mtx));
2967 211
        hp->h2 = 0;
2968 211
        PTOK(pthread_cond_signal(&hp->cond));
2969 211
        PTOK(pthread_mutex_unlock(&hp->mtx));
2970
        PTOK(pthread_join(hp->tp, NULL));
2971 211
2972 211
        HPK_FreeCtx(hp->decctx);
2973
        HPK_FreeCtx(hp->encctx);
2974 211
2975 211
        PTOK(pthread_mutex_destroy(&hp->mtx));
2976 211
        PTOK(pthread_cond_destroy(&hp->cond));
2977
}