Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 9d4c0ef

Browse files
Victor QuVictor Qu
authored andcommitted
HttpRequestHeaders
1 parent 0e488a8 commit 9d4c0ef

File tree

9 files changed

+748
-262
lines changed

9 files changed

+748
-262
lines changed

src/NZ.Orz.Core/Http/Abstractions/HeaderNames.cs

Lines changed: 211 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using System;
2+
using System.Collections.Frozen;
23
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Runtime.CompilerServices;
46
using System.Text;
57
using System.Threading.Tasks;
68

79
namespace NZ.Orz.Http;
810

911
public static class HeaderNames
1012
{
11-
private const string PseudoHeaderNamesObsoletionMessage = "This is obsolete and will be removed in a future version. Header dictionaries do not contain this key.";
12-
13-
// Use readonly statics rather than constants so ReferenceEquals works
14-
1513
/// <summary>Gets the <c>Accept</c> HTTP header name.</summary>
1614
public static readonly string Accept = "Accept";
1715

@@ -60,10 +58,6 @@ public static class HeaderNames
6058
/// <summary>Gets the <c>Alt-Svc</c> HTTP header name.</summary>
6159
public static readonly string AltSvc = "Alt-Svc";
6260

63-
/// <summary>Gets the <c>:authority</c> HTTP header name.</summary>
64-
[Obsolete(PseudoHeaderNamesObsoletionMessage, false)]
65-
public static readonly string Authority = ":authority";
66-
6761
/// <summary>Gets the <c>Authorization</c> HTTP header name.</summary>
6862
public static readonly string Authorization = "Authorization";
6963

@@ -178,17 +172,9 @@ public static class HeaderNames
178172
/// <summary>Gets the <c>Max-Forwards</c> HTTP header name.</summary>
179173
public static readonly string MaxForwards = "Max-Forwards";
180174

181-
/// <summary>Gets the <c>:method</c> HTTP header name.</summary>
182-
[Obsolete(PseudoHeaderNamesObsoletionMessage, false)]
183-
public static readonly string Method = ":method";
184-
185175
/// <summary>Gets the <c>Origin</c> HTTP header name.</summary>
186176
public static readonly string Origin = "Origin";
187177

188-
/// <summary>Gets the <c>:path</c> HTTP header name.</summary>
189-
[Obsolete(PseudoHeaderNamesObsoletionMessage, false)]
190-
public static readonly string Path = ":path";
191-
192178
/// <summary>Gets the <c>Pragma</c> HTTP header name.</summary>
193179
public static readonly string Pragma = "Pragma";
194180

@@ -213,10 +199,6 @@ public static class HeaderNames
213199
/// <summary>Gets the <c>Request-Id</c> HTTP header name.</summary>
214200
public static readonly string RequestId = "Request-Id";
215201

216-
/// <summary>Gets the <c>:scheme</c> HTTP header name.</summary>
217-
[Obsolete(PseudoHeaderNamesObsoletionMessage, false)]
218-
public static readonly string Scheme = ":scheme";
219-
220202
/// <summary>Gets the <c>Sec-WebSocket-Accept</c> HTTP header name.</summary>
221203
public static readonly string SecWebSocketAccept = "Sec-WebSocket-Accept";
222204

@@ -238,10 +220,6 @@ public static class HeaderNames
238220
/// <summary>Gets the <c>Set-Cookie</c> HTTP header name.</summary>
239221
public static readonly string SetCookie = "Set-Cookie";
240222

241-
/// <summary>Gets the <c>:status</c> HTTP header name.</summary>
242-
[Obsolete(PseudoHeaderNamesObsoletionMessage, false)]
243-
public static readonly string Status = ":status";
244-
245223
/// <summary>Gets the <c>Strict-Transport-Security</c> HTTP header name.</summary>
246224
public static readonly string StrictTransportSecurity = "Strict-Transport-Security";
247225

@@ -281,9 +259,6 @@ public static class HeaderNames
281259
/// <summary>Gets the <c>Warning</c> HTTP header name.</summary>
282260
public static readonly string Warning = "Warning";
283261

284-
/// <summary>Gets the <c>Sec-WebSocket-Protocol</c> HTTP header name.</summary>
285-
public static readonly string WebSocketSubProtocols = "Sec-WebSocket-Protocol";
286-
287262
/// <summary>Gets the <c>WWW-Authenticate</c> HTTP header name.</summary>
288263
public static readonly string WWWAuthenticate = "WWW-Authenticate";
289264

@@ -305,7 +280,7 @@ public static class HeaderNames
305280
/// <summary>Gets the <c>X-XSS-Protection</c> HTTP header name.</summary>
306281
public static readonly string XXSSProtection = "X-XSS-Protection";
307282

308-
private static readonly HashSet<string> _internedHeaderNames = new HashSet<string>(91, StringComparer.OrdinalIgnoreCase)
283+
private static readonly FrozenSet<string> _internedHeaderNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
309284
{
310285
HeaderNames.Accept,
311286
HeaderNames.AcceptCharset,
@@ -390,16 +365,16 @@ public static class HeaderNames
390365
HeaderNames.Vary,
391366
HeaderNames.Via,
392367
HeaderNames.Warning,
393-
HeaderNames.WebSocketSubProtocols,
394368
HeaderNames.WWWAuthenticate,
395369
HeaderNames.XContentTypeOptions,
396370
HeaderNames.XFrameOptions,
397371
HeaderNames.XPoweredBy,
398372
HeaderNames.XRequestedWith,
399373
HeaderNames.XUACompatible,
400374
HeaderNames.XXSSProtection,
401-
};
375+
}.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
402376

