From 8733bd0e4e1d2630fac783eb2e1a7f31e0d70a07 Mon Sep 17 00:00:00 2001 From: Lunar Starstrum Date: Thu, 27 Jun 2024 16:36:04 -0500 Subject: [PATCH] Add HTTP header constructors --- .../Protocol/HyperHeaderCollection.cs | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/HyperSharp/Protocol/HyperHeaderCollection.cs b/src/HyperSharp/Protocol/HyperHeaderCollection.cs index a97f20a..fccb87a 100644 --- a/src/HyperSharp/Protocol/HyperHeaderCollection.cs +++ b/src/HyperSharp/Protocol/HyperHeaderCollection.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Net.Http.Headers; using System.Text; namespace HyperSharp.Protocol @@ -16,7 +17,7 @@ public sealed partial class HyperHeaderCollection : IList /// Initializes a new instance of the class that is empty and has the default initial capacity. /// - public HyperHeaderCollection() => _headers = new(); + public HyperHeaderCollection() => _headers = []; /// /// Initializes a new instance of the class that is empty and has the specified initial capacity. @@ -30,6 +31,38 @@ public sealed partial class HyperHeaderCollection : IListThe collection whose elements are copied to the new list. public HyperHeaderCollection(IEnumerable> headers) => _headers = new(headers); + /// + /// Creates a new instance of the class from the specified . + /// + /// The to create the collection from. + public HyperHeaderCollection(HttpRequestHeaders headers) + { + _headers = []; + foreach (KeyValuePair> header in headers) + { + foreach (string value in header.Value) + { + Add(header.Key, value); + } + } + } + + /// + /// Creates a new instance of the class from the specified . + /// + /// The to create the collection from. + public HyperHeaderCollection(HttpResponseHeaders headers) + { + _headers = []; + foreach (KeyValuePair> header in headers) + { + foreach (string value in header.Value) + { + Add(header.Key, value); + } + } + } + /// public KeyValuePair this[int index] { @@ -470,7 +503,7 @@ public bool TryGetValues(string key, [NotNullWhen(true)] out List? value { if (ReferenceEquals(header.Key, key)) { - values ??= new(); + values ??= []; values.Add(Encoding.ASCII.GetString(header.Value)); } } @@ -498,7 +531,7 @@ public bool TryGetValues(string key, [NotNullWhen(true)] out List? value { if (ReferenceEquals(header.Key, key)) { - values ??= new(); + values ??= []; values.Add(header.Value); } }