/
HpackTest.kt
1138 lines (1032 loc) · 38.2 KB
/
HpackTest.kt
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) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.http2
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import java.io.IOException
import java.util.Arrays
import kotlin.test.assertFailsWith
import okhttp3.TestUtil.headerEntries
import okio.Buffer
import okio.ByteString
import okio.ByteString.Companion.decodeHex
import okio.ByteString.Companion.encodeUtf8
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class HpackTest {
private val bytesIn = Buffer()
private var hpackReader: Hpack.Reader? = null
private val bytesOut = Buffer()
private var hpackWriter: Hpack.Writer? = null
@BeforeEach
fun reset() {
hpackReader = newReader(bytesIn)
hpackWriter = Hpack.Writer(4096, false, bytesOut)
}
/**
* Variable-length quantity special cases strings which are longer than 127 bytes. Values such as
* cookies can be 4KiB, and should be possible to send.
*
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#section-5.2
*/
@Test
fun largeHeaderValue() {
val value = CharArray(4096)
Arrays.fill(value, '!')
val headerBlock = headerEntries("cookie", String(value))
hpackWriter!!.writeHeaders(headerBlock)
bytesIn.writeAll(bytesOut)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
/**
* HPACK has a max header table size, which can be smaller than the max header message. Ensure the
* larger header content is not lost.
*/
@Test
fun tooLargeToHPackIsStillEmitted() {
bytesIn.writeByte(0x21) // Dynamic table size update (size = 1).
bytesIn.writeByte(0x00) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries("custom-key", "custom-header"),
)
}
/** Oldest entries are evicted to support newer ones. */
@Test
fun writerEviction() {
val headerBlock =
headerEntries(
"custom-foo",
"custom-header",
"custom-bar",
"custom-header",
"custom-baz",
"custom-header",
)
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-foo")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-bar")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-baz")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
// Set to only support 110 bytes (enough for 2 headers).
// Use a new Writer because we don't support change the dynamic table
// size after Writer constructed.
val writer = Hpack.Writer(110, false, bytesOut)
writer.writeHeaders(headerBlock)
assertThat(bytesOut).isEqualTo(bytesIn)
assertThat(writer.headerCount).isEqualTo(2)
val tableLength = writer.dynamicTable.size
var entry = writer.dynamicTable[tableLength - 1]!!
checkEntry(entry, "custom-bar", "custom-header", 55)
entry = writer.dynamicTable[tableLength - 2]!!
checkEntry(entry, "custom-baz", "custom-header", 55)
}
@Test
fun readerEviction() {
val headerBlock =
headerEntries(
"custom-foo",
"custom-header",
"custom-bar",
"custom-header",
"custom-baz",
"custom-header",
)
// Set to only support 110 bytes (enough for 2 headers).
bytesIn.writeByte(0x3F) // Dynamic table size update (size = 110).
bytesIn.writeByte(0x4F)
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-foo")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-bar")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-baz")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(2)
val entry1 = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry1, "custom-bar", "custom-header", 55)
val entry2 = hpackReader!!.dynamicTable[readerHeaderTableLength() - 2]!!
checkEntry(entry2, "custom-baz", "custom-header", 55)
// Once a header field is decoded and added to the reconstructed header
// list, it cannot be removed from it. Hence, foo is here.
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
// Simulate receiving a small dynamic table size update, that implies eviction.
bytesIn.writeByte(0x3F) // Dynamic table size update (size = 55).
bytesIn.writeByte(0x18)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
}
/** Header table backing array is initially 8 long, let's ensure it grows. */
@Test
fun dynamicallyGrowsBeyond64Entries() {
// Lots of headers need more room!
hpackReader = Hpack.Reader(bytesIn, 16384, 4096)
bytesIn.writeByte(0x3F) // Dynamic table size update (size = 16384).
bytesIn.writeByte(0xE1)
bytesIn.writeByte(0x7F)
for (i in 0..255) {
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-foo")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
}
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(256)
}
@Test
fun huffmanDecodingSupported() {
bytesIn.writeByte(0x44) // == Literal indexed ==
// Indexed name (idx = 4) -> :path
bytesIn.writeByte(0x8c) // Literal value Huffman encoded 12 bytes
// decodes to www.example.com which is length 15
bytesIn.write("f1e3c2e5f23a6ba0ab90f4ff".decodeHex())
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(52)
val entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":path", "www.example.com", 52)
}
/**
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.2.1
*/
@Test
fun readLiteralHeaderFieldWithIndexing() {
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(55)
val entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, "custom-key", "custom-header", 55)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries("custom-key", "custom-header"),
)
}
/**
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.2.2
*/
@Test
fun literalHeaderFieldWithoutIndexingIndexedName() {
val headerBlock = headerEntries(":path", "/sample/path")
bytesIn.writeByte(0x04) // == Literal not indexed ==
// Indexed name (idx = 4) -> :path
bytesIn.writeByte(0x0c) // Literal value (len = 12)
bytesIn.writeUtf8("/sample/path")
hpackWriter!!.writeHeaders(headerBlock)
assertThat(bytesOut).isEqualTo(bytesIn)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun literalHeaderFieldWithoutIndexingNewName() {
val headerBlock = headerEntries("custom-key", "custom-header")
bytesIn.writeByte(0x00) // Not indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun literalHeaderFieldNeverIndexedIndexedName() {
bytesIn.writeByte(0x14) // == Literal never indexed ==
// Indexed name (idx = 4) -> :path
bytesIn.writeByte(0x0c) // Literal value (len = 12)
bytesIn.writeUtf8("/sample/path")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(":path", "/sample/path"),
)
}
@Test
fun literalHeaderFieldNeverIndexedNewName() {
val headerBlock = headerEntries("custom-key", "custom-header")
bytesIn.writeByte(0x10) // Never indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun literalHeaderFieldWithIncrementalIndexingIndexedName() {
val headerBlock = headerEntries(":path", "/sample/path")
bytesIn.writeByte(0x44) // Indexed name (idx = 4) -> :path
bytesIn.writeByte(0x0c) // Literal value (len = 12)
bytesIn.writeUtf8("/sample/path")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun literalHeaderFieldWithIncrementalIndexingNewName() {
val headerBlock = headerEntries("custom-key", "custom-header")
bytesIn.writeByte(0x40) // Never indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
hpackWriter!!.writeHeaders(headerBlock)
assertThat(bytesOut).isEqualTo(bytesIn)
assertThat(hpackWriter!!.headerCount).isEqualTo(1)
val entry = hpackWriter!!.dynamicTable[hpackWriter!!.dynamicTable.size - 1]!!
checkEntry(entry, "custom-key", "custom-header", 55)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun theSameHeaderAfterOneIncrementalIndexed() {
val headerBlock =
headerEntries(
"custom-key",
"custom-header",
"custom-key",
"custom-header",
)
bytesIn.writeByte(0x40) // Never indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0d) // Literal value (len = 13)
bytesIn.writeUtf8("custom-header")
bytesIn.writeByte(0xbe) // Indexed name and value (idx = 63)
hpackWriter!!.writeHeaders(headerBlock)
assertThat(bytesOut).isEqualTo(bytesIn)
assertThat(hpackWriter!!.headerCount).isEqualTo(1)
val entry = hpackWriter!!.dynamicTable[hpackWriter!!.dynamicTable.size - 1]!!
checkEntry(entry, "custom-key", "custom-header", 55)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(1)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(headerBlock)
}
@Test
fun staticHeaderIsNotCopiedIntoTheIndexedTable() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
hpackReader!!.readHeaders()
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(0)
assertThat(hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]).isNull()
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(":method", "GET"),
)
}
// Example taken from twitter/hpack DecoderTest.testUnusedIndex
@Test
fun readIndexedHeaderFieldIndex0() {
bytesIn.writeByte(0x80) // == Indexed - Add idx = 0
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo("index == 0")
}
}
// Example taken from twitter/hpack DecoderTest.testIllegalIndex
@Test
fun readIndexedHeaderFieldTooLargeIndex() {
bytesIn.writeShort(0xff00) // == Indexed - Add idx = 127
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo("Header index too large 127")
}
}
// Example taken from twitter/hpack DecoderTest.testInsidiousIndex
@Test
fun readIndexedHeaderFieldInsidiousIndex() {
bytesIn.writeByte(0xff) // == Indexed - Add ==
bytesIn.write("8080808008".decodeHex()) // idx = -2147483521
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo("Header index too large -2147483521")
}
}
// Example taken from twitter/hpack DecoderTest.testHeaderTableSizeUpdate
@Test
fun minMaxHeaderTableSize() {
bytesIn.writeByte(0x20)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.maxDynamicTableByteCount()).isEqualTo(0)
bytesIn.writeByte(0x3f) // encode size 4096
bytesIn.writeByte(0xe1)
bytesIn.writeByte(0x1f)
hpackReader!!.readHeaders()
assertThat(hpackReader!!.maxDynamicTableByteCount()).isEqualTo(4096)
}
// Example taken from twitter/hpack DecoderTest.testIllegalHeaderTableSizeUpdate
@Test
fun cannotSetTableSizeLargerThanSettingsValue() {
bytesIn.writeByte(0x3f) // encode size 4097
bytesIn.writeByte(0xe2)
bytesIn.writeByte(0x1f)
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo("Invalid dynamic table size update 4097")
}
}
// Example taken from twitter/hpack DecoderTest.testInsidiousMaxHeaderSize
@Test
fun readHeaderTableStateChangeInsidiousMaxHeaderByteCount() {
bytesIn.writeByte(0x3f)
bytesIn.write("e1ffffff07".decodeHex()) // count = -2147483648
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message)
.isEqualTo("Invalid dynamic table size update -2147483648")
}
}
/**
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.2.4
*/
@Test
fun readIndexedHeaderFieldFromStaticTableWithoutBuffering() {
bytesIn.writeByte(0x20) // Dynamic table size update (size = 0).
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
hpackReader!!.readHeaders()
// Not buffered in header table.
assertThat(hpackReader!!.headerCount).isEqualTo(0)
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(":method", "GET"),
)
}
@Test
fun readLiteralHeaderWithIncrementalIndexingStaticName() {
bytesIn.writeByte(0x7d) // == Literal indexed ==
// Indexed name (idx = 60) -> "www-authenticate"
bytesIn.writeByte(0x05) // Literal value (len = 5)
bytesIn.writeUtf8("Basic")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.getAndResetHeaderList())
.containsExactly(Header("www-authenticate", "Basic"))
}
@Test
fun readLiteralHeaderWithIncrementalIndexingDynamicName() {
bytesIn.writeByte(0x40)
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-foo")
bytesIn.writeByte(0x05) // Literal value (len = 5)
bytesIn.writeUtf8("Basic")
bytesIn.writeByte(0x7e)
bytesIn.writeByte(0x06) // Literal value (len = 6)
bytesIn.writeUtf8("Basic2")
hpackReader!!.readHeaders()
assertThat(hpackReader!!.getAndResetHeaderList()).containsExactly(
Header("custom-foo", "Basic"),
Header("custom-foo", "Basic2"),
)
}
/**
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.2
*/
@Test
fun readRequestExamplesWithoutHuffman() {
firstRequestWithoutHuffman()
hpackReader!!.readHeaders()
checkReadFirstRequestWithoutHuffman()
secondRequestWithoutHuffman()
hpackReader!!.readHeaders()
checkReadSecondRequestWithoutHuffman()
thirdRequestWithoutHuffman()
hpackReader!!.readHeaders()
checkReadThirdRequestWithoutHuffman()
}
@Test
fun readFailingRequestExample() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x86) // == Indexed - Add ==
// idx = 7 -> :scheme: http
bytesIn.writeByte(0x84) // == Indexed - Add ==
bytesIn.writeByte(0x7f) // == Bad index! ==
// Indexed name (idx = 4) -> :authority
bytesIn.writeByte(0x0f) // Literal value (len = 15)
bytesIn.writeUtf8("www.example.com")
assertFailsWith<IOException> {
hpackReader!!.readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo("Header index too large 78")
}
}
private fun firstRequestWithoutHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x86) // == Indexed - Add ==
// idx = 7 -> :scheme: http
bytesIn.writeByte(0x84) // == Indexed - Add ==
// idx = 6 -> :path: /
bytesIn.writeByte(0x41) // == Literal indexed ==
// Indexed name (idx = 4) -> :authority
bytesIn.writeByte(0x0f) // Literal value (len = 15)
bytesIn.writeUtf8("www.example.com")
}
private fun checkReadFirstRequestWithoutHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(1)
// [ 1] (s = 57) :authority: www.example.com
val entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 57
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(57)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method",
"GET",
":scheme",
"http",
":path",
"/",
":authority",
"www.example.com",
),
)
}
private fun secondRequestWithoutHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x86) // == Indexed - Add ==
// idx = 7 -> :scheme: http
bytesIn.writeByte(0x84) // == Indexed - Add ==
// idx = 6 -> :path: /
bytesIn.writeByte(0xbe) // == Indexed - Add ==
// Indexed name (idx = 62) -> :authority: www.example.com
bytesIn.writeByte(0x58) // == Literal indexed ==
// Indexed name (idx = 24) -> cache-control
bytesIn.writeByte(0x08) // Literal value (len = 8)
bytesIn.writeUtf8("no-cache")
}
private fun checkReadSecondRequestWithoutHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(2)
// [ 1] (s = 53) cache-control: no-cache
var entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 2]!!
checkEntry(entry, "cache-control", "no-cache", 53)
// [ 2] (s = 57) :authority: www.example.com
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 110
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(110)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method", "GET",
":scheme", "http",
":path", "/",
":authority", "www.example.com",
"cache-control", "no-cache",
),
)
}
private fun thirdRequestWithoutHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x87) // == Indexed - Add ==
// idx = 7 -> :scheme: http
bytesIn.writeByte(0x85) // == Indexed - Add ==
// idx = 5 -> :path: /index.html
bytesIn.writeByte(0xbf) // == Indexed - Add ==
// Indexed name (idx = 63) -> :authority: www.example.com
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x0a) // Literal name (len = 10)
bytesIn.writeUtf8("custom-key")
bytesIn.writeByte(0x0c) // Literal value (len = 12)
bytesIn.writeUtf8("custom-value")
}
private fun checkReadThirdRequestWithoutHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(3)
// [ 1] (s = 54) custom-key: custom-value
var entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 3]!!
checkEntry(entry, "custom-key", "custom-value", 54)
// [ 2] (s = 53) cache-control: no-cache
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 2]!!
checkEntry(entry, "cache-control", "no-cache", 53)
// [ 3] (s = 57) :authority: www.example.com
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 164
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(164)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method", "GET",
":scheme", "https",
":path", "/index.html",
":authority", "www.example.com",
"custom-key", "custom-value",
),
)
}
/**
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.4
*/
@Test
fun readRequestExamplesWithHuffman() {
firstRequestWithHuffman()
hpackReader!!.readHeaders()
checkReadFirstRequestWithHuffman()
secondRequestWithHuffman()
hpackReader!!.readHeaders()
checkReadSecondRequestWithHuffman()
thirdRequestWithHuffman()
hpackReader!!.readHeaders()
checkReadThirdRequestWithHuffman()
}
private fun firstRequestWithHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x86) // == Indexed - Add ==
// idx = 6 -> :scheme: http
bytesIn.writeByte(0x84) // == Indexed - Add ==
// idx = 4 -> :path: /
bytesIn.writeByte(0x41) // == Literal indexed ==
// Indexed name (idx = 1) -> :authority
bytesIn.writeByte(0x8c) // Literal value Huffman encoded 12 bytes
// decodes to www.example.com which is length 15
bytesIn.write("f1e3c2e5f23a6ba0ab90f4ff".decodeHex())
}
private fun checkReadFirstRequestWithHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(1)
// [ 1] (s = 57) :authority: www.example.com
val entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 57
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(57)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method",
"GET",
":scheme",
"http",
":path",
"/",
":authority",
"www.example.com",
),
)
}
private fun secondRequestWithHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x86) // == Indexed - Add ==
// idx = 6 -> :scheme: http
bytesIn.writeByte(0x84) // == Indexed - Add ==
// idx = 4 -> :path: /
bytesIn.writeByte(0xbe) // == Indexed - Add ==
// idx = 62 -> :authority: www.example.com
bytesIn.writeByte(0x58) // == Literal indexed ==
// Indexed name (idx = 24) -> cache-control
bytesIn.writeByte(0x86) // Literal value Huffman encoded 6 bytes
// decodes to no-cache which is length 8
bytesIn.write("a8eb10649cbf".decodeHex())
}
private fun checkReadSecondRequestWithHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(2)
// [ 1] (s = 53) cache-control: no-cache
var entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 2]!!
checkEntry(entry, "cache-control", "no-cache", 53)
// [ 2] (s = 57) :authority: www.example.com
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 110
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(110)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method", "GET",
":scheme", "http",
":path", "/",
":authority", "www.example.com",
"cache-control", "no-cache",
),
)
}
private fun thirdRequestWithHuffman() {
bytesIn.writeByte(0x82) // == Indexed - Add ==
// idx = 2 -> :method: GET
bytesIn.writeByte(0x87) // == Indexed - Add ==
// idx = 7 -> :scheme: https
bytesIn.writeByte(0x85) // == Indexed - Add ==
// idx = 5 -> :path: /index.html
bytesIn.writeByte(0xbf) // == Indexed - Add ==
// idx = 63 -> :authority: www.example.com
bytesIn.writeByte(0x40) // Literal indexed
bytesIn.writeByte(0x88) // Literal name Huffman encoded 8 bytes
// decodes to custom-key which is length 10
bytesIn.write("25a849e95ba97d7f".decodeHex())
bytesIn.writeByte(0x89) // Literal value Huffman encoded 9 bytes
// decodes to custom-value which is length 12
bytesIn.write("25a849e95bb8e8b4bf".decodeHex())
}
private fun checkReadThirdRequestWithHuffman() {
assertThat(hpackReader!!.headerCount).isEqualTo(3)
// [ 1] (s = 54) custom-key: custom-value
var entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 3]!!
checkEntry(entry, "custom-key", "custom-value", 54)
// [ 2] (s = 53) cache-control: no-cache
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 2]!!
checkEntry(entry, "cache-control", "no-cache", 53)
// [ 3] (s = 57) :authority: www.example.com
entry = hpackReader!!.dynamicTable[readerHeaderTableLength() - 1]!!
checkEntry(entry, ":authority", "www.example.com", 57)
// Table size: 164
assertThat(hpackReader!!.dynamicTableByteCount).isEqualTo(164)
// Decoded header list:
assertThat(hpackReader!!.getAndResetHeaderList()).isEqualTo(
headerEntries(
":method", "GET",
":scheme", "https",
":path", "/index.html",
":authority", "www.example.com",
"custom-key", "custom-value",
),
)
}
@Test
fun readSingleByteInt() {
assertThat(newReader(byteStream()).readInt(10, 31)).isEqualTo(10)
assertThat(newReader(byteStream()).readInt(0xe0 or 10, 31)).isEqualTo(10)
}
@Test
fun readMultibyteInt() {
assertThat(newReader(byteStream(154, 10)).readInt(31, 31)).isEqualTo(1337)
}
@Test
fun writeSingleByteInt() {
hpackWriter!!.writeInt(10, 31, 0)
assertBytes(10)
hpackWriter!!.writeInt(10, 31, 0xe0)
assertBytes(0xe0 or 10)
}
@Test
fun writeMultibyteInt() {
hpackWriter!!.writeInt(1337, 31, 0)
assertBytes(31, 154, 10)
hpackWriter!!.writeInt(1337, 31, 0xe0)
assertBytes(0xe0 or 31, 154, 10)
}
@Test
fun max31BitValue() {
hpackWriter!!.writeInt(0x7fffffff, 31, 0)
assertBytes(31, 224, 255, 255, 255, 7)
assertThat(newReader(byteStream(224, 255, 255, 255, 7)).readInt(31, 31))
.isEqualTo(0x7fffffff)
}
@Test
fun prefixMask() {
hpackWriter!!.writeInt(31, 31, 0)
assertBytes(31, 0)
assertThat(newReader(byteStream(0)).readInt(31, 31)).isEqualTo(31)
}
@Test
fun prefixMaskMinusOne() {
hpackWriter!!.writeInt(30, 31, 0)
assertBytes(30)
assertThat(newReader(byteStream(0)).readInt(31, 31)).isEqualTo(31)
}
@Test
fun zero() {
hpackWriter!!.writeInt(0, 31, 0)
assertBytes(0)
assertThat(newReader(byteStream()).readInt(0, 31)).isEqualTo(0)
}
@Test
fun lowercaseHeaderNameBeforeEmit() {
hpackWriter!!.writeHeaders(listOf(Header("FoO", "BaR")))
assertBytes(0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'B'.code, 'a'.code, 'R'.code)
}
@Test
fun mixedCaseHeaderNameIsMalformed() {
assertFailsWith<IOException> {
newReader(
byteStream(
0,
3,
'F'.code,
'o'.code,
'o'.code,
3,
'B'.code,
'a'.code,
'R'.code,
),
).readHeaders()
}.also { expected ->
assertThat(expected.message).isEqualTo(
"PROTOCOL_ERROR response malformed: mixed case name: Foo",
)
}
}
@Test
fun emptyHeaderName() {
hpackWriter!!.writeByteString("".encodeUtf8())
assertBytes(0)
assertThat(newReader(byteStream(0)).readByteString())
.isEqualTo(ByteString.EMPTY)
}
@Test
fun emitsDynamicTableSizeUpdate() {
hpackWriter!!.resizeHeaderTable(2048)
hpackWriter!!.writeHeaders(listOf(Header("foo", "bar")))
assertBytes(
// Dynamic table size update (size = 2048).
0x3F, 0xE1, 0xF,
0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'b'.code, 'a'.code, 'r'.code,
)
hpackWriter!!.resizeHeaderTable(8192)
hpackWriter!!.writeHeaders(listOf(Header("bar", "foo")))
assertBytes(
// Dynamic table size update (size = 8192).
0x3F, 0xE1, 0x3F,
0x40, 3, 'b'.code, 'a'.code, 'r'.code, 3, 'f'.code, 'o'.code, 'o'.code,
)
// No more dynamic table updates should be emitted.
hpackWriter!!.writeHeaders(listOf(Header("far", "boo")))
assertBytes(0x40, 3, 'f'.code, 'a'.code, 'r'.code, 3, 'b'.code, 'o'.code, 'o'.code)
}
@Test
fun noDynamicTableSizeUpdateWhenSizeIsEqual() {
val currentSize = hpackWriter!!.headerTableSizeSetting
hpackWriter!!.resizeHeaderTable(currentSize)
hpackWriter!!.writeHeaders(listOf(Header("foo", "bar")))
assertBytes(0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'b'.code, 'a'.code, 'r'.code)
}
@Test
fun growDynamicTableSize() {
hpackWriter!!.resizeHeaderTable(8192)
hpackWriter!!.resizeHeaderTable(16384)
hpackWriter!!.writeHeaders(listOf(Header("foo", "bar")))
assertBytes(
// Dynamic table size update (size = 16384).
0x3F, 0xE1, 0x7F,
0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'b'.code, 'a'.code, 'r'.code,
)
}
@Test
fun shrinkDynamicTableSize() {
hpackWriter!!.resizeHeaderTable(2048)
hpackWriter!!.resizeHeaderTable(0)
hpackWriter!!.writeHeaders(listOf(Header("foo", "bar")))
assertBytes(
// Dynamic size update (size = 0).
0x20,
0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'b'.code, 'a'.code, 'r'.code,
)
}
@Test
fun manyDynamicTableSizeChanges() {
hpackWriter!!.resizeHeaderTable(16384)
hpackWriter!!.resizeHeaderTable(8096)
hpackWriter!!.resizeHeaderTable(0)
hpackWriter!!.resizeHeaderTable(4096)
hpackWriter!!.resizeHeaderTable(2048)
hpackWriter!!.writeHeaders(listOf(Header("foo", "bar")))
assertBytes(
// Dynamic size update (size = 0).
0x20,
// Dynamic size update (size = 2048).
0x3F, 0xE1, 0xF,
0x40, 3, 'f'.code, 'o'.code, 'o'.code, 3, 'b'.code, 'a'.code, 'r'.code,
)
}
@Test
fun dynamicTableEvictionWhenSizeLowered() {
val headerBlock =
headerEntries(
"custom-key1",
"custom-header",
"custom-key2",
"custom-header",
)
hpackWriter!!.writeHeaders(headerBlock)
assertThat(hpackWriter!!.headerCount).isEqualTo(2)
hpackWriter!!.resizeHeaderTable(56)
assertThat(hpackWriter!!.headerCount).isEqualTo(1)
hpackWriter!!.resizeHeaderTable(0)
assertThat(hpackWriter!!.headerCount).isEqualTo(0)
}
@Test
fun noEvictionOnDynamicTableSizeIncrease() {
val headerBlock =
headerEntries(
"custom-key1",
"custom-header",
"custom-key2",
"custom-header",
)
hpackWriter!!.writeHeaders(headerBlock)
assertThat(hpackWriter!!.headerCount).isEqualTo(2)
hpackWriter!!.resizeHeaderTable(8192)
assertThat(hpackWriter!!.headerCount).isEqualTo(2)
}
@Test
fun dynamicTableSizeHasAnUpperBound() {
hpackWriter!!.resizeHeaderTable(1048576)
assertThat(hpackWriter!!.maxDynamicTableByteCount).isEqualTo(16384)
}
@Test
fun huffmanEncode() {
hpackWriter = Hpack.Writer(4096, true, bytesOut)
hpackWriter!!.writeHeaders(headerEntries("foo", "bar"))
val expected =
Buffer()
.writeByte(0x40) // Literal header, new name.
.writeByte(0x82) // String literal is Huffman encoded (len = 2).
.writeByte(0x94) // 'foo' Huffman encoded.
.writeByte(0xE7)
.writeByte(3) // String literal not Huffman encoded (len = 3).
.writeByte('b'.code)
.writeByte('a'.code)
.writeByte('r'.code)
.readByteString()
val actual = bytesOut.readByteString()
assertThat(actual).isEqualTo(expected)
}
@Test
fun staticTableIndexedHeaders() {
hpackWriter!!.writeHeaders(headerEntries(":method", "GET"))
assertBytes(0x82)
assertThat(hpackWriter!!.headerCount).isEqualTo(0)
hpackWriter!!.writeHeaders(headerEntries(":method", "POST"))
assertBytes(0x83)
assertThat(hpackWriter!!.headerCount).isEqualTo(0)
hpackWriter!!.writeHeaders(headerEntries(":path", "/"))