-
Notifications
You must be signed in to change notification settings - Fork 183
/
HPack.jl
872 lines (687 loc) · 27.9 KB
/
HPack.jl
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
"""
Lazy Parsing and String comparison for
[RFC7541](https://tools.ietf.org/html/rfc7541)
"HPACK Header Compression for HTTP/2".
See ../test/HPack.jl
Copyright (c) 2018, Sam O'Connor
huffmandata.jl and hp_huffman_encode created by Wei Tang:
Copyright (c) 2016: Wei Tang, MIT "Expat" License:
https://github.com/sorpaas/HPack.jl/blob/master/LICENSE.md
"""
module HPack
# Decoding Errors
abstract type DecodingError <: Exception end
struct IntegerDecodingError <: DecodingError end
struct FieldBoundsError <: DecodingError end
struct IndexBoundsError <: DecodingError end
struct TableUpdateError <: DecodingError end
function Base.show(io::IO, ::IntegerDecodingError)
println(io, """
HPack.IntegerDecodingError()
Encoded integer length exceeds Implementation Limit (~ 2097278).
See: https://tools.ietf.org/html/rfc7541#section-7.4
""")
end
function Base.show(io::IO, ::FieldBoundsError)
println(io, """
HPack.FieldBoundsError()
Encoded field length exceeds Header Block size.
""")
end
function Base.show(io::IO, ::IndexBoundsError)
println(io, """
HPack.IndexBoundsError()
Encoded field index exceeds Dynamic Table size.
""")
end
function Base.show(io::IO, ::TableUpdateError)
println(io, """
HPack.TableUpdateError()
This dynamic table size update MUST occur at the beginning of the
first header block following the change to the dynamic table size.
https://tools.ietf.org/html/rfc7541#section-4.2
""")
end
# Integers
"""
Decode integer at index `i` in `buf` with prefix `mask`.
Return the index of next byte in `buf` and the decoded value.
> An integer is represented in two parts: a prefix that fills the
> current octet and an optional list of octets that are used if the
> integer value does not fit within the prefix. The number of bits of
> the prefix (called N) is a parameter of the integer representation.
> If the integer value is small enough, i.e., strictly less than 2^N-1,
> it is encoded within the N-bit prefix.
https://tools.ietf.org/html/rfc7541#section-5.1
"""
function hp_integer(buf::Array{UInt8}, i::UInt, mask::UInt8)::Tuple{UInt,UInt}
v = @inbounds UInt(buf[i]) & mask
i += 1
if v >= mask
c = @inbounds buf[i]
i += 1
v += UInt(c & 0b01111111)
if c & 0b10000000 != 0
c = @inbounds buf[i]
i += 1
v += UInt(c & 0b01111111) << (1 * 7)
if c & 0b10000000 != 0
c = @inbounds buf[i]
i += 1
v += UInt(c & 0b01111111) << (2 * 7)
if c & 0b10000000 != 0
throw(IntegerDecodingError())
end
end
end
end
return i, v
end
hp_integer(buf, i, mask) = hp_integer(buf, UInt(i), mask)
# Huffman Encoded Strings
include("Nibbles.jl")
include("huffmandata.jl")
struct Huffman
nibbles::typeof(Iterators.Stateful(Nibbles.Iterator(UInt8[])))
end
Huffman(bytes::AbstractVector{UInt8}, i, j) =
Huffman(Iterators.Stateful(Nibbles.Iterator(bytes, i, j)))
Base.eltype(::Type{Huffman}) = UInt8
Base.IteratorSize(::Type{Huffman}) = Base.SizeUnknown()
function Base.iterate(s::Huffman, state::UInt = UInt(0))
for n in s.nibbles
state::UInt, flags, c = HUFFMAN_DECODE_TABLE[(state << 4) + n + 1, :]
if flags & 0x04 != 0
return nothing
elseif flags & 0x02 != 0
return c, state
end
end
return nothing
end
function hp_huffman_encode(data)
out = IOBuffer()
current::UInt64 = 0
n = 0
for i = 1:length(data)
b = data[i] & 0xFF
nbits = HUFFMAN_SYMBOL_TABLE[b + 1, 1]
code = HUFFMAN_SYMBOL_TABLE[b + 1, 2]
current <<= nbits
current |= code
n += nbits
while n >= 8
n -= 8
write(out, UInt8(current >>> n))
current = current & ~(current >>> n << n)
end
end
if n > 0
current <<= (8 - n)
current |= (0xFF >>> n)
write(out, UInt8(current))
end
return take!(out)
end
# HPack Strings
"""
`HPackString` fields:
- `bytes`: reference a HPACK header block.
- `i`: index of the start of a string literal in the header block.
> Header field names and header field values can be represented as
> string literals. A string literal is encoded as a sequence of
> octets, either by directly encoding the string literal's octets or by
> using a Huffman code (see [HUFFMAN]).
>
> 0 1 2 3 4 5 6 7
> +---+---+---+---+---+---+---+---+
> | H | String Length (7+) |
> +---+---------------------------+
> | String Data (Length octets) |
> +-------------------------------+
>
> String Data: The encoded data of the string literal. If H is '0',
> then the encoded data is the raw octets of the string literal. If
> H is '1', then the encoded data is the Huffman encoding of the
> string literal.
https://tools.ietf.org/html/rfc7541#section-5.2
"""
struct HPackString
bytes::Vector{UInt8}
i::UInt
end
@inline HPackString(s::HPackString) = HPackString(s.bytes, s.i)
@inline HPackString(bytes::Vector{UInt8}, i::Integer=1) =
HPackString(bytes, UInt(i))
function HPackString(s)
@assert length(s) < 127
buf = IOBuffer()
write(buf, UInt8(length(s)))
write(buf, s)
HPackString(take!(buf), 1)
end
HPackString() = HPackString("")
"""
Is `s` Huffman encoded?
"""
@inline hp_ishuffman(s::HPackString) = hp_ishuffman(@inbounds s.bytes[s.i])
@inline hp_ishuffman(flags::UInt8) = flags & 0b10000000 != 0
"""
Find encoded byte-length of string starting at `i` in `bytes`.
Returns:
- index of first byte after the string and
- byte-length of string.
"""
@inline function hp_string_length(bytes, i)::Tuple{UInt,UInt}
i, l = hp_integer(bytes, i, 0b01111111)
if i + l > length(bytes) + 1 # Allow next i to be one past
throw(FieldBoundsError()) # the end of the buffer.
end
return i, l
end
@inline hp_string_length(s::HPackString)::Tuple{UInt,UInt} =
hp_string_length(s.bytes, s.i)
"""
Find the index of the first byte after the string starting at `i` in `bytes`.
"""
@inline function hp_string_nexti(bytes::Array{UInt8}, i::UInt)
j, l = hp_string_length(bytes, i)
return j + l
end
"""
Number of decoded bytes in `s`.
"""
Base.length(s::HPackString) =
hp_ishuffman(s) ? (l = 0; for c in s l += 1 end; l) : hp_string_length(s)[2]
# Iteration Over Decoded Bytes
Base.eltype(::Type{HPackString}) = UInt8
Base.IteratorSize(::Type{HPackString}) = Base.SizeUnknown()
const StrItrState = Tuple{Union{Huffman, UInt}, UInt}
const StrItrReturn = Union{Nothing, Tuple{UInt8, StrItrState}}
@inline function Base.iterate(s::HPackString)::StrItrReturn
i, l = hp_string_length(s)
max = i + l - 1
if hp_ishuffman(s)
h = Huffman(s.bytes, i, max)
return hp_iterate_huffman(h, UInt(0))
else
return hp_iterate_ascii(s.bytes, max, i)
end
end
@inline function Base.iterate(s::HPackString, state::StrItrState)::StrItrReturn
huf_or_max, i = state
return huf_or_max isa Huffman ? hp_iterate_huffman(huf_or_max, i) :
hp_iterate_ascii(s.bytes, huf_or_max, i)
end
@inline function hp_iterate_ascii(bytes, max::UInt, i::UInt)::StrItrReturn
if i > max
return nothing
end
return (@inbounds bytes[i], (max, i+1))
end
@inline function hp_iterate_huffman(h::Huffman, i::UInt)::StrItrReturn
hstate = iterate(h, i)
if hstate == nothing
return nothing
end
c, i = hstate
return c, (h, i)
end
# Conversion to Base.String
"""
Copy raw ASCII bytes into new String.
Collect decoded Huffman bytes into new String.
"""
function Base.convert(::Type{String}, s::HPackString)
if hp_ishuffman(s)
return String(collect(s))
else
i, l = hp_string_length(s)
buf = Base.StringVector(l)
unsafe_copyto!(buf, 1, s.bytes, i, l)
return String(buf)
end
end
Base.string(s::HPackString) = convert(String, s)
Base.print(io::IO, s::HPackString) = print(io, string(s))
Base.show(io::IO, s::HPackString) = show(io, string(s))
# String Comparison
import Base.==
==(a::HPackString, b::HPackString) = hp_cmp_hpack_hpack(a, b)
==(a::HPackString, b) = hp_cmp(a, b)
==(a, b::HPackString) = hp_cmp(b, a)
function hp_cmp_hpack_hpack(a::HPackString, b::HPackString)
if hp_ishuffman(a) != hp_ishuffman(b)
return hp_cmp(a, b)
end
if @inbounds a.bytes[a.i] != b.bytes[b.i]
return false
end
ai, al = hp_string_length(a)
bi, bl = hp_string_length(b)
if al != bl
return false
end
return Base._memcmp(pointer(a.bytes, ai),
pointer(b.bytes, bi), al) == 0
end
function hp_cmp(a::HPackString, b::Union{String, SubString{String}})
if hp_ishuffman(a)
return hp_cmp(a, codeunits(b))
end
ai, al = hp_string_length(a)
if al != length(b)
return false
end
return Base._memcmp(pointer(a.bytes, ai),
pointer(b), al) == 0
end
function hp_cmp(a, b)
ai = Iterators.Stateful(a)
bi = Iterators.Stateful(b)
for (i, j) in zip(ai, bi)
if i != j
return false
end
end
return isempty(ai) && isempty(bi)
end
hp_cmp(a::HPackString, b::AbstractString) = hp_cmp(a, (UInt(c) for c in b))
# Connection State
mutable struct HPackSession
names::Vector{HPackString}
values::Vector{HPackString}
max_table_size::UInt
table_size::UInt
end
HPackSession() = HPackSession([],[],default_max_table_size,0)
#https://tools.ietf.org/html/rfc7540#section-6.5.2
const default_max_table_size = 4096
function Base.show(io::IO, s::HPackSession)
println(io, "HPackSession with Table Size $(s.table_size):")
i = hp_static_max + 1
for (n, v) in zip(s.names, s.values)
println(io, " [$i] $n: $v")
i += 1
end
println(io, "")
end
function set_max_table_size(s::HPackSession, n)
s.max_table_size = n
purge(s)
end
function purge(s::HPackSession)
return #FIXME can't purge stuff that old lazy blocks may refer to.
while s.table_size > s.max_table_size
s.table_size -= hp_field_size(s, lastindex(s.names))
pop!(s.names)
pop!(s.values)
end
end
"""
The size of an entry is the sum of its name's length in octets (as
defined in Section 5.2), its value's length in octets, and 32.
https://tools.ietf.org/html/rfc7541#section-4.1
"""
hp_field_size(s, i) = hp_string_length(s.names[i])[2] +
hp_string_length(s.values[i])[2] +
32
# Note: implemented as the non huffman decoded length.
# More efficient than decoding and probably has no
# impact other than slightly fewer evictions than normal.
# See https://github.com/http2/http2-spec/issues/767
# Strict decoded length version is:
# hp_field_size(field) = length(field.first) +
# length(field.second) +
# 32
const table_index_flag = UInt(1) << 63
is_tableindex(i) = i > table_index_flag
is_dynamicindex(i) = i > (table_index_flag | hp_static_max)
Base.lastindex(s::HPackSession) = hp_static_max + lastindex(s.names)
@noinline function Base.pushfirst!(s::HPackSession, bytes,
namei::UInt, valuei::UInt, offset::UInt)
name = is_tableindex(namei) ? get_name(s, namei, offset) :
HPackString(bytes, namei)
value = is_tableindex(valuei) ? get_value(s, valuei, offset) :
HPackString(bytes, valuei)
pushfirst!(s.names, name)
pushfirst!(s.values, value)
s.table_size += hp_field_size(s, 1)
purge(s)
end
function get_name(s::HPackSession, i::UInt, offset::UInt=0)::HPackString
i &= ~table_index_flag
if i + offset > lastindex(s)
throw(IndexBoundsError())
end
return i <= hp_static_max ? hp_static_names[i] :
s.names[i + offset - hp_static_max]
end
function get_value(s::HPackSession, i::UInt, offset::UInt=0)::HPackString
i &= ~table_index_flag
if i + offset > lastindex(s)
throw(IndexBoundsError())
end
return i <= hp_static_max ? hp_static_values[i] :
s.values[i + offset - hp_static_max]
end
# Header Fields
mutable struct HPackBlock
session::HPackSession
bytes::Vector{UInt8}
i::UInt
cursor::UInt
dynamic_table_offset::UInt
end
@inline get_name(b::HPackBlock, i::UInt, offset::UInt)::HPackString =
is_tableindex(i) ? HPackString(get_name(b.session, i, offset)) :
HPackString(b.bytes, i)
# FIXME get_name already returns HPackString
# Copy of HPackString might allow iteration
# loop optimisation to eliminate struct?
@inline get_value(b::HPackBlock, i::UInt, offset::UInt)::HPackString =
is_tableindex(i) ? HPackString(get_value(b.session, i, offset)) :
HPackString(b.bytes, i)
HPackBlock(session, bytes, i) = HPackBlock(session, bytes, i, 0, 0)
# Key Lookup Interface
Base.getproperty(h::HPackBlock, s::Symbol) =
s === :authority ? h[":authority"] :
s === :method ? h[":method"] :
s === :path ? h[":path"] :
s === :scheme ? h[":scheme"] :
s === :status ? h[":status"] :
getfield(h, s)
function Base.getindex(b::HPackBlock, key)
for (n, v) in b
if n == key
return v
end
end
throw(KeyError(key))
end
# Iteration Interface
struct BlockKeys b::HPackBlock end
struct BlockValues b::HPackBlock end
const BlockIterator = Union{BlockKeys, BlockValues, HPackBlock}
Base.eltype(::Type{BlockKeys}) = HPackString
Base.eltype(::Type{BlockValues}) = HPackString
Base.eltype(::Type{HPackBlock}) = Pair{HPackString, HPackString}
elvalue(b::BlockKeys, name, value, offset) = get_name(b.b, name, offset)
elvalue(b::BlockValues, name, value, offset) = get_name(b.b, value, offset)
elvalue(b::HPackBlock, name, value, offset) = get_name(b, name, offset) =>
get_value(b, value, offset)
Base.keys(b::HPackBlock) = BlockKeys(b)
Base.values(b::HPackBlock) = BlockValues(b)
Base.IteratorSize(::BlockIterator) = Base.SizeUnknown()
@inline function Base.iterate(bi::BlockIterator)
b::HPackBlock = bi isa HPackBlock ? bi : bi.b
buf = b.bytes
i = b.i # 6.3 Dynamic Table Size Update
# 0 1 2 3 4 5 6 7
flags = @inbounds buf[i] # +---+---+---+---+---+---+---+---+
if flags & 0b11100000 == 0b00100000 # | 0 | 0 | 1 | Max size (5+) |
i, n = hp_integer(buf, i, # +---+---------------------------+
0b00011111)
if b.cursor == 0
b.cursor = i # FIXME Limit to HTTP setting value
@assert n < 64000 # SETTINGS_HEADER_TABLE_SIZE
set_max_table_size(b.session, n)
end
end
return iterate(bi, (i, b.dynamic_table_offset))
end
@inline function Base.iterate(bi::BlockIterator, state::Tuple{UInt,UInt})
b::HPackBlock = bi isa HPackBlock ? bi : bi.b
buf = b.bytes
i, dynamic_table_offset = state
if i > length(buf)
@assert i == length(buf) + 1
return nothing
end
flags = @inbounds buf[i]
name, value, i = hp_field(buf, i, flags)
v = elvalue(bi, name, value, dynamic_table_offset)
if flags & 0b11000000 == 0b01000000 # 6.2.1. Incremental Indexing.
if i <= b.cursor # For old fields (behind the cursor),
dynamic_table_offset -= 1 # update the Dynamic Table offset.
else
b.cursor = i # For new fields, update the cursor
b.dynamic_table_offset += 1 # and push new row into Dynamic Table.
pushfirst!(b.session, buf,
name, value,
dynamic_table_offset)
end
end
return v, (i, dynamic_table_offset)
end
# Field Binary Format Decoding
"""
Returns name and value byte-indexes of the field starting at `i` in `buf`,
and the index of the next field (or `length(buf) + 1`).
If the name or value is a reference to the Indexing Tables then
the table index has the `table_index_flag` flag set. See `is_tableindex`.
"""
function hp_field(buf, i::UInt, flags::UInt8)::Tuple{UInt,UInt,UInt}
index_mask, literal_string_count = hp_field_format(flags)
if index_mask == 0b00011111 # Dynamic Table Size Update
throw(TableUpdateError()) # only allowed at start of
end # block [RFC7541 4.2, 6.3]
if index_mask != 0 # Name is a reference to
i, name_i = hp_integer(buf, i, index_mask) # the Indexing Tables.
if name_i == 0
throw(IndexBoundsError())
end
name_i |= table_index_flag
if literal_string_count == 0
value_i = name_i
else
value_i = i
i = hp_string_nexti(buf, i)
end
else
name_i = i + 1 # Name and Values are
value_i = hp_string_nexti(buf, name_i) # both literal strings.
i = hp_string_nexti(buf, value_i)
end
return name_i, value_i, i
end
"""
Determine a field's Binary Format from `flags`.
Returns:
- the integer decoding prefix mask for an indexed field (or zero) and
- the number of literal strings.
https://tools.ietf.org/html/rfc7541#section-6
"""
function hp_field_format(flags::UInt8)
index_mask::UInt8 = 0 # 6.1. Indexed Header Field
literal_string_count::UInt = 0 # 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
if flags & 0b10000000 != 0 # | 1 | Index (7+) |
# +---+---------------------------+
index_mask = 0b01111111
# 6.2.1. Literal Header Field
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
elseif flags == 0b01000000 || # | 0 | 1 | 0 |
# +---+---+-----------------------+
# | H | Name Length (7+) |
# +---+---------------------------+
# | Name String (Length octets) |
# +---+---------------------------+
# | H | Value Length (7+) |
# +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
#
# 6.2.2. Literal Header Field
# without Indexing
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
flags == 0b00000000 || # | 0 | 0 | 0 | 0 | 0 |
# +---+---+-----------------------+
# | H | Name Length (7+) |
# +---+---------------------------+
# | Name String (Length octets) |
# +---+---------------------------+
# | H | Value Length (7+) |
# +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
#
# 6.2.3. Literal Header Field
# Never Indexed
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
flags == 0b00010000 # | 0 | 0 | 0 | 1 | 0 |
# +---+---+-----------------------+
literal_string_count = 2 # | H | Name Length (7+) |
# +---+---------------------------+
# | Name String (Length octets) |
# +---+---------------------------+
# | H | Value Length (7+) |
# +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
# 6.2.1. Literal Header Field
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
elseif flags & 0b01000000 != 0 # | 0 | 1 | Index (6+) |
# +---+---+-----------------------+
index_mask = 0b00111111 # | H | Value Length (7+) |
literal_string_count = 1 # +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
# 6.3 Dynamic Table Size Update
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
elseif flags & 0b00100000 != 0 # | 0 | 0 | 1 | Max size (5+) |
# +---+---------------------------+
index_mask = 0b00011111
# 6.2.2. Literal Header Field
# without Indexing
else # 0 1 2 3 4 5 6 7
index_mask = 0b00001111 # +---+---+---+---+---+---+---+---+
literal_string_count = 1 # | 0 | 0 | 0 | 0 | Index (4+) |
end # +---+---+-----------------------+
# | H | Value Length (7+) |
# +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
#
# 6.2.3. Literal Header Field
# Never Indexed
# 0 1 2 3 4 5 6 7
# +---+---+---+---+---+---+---+---+
# | 0 | 0 | 0 | 1 | Index (4+) |
# +---+---+-----------------------+
# | H | Value Length (7+) |
# +---+---------------------------+
# | Value String (Length octets) |
# +-------------------------------+
return index_mask, literal_string_count
end
# Static Table
"""
> The static table (see Section 2.3.1) consists in a predefined and
> unchangeable list of header fields.
https://tools.ietf.org/html/rfc7541#appendix-A
"""
const hp_static_strings = [
":authority" => "",
":method" => "GET",
":method" => "POST",
":path" => "/",
":path" => "/index.html",
":scheme" => "http",
":scheme" => "https",
":status" => "200",
":status" => "204",
":status" => "206",
":status" => "304",
":status" => "400",
":status" => "404",
":status" => "500",
"accept-" => "",
"accept-encoding" => "gzip, deflate",
"accept-language" => "",
"accept-ranges" => "",
"accept" => "",
"access-control-allow-origin" => "",
"age" => "",
"allow" => "",
"authorization" => "",
"cache-control" => "",
"content-disposition" => "",
"content-encoding" => "",
"content-language" => "",
"content-length" => "",
"content-location" => "",
"content-range" => "",
"content-type" => "",
"cookie" => "",
"date" => "",
"etag" => "",
"expect" => "",
"expires" => "",
"from" => "",
"host" => "",
"if-match" => "",
"if-modified-since" => "",
"if-none-match" => "",
"if-range" => "",
"if-unmodified-since" => "",
"last-modified" => "",
"link" => "",
"location" => "",
"max-forwards" => "",
"proxy-authenticate" => "",
"proxy-authorization" => "",
"range" => "",
"referer" => "",
"refresh" => "",
"retry-after" => "",
"server" => "",
"set-cookie" => "",
"strict-transport-security" => "",
"transfer-encoding" => "",
"user-agent" => "",
"vary" => "",
"via" => "",
"www-authenticate" => ""
]
const hp_static_max = lastindex(hp_static_strings)
const hp_static_names = [HPackString(n) for (n, v) in hp_static_strings]
const hp_static_values = [HPackString(v) for (n, v) in hp_static_strings]
# Fast skipping of fields without decoding
#=
function hp_integer_nexti(buf::Array{UInt8}, i::UInt,
mask::UInt8, flags::UInt8)::UInt
if flags & mask == mask
flags = @inbounds v[i += 1]
while flags & 0b10000000 != 0
flags = @inbounds v[i += 1]
end
end
i += 1
if i > length(buf) + 1 # Allow next i to be one past
throw(IntegerDecodingError()) # the end of the buffer.
end
return i
end
hp_field_nexti(buf, i) = hp_field_nexti(buf, i, @inbounds buf[i])
function hp_field_nexti(buf::Vector{UInt8}, i::UInt, flags::UInt8)::UInt
int_mask, string_count = hp_field_format(buf, i, flags)
if int_mask != 0
i = hp_integer_nexti(buf, i, int_mask, flags)
else
i += 1
end
while string_count > 0
i = hp_string_nexti(buf, i)
string_count -= 1
end
@assert i <= length(buf) + 1
return i
end
=#
end # module HPack