From 50885f7cd7e0519c96b0c95fce9ba6bf69502cf7 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 8 Jan 2024 22:04:41 -0500 Subject: Some net 8.0 goodies like sha3 support --- lib/Net.Http/src/Helpers/HttpHelpers.cs | 35 +++++++++++++++++---------------- lib/Net.Http/src/Helpers/MimeLookups.cs | 19 ++++++++++-------- 2 files changed, 29 insertions(+), 25 deletions(-) (limited to 'lib/Net.Http/src/Helpers') 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 MethodHashLookup = HashHttpMethods(); + private static readonly FrozenDictionary 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 RequestHeaderLookup = new Dictionary(StringComparer.OrdinalIgnoreCase) + private static readonly FrozenDictionary RequestHeaderLookup = new Dictionary(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 RequestHeaderHashLookup = ComputeCodeHashLookup(RequestHeaderLookup); + private static readonly FrozenDictionary RequestHeaderHashLookup = ComputeCodeHashLookup(RequestHeaderLookup); /* * Provides a constant lookup table for http version hashcodes to an http * version enum value */ - private static readonly IReadOnlyDictionary VersionHashLookup = new Dictionary() + private static readonly FrozenDictionary VersionHashLookup = new Dictionary() { { 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 V0_9_STATUS_CODES = GetStatusCodes("0.9"); - private static readonly IReadOnlyDictionary V1_STAUTS_CODES = GetStatusCodes("1.0"); - private static readonly IReadOnlyDictionary V1_1_STATUS_CODES = GetStatusCodes("1.1"); - private static readonly IReadOnlyDictionary V2_STATUS_CODES = GetStatusCodes("2.0"); + private static readonly FrozenDictionary V0_9_STATUS_CODES = GetStatusCodes("0.9"); + private static readonly FrozenDictionary V1_STAUTS_CODES = GetStatusCodes("1.0"); + private static readonly FrozenDictionary V1_1_STATUS_CODES = GetStatusCodes("1.1"); + private static readonly FrozenDictionary V2_STATUS_CODES = GetStatusCodes("2.0"); - private static IReadOnlyDictionary GetStatusCodes(string version) + private static FrozenDictionary GetStatusCodes(string version) { //Setup status code dict Dictionary 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 HashHttpMethods() + private static FrozenDictionary 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 ComputeCodeHashLookup(IEnumerable> enumerable) - => enumerable.ToDictionary( + private static FrozenDictionary ComputeCodeHashLookup(IEnumerable> 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 CtToMime = new Dictionary() + private static readonly FrozenDictionary CtToMime = new Dictionary() { { 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 ExtensionToCt = new Dictionary() + }.ToFrozenDictionary(); + + private static readonly FrozenDictionary ExtensionToCt = new Dictionary() { { "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 MimeToCt = new Dictionary() + }.ToFrozenDictionary(); + + private static readonly FrozenDictionary MimeToCt = new Dictionary() { { "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 ContentTypeHashLookup = ComputeCodeHashLookup(MimeToCt); + private static readonly FrozenDictionary ContentTypeHashLookup = ComputeCodeHashLookup(MimeToCt); } } -- cgit