Make H3StaticTable class static (#36222)
authorYoussef Victor <31348972+Youssef1313@users.noreply.github.com>
Fri, 29 May 2020 22:35:04 +0000 (00:35 +0200)
committerGitHub <noreply@github.com>
Fri, 29 May 2020 22:35:04 +0000 (18:35 -0400)
* Make class as static

* Update H3StaticTable.cs

* Update H3StaticTable.cs

* Update H3StaticTable.cs

src/libraries/Common/src/System/Net/Http/aspnetcore/Http3/QPack/H3StaticTable.cs
src/libraries/Common/src/System/Net/Http/aspnetcore/Http3/QPack/QPackDecoder.cs
src/libraries/Common/src/System/Net/Http/aspnetcore/Http3/QPack/QPackEncoder.cs

index 13fc509..c4ca224 100644 (file)
@@ -7,10 +7,9 @@ using System.Text;
 
 namespace System.Net.Http.QPack
 {
-    // TODO: make class static.
-    internal class H3StaticTable
+    internal static class H3StaticTable
     {
-        private readonly Dictionary<int, int> _statusIndex = new Dictionary<int, int>
+        private static readonly Dictionary<int, int> s_statusIndex = new Dictionary<int, int>
         {
             [103] = 24,
             [200] = 25,
@@ -28,7 +27,7 @@ namespace System.Net.Http.QPack
             [500] = 71,
         };
 
-        private readonly Dictionary<HttpMethod, int> _methodIndex = new Dictionary<HttpMethod, int>
+        private static readonly Dictionary<HttpMethod, int> s_methodIndex = new Dictionary<HttpMethod, int>
         {
             // TODO connect is internal to system.net.http
             [HttpMethod.Delete] = 16,
@@ -39,21 +38,15 @@ namespace System.Net.Http.QPack
             [HttpMethod.Put] = 21,
         };
 
-        private H3StaticTable()
-        {
-        }
-
-        public static H3StaticTable Instance { get; } = new H3StaticTable();
-
-        public int Count => _staticTable.Length;
-
-        public HeaderField this[int index] => _staticTable[index];
+        public static int Count => s_staticTable.Length;
 
         // TODO: just use Dictionary directly to avoid interface dispatch.
-        public IReadOnlyDictionary<int, int> StatusIndex => _statusIndex;
-        public IReadOnlyDictionary<HttpMethod, int> MethodIndex => _methodIndex;
+        public static IReadOnlyDictionary<int, int> StatusIndex => s_statusIndex;
+        public static IReadOnlyDictionary<HttpMethod, int> MethodIndex => s_methodIndex;
+
+        public static HeaderField GetHeaderFieldAt(int index) => s_staticTable[index];
 
-        private readonly HeaderField[] _staticTable = new HeaderField[]
+        private static readonly HeaderField[] s_staticTable = new HeaderField[]
         {
             CreateHeaderField(":authority", ""), // 0
             CreateHeaderField(":path", "/"), // 1
index e3b8587..0ac27cd 100644 (file)
@@ -413,7 +413,7 @@ namespace System.Net.Http.QPack
 
             if (_index is int index)
             {
-                Debug.Assert(index >= 0 && index <= H3StaticTable.Instance.Count, $"The index should be a valid static index here. {nameof(QPackDecoder)} should have previously thrown if it read a dynamic index.");
+                Debug.Assert(index >= 0 && index <= H3StaticTable.Count, $"The index should be a valid static index here. {nameof(QPackDecoder)} should have previously thrown if it read a dynamic index.");
                 handler.OnStaticIndexedHeader(index, headerValueSpan);
                 _index = null;
 
index c956095..49cfd84 100644 (file)
@@ -416,7 +416,7 @@ namespace System.Net.Http.QPack
                 case 404:
                 case 500:
                     // TODO this isn't safe, some index can be larger than 64. Encoded here!
-                    buffer[0] = (byte)(0xC0 | H3StaticTable.Instance.StatusIndex[statusCode]);
+                    buffer[0] = (byte)(0xC0 | H3StaticTable.StatusIndex[statusCode]);
                     return 1;
                 default:
                     // Send as Literal Header Field Without Indexing - Indexed Name