Skip to content

Commit

Permalink
Add HTTP header constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Jun 27, 2024
1 parent d934973 commit 8733bd0
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/HyperSharp/Protocol/HyperHeaderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,7 @@ public sealed partial class HyperHeaderCollection : IList<KeyValuePair<string, b
/// <summary>
/// Initializes a new instance of the <see cref="HyperHeaderCollection"/> class that is empty and has the default initial capacity.
/// </summary>
public HyperHeaderCollection() => _headers = new();
public HyperHeaderCollection() => _headers = [];

/// <summary>
/// Initializes a new instance of the <see cref="HyperHeaderCollection"/> class that is empty and has the specified initial capacity.
Expand All @@ -30,6 +31,38 @@ public sealed partial class HyperHeaderCollection : IList<KeyValuePair<string, b
/// <param name="headers">The collection whose elements are copied to the new list.</param>
public HyperHeaderCollection(IEnumerable<KeyValuePair<string, byte[]>> headers) => _headers = new(headers);

/// <summary>
/// Creates a new instance of the <see cref="HyperHeaderCollection"/> class from the specified <see cref="HttpRequestHeaders"/>.
/// </summary>
/// <param name="headers">The <see cref="HttpRequestHeaders"/> to create the collection from.</param>
public HyperHeaderCollection(HttpRequestHeaders headers)
{
_headers = [];
foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
{
foreach (string value in header.Value)
{
Add(header.Key, value);
}
}
}

/// <summary>
/// Creates a new instance of the <see cref="HyperHeaderCollection"/> class from the specified <see cref="HttpResponseHeaders"/>.
/// </summary>
/// <param name="headers">The <see cref="HttpResponseHeaders"/> to create the collection from.</param>
public HyperHeaderCollection(HttpResponseHeaders headers)
{
_headers = [];
foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
{
foreach (string value in header.Value)
{
Add(header.Key, value);
}
}
}

/// <inheritdoc />
public KeyValuePair<string, byte[]> this[int index]
{
Expand Down Expand Up @@ -470,7 +503,7 @@ public bool TryGetValues(string key, [NotNullWhen(true)] out List<string>? value
{
if (ReferenceEquals(header.Key, key))
{
values ??= new();
values ??= [];
values.Add(Encoding.ASCII.GetString(header.Value));
}
}
Expand Down Expand Up @@ -498,7 +531,7 @@ public bool TryGetValues(string key, [NotNullWhen(true)] out List<byte[]>? value
{
if (ReferenceEquals(header.Key, key))
{
values ??= new();
values ??= [];
values.Add(header.Value);
}
}
Expand Down

1 comment on commit 8733bd0

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine Information:

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)

  • AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
  • Hardware Intrinsics: AVX2, AES, BMI1, BMI2, FMA, LZCNT, PCLMUL, POPCNT VectorSize=256
  • .NET 8.0.7 (8.0.724.27014), x64, RyuJIT
  • Total Execution Time: 49.387s

HyperBenchmarks

Execution Time: 47.956s

FullHttpRequest:

Mean: 380.29μs
Error: 514ns
StdDev: 1.99μs
Max per second: 2,629.58 (1,000,000,000ns / 380,288.31ns)

HyperContextRespondAsync:

Mean: 13.11μs
Error: 21ns
StdDev: 83ns
Max per second: 76,252.72 (1,000,000,000ns / 13,114.29ns)

ParseHeadersTestAsync:

Mean: 5.99μs
Error: 11ns
StdDev: 40ns
Max per second: 166,915.25 (1,000,000,000ns / 5,991.06ns)

HttpBenchmarks

Execution Time: 1.431s

HyperSharpTestAsync, Baseline, Failed:

No results.

EmbedIoTestAsync, Failed:

No results.

GenHttpTestAsync, Failed:

No results.

HttpCoreTestAsync, Failed:

No results.

HttpListenerTestAsync, Failed:

No results.

Please sign in to comment.