aboutsummaryrefslogtreecommitdiff
path: root/lib/Net.Http/src/Helpers
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-01-08 22:04:41 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-01-08 22:04:41 -0500
commit50885f7cd7e0519c96b0c95fce9ba6bf69502cf7 (patch)
tree11db78a9c2b9da56de92ebe27183c0ce12098947 /lib/Net.Http/src/Helpers
parent86c0532edc0042b26dda5f1ca41abdf80db46414 (diff)
Some net 8.0 goodies like sha3 support
Diffstat (limited to 'lib/Net.Http/src/Helpers')
-rw-r--r--lib/Net.Http/src/Helpers/HttpHelpers.cs35
-rw-r--r--lib/Net.Http/src/Helpers/MimeLookups.cs19
2 files changed, 29 insertions, 25 deletions
diff --git a/lib/Net.Http/src/Helpers/HttpHelpers.cs b/lib/Net.Http/src/Helpers/HttpHelpers.cs
index a4486cd..86511a5 100644
--- a/lib/Net.Http/src/Helpers/HttpHelpers.cs
+++ b/lib/Net.Http/src/Helpers/HttpHelpers.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Http
@@ -27,6 +27,7 @@ using System.IO;
using System.Net;
using System.Linq;
using System.Net.Sockets;
+using System.Collections.Frozen;
using System.Collections.Generic;
using System.Text.RegularExpressions;
@@ -66,14 +67,14 @@ namespace VNLib.Net.Http
* an HttpMethod enum value,
*/
- private static readonly IReadOnlyDictionary<int, HttpMethod> MethodHashLookup = HashHttpMethods();
+ private static readonly FrozenDictionary<int, HttpMethod> MethodHashLookup = HashHttpMethods();
/*
* Provides a constant lookup table from an MIME http request header string to a .NET
* enum value (with some extra support)
*/
- private static readonly IReadOnlyDictionary<string, HttpRequestHeader> RequestHeaderLookup = new Dictionary<string, HttpRequestHeader>(StringComparer.OrdinalIgnoreCase)
+ private static readonly FrozenDictionary<string, HttpRequestHeader> RequestHeaderLookup = new Dictionary<string, HttpRequestHeader>(StringComparer.OrdinalIgnoreCase)
{
{"CacheControl", HttpRequestHeader.CacheControl },
{"Connection", HttpRequestHeader.Connection },
@@ -119,7 +120,7 @@ namespace VNLib.Net.Http
//Custom request headers
{ "Content-Disposition", ContentDisposition },
{ "origin", Origin }
- };
+ }.ToFrozenDictionary();
/*
* Provides a lookup table for request header hashcodes (that are hashed in
@@ -128,28 +129,28 @@ namespace VNLib.Net.Http
* during request parsing)
*
*/
- private static readonly IReadOnlyDictionary<int, HttpRequestHeader> RequestHeaderHashLookup = ComputeCodeHashLookup(RequestHeaderLookup);
+ private static readonly FrozenDictionary<int, HttpRequestHeader> RequestHeaderHashLookup = ComputeCodeHashLookup(RequestHeaderLookup);
/*
* Provides a constant lookup table for http version hashcodes to an http
* version enum value
*/
- private static readonly IReadOnlyDictionary<int, HttpVersion> VersionHashLookup = new Dictionary<int, HttpVersion>()
+ private static readonly FrozenDictionary<int, HttpVersion> VersionHashLookup = new Dictionary<int, HttpVersion>()
{
{ string.GetHashCode("HTTP/0.9", StringComparison.OrdinalIgnoreCase), HttpVersion.Http09 },
{ string.GetHashCode("HTTP/1.0", StringComparison.OrdinalIgnoreCase), HttpVersion.Http1 },
{ string.GetHashCode("HTTP/1.1", StringComparison.OrdinalIgnoreCase), HttpVersion.Http11 },
{ string.GetHashCode("HTTP/2.0", StringComparison.OrdinalIgnoreCase), HttpVersion.Http2 }
- };
+ }.ToFrozenDictionary();
//Pre-compiled strings for all status codes for http 0.9 1, 1.1
- private static readonly IReadOnlyDictionary<HttpStatusCode, string> V0_9_STATUS_CODES = GetStatusCodes("0.9");
- private static readonly IReadOnlyDictionary<HttpStatusCode, string> V1_STAUTS_CODES = GetStatusCodes("1.0");
- private static readonly IReadOnlyDictionary<HttpStatusCode, string> V1_1_STATUS_CODES = GetStatusCodes("1.1");
- private static readonly IReadOnlyDictionary<HttpStatusCode, string> V2_STATUS_CODES = GetStatusCodes("2.0");
+ private static readonly FrozenDictionary<HttpStatusCode, string> V0_9_STATUS_CODES = GetStatusCodes("0.9");
+ private static readonly FrozenDictionary<HttpStatusCode, string> V1_STAUTS_CODES = GetStatusCodes("1.0");
+ private static readonly FrozenDictionary<HttpStatusCode, string> V1_1_STATUS_CODES = GetStatusCodes("1.1");
+ private static readonly FrozenDictionary<HttpStatusCode, string> V2_STATUS_CODES = GetStatusCodes("2.0");
- private static IReadOnlyDictionary<HttpStatusCode, string> GetStatusCodes(string version)
+ private static FrozenDictionary<HttpStatusCode, string> GetStatusCodes(string version)
{
//Setup status code dict
Dictionary<HttpStatusCode, string> statusCodes = new();
@@ -159,10 +160,10 @@ namespace VNLib.Net.Http
//Use a regex to write the status code value as a string
statusCodes[code] = $"HTTP/{version} {(int)code} {HttpRequestBuilderRegex.Replace(code.ToString(), " $1")}";
}
- return statusCodes;
+ return statusCodes.ToFrozenDictionary();
}
- private static IReadOnlyDictionary<int, HttpMethod> HashHttpMethods()
+ private static FrozenDictionary<int, HttpMethod> HashHttpMethods()
{
/*
* Http methods are hashed at runtime using the HttpMethod enum
@@ -173,11 +174,11 @@ namespace VNLib.Net.Http
//Exclude the not supported method
.Except(new HttpMethod[] { HttpMethod.None })
.Select(m => KeyValuePair.Create(m.ToString(), m))
- );
+ ).ToFrozenDictionary();
}
- private static IReadOnlyDictionary<int, T> ComputeCodeHashLookup<T>(IEnumerable<KeyValuePair<string, T>> enumerable)
- => enumerable.ToDictionary(
+ private static FrozenDictionary<int, T> ComputeCodeHashLookup<T>(IEnumerable<KeyValuePair<string, T>> enumerable)
+ => enumerable.ToFrozenDictionary(
static kv => string.GetHashCode(kv.Key, StringComparison.OrdinalIgnoreCase),
static kv => kv.Value
);
diff --git a/lib/Net.Http/src/Helpers/MimeLookups.cs b/lib/Net.Http/src/Helpers/MimeLookups.cs
index c0dd82c..ab1aef4 100644
--- a/lib/Net.Http/src/Helpers/MimeLookups.cs
+++ b/lib/Net.Http/src/Helpers/MimeLookups.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Http
@@ -23,6 +23,7 @@
*/
+using System.Collections.Frozen;
using System.Collections.Generic;
namespace VNLib.Net.Http
@@ -30,7 +31,7 @@ namespace VNLib.Net.Http
public static partial class HttpHelpers
{
//Content type lookup dict
- private static readonly IReadOnlyDictionary<ContentType, string> CtToMime = new Dictionary<ContentType, string>()
+ private static readonly FrozenDictionary<ContentType, string> CtToMime = new Dictionary<ContentType, string>()
{
{ ContentType.NonSupported, "application/octet-stream" },
{ ContentType.UrlEncoded, "application/x-www-form-urlencoded" },
@@ -1179,8 +1180,9 @@ namespace VNLib.Net.Http
{ ContentType.Zir, "application/vnd.zul" },
{ ContentType.Zirz, "application/vnd.zul" },
{ ContentType.Zmm, "application/vnd.handheld-entertainment+xml" },
- };
- private static readonly IReadOnlyDictionary<string, ContentType> ExtensionToCt = new Dictionary<string, ContentType>()
+ }.ToFrozenDictionary();
+
+ private static readonly FrozenDictionary<string, ContentType> ExtensionToCt = new Dictionary<string, ContentType>()
{
{ "aab", ContentType.Aab },
{ "aac", ContentType.Aac },
@@ -2327,8 +2329,9 @@ namespace VNLib.Net.Http
{ "zir", ContentType.Zir },
{ "zirz", ContentType.Zirz },
{ "zmm", ContentType.Zmm },
- };
- private static readonly IReadOnlyDictionary<string, ContentType> MimeToCt = new Dictionary<string, ContentType>()
+ }.ToFrozenDictionary();
+
+ private static readonly FrozenDictionary<string, ContentType> MimeToCt = new Dictionary<string, ContentType>()
{
{ "application/x-www-form-urlencoded", ContentType.UrlEncoded },
{ "multipart/form-data", ContentType.MultiPart },
@@ -3230,12 +3233,12 @@ namespace VNLib.Net.Http
{ "application/zip", ContentType.Zip },
{ "application/vnd.zul", ContentType.Zir },
{ "application/vnd.handheld-entertainment+xml", ContentType.Zmm },
- };
+ }.ToFrozenDictionary();
/*
* A string hashcode lookup table for MIME content types
*/
- private static readonly IReadOnlyDictionary<int, ContentType> ContentTypeHashLookup = ComputeCodeHashLookup(MimeToCt);
+ private static readonly FrozenDictionary<int, ContentType> ContentTypeHashLookup = ComputeCodeHashLookup(MimeToCt);
}
}