forked from nghttp2/nghttp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nghttp2_hd.c
1942 lines (1595 loc) · 50.9 KB
/
nghttp2_hd.c
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
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2013 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "nghttp2_hd.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "nghttp2_helper.h"
#include "nghttp2_int.h"
#define STATIC_TABLE_LENGTH 61
/* Make scalar initialization form of nghttp2_nv */
#define MAKE_STATIC_ENT(I, N, V, NH, VH) \
{ { { (uint8_t*)N, (uint8_t*)V, sizeof(N) - 1, sizeof(V) - 1, 0 }, \
NH, VH, 1, NGHTTP2_HD_FLAG_NONE }, I }
/* Generated by mkstatictbl.py */
/* Sorted by hash(name) and its table index */
static nghttp2_hd_static_entry static_table[] = {
MAKE_STATIC_ENT(20, "age", "", 96511u, 0u),
MAKE_STATIC_ENT(59, "via", "", 116750u, 0u),
MAKE_STATIC_ENT(32, "date", "", 3076014u, 0u),
MAKE_STATIC_ENT(33, "etag", "", 3123477u, 0u),
MAKE_STATIC_ENT(36, "from", "", 3151786u, 0u),
MAKE_STATIC_ENT(37, "host", "", 3208616u, 0u),
MAKE_STATIC_ENT(44, "link", "", 3321850u, 0u),
MAKE_STATIC_ENT(58, "vary", "", 3612210u, 0u),
MAKE_STATIC_ENT(38, "if-match", "", 34533653u, 0u),
MAKE_STATIC_ENT(41, "if-range", "", 39145613u, 0u),
MAKE_STATIC_ENT(3, ":path", "/", 56997727u, 47u),
MAKE_STATIC_ENT(4, ":path", "/index.html", 56997727u, 2144181430u),
MAKE_STATIC_ENT(21, "allow", "", 92906313u, 0u),
MAKE_STATIC_ENT(49, "range", "", 108280125u, 0u),
MAKE_STATIC_ENT(14, "accept-charset", "", 124285319u, 0u),
MAKE_STATIC_ENT(43, "last-modified", "", 150043680u, 0u),
MAKE_STATIC_ENT(48, "proxy-authorization", "", 329532250u, 0u),
MAKE_STATIC_ENT(57, "user-agent", "", 486342275u, 0u),
MAKE_STATIC_ENT(40, "if-none-match", "", 646073760u, 0u),
MAKE_STATIC_ENT(30, "content-type", "", 785670158u, 0u),
MAKE_STATIC_ENT(16, "accept-language", "", 802785917u, 0u),
MAKE_STATIC_ENT(50, "referer", "", 1085069613u, 0u),
MAKE_STATIC_ENT(51, "refresh", "", 1085444827u, 0u),
MAKE_STATIC_ENT(55, "strict-transport-security", "", 1153852136u, 0u),
MAKE_STATIC_ENT(54, "set-cookie", "", 1237214767u, 0u),
MAKE_STATIC_ENT(56, "transfer-encoding", "", 1274458357u, 0u),
MAKE_STATIC_ENT(17, "accept-ranges", "", 1397189435u, 0u),
MAKE_STATIC_ENT(42, "if-unmodified-since", "", 1454068927u, 0u),
MAKE_STATIC_ENT(46, "max-forwards", "", 1619948695u, 0u),
MAKE_STATIC_ENT(45, "location", "", 1901043637u, 0u),
MAKE_STATIC_ENT(52, "retry-after", "", 1933352567u, 0u),
MAKE_STATIC_ENT(25, "content-encoding", "", 2095084583u, 0u),
MAKE_STATIC_ENT(28, "content-location", "", 2284906121u, 0u),
MAKE_STATIC_ENT(39, "if-modified-since", "", 2302095846u, 0u),
MAKE_STATIC_ENT(18, "accept", "", 2871506184u, 0u),
MAKE_STATIC_ENT(29, "content-range", "", 2878374633u, 0u),
MAKE_STATIC_ENT(22, "authorization", "", 2909397113u, 0u),
MAKE_STATIC_ENT(31, "cookie", "", 2940209764u, 0u),
MAKE_STATIC_ENT(0, ":authority", "", 2962729033u, 0u),
MAKE_STATIC_ENT(35, "expires", "", 2985731892u, 0u),
MAKE_STATIC_ENT(34, "expect", "", 3005803609u, 0u),
MAKE_STATIC_ENT(24, "content-disposition", "", 3027699811u, 0u),
MAKE_STATIC_ENT(26, "content-language", "", 3065240108u, 0u),
MAKE_STATIC_ENT(1, ":method", "GET", 3153018267u, 70454u),
MAKE_STATIC_ENT(2, ":method", "POST", 3153018267u, 2461856u),
MAKE_STATIC_ENT(27, "content-length", "", 3162187450u, 0u),
MAKE_STATIC_ENT(19, "access-control-allow-origin", "", 3297999203u, 0u),
MAKE_STATIC_ENT(5, ":scheme", "http", 3322585695u, 3213448u),
MAKE_STATIC_ENT(6, ":scheme", "https", 3322585695u, 99617003u),
MAKE_STATIC_ENT(7, ":status", "200", 3338091692u, 49586u),
MAKE_STATIC_ENT(8, ":status", "204", 3338091692u, 49590u),
MAKE_STATIC_ENT(9, ":status", "206", 3338091692u, 49592u),
MAKE_STATIC_ENT(10, ":status", "304", 3338091692u, 50551u),
MAKE_STATIC_ENT(11, ":status", "400", 3338091692u, 51508u),
MAKE_STATIC_ENT(12, ":status", "404", 3338091692u, 51512u),
MAKE_STATIC_ENT(13, ":status", "500", 3338091692u, 52469u),
MAKE_STATIC_ENT(53, "server", "", 3389140803u, 0u),
MAKE_STATIC_ENT(47, "proxy-authenticate", "", 3993199572u, 0u),
MAKE_STATIC_ENT(60, "www-authenticate", "", 4051929931u, 0u),
MAKE_STATIC_ENT(23, "cache-control", "", 4086191634u, 0u),
MAKE_STATIC_ENT(15, "accept-encoding", "gzip, deflate", 4127597688u, 1733326877u),
};
/* Index to the position in static_table */
const size_t static_table_index[] = {
38, 43, 44, 10, 11, 47, 48, 49, 50, 51, 52, 53, 54, 55, 14, 60,
20, 26, 34, 46, 0 , 12, 36, 59, 41, 31, 42, 45, 32, 35, 19, 37,
2 , 3 , 40, 39, 4 , 5 , 8 , 33, 18, 9 , 27, 15, 6 , 29, 28, 57,
16, 13, 21, 22, 30, 56, 24, 23, 25, 17, 7 , 1 , 58
};
const size_t NGHTTP2_STATIC_TABLE_LENGTH =
sizeof(static_table)/sizeof(static_table[0]);
static int memeq(const void *s1, const void *s2, size_t n)
{
const uint8_t *a = (const uint8_t*)s1, *b = (const uint8_t*)s2;
uint8_t c = 0;
while(n > 0) {
c |= (*a++) ^ (*b++);
--n;
}
return c == 0;
}
static uint32_t hash(const uint8_t *s, size_t n)
{
uint32_t h = 0;
while(n > 0) {
h = h * 31 + *s++;
--n;
}
return h;
}
int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags,
uint8_t *name, size_t namelen,
uint8_t *value, size_t valuelen,
uint32_t name_hash, uint32_t value_hash)
{
int rv = 0;
/* Since nghttp2_hd_entry is used for indexing, ent->nv.flags always
NGHTTP2_NV_FLAG_NONE */
ent->nv.flags = NGHTTP2_NV_FLAG_NONE;
if((flags & NGHTTP2_HD_FLAG_NAME_ALLOC) &&
(flags & NGHTTP2_HD_FLAG_NAME_GIFT) == 0) {
if(namelen == 0) {
/* We should not allow empty header field name */
ent->nv.name = NULL;
} else {
ent->nv.name = nghttp2_memdup(name, namelen);
if(ent->nv.name == NULL) {
rv = NGHTTP2_ERR_NOMEM;
goto fail;
}
}
} else {
ent->nv.name = name;
}
if((flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) &&
(flags & NGHTTP2_HD_FLAG_VALUE_GIFT) == 0) {
if(valuelen == 0) {
ent->nv.value = NULL;
} else {
ent->nv.value = nghttp2_memdup(value, valuelen);
if(ent->nv.value == NULL) {
rv = NGHTTP2_ERR_NOMEM;
goto fail2;
}
}
} else {
ent->nv.value = value;
}
ent->nv.namelen = namelen;
ent->nv.valuelen = valuelen;
ent->ref = 1;
ent->flags = flags;
ent->name_hash = name_hash;
ent->value_hash = value_hash;
return 0;
fail2:
if(flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
free(ent->nv.name);
}
fail:
return rv;
}
void nghttp2_hd_entry_free(nghttp2_hd_entry *ent)
{
assert(ent->ref == 0);
if(ent->flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
free(ent->nv.name);
}
if(ent->flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) {
free(ent->nv.value);
}
}
static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize)
{
size_t size;
for(size = 1; size < bufsize; size <<= 1);
ringbuf->buffer = malloc(sizeof(nghttp2_hd_entry*)*size);
if(ringbuf->buffer == NULL) {
return NGHTTP2_ERR_NOMEM;
}
ringbuf->mask = size - 1;
ringbuf->first = 0;
ringbuf->len = 0;
return 0;
}
static nghttp2_hd_entry* hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
size_t idx)
{
assert(idx < ringbuf->len);
return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
}
static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize)
{
size_t i;
size_t size;
nghttp2_hd_entry **buffer;
if(ringbuf->mask + 1 >= bufsize) {
return 0;
}
for(size = 1; size < bufsize; size <<= 1);
buffer = malloc(sizeof(nghttp2_hd_entry*) * size);
if(buffer == NULL) {
return NGHTTP2_ERR_NOMEM;
}
for(i = 0; i < ringbuf->len; ++i) {
buffer[i] = hd_ringbuf_get(ringbuf, i);
}
free(ringbuf->buffer);
ringbuf->buffer = buffer;
ringbuf->mask = size - 1;
ringbuf->first = 0;
return 0;
}
static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf)
{
size_t i;
if(ringbuf == NULL) {
return;
}
for(i = 0; i < ringbuf->len; ++i) {
nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
--ent->ref;
nghttp2_hd_entry_free(ent);
free(ent);
}
free(ringbuf->buffer);
}
static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
nghttp2_hd_entry *ent)
{
int rv;
rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1);
if(rv != 0) {
return rv;
}
ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
++ringbuf->len;
return 0;
}
static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf)
{
assert(ringbuf->len > 0);
--ringbuf->len;
}
static int hd_context_init(nghttp2_hd_context *context)
{
int rv;
context->bad = 0;
context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
rv = hd_ringbuf_init
(&context->hd_table,
context->hd_table_bufsize_max/NGHTTP2_HD_ENTRY_OVERHEAD);
if(rv != 0) {
return rv;
}
context->hd_table_bufsize = 0;
return 0;
}
static void hd_context_free(nghttp2_hd_context *context)
{
hd_ringbuf_free(&context->hd_table);
}
int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater)
{
return nghttp2_hd_deflate_init2(deflater,
NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE);
}
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
size_t deflate_hd_table_bufsize_max)
{
int rv;
rv = hd_context_init(&deflater->ctx);
if(rv != 0) {
return rv;
}
if(deflate_hd_table_bufsize_max < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
deflater->notify_table_size_change = 1;
deflater->ctx.hd_table_bufsize_max = deflate_hd_table_bufsize_max;
} else {
deflater->notify_table_size_change = 0;
}
deflater->deflate_hd_table_bufsize_max = deflate_hd_table_bufsize_max;
deflater->min_hd_table_bufsize_max = UINT32_MAX;
return 0;
}
int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater)
{
int rv;
rv = hd_context_init(&inflater->ctx);
if(rv != 0) {
goto fail;
}
inflater->settings_hd_table_bufsize_max =
NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
inflater->ent_keep = NULL;
inflater->nv_keep = NULL;
inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
inflater->state = NGHTTP2_HD_STATE_OPCODE;
rv = nghttp2_bufs_init3(&inflater->nvbufs, NGHTTP2_HD_MAX_NV / 8, 8, 1, 0);
if(rv != 0) {
goto nvbufs_fail;
}
inflater->huffman_encoded = 0;
inflater->index = 0;
inflater->left = 0;
inflater->shift = 0;
inflater->newnamelen = 0;
inflater->index_required = 0;
inflater->no_index = 0;
return 0;
nvbufs_fail:
hd_context_free(&inflater->ctx);
fail:
return rv;
}
static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater)
{
if(inflater->ent_keep) {
if(inflater->ent_keep->ref == 0) {
nghttp2_hd_entry_free(inflater->ent_keep);
free(inflater->ent_keep);
}
inflater->ent_keep = NULL;
}
free(inflater->nv_keep);
inflater->nv_keep = NULL;
}
void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater)
{
hd_context_free(&deflater->ctx);
}
void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater)
{
hd_inflate_keep_free(inflater);
nghttp2_bufs_free(&inflater->nvbufs);
hd_context_free(&inflater->ctx);
}
static size_t entry_room(size_t namelen, size_t valuelen)
{
return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
}
static int emit_indexed_header(nghttp2_nv *nv_out, nghttp2_hd_entry *ent)
{
DEBUGF(fprintf(stderr, "inflatehd: header emission: "));
DEBUGF(fwrite(ent->nv.name, ent->nv.namelen, 1, stderr));
DEBUGF(fprintf(stderr, ": "));
DEBUGF(fwrite(ent->nv.value, ent->nv.valuelen, 1, stderr));
DEBUGF(fprintf(stderr, "\n"));
/* ent->ref may be 0. This happens if the encoder emits literal
block larger than header table capacity with indexing. */
*nv_out = ent->nv;
return 0;
}
static int emit_literal_header(nghttp2_nv *nv_out, nghttp2_nv *nv)
{
DEBUGF(fprintf(stderr, "inflatehd: header emission: "));
DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr));
DEBUGF(fprintf(stderr, ": "));
DEBUGF(fwrite(nv->value, nv->valuelen, 1, stderr));
DEBUGF(fprintf(stderr, "\n"));
*nv_out = *nv;
return 0;
}
static size_t count_encoded_length(size_t n, size_t prefix)
{
size_t k = (1 << prefix) - 1;
size_t len = 0;
if(n < k) {
return 1;
}
n -= k;
++len;
for(; n >= 128; n >>= 7, ++len);
return len + 1;
}
static size_t encode_length(uint8_t *buf, size_t n, size_t prefix)
{
size_t k = (1 << prefix) - 1;
uint8_t *begin = buf;
*buf &= ~k;
if(n < k) {
*buf |= n;
return 1;
}
*buf++ |= k;
n -= k;
for(; n >= 128; n >>= 7) {
*buf++ = (1 << 7) | (n & 0x7f);
}
*buf++ = (uint8_t)n;
return (size_t)(buf - begin);
}
/*
* Decodes |prefix| prefixed integer stored from |in|. The |last|
* represents the 1 beyond the last of the valid contiguous memory
* region from |in|. The decoded integer must be less than or equal
* to UINT32_MAX.
*
* If the |initial| is nonzero, it is used as a initial value, this
* function assumes the |in| starts with intermediate data.
*
* An entire integer is decoded successfully, decoded, the |*final| is
* set to nonzero.
*
* This function stores the decoded integer in |*res| if it succeed,
* including partial decoding (in this case, number of shift to make
* in the next call will be stored in |*shift_ptr|) and returns number
* of bytes processed, or returns -1, indicating decoding error.
*/
static ssize_t decode_length(uint32_t *res, size_t *shift_ptr, int *final,
uint32_t initial, size_t shift,
uint8_t *in, uint8_t *last, size_t prefix)
{
uint32_t k = (1 << prefix) - 1;
uint32_t n = initial;
uint8_t *start = in;
*shift_ptr = 0;
*final = 0;
if(n == 0) {
if((*in & k) != k) {
*res = (*in) & k;
*final = 1;
return 1;
}
n = k;
if(++in == last) {
*res = n;
return (ssize_t)(in - start);
}
}
for(; in != last; ++in, shift += 7) {
uint32_t add = *in & 0x7f;
if((UINT32_MAX >> shift) < add) {
DEBUGF(fprintf(stderr, "inflate: integer overflow on shift\n"));
return -1;
}
add <<= shift;
if(UINT32_MAX - add < n) {
DEBUGF(fprintf(stderr, "inflate: integer overflow on addition\n"));
return -1;
}
n += add;
if((*in & (1 << 7)) == 0) {
break;
}
}
*shift_ptr = shift;
if(in == last) {
*res = n;
return (ssize_t)(in - start);
}
*res = n;
*final = 1;
return (ssize_t)(in + 1 - start);
}
static int emit_table_size(nghttp2_bufs *bufs, size_t table_size)
{
int rv;
uint8_t *bufp;
size_t blocklen;
uint8_t sb[16];
DEBUGF(fprintf(stderr, "deflatehd: emit table_size=%zu\n", table_size));
blocklen = count_encoded_length(table_size, 5);
if(sizeof(sb) < blocklen) {
return NGHTTP2_ERR_HEADER_COMP;
}
bufp = sb;
*bufp = 0x20u;
encode_length(bufp, table_size, 5);
rv = nghttp2_bufs_add(bufs, sb, blocklen);
if(rv != 0) {
return rv;
}
return 0;
}
static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx)
{
int rv;
size_t blocklen;
uint8_t sb[16];
uint8_t *bufp;
blocklen = count_encoded_length(idx + 1, 7);
DEBUGF(fprintf(stderr, "deflatehd: emit indexed index=%zu, %zu bytes\n",
idx, blocklen));
if(sizeof(sb) < blocklen) {
return NGHTTP2_ERR_HEADER_COMP;
}
bufp = sb;
*bufp = 0x80u;
encode_length(bufp, idx + 1, 7);
rv = nghttp2_bufs_add(bufs, sb, blocklen);
if(rv != 0) {
return rv;
}
return 0;
}
static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len)
{
int rv;
uint8_t sb[16];
uint8_t *bufp;
size_t blocklen;
size_t enclen;
int huffman = 0;
enclen = nghttp2_hd_huff_encode_count(str, len);
if(enclen < len) {
huffman = 1;
} else {
enclen = len;
}
blocklen = count_encoded_length(enclen, 7);
DEBUGF(fprintf(stderr,
"deflatehd: emit string str="));
DEBUGF(fwrite(str, len, 1, stderr));
DEBUGF(fprintf(stderr, ", length=%zu, huffman=%d, encoded_length=%zu\n",
len, huffman, enclen));
if(sizeof(sb) < blocklen) {
return NGHTTP2_ERR_HEADER_COMP;
}
bufp = sb;
*bufp = huffman ? 1 << 7 : 0;
encode_length(bufp, enclen, 7);
rv = nghttp2_bufs_add(bufs, sb, blocklen);
if(rv != 0) {
return rv;
}
if(huffman) {
rv = nghttp2_hd_huff_encode(bufs, str, len);
} else {
assert(enclen == len);
rv = nghttp2_bufs_add(bufs, str, len);
}
return rv;
}
static uint8_t pack_first_byte(int inc_indexing, int no_index)
{
if(inc_indexing) {
return 0x40u;
}
if(no_index) {
return 0x10u;
}
return 0;
}
static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
const nghttp2_nv *nv,
int inc_indexing)
{
int rv;
uint8_t *bufp;
size_t blocklen;
uint8_t sb[16];
size_t prefixlen;
int no_index;
no_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) != 0;
if(inc_indexing) {
prefixlen = 6;
} else {
prefixlen = 4;
}
DEBUGF(fprintf(stderr,
"deflatehd: emit indname index=%zu, valuelen=%zu, "
"indexing=%d, no_index=%d\n",
idx, nv->valuelen, inc_indexing, no_index));
blocklen = count_encoded_length(idx + 1, prefixlen);
if(sizeof(sb) < blocklen) {
return NGHTTP2_ERR_HEADER_COMP;
}
bufp = sb;
*bufp = pack_first_byte(inc_indexing, no_index);
encode_length(bufp, idx + 1, prefixlen);
rv = nghttp2_bufs_add(bufs, sb, blocklen);
if(rv != 0) {
return rv;
}
rv = emit_string(bufs, nv->value, nv->valuelen);
if(rv != 0) {
return rv;
}
return 0;
}
static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
int inc_indexing)
{
int rv;
int no_index;
no_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) != 0;
DEBUGF(fprintf(stderr,
"deflatehd: emit newname namelen=%zu, valuelen=%zu, "
"indexing=%d, no_index=%d\n",
nv->namelen, nv->valuelen, inc_indexing, no_index));
rv = nghttp2_bufs_addb(bufs, pack_first_byte(inc_indexing, no_index));
if(rv != 0) {
return rv;
}
rv = emit_string(bufs, nv->name, nv->namelen);
if(rv != 0) {
return rv;
}
rv = emit_string(bufs, nv->value, nv->valuelen);
if(rv != 0) {
return rv;
}
return 0;
}
static nghttp2_hd_entry* add_hd_table_incremental(nghttp2_hd_context *context,
const nghttp2_nv *nv,
uint32_t name_hash,
uint32_t value_hash,
uint8_t entry_flags)
{
int rv;
nghttp2_hd_entry *new_ent;
size_t room;
room = entry_room(nv->namelen, nv->valuelen);
while(context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
context->hd_table.len > 0) {
size_t idx = context->hd_table.len - 1;
nghttp2_hd_entry* ent = hd_ringbuf_get(&context->hd_table, idx);
context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
DEBUGF(fprintf(stderr, "hpack: remove item from header table: "));
DEBUGF(fwrite(ent->nv.name, ent->nv.namelen, 1, stderr));
DEBUGF(fprintf(stderr, ": "));
DEBUGF(fwrite(ent->nv.value, ent->nv.valuelen, 1, stderr));
DEBUGF(fprintf(stderr, "\n"));
hd_ringbuf_pop_back(&context->hd_table);
if(--ent->ref == 0) {
nghttp2_hd_entry_free(ent);
free(ent);
}
}
new_ent = malloc(sizeof(nghttp2_hd_entry));
if(new_ent == NULL) {
return NULL;
}
rv = nghttp2_hd_entry_init(new_ent, entry_flags,
nv->name, nv->namelen, nv->value, nv->valuelen,
name_hash, value_hash);
if(rv != 0) {
free(new_ent);
return NULL;
}
if(room > context->hd_table_bufsize_max) {
/* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
immediately evicted. */
--new_ent->ref;
} else {
rv = hd_ringbuf_push_front(&context->hd_table, new_ent);
if(rv != 0) {
--new_ent->ref;
/* nv->name and nv->value are managed by caller. */
new_ent->nv.name = NULL;
new_ent->nv.namelen = 0;
new_ent->nv.value = NULL;
new_ent->nv.valuelen = 0;
nghttp2_hd_entry_free(new_ent);
free(new_ent);
return NULL;
}
context->hd_table_bufsize += room;
}
return new_ent;
}
static int name_eq(const nghttp2_nv *a, const nghttp2_nv *b)
{
return a->namelen == b->namelen && memeq(a->name, b->name, a->namelen);
}
static int value_eq(const nghttp2_nv *a, const nghttp2_nv *b)
{
return a->valuelen == b->valuelen && memeq(a->value, b->value, a->valuelen);
}
typedef struct {
ssize_t index;
/* Nonzero if both name and value are matched. */
uint8_t name_value_match;
} search_result;
static search_result search_hd_table(nghttp2_hd_context *context,
const nghttp2_nv *nv,
uint32_t name_hash, uint32_t value_hash)
{
ssize_t left = -1, right = (ssize_t)STATIC_TABLE_LENGTH;
search_result res = { -1, 0 };
size_t i;
int use_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) == 0;
/* Search dynamic table first, so that we can find recently used
entry first */
if(use_index) {
for(i = 0; i < context->hd_table.len; ++i) {
nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, i);
if(ent->name_hash != name_hash || !name_eq(&ent->nv, nv)) {
continue;
}
if(res.index == -1) {
res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
}
if(ent->value_hash == value_hash && value_eq(&ent->nv, nv)) {
res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
res.name_value_match = 1;
return res;
}
}
}
while(right - left > 1) {
ssize_t mid = (left + right) / 2;
nghttp2_hd_entry *ent = &static_table[mid].ent;
if(ent->name_hash < name_hash) {
left = mid;
} else {
right = mid;
}
}
for(i = right; i < STATIC_TABLE_LENGTH; ++i) {
nghttp2_hd_entry *ent = &static_table[i].ent;
if(ent->name_hash != name_hash) {
break;
}
if(name_eq(&ent->nv, nv)) {
if(res.index == -1) {
res.index = (ssize_t)(static_table[i].index);
}
if(use_index &&
ent->value_hash == value_hash && value_eq(&ent->nv, nv)) {
res.index = (ssize_t)(static_table[i].index);
res.name_value_match = 1;
return res;
}
}
}
return res;
}
static void hd_context_shrink_table_size(nghttp2_hd_context *context)
{
while(context->hd_table_bufsize > context->hd_table_bufsize_max &&
context->hd_table.len > 0) {
size_t idx = context->hd_table.len - 1;
nghttp2_hd_entry* ent = hd_ringbuf_get(&context->hd_table, idx);
context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
hd_ringbuf_pop_back(&context->hd_table);
if(--ent->ref == 0) {
nghttp2_hd_entry_free(ent);
free(ent);
}
}
}
int nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
size_t settings_hd_table_bufsize_max)
{
size_t next_bufsize = nghttp2_min(settings_hd_table_bufsize_max,
deflater->deflate_hd_table_bufsize_max);
deflater->ctx.hd_table_bufsize_max = next_bufsize;
deflater->min_hd_table_bufsize_max =
nghttp2_min(deflater->min_hd_table_bufsize_max, next_bufsize);
deflater->notify_table_size_change = 1;
hd_context_shrink_table_size(&deflater->ctx);
return 0;
}
int nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
size_t settings_hd_table_bufsize_max)
{
inflater->settings_hd_table_bufsize_max = settings_hd_table_bufsize_max;
inflater->ctx.hd_table_bufsize_max = settings_hd_table_bufsize_max;
hd_context_shrink_table_size(&inflater->ctx);
return 0;
}
#define INDEX_RANGE_VALID(context, idx) \
((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
static size_t get_max_index(nghttp2_hd_context *context)
{
return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH - 1;
}
nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context,
size_t idx)
{
assert(INDEX_RANGE_VALID(context, idx));
if(idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
return hd_ringbuf_get(&context->hd_table, idx - NGHTTP2_STATIC_TABLE_LENGTH);
} else {
return &static_table[static_table_index[idx]].ent;
}
}
#define name_match(NV, NAME) \
(nv->namelen == sizeof(NAME) - 1 && memeq(nv->name, NAME, sizeof(NAME) - 1))
static int hd_deflate_should_indexing(nghttp2_hd_deflater *deflater,
const nghttp2_nv *nv)
{
if((nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) ||
entry_room(nv->namelen, nv->valuelen) >
deflater->ctx.hd_table_bufsize_max * 3 / 4) {
return 0;
}
#ifdef NGHTTP2_XHD
return !name_match(nv, NGHTTP2_XHD);
#else /* !NGHTTP2_XHD */
return
!name_match(nv, ":path") &&
!name_match(nv, "content-length") &&
!name_match(nv, "set-cookie") &&
!name_match(nv, "etag") &&
!name_match(nv, "if-modified-since") &&
!name_match(nv, "if-none-match") &&
!name_match(nv, "location") &&
!name_match(nv, "age");
#endif /* !NGHTTP2_XHD */
}
static int deflate_nv(nghttp2_hd_deflater *deflater,
nghttp2_bufs *bufs, const nghttp2_nv *nv)
{
int rv;
search_result res;
ssize_t idx;
int incidx = 0;
uint32_t name_hash = hash(nv->name, nv->namelen);
uint32_t value_hash = hash(nv->value, nv->valuelen);
DEBUGF(fprintf(stderr, "deflatehd: deflating "));
DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr));
DEBUGF(fprintf(stderr, ": "));
DEBUGF(fwrite(nv->value, nv->valuelen, 1, stderr));