377+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
403378
public static string GetInternedHeaderName(string name)
404379
{
405380
// Some headers can be very long lived; for example those on a WebSocket connection
@@ -411,4 +386,210 @@ public static string GetInternedHeaderName(string name)
411386

412387
return name;
413388
}
389+
390+
private static readonly FrozenDictionary<string, KnownHeaderType> _internedHeaderType = new Dictionary<string, KnownHeaderType>(StringComparer.OrdinalIgnoreCase)
391+
{
392+
{ HeaderNames.Host, KnownHeaderType.Host },
393+
//{ HeaderNames.Accept, KnownHeaderType.Accept },
394+
//{ HeaderNames.AcceptCharset, KnownHeaderType.AcceptCharset },
395+
//{ HeaderNames.AcceptEncoding, KnownHeaderType.AcceptEncoding },
396+
//{ HeaderNames.AcceptLanguage, KnownHeaderType.AcceptLanguage },
397+
//{ HeaderNames.AcceptRanges, KnownHeaderType.AcceptRanges },
398+
//{ HeaderNames.AccessControlAllowCredentials, KnownHeaderType.AccessControlAllowCredentials },
399+
//{ HeaderNames.AccessControlAllowHeaders, KnownHeaderType.AccessControlAllowHeaders },
400+
//{ HeaderNames.AccessControlAllowMethods, KnownHeaderType.AccessControlAllowMethods },
401+
//{ HeaderNames.AccessControlAllowOrigin, KnownHeaderType.AccessControlAllowOrigin },
402+
//{ HeaderNames.AccessControlExposeHeaders, KnownHeaderType.AccessControlExposeHeaders },
403+
//{ HeaderNames.AccessControlMaxAge, KnownHeaderType.AccessControlMaxAge },
404+
//{ HeaderNames.AccessControlRequestHeaders, KnownHeaderType.AccessControlRequestHeaders },
405+
//{ HeaderNames.AccessControlRequestMethod, KnownHeaderType.AccessControlRequestMethod },
406+
//{ HeaderNames.Age, KnownHeaderType.Age },
407+
//{ HeaderNames.Allow, KnownHeaderType.Allow },
408+
//{ HeaderNames.AltSvc, KnownHeaderType.AltSvc },
409+
//{ HeaderNames.Authorization, KnownHeaderType.Authorization },
410+
//{ HeaderNames.Baggage, KnownHeaderType.Baggage },
411+
//{ HeaderNames.CacheControl, KnownHeaderType.CacheControl },
412+
//{ HeaderNames.Connection, KnownHeaderType.Connection },
413+
//{ HeaderNames.ContentDisposition, KnownHeaderType.ContentDisposition },
414+
//{ HeaderNames.ContentEncoding, KnownHeaderType.ContentEncoding },
415+
//{ HeaderNames.ContentLanguage, KnownHeaderType.ContentLanguage },
416+
//{ HeaderNames.ContentLength, KnownHeaderType.ContentLength },
417+
//{ HeaderNames.ContentLocation, KnownHeaderType.ContentLocation },
418+
//{ HeaderNames.ContentMD5, KnownHeaderType.ContentMD5 },
419+
//{ HeaderNames.ContentRange, KnownHeaderType.ContentRange },
420+
//{ HeaderNames.ContentSecurityPolicy, KnownHeaderType.ContentSecurityPolicy },
421+
//{ HeaderNames.ContentSecurityPolicyReportOnly, KnownHeaderType.ContentSecurityPolicyReportOnly },
422+
//{ HeaderNames.ContentType, KnownHeaderType.ContentType },
423+
//{ HeaderNames.CorrelationContext, KnownHeaderType.CorrelationContext },
424+
//{ HeaderNames.Cookie, KnownHeaderType.Cookie },
425+
//{ HeaderNames.Date, KnownHeaderType.Date },
426+
//{ HeaderNames.DNT, KnownHeaderType.DNT },
427+
//{ HeaderNames.ETag, KnownHeaderType.ETag },
428+
//{ HeaderNames.Expires, KnownHeaderType.Expires },
429+
//{ HeaderNames.Expect, KnownHeaderType.Expect },
430+
//{ HeaderNames.From, KnownHeaderType.From },
431+
//{ HeaderNames.GrpcAcceptEncoding, KnownHeaderType.GrpcAcceptEncoding },
432+
//{ HeaderNames.GrpcEncoding, KnownHeaderType.GrpcEncoding },
433+
//{ HeaderNames.GrpcMessage, KnownHeaderType.GrpcMessage },
434+
//{ HeaderNames.GrpcStatus, KnownHeaderType.GrpcStatus },
435+
//{ HeaderNames.GrpcTimeout, KnownHeaderType.GrpcTimeout },
436+
//{ HeaderNames.KeepAlive, KnownHeaderType.KeepAlive },
437+
//{ HeaderNames.IfMatch, KnownHeaderType.IfMatch },
438+
//{ HeaderNames.IfModifiedSince, KnownHeaderType.IfModifiedSince },
439+
//{ HeaderNames.IfNoneMatch, KnownHeaderType.IfNoneMatch },
440+
//{ HeaderNames.IfRange, KnownHeaderType.IfRange },
441+
//{ HeaderNames.IfUnmodifiedSince, KnownHeaderType.IfUnmodifiedSince },
442+
//{ HeaderNames.LastModified, KnownHeaderType.LastModified },
443+
//{ HeaderNames.Link, KnownHeaderType.Link },
444+
//{ HeaderNames.Location, KnownHeaderType.Location },
445+
//{ HeaderNames.MaxForwards, KnownHeaderType.MaxForwards },
446+
//{ HeaderNames.Origin, KnownHeaderType.Origin },
447+
//{ HeaderNames.Pragma, KnownHeaderType.Pragma },
448+
//{ HeaderNames.ProxyAuthenticate, KnownHeaderType.ProxyAuthenticate },
449+
//{ HeaderNames.ProxyAuthorization, KnownHeaderType.ProxyAuthorization },
450+
//{ HeaderNames.ProxyConnection, KnownHeaderType.ProxyConnection },
451+
//{ HeaderNames.Range, KnownHeaderType.Range },
452+
//{ HeaderNames.Referer, KnownHeaderType.Referer },
453+
//{ HeaderNames.RetryAfter, KnownHeaderType.RetryAfter },
454+
//{ HeaderNames.RequestId, KnownHeaderType.RequestId },
455+
//{ HeaderNames.SecWebSocketAccept, KnownHeaderType.SecWebSocketAccept },
456+
//{ HeaderNames.SecWebSocketKey, KnownHeaderType.SecWebSocketKey },
457+
//{ HeaderNames.SecWebSocketProtocol, KnownHeaderType.SecWebSocketProtocol },
458+
//{ HeaderNames.SecWebSocketVersion, KnownHeaderType.SecWebSocketVersion },
459+
//{ HeaderNames.SecWebSocketExtensions, KnownHeaderType.SecWebSocketExtensions },
460+
//{ HeaderNames.Server, KnownHeaderType.Server },
461+
//{ HeaderNames.SetCookie, KnownHeaderType.SetCookie },
462+
//{ HeaderNames.StrictTransportSecurity, KnownHeaderType.StrictTransportSecurity },
463+
//{ HeaderNames.TE, KnownHeaderType.TE },
464+
//{ HeaderNames.Trailer, KnownHeaderType.Trailer },
465+
//{ HeaderNames.TransferEncoding, KnownHeaderType.TransferEncoding },
466+
//{ HeaderNames.Translate, KnownHeaderType.Translate },
467+
//{ HeaderNames.TraceParent, KnownHeaderType.TraceParent },
468+
//{ HeaderNames.TraceState, KnownHeaderType.TraceState },
469+
//{ HeaderNames.Upgrade, KnownHeaderType.Upgrade },
470+
//{ HeaderNames.UpgradeInsecureRequests, KnownHeaderType.UpgradeInsecureRequests },
471+
//{ HeaderNames.UserAgent, KnownHeaderType.UserAgent },
472+
//{ HeaderNames.Vary, KnownHeaderType.Vary },
473+
//{ HeaderNames.Via, KnownHeaderType.Via },
474+
//{ HeaderNames.Warning, KnownHeaderType.Warning },
475+
//{ HeaderNames.WWWAuthenticate, KnownHeaderType.WWWAuthenticate },
476+
//{ HeaderNames.XContentTypeOptions, KnownHeaderType.XContentTypeOptions },
477+
//{ HeaderNames.XFrameOptions, KnownHeaderType.XFrameOptions },
478+
//{ HeaderNames.XPoweredBy, KnownHeaderType.XPoweredBy },
479+
//{ HeaderNames.XRequestedWith, KnownHeaderType.XRequestedWith },
480+
//{ HeaderNames.XUACompatible, KnownHeaderType.XUACompatible },
481+
//{ HeaderNames.XXSSProtection, KnownHeaderType.XXSSProtection }
482+
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
483+
484+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
485+
public static bool GetInternedHeaderType(string name, out KnownHeaderType type)
486+
{
487+
if (_internedHeaderType.TryGetValue(name, out type))
488+
{
489+
return true;
490+
}
491+
492+
return false;
493+
}
494+
}
495+
496+
public enum KnownHeaderType
497+
{
498+
Unknown = 0,
499+
Host,
500+
//Accept = 1,
501+
//AcceptCharset = 2,
502+
//AcceptEncoding = 3,
503+
//AcceptLanguage,
504+
//AcceptRanges,
505+
//AccessControlAllowCredentials,
506+
//AccessControlAllowHeaders,
507+
//AccessControlAllowMethods,
508+
//AccessControlAllowOrigin,
509+
//AccessControlExposeHeaders,
510+
//AccessControlMaxAge,
511+
//AccessControlRequestHeaders,
512+
//AccessControlRequestMethod,
513+
//Age,
514+
//Allow,
515+
//AltSvc,
516+
//AltUsed,
517+
//Authority,
518+
//Authorization,
519+
//Baggage,
520+
//CacheControl,
521+
//Connection,
522+
//ContentDisposition,
523+
//ContentEncoding,
524+
//ContentLanguage,
525+
//ContentLength,
526+
//ContentLocation,
527+
//ContentMD5,
528+
//ContentRange,
529+
//ContentSecurityPolicy,
530+
//ContentSecurityPolicyReportOnly,
531+
//ContentType,
532+
//Cookie,
533+
//CorrelationContext,
534+
//Date,
535+
//DNT,
536+
//ETag,
537+
//Expect,
538+
//Expires,
539+
//From,
540+
//GrpcAcceptEncoding,
541+
//GrpcEncoding,
542+
//GrpcMessage,
543+
//GrpcStatus,
544+
//GrpcTimeout,
545+
//IfMatch,
546+
//IfModifiedSince,
547+
//IfNoneMatch,
548+
//IfRange,
549+
//IfUnmodifiedSince,
550+
//KeepAlive,
551+
//LastModified,
552+
//Link,
553+
//Location,
554+
//MaxForwards,
555+
//Method,
556+
//Origin,
557+
//Path,
558+
//Pragma,
559+
//Protocol,
560+
//ProxyAuthenticate,
561+
//ProxyAuthorization,
562+
//ProxyConnection,
563+
//Range,
564+
//Referer,
565+
//RequestId,
566+
//SecWebSocketAccept,
567+
//SecWebSocketKey,
568+
//SecWebSocketProtocol,
569+
//SecWebSocketVersion,
570+
//SecWebSocketExtensions,
571+
//StrictTransportSecurity,
572+
//RetryAfter,
573+
//Scheme,
574+
//Server,
575+
//SetCookie,
576+
//TE,
577+
//TraceParent,
578+
//TraceState,
579+
//Trailer,
580+
//TransferEncoding,
581+
//Translate,
582+
//Upgrade,
583+
//UpgradeInsecureRequests,
584+
//UserAgent,
585+
//Vary,
586+
//Via,
587+
//Warning,
588+
//WWWAuthenticate,
589+
//XContentTypeOptions,
590+
//XFrameOptions,
591+
//XPoweredBy,
592+
//XRequestedWith,
593+
//XUACompatible,
594+
//XXSSProtection,
414595
}

0 commit comments

Comments
 (0)