File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 5
5
6
6
namespace Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Http2 . HPack
7
7
{
8
+ // The dynamic table is defined as a queue where items are inserted at the front and removed from the back.
9
+ // It's implemented as a circular buffer that appends to the end and trims from the front. Thus index are reversed.
8
10
internal class DynamicTable
9
11
{
10
12
private HeaderField [ ] _buffer ;
@@ -35,7 +37,13 @@ public HeaderField this[int index]
35
37
throw new IndexOutOfRangeException ( ) ;
36
38
}
37
39
38
- return _buffer [ _insertIndex == 0 ? _buffer . Length - 1 : _insertIndex - index - 1 ] ;
40
+ var modIndex = _insertIndex - index - 1 ;
41
+ if ( modIndex < 0 )
42
+ {
43
+ modIndex += _buffer . Length ;
44
+ }
45
+
46
+ return _buffer [ modIndex ] ;
39
47
}
40
48
}
41
49
Original file line number Diff line number Diff line change 1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
@@ -58,6 +58,22 @@ public void FirstEntryIsMostRecentEntry()
58
58
VerifyTableEntries ( dynamicTable , _header2 , _header1 ) ;
59
59
}
60
60
61
+ [ Fact ]
62
+ public void WrapsAroundBuffer ( )
63
+ {
64
+ var header3 = new HeaderField ( Encoding . ASCII . GetBytes ( "header-3" ) , Encoding . ASCII . GetBytes ( "value3" ) ) ;
65
+ var header4 = new HeaderField ( Encoding . ASCII . GetBytes ( "header-4" ) , Encoding . ASCII . GetBytes ( "value4" ) ) ;
66
+
67
+ // Make the table small enough that the circular buffer kicks in.
68
+ var dynamicTable = new DynamicTable ( HeaderField . RfcOverhead * 3 ) ;
69
+ dynamicTable . Insert ( header4 . Name , header4 . Value ) ;
70
+ dynamicTable . Insert ( header3 . Name , header3 . Value ) ;
71
+ dynamicTable . Insert ( _header2 . Name , _header2 . Value ) ;
72
+ dynamicTable . Insert ( _header1 . Name , _header1 . Value ) ;
73
+
74
+ VerifyTableEntries ( dynamicTable , _header1 , _header2 ) ;
75
+ }
76
+
61
77
[ Fact ]
62
78
public void ThrowsIndexOutOfRangeException ( )
63
79
{
You can’t perform that action at this time.
0 commit comments