This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvtc_http2.c
More file actions
2976 lines (2680 loc) · 67.6 KB
/
vtc_http2.c
File metadata and controls
2976 lines (2680 loc) · 67.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2008-2016 Varnish Software AS
* All rights reserved.
*
* Author: Guillaume Quintard <guillaume.quintard@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <sys/socket.h>
#include <math.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include "vtc.h"
#include "vtc_http.h"
#include "vfil.h"
#include "hpack.h"
#include "vend.h"
#define ERR_MAX 13
#define BUF_SIZE (1024*2048)
static const char *const h2_errs[] = {
#define H2_ERROR(n,v,sc,g,r,t) [v] = #n,
#include <tbl/h2_error.h>
NULL
};
static const char *const h2_types[] = {
#define H2_FRAME(l,u,t,f,...) [t] = #u,
#include <tbl/h2_frames.h>
NULL
};
static const char * const h2_settings[] = {
[0] = "unknown",
#define H2_SETTING(U,l,v,...) [v] = #U,
#include <tbl/h2_settings.h>
NULL
};
enum h2_settings_e {
#define H2_SETTING(U,l,v,...) SETTINGS_##U = v,
#include <tbl/h2_settings.h>
SETTINGS_MAX
};
enum h2_type_e {
#define H2_FRAME(l,u,t,f,...) TYPE_##u = t,
#include <tbl/h2_frames.h>
TYPE_MAX
};
//lint -save -e849 Same enum value
enum {
ACK = 0x1,
END_STREAM = 0x1,
PADDED = 0x8,
END_HEADERS = 0x4,
PRIORITY = 0x20,
};
//lint -restore
struct stream {
unsigned magic;
#define STREAM_MAGIC 0x63f1fac2
uint32_t id;
struct vtclog *vl;
char *spec;
char *name;
VTAILQ_ENTRY(stream) list;
unsigned running;
pthread_cond_t cond;
struct frame *frame;
pthread_t tp;
struct http *hp;
int64_t win_self;
int64_t win_peer;
int wf;
VTAILQ_HEAD(, frame) fq;
char *body;
long bodylen;
struct hpk_hdr req[MAX_HDR];
struct hpk_hdr resp[MAX_HDR];
int dependency;
int weight;
};
static void
clean_headers(struct hpk_hdr *h)
{
unsigned n = MAX_HDR;
while (h->t && n > 0) {
if (h->key.len)
free(h->key.ptr);
free(h->value.ptr);
memset(h, 0, sizeof(*h));
h++;
n--;
}
}
#define ONLY_H2_CLIENT(hp, av) \
do { \
if (hp->sfd != NULL) \
vtc_fatal(s->vl, \
"\"%s\" only possible in client", av[0]); \
} while (0)
#define ONLY_H2_SERVER(hp, av) \
do { \
if (hp->sfd == NULL) \
vtc_fatal(s->vl, \
"\"%s\" only possible in server", av[0]); \
} while (0)
static void
http_write(const struct http *hp, int lvl,
const char *buf, int s, const char *pfx)
{
ssize_t l;
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
AN(buf);
AN(pfx);
vtc_dump(hp->vl, lvl, pfx, buf, s);
l = write(hp->sess->fd, buf, s);
if (l != s)
vtc_log(hp->vl, hp->fatal, "Write failed: (%zd vs %d) %s",
l, s, strerror(errno));
}
static int
get_bytes(const struct http *hp, char *buf, size_t n)
{
int i;
struct pollfd pfd[1];
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
AN(buf);
while (n > 0) {
pfd[0].fd = hp->sess->fd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
i = poll(pfd, 1, (int)(hp->timeout * 1000));
if (i < 0 && errno == EINTR)
continue;
if (i == 0)
vtc_log(hp->vl, 3,
"HTTP2 rx timeout (fd:%d %.3fs)",
hp->sess->fd, hp->timeout);
if (i < 0)
vtc_log(hp->vl, 3,
"HTTP2 rx failed (fd:%d poll: %s)",
hp->sess->fd, strerror(errno));
if (i <= 0)
return (i);
i = read(hp->sess->fd, buf, n);
if (!(pfd[0].revents & POLLIN))
vtc_log(hp->vl, 4,
"HTTP2 rx poll (fd:%d revents: %x n=%zu, i=%d)",
hp->sess->fd, pfd[0].revents, n, i);
if (i == 0)
vtc_log(hp->vl, 3,
"HTTP2 rx EOF (fd:%d read: %s)",
hp->sess->fd, strerror(errno));
if (i < 0)
vtc_log(hp->vl, 3,
"HTTP2 rx failed (fd:%d read: %s)",
hp->sess->fd, strerror(errno));
if (i <= 0)
return (i);
n -= i;
}
return (1);
}
VTAILQ_HEAD(fq_head, frame);
struct frame {
unsigned magic;
#define FRAME_MAGIC 0x5dd3ec4
uint32_t size;
uint32_t stid;
uint8_t type;
uint8_t flags;
char *data;
VTAILQ_ENTRY(frame) list;
union {
struct {
uint32_t stream;
uint8_t exclusive;
uint8_t weight;
} prio;
uint32_t rst_err;
double settings[SETTINGS_MAX+1];
struct {
char data[9];
int ack;
} ping;
struct {
uint32_t err;
uint32_t stream;
char *debug;
} goaway;
uint32_t winup_size;
uint32_t promised;
uint8_t padded;
} md;
};
static void
readFrameHeader(struct frame *f, const char *buf)
{
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
AN(buf);
f->size = (unsigned char)buf[0] << 16;
f->size += (unsigned char)buf[1] << 8;
f->size += (unsigned char)buf[2];
f->type = (unsigned char)buf[3];
f->flags = (unsigned char)buf[4];
f->stid = vbe32dec(buf+5);
}
static void
writeFrameHeader(char *buf, const struct frame *f)
{
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
AN(buf);
buf[0] = (f->size >> 16) & 0xff;
buf[1] = (f->size >> 8) & 0xff;
buf[2] = (f->size ) & 0xff;
buf[3] = f->type;
buf[4] = f->flags;
vbe32enc(buf + 5, f->stid);
}
#define INIT_FRAME(f, ty, sz, id, fl) \
do { \
f.magic = FRAME_MAGIC; \
f.type = TYPE_ ## ty; \
f.size = sz; \
f.stid = id; \
f.flags = fl; \
f.data = NULL; \
} while(0)
static void
replace_frame(struct frame **fp, struct frame *new)
{
struct frame *old;
AN(fp);
CHECK_OBJ_ORNULL(new, FRAME_MAGIC);
old = *fp;
*fp = new;
if (old == NULL)
return;
CHECK_OBJ(old, FRAME_MAGIC);
if (old->type == TYPE_GOAWAY)
free(old->md.goaway.debug);
free(old->data);
FREE_OBJ(old);
}
static void
clean_frame(struct frame **fp)
{
replace_frame(fp, NULL);
}
static void
write_frame(struct stream *sp, const struct frame *f, const unsigned lock)
{
struct http *hp;
ssize_t l;
char hdr[9];
CHECK_OBJ_NOTNULL(sp, STREAM_MAGIC);
hp = sp->hp;
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
writeFrameHeader(hdr, f);
vtc_log(sp->vl, 3,
"tx: stream: %d, type: %s (%d), flags: 0x%02x, size: %d",
f->stid,
f->type < TYPE_MAX ? h2_types[f->type] : "?",
f->type, f->flags, f->size);
if (f->type == TYPE_DATA) {
sp->win_peer -= f->size;
hp->h2_win_peer->size -= f->size;
}
if (lock)
PTOK(pthread_mutex_lock(&hp->mtx));
l = write(hp->sess->fd, hdr, sizeof(hdr));
if (l != sizeof(hdr))
vtc_log(sp->vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
l, sizeof(hdr), strerror(errno));
if (f->size) {
AN(f->data);
l = write(hp->sess->fd, f->data, f->size);
if (l != f->size)
vtc_log(sp->vl, hp->fatal,
"Write failed: (%zd vs %d) %s",
l, f->size, strerror(errno));
}
if (lock)
PTOK(pthread_mutex_unlock(&hp->mtx));
}
static void
exclusive_stream_dependency(const struct stream *s)
{
struct stream *target;
struct http *hp = s->hp;
if (s->id == 0)
return;
VTAILQ_FOREACH(target, &hp->streams, list) {
if (target->id != s->id && target->dependency == s->dependency)
target->dependency = s->id;
}
}
static void
explain_flags(uint8_t flags, uint8_t type, struct vtclog *vl)
{
if (flags & ACK && (type == TYPE_PING || type == TYPE_SETTINGS)) {
vtc_log(vl, 3, "flag: ACK");
} else if (flags & END_STREAM && (type == TYPE_HEADERS ||
type == TYPE_PUSH_PROMISE || type == TYPE_DATA)) {
vtc_log(vl, 3, "flag: END_STREAM");
} else if (flags & END_HEADERS && (type == TYPE_HEADERS ||
type == TYPE_PUSH_PROMISE || type == TYPE_CONTINUATION)) {
vtc_log(vl, 3, "flag: END_TYPE_HEADERS");
} else if (flags & PRIORITY && (type == TYPE_HEADERS ||
type == TYPE_PUSH_PROMISE)) {
vtc_log(vl, 3, "flag: END_PRIORITY");
} else if (flags & PADDED && (type == TYPE_DATA || type ==
TYPE_HEADERS || type == TYPE_PUSH_PROMISE)) {
vtc_log(vl, 3, "flag: PADDED");
} else if (flags)
vtc_log(vl, 3, "UNKNOWN FLAG(S): 0x%02x", flags);
}
static void
parse_data(struct stream *s, struct frame *f)
{
struct http *hp;
uint32_t size = f->size;
char *data = f->data;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->flags & PADDED) {
f->md.padded = *((uint8_t *)data);
if (f->md.padded >= size) {
vtc_log(s->vl, hp->fatal,
"invalid padding: %d reported,"
"but size is only %d",
f->md.padded, size);
size = 0;
f->md.padded = 0;
}
data++;
size -= f->md.padded + 1;
vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
}
if (s->id)
s->win_self -= size;
s->hp->h2_win_self->size -= size;
if (!size) {
AZ(data);
vtc_log(s->vl, 4, "s%u - no data", s->id);
return;
}
s->body = realloc(s->body, s->bodylen + size + 1L);
AN(s->body);
memcpy(s->body + s->bodylen, data, size);
s->bodylen += size;
s->body[s->bodylen] = '\0';
}
static void
decode_hdr(struct http *hp, struct hpk_hdr *h, const struct vsb *vsb)
{
struct hpk_iter *iter;
enum hpk_result r = hpk_err;
int n;
CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
CAST_OBJ_NOTNULL(hp, hp, HTTP_MAGIC);;
if (VSB_len(vsb) == 0)
return;
iter = HPK_NewIter(hp->decctx, VSB_data(vsb), VSB_len(vsb));
n = 0;
while (n < MAX_HDR && h[n].t)
n++;
while (n < MAX_HDR) {
r = HPK_DecHdr(iter, h + n);
if (r == hpk_err )
break;
vtc_log(hp->vl, 4, "header[%2d]: %s: %s",
n, h[n].key.ptr, h[n].value.ptr);
n++;
if (r == hpk_done)
break;
}
if (r != hpk_done) {
vtc_log(hp->vl, hp->fatal ? 4 : 0,
"Header decoding failed (%d) %d", r, hp->fatal);
} else if (n == MAX_HDR) {
vtc_log(hp->vl, hp->fatal,
"Max number of headers reached (%d)", MAX_HDR);
}
HPK_FreeIter(iter);
}
static void
parse_hdr(struct stream *s, struct frame *f, struct vsb *vsb)
{
int shift = 0;
int exclusive = 0;
uint32_t size = f->size;
char *data = f->data;
struct http *hp;
uint32_t n;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->flags & PADDED && f->type != TYPE_CONTINUATION) {
f->md.padded = *((uint8_t *)data);
if (f->md.padded >= size) {
vtc_log(s->vl, hp->fatal,
"invalid padding: %d reported,"
"but size is only %d",
f->md.padded, size);
size = 0;
f->md.padded = 0;
}
shift += 1;
size -= f->md.padded;
vtc_log(s->vl, 4, "padding: %3d", f->md.padded);
}
if (f->type == TYPE_HEADERS && f->flags & PRIORITY){
shift += 5;
n = vbe32dec(f->data);
s->dependency = n & ~(1U << 31);
exclusive = n >> 31;
s->weight = f->data[4];
if (exclusive)
exclusive_stream_dependency(s);
vtc_log(s->vl, 4, "stream->dependency: %u", s->dependency);
vtc_log(s->vl, 4, "stream->weight: %u", s->weight);
} else if (f->type == TYPE_PUSH_PROMISE){
shift += 4;
n = vbe32dec(f->data);
f->md.promised = n & ~(1U << 31);
}
AZ(VSB_bcat(vsb, data + shift, size - shift));
}
static void
parse_prio(struct stream *s, struct frame *f)
{
struct http *hp;
char *buf;
uint32_t n;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size != 5)
vtc_fatal(s->vl, "Size should be 5, but isn't (%d)", f->size);
buf = f->data;
AN(buf);
n = vbe32dec(f->data);
f->md.prio.stream = n & ~(1U << 31);
s->dependency = f->md.prio.stream;
if (n >> 31){
f->md.prio.exclusive = 1;
exclusive_stream_dependency(s);
}
buf += 4;
f->md.prio.weight = *buf;
s->weight = f->md.prio.weight;
vtc_log(s->vl, 3, "prio->stream: %u", f->md.prio.stream);
vtc_log(s->vl, 3, "prio->weight: %u", f->md.prio.weight);
}
static void
parse_rst(const struct stream *s, struct frame *f)
{
struct http *hp;
uint32_t err;
const char *buf;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size != 4)
vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
err = vbe32dec(f->data);
f->md.rst_err = err;
vtc_log(s->vl, 2, "ouch");
if (err <= ERR_MAX)
buf = h2_errs[err];
else
buf = "unknown";
vtc_log(s->vl, 4, "rst->err: %s (%d)", buf, err);
}
static void
parse_settings(const struct stream *s, struct frame *f)
{
struct http *hp;
int v;
unsigned u, t;
const char *buf;
enum hpk_result r;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size % 6)
vtc_fatal(s->vl,
"Size should be a multiple of 6, but isn't (%d)", f->size);
if (s->id != 0)
vtc_fatal(s->vl,
"Setting frames should only be on stream 0, but received on stream: %d", s->id);
for (u = 0; u <= SETTINGS_MAX; u++)
f->md.settings[u] = NAN;
for (u = 0; u < f->size;) {
t = vbe16dec(f->data + u);
u += 2;
v = vbe32dec(f->data + u);
if (t <= SETTINGS_MAX) {
buf = h2_settings[t];
f->md.settings[t] = v;
} else
buf = "unknown";
u += 4;
if (t == SETTINGS_HEADER_TABLE_SIZE) {
r = HPK_ResizeTbl(s->hp->encctx, v);
assert(r == hpk_done);
}
vtc_log(s->vl, 4, "settings->%s (%u): %d", buf, t, v);
}
}
static void
parse_ping(const struct stream *s, struct frame *f)
{
struct http *hp;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size != 8)
vtc_fatal(s->vl, "Size should be 8, but isn't (%d)", f->size);
f->md.ping.ack = f->flags & ACK;
memcpy(f->md.ping.data, f->data, 8);
f->md.ping.data[8] = '\0';
vtc_log(s->vl, 4, "ping->data: %s", f->md.ping.data);
}
static void
parse_goaway(const struct stream *s, struct frame *f)
{
struct http *hp;
const char *err_buf;
uint32_t err, stid;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size < 8)
vtc_fatal(s->vl,
"Size should be at least 8, but isn't (%d)", f->size);
if (f->data[0] & (1<<7))
vtc_fatal(s->vl,
"First bit of data is reserved and should be 0");
stid = vbe32dec(f->data);
err = vbe32dec(f->data + 4);
f->md.goaway.err = err;
f->md.goaway.stream = stid;
if (err <= ERR_MAX)
err_buf = h2_errs[err];
else
err_buf = "unknown";
if (f->size > 8) {
f->md.goaway.debug = malloc((f->size - 8) + 1L);
AN(f->md.goaway.debug);
f->md.goaway.debug[f->size - 8] = '\0';
memcpy(f->md.goaway.debug, f->data + 8, f->size - 8);
}
vtc_log(s->vl, 3, "goaway->laststream: %d", stid);
vtc_log(s->vl, 3, "goaway->err: %s (%d)", err_buf, err);
if (f->md.goaway.debug)
vtc_log(s->vl, 3, "goaway->debug: %s", f->md.goaway.debug);
}
static void
parse_winup(const struct stream *s, struct frame *f)
{
struct http *hp;
uint32_t size;
CHECK_OBJ_NOTNULL(f, FRAME_MAGIC);
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);;
if (f->size != 4)
vtc_fatal(s->vl, "Size should be 4, but isn't (%d)", f->size);
if (f->data[0] & (1<<7))
vtc_log(s->vl, s->hp->fatal,
"First bit of data is reserved and should be 0");
size = vbe32dec(f->data);
f->md.winup_size = size;
vtc_log(s->vl, 3, "winup->size: %d", size);
}
/* read a frame and queue it in the relevant stream, wait if not present yet.
*/
static void *
receive_frame(void *priv)
{
struct http *hp;
char hdr[9];
struct frame *f;
struct stream *s;
int expect_cont = 0;
struct vsb *vsb = NULL;
struct hpk_hdr *hdrs = NULL;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
PTOK(pthread_mutex_lock(&hp->mtx));
while (hp->h2) {
/*no wanted frames? */
assert(hp->wf >= 0);
if (hp->wf == 0) {
PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
continue;
}
PTOK(pthread_mutex_unlock(&hp->mtx));
if (get_bytes(hp, hdr, sizeof hdr) <= 0) {
PTOK(pthread_mutex_lock(&hp->mtx));
VTAILQ_FOREACH(s, &hp->streams, list)
PTOK(pthread_cond_signal(&s->cond));
PTOK(pthread_mutex_unlock(&hp->mtx));
vtc_log(hp->vl, hp->fatal,
"could not get frame header");
return (NULL);
}
ALLOC_OBJ(f, FRAME_MAGIC);
AN(f);
readFrameHeader(f, hdr);
vtc_log(hp->vl, 3, "rx: stream: %d, type: %s (%d), "
"flags: 0x%02x, size: %d",
f->stid,
f->type < TYPE_MAX ? h2_types[f->type] : "?",
f->type, f->flags, f->size);
explain_flags(f->flags, f->type, hp->vl);
if (f->size) {
f->data = malloc(f->size + 1L);
AN(f->data);
f->data[f->size] = '\0';
if (get_bytes(hp, f->data, f->size) <= 0) {
PTOK(pthread_mutex_lock(&hp->mtx));
VTAILQ_FOREACH(s, &hp->streams, list)
PTOK(pthread_cond_signal(&s->cond));
clean_frame(&f);
PTOK(pthread_mutex_unlock(&hp->mtx));
vtc_log(hp->vl, hp->fatal,
"could not get frame body");
return (NULL);
}
}
/* is the corresponding stream waiting? */
PTOK(pthread_mutex_lock(&hp->mtx));
s = NULL;
while (!s) {
VTAILQ_FOREACH(s, &hp->streams, list)
if (s->id == f->stid)
break;
if (!s)
PTOK(pthread_cond_wait(&hp->cond, &hp->mtx));
if (!hp->h2) {
clean_frame(&f);
PTOK(pthread_mutex_unlock(&hp->mtx));
return (NULL);
}
}
PTOK(pthread_mutex_unlock(&hp->mtx));
AN(s);
if (expect_cont &&
(f->type != TYPE_CONTINUATION || expect_cont != s->id))
vtc_fatal(s->vl, "Expected CONTINUATION frame for "
"stream %u", expect_cont);
/* parse the frame according to it type, and fill the metadata */
switch (f->type) {
case TYPE_DATA:
parse_data(s, f);
break;
case TYPE_PUSH_PROMISE:
hdrs = s->req;
/*FALLTHROUGH*/
case TYPE_HEADERS:
if (!hdrs) {
if (hp->sfd)
hdrs = s->req;
else
hdrs = s->resp;
}
clean_headers(hdrs);
hdrs[0].t = hpk_unset;
AZ(vsb);
vsb = VSB_new_auto();
/*FALLTHROUGH*/
case TYPE_CONTINUATION:
AN(hdrs);
expect_cont = s->id;
parse_hdr(s, f, vsb);
if (f->flags & END_HEADERS) {
expect_cont = 0;
AZ(VSB_finish(vsb));
decode_hdr(hp, hdrs, vsb);
VSB_destroy(&vsb);
hdrs = NULL;
}
break;
case TYPE_PRIORITY:
parse_prio(s, f);
break;
case TYPE_RST_STREAM:
parse_rst(s, f);
break;
case TYPE_SETTINGS:
parse_settings(s, f);
break;
case TYPE_PING:
parse_ping(s, f);
break;
case TYPE_GOAWAY:
parse_goaway(s, f);
break;
case TYPE_WINDOW_UPDATE:
parse_winup(s, f);
break;
default:
WRONG("wrong frame type");
}
PTOK(pthread_mutex_lock(&hp->mtx));
VTAILQ_INSERT_HEAD(&s->fq, f, list);
if (s->wf) {
assert(hp->wf > 0);
hp->wf--;
s->wf = 0;
PTOK(pthread_cond_signal(&s->cond));
}
continue;
}
PTOK(pthread_mutex_unlock(&hp->mtx));
if (vsb != NULL)
VSB_destroy(&vsb);
return (NULL);
}
#define STRTOU32(n, ss, p, v, c) \
do { \
n = strtoul(ss, &p, 0); \
if (*p != '\0') \
vtc_fatal(v, "%s takes an integer as argument " \
"(found %s)", c, ss); \
} while (0)
#define STRTOU32_CHECK(n, sp, p, v, c, l) \
do { \
sp++; \
AN(*sp); \
STRTOU32(n, *sp, p, v, c); \
if (l && n >= (1U << l)) \
vtc_fatal(v, \
c " must be a %d-bits integer (found %s)", l, *sp); \
} while (0)
#define CHECK_LAST_FRAME(TYPE) \
if (!f || f->type != TYPE_ ## TYPE) { \
vtc_fatal(s->vl, "Last frame was not of type " #TYPE); \
}
#define RETURN_SETTINGS(idx) \
do { \
if (isnan(f->md.settings[idx])) { \
return (NULL); \
} \
snprintf(buf, 20, "%.0f", f->md.settings[idx]); \
return (buf); \
} while (0)
#define RETURN_BUFFED(val) \
do { \
snprintf(buf, 20, "%ld", (long)val); \
return (buf); \
} while (0)
static char *
find_header(const struct hpk_hdr *h, const char *k)
{
AN(k);
int kl = strlen(k);
while (h->t) {
if (kl == h->key.len && !strncasecmp(h->key.ptr, k, kl))
return (h->value.ptr);
h++;
}
return (NULL);
}
/* SECTION: stream.spec.zexpect expect
*
* expect in stream works as it does in client or server, except that the
* elements compared will be different.
*
* Most of these elements will be frame specific, meaning that the last frame
* received on that stream must of the correct type.
*
* Here the list of keywords you can look at.
*/
static const char *
cmd_var_resolve(const struct stream *s, const char *spec, char *buf)
{
uint32_t idx;
int n;
const struct hpk_hdr *h;
struct hpk_ctx *ctx;
struct frame *f = s->frame;
CHECK_OBJ_NOTNULL(s, STREAM_MAGIC);
CHECK_OBJ_NOTNULL(s->hp, HTTP_MAGIC);
AN(spec);
AN(buf);
n = 0;
/* SECTION: stream.spec.zexpect.ping PING specific
*
* ping.data
* The 8-bytes string of the PING frame payload.
* ping.ack (PING)
* "true" if the ACK flag was set, "false" otherwise.
*/
if (!strcmp(spec, "ping.data")) {
CHECK_LAST_FRAME(PING);
return (f->md.ping.data);
}
if (!strcmp(spec, "ping.ack")) {
CHECK_LAST_FRAME(PING);
snprintf(buf, 20, (f->flags & ACK) ? "true" : "false");
return (buf);
}
/* SECTION: stream.spec.zexpect.winup WINDOW_UPDATE specific
*
* winup.size
* The size of the upgrade given by the WINDOW_UPDATE frame.
*/
if (!strcmp(spec, "winup.size")) {
CHECK_LAST_FRAME(WINDOW_UPDATE);
RETURN_BUFFED(f->md.winup_size);
}
/* SECTION: stream.spec.zexpect.prio PRIORITY specific
*
* prio.stream
* The stream ID announced.
*
* prio.exclusive
* "true" if the priority is exclusive, else "false".
*
* prio.weight
* The dependency weight.
*/
if (!strcmp(spec, "prio.stream")) {
CHECK_LAST_FRAME(PRIORITY);
RETURN_BUFFED(f->md.prio.stream);
}
if (!strcmp(spec, "prio.exclusive")) {
CHECK_LAST_FRAME(PRIORITY);
snprintf(buf, 20, f->md.prio.exclusive ? "true" : "false");
return (buf);
}
if (!strcmp(spec, "prio.weight")) {
CHECK_LAST_FRAME(PRIORITY);
RETURN_BUFFED(f->md.prio.weight);
}
/* SECTION: stream.spec.zexpect.rst RESET_STREAM specific
*
* rst.err