diff options
Diffstat (limited to 'lib/Net.Http/src')
-rw-r--r-- | lib/Net.Http/src/Core/HttpServerBase.cs | 14 | ||||
-rw-r--r-- | lib/Net.Http/src/Core/HttpServerProcessing.cs | 2 | ||||
-rw-r--r-- | lib/Net.Http/src/Core/InitDataBuffer.cs | 4 | ||||
-rw-r--r-- | lib/Net.Http/src/Helpers/HttpHelpers.cs | 35 | ||||
-rw-r--r-- | lib/Net.Http/src/Helpers/MimeLookups.cs | 19 |
5 files changed, 39 insertions, 35 deletions
diff --git a/lib/Net.Http/src/Core/HttpServerBase.cs b/lib/Net.Http/src/Core/HttpServerBase.cs index ec42e5b..a73867f 100644 --- a/lib/Net.Http/src/Core/HttpServerBase.cs +++ b/lib/Net.Http/src/Core/HttpServerBase.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Http @@ -36,10 +36,10 @@ */ using System; -using System.Linq; using System.Threading; using System.Net.Sockets; using System.Threading.Tasks; +using System.Collections.Frozen; using System.Collections.Generic; using System.Security.Authentication; @@ -76,7 +76,7 @@ namespace VNLib.Net.Http internal static readonly Memory<byte> WriteOnlyScratchBuffer = new byte[64 * 1024]; private readonly ITransportProvider Transport; - private readonly IReadOnlyDictionary<string, IWebRoot> ServerRoots; + private readonly FrozenDictionary<string, IWebRoot> ServerRoots; private readonly IWebRoot? _wildcardRoot; private readonly HttpConfig _config; @@ -139,7 +139,7 @@ namespace VNLib.Net.Http _config = config; //Configure roots and their directories - ServerRoots = sites.ToDictionary(static r => r.Hostname, static tv => tv, StringComparer.OrdinalIgnoreCase); + ServerRoots = sites.ToFrozenDictionary(static r => r.Hostname, static tv => tv, StringComparer.OrdinalIgnoreCase); //Compile and store the timeout keepalive header KeepAliveTimeoutHeaderValue = $"timeout={(int)_config.ConnectionKeepAlive.TotalSeconds}"; //Create a new context store @@ -164,9 +164,9 @@ namespace VNLib.Net.Http private static void ValidateConfig(in HttpConfig conf) { - _ = conf.HttpEncoding ?? throw new ArgumentException("HttpEncoding cannot be null", nameof(conf)); - _ = conf.ServerLog ?? throw new ArgumentException("ServerLog cannot be null", nameof(conf)); - _ = conf.MemoryPool ?? throw new ArgumentNullException(nameof(conf)); + ArgumentNullException.ThrowIfNull(conf.HttpEncoding, nameof(conf.HttpEncoding)); + ArgumentNullException.ThrowIfNull(conf.ServerLog, nameof(conf.ServerLog)); + ArgumentNullException.ThrowIfNull(conf.MemoryPool, nameof(conf.MemoryPool)); if (conf.ActiveConnectionRecvTimeout < -1) { diff --git a/lib/Net.Http/src/Core/HttpServerProcessing.cs b/lib/Net.Http/src/Core/HttpServerProcessing.cs index 61adbbc..4595f3e 100644 --- a/lib/Net.Http/src/Core/HttpServerProcessing.cs +++ b/lib/Net.Http/src/Core/HttpServerProcessing.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Http diff --git a/lib/Net.Http/src/Core/InitDataBuffer.cs b/lib/Net.Http/src/Core/InitDataBuffer.cs index 323208a..4d215d9 100644 --- a/lib/Net.Http/src/Core/InitDataBuffer.cs +++ b/lib/Net.Http/src/Core/InitDataBuffer.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Http @@ -87,7 +87,7 @@ namespace VNLib.Net.Http.Core get => MemoryMarshal.Read<int>(_positionSegment); [MethodImpl(MethodImplOptions.AggressiveInlining)] - set => MemoryMarshal.Write(_positionSegment, ref value); + set => MemoryMarshal.Write(_positionSegment, in value); } /// <summary> 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); } } |