Skip to content

Commit e5d4083

Browse files
committed
Use DI more correctly; New Raw HyperSerializerDelegate, intending for passing byte arrays as responses; HyperStatus now includes the hyper serializer to use
1 parent 9c1bd06 commit e5d4083

File tree

80 files changed

+2937
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2937
-389
lines changed

benchmarks/Responders/OkResponder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace HyperSharp.Benchmarks.Responders
1111
{
1212
public static Type[] Needs => Type.EmptyTypes;
1313

14-
public Result<HyperStatus> Respond(HyperContext context, CancellationToken cancellationToken = default) => Result.Success(new HyperStatus(HttpStatusCode.OK));
14+
public Result<HyperStatus> Respond(HyperContext context, CancellationToken cancellationToken = default) => Result.Success(HyperStatus.OK());
1515
}
1616
}

benchmarks/Responders/OkTaskResponder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace HyperSharp.Benchmarks.Responders
1414

1515
public async Task<Result<HyperStatus>> RespondAsync(HyperContext context, CancellationToken cancellationToken = default)
1616
{
17-
await context.RespondAsync(new HyperStatus(HttpStatusCode.OK), HyperSerializers.PlainTextAsync, cancellationToken);
17+
await context.RespondAsync(HyperStatus.OK(), HyperSerializers.PlainTextAsync, cancellationToken);
1818
return Result.Success(default(HyperStatus));
1919
}
2020
}

src/HyperSharp.SourceGeneration/src/Tasks/HyperStatusConstructorGenerator.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,57 @@ namespace HyperSharp.Protocol
2323
public readonly partial record struct HyperStatus
2424
{
2525
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
26-
public static HyperStatus {{Code}}() => new(global::System.Net.HttpStatusCode.{{Code}}, new HyperHeaderCollection(), null);
26+
public static HyperStatus {{Code}}() => new HyperStatus()
27+
{
28+
Code = global::System.Net.HttpStatusCode.{{Code}}
29+
};
2730
2831
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
2932
/// <param name="body">The body of the response.</param>
30-
public static HyperStatus {{Code}}(object? body) => new(global::System.Net.HttpStatusCode.{{Code}}, new HyperHeaderCollection(), body);
33+
public static HyperStatus {{Code}}(object? body) => new HyperStatus()
34+
{
35+
Code = global::System.Net.HttpStatusCode.{{Code}},
36+
Body = body
37+
};
38+
39+
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
40+
/// <param name="body">The body of the response.</param>
41+
/// <param name="serializer">Which serializer to use to serialize the body.</param>
42+
public static HyperStatus {{Code}}(object? body, HyperSerializerDelegate serializer) => new HyperStatus()
43+
{
44+
Code = global::System.Net.HttpStatusCode.{{Code}},
45+
Body = body,
46+
Serializer = serializer
47+
};
3148
3249
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
3350
/// <param name="headers">The headers of the response.</param>
34-
public static HyperStatus {{Code}}(HyperHeaderCollection headers) => new(global::System.Net.HttpStatusCode.{{Code}}, headers, null);
51+
public static HyperStatus {{Code}}(HyperHeaderCollection headers) => new HyperStatus()
52+
{
53+
Code = global::System.Net.HttpStatusCode.{{Code}},
54+
Headers = headers
55+
};
3556
3657
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
3758
/// <param name="headers">The headers of the response.</param>
3859
/// <param name="body">The body of the response.</param>
39-
public static HyperStatus {{Code}}(HyperHeaderCollection headers, object? body) => new(global::System.Net.HttpStatusCode.{{Code}}, headers, body);
60+
public static HyperStatus {{Code}}(HyperHeaderCollection headers, object? body) => new HyperStatus()
61+
{
62+
Code = global::System.Net.HttpStatusCode.{{Code}},
63+
Headers = headers,
64+
Body = body
65+
};
66+
67+
/// <inheritdoc cref="global::System.Net.HttpStatusCode.{{Code}}" />
68+
/// <param name="headers">The headers of the response.</param>
69+
/// <param name="body">The body of the response.</param>
70+
public static HyperStatus {{Code}}(HyperHeaderCollection headers, object? body, HyperSerializerDelegate serializer) => new HyperStatus()
71+
{
72+
Code = global::System.Net.HttpStatusCode.{{Code}},
73+
Headers = headers,
74+
Body = body,
75+
Serializer = serializer
76+
};
4077
}
4178
}
4279

src/HyperSharp/HyperConfiguration.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,30 @@ public sealed record HyperConfiguration
5555
/// <summary>
5656
/// Creates a new <see cref="HyperConfiguration"/> with the default values.
5757
/// </summary>
58-
public HyperConfiguration() : this(new ServiceCollection().AddLogging(builder => builder.AddProvider(NullLoggerProvider.Instance)).AddHyperSharp(), new HyperConfigurationBuilder()) { }
58+
public HyperConfiguration() : this(new ServiceCollection().AddLogging(builder => builder.AddProvider(NullLoggerProvider.Instance)).AddHyperSharp().BuildServiceProvider(), new HyperConfigurationBuilder()) { }
5959

6060
/// <summary>
6161
/// Creates a new <see cref="HyperConfiguration"/> with the specified values.
6262
/// </summary>
6363
/// <param name="builder">The <see cref="HyperConfigurationBuilder"/> to use.</param>
64-
public HyperConfiguration(HyperConfigurationBuilder builder) : this(new ServiceCollection().AddLogging(builder => builder.AddProvider(NullLoggerProvider.Instance)).AddHyperSharp(), builder) { }
64+
public HyperConfiguration(HyperConfigurationBuilder builder) : this(new ServiceCollection().AddLogging(builder => builder.AddProvider(NullLoggerProvider.Instance)).AddHyperSharp().BuildServiceProvider(), builder) { }
6565

6666
/// <summary>
6767
/// Creates a new <see cref="HyperConfiguration"/> using the specified <see cref="IServiceCollection"/>.
6868
/// </summary>
6969
/// <param name="serviceDescriptors">The <see cref="IServiceCollection"/> to use when creating the responders.</param>
70-
public HyperConfiguration(IServiceCollection serviceDescriptors) : this(serviceDescriptors, new HyperConfigurationBuilder()) { }
70+
public HyperConfiguration(IServiceCollection serviceDescriptors) : this(serviceDescriptors.BuildServiceProvider(), new HyperConfigurationBuilder()) { }
7171

7272
/// <summary>
7373
/// Creates a new <see cref="HyperConfiguration"/> using the specified <see cref="IServiceCollection"/> and <see cref="HyperConfigurationBuilder"/>.
7474
/// </summary>
75-
/// <param name="serviceDescriptors">The <see cref="IServiceCollection"/> to use when creating the responders.</param>
75+
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> to use.</param>
7676
/// <param name="builder">The <see cref="HyperConfigurationBuilder"/> to use.</param>
77-
public HyperConfiguration(IServiceCollection serviceDescriptors, HyperConfigurationBuilder builder)
77+
public HyperConfiguration(IServiceProvider serviceProvider, HyperConfigurationBuilder builder)
7878
{
79-
ArgumentNullException.ThrowIfNull(serviceDescriptors, nameof(serviceDescriptors));
79+
ArgumentNullException.ThrowIfNull(serviceProvider, nameof(serviceProvider));
8080
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
8181

82-
IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
8382
if (!Uri.TryCreate($"http://{builder.ListeningEndpoint}/", UriKind.Absolute, out Uri? host))
8483
{
8584
throw new ArgumentException("The listening endpoint is invalid.", nameof(builder));

src/HyperSharp/HyperServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private async Task HandleConnectionAsync(Ulid id, NetworkStream networkStream)
178178
_ => throw new NotImplementedException("Unimplemented result status, please open a GitHub issue as this is a bug.")
179179
};
180180

181-
await context.Value.RespondAsync(response, HyperSerializers.JsonAsync, cancellationTokenSource.Token);
181+
await context.Value.RespondAsync(response, cancellationTokenSource.Token);
182182
HyperLogging.HttpResponded(_logger, connection.Id, response, null);
183183
}
184184
}

src/HyperSharp/Protocol/HyperContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public HyperContext(HttpMethod method, Uri route, Version version, HyperHeaderCo
105105
}
106106

107107
/// <inheritdoc cref="RespondAsync(HyperStatus, HyperSerializerDelegate, CancellationToken)"/>
108-
public ValueTask RespondAsync(HyperStatus status, CancellationToken cancellationToken = default) => RespondAsync(status, HyperSerializers.JsonAsync, cancellationToken);
108+
public ValueTask RespondAsync(HyperStatus status, CancellationToken cancellationToken = default) => RespondAsync(status, status.Serializer, cancellationToken);
109109

110110
/// <summary>
111111
/// Responds to the request with the specified status in plain text.

src/HyperSharp/Protocol/HyperHeaderCollection.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
57
using System.Net.Http.Headers;
68
using System.Text;
79

@@ -10,6 +12,7 @@ namespace HyperSharp.Protocol
1012
/// <summary>
1113
/// Represents a collection of headers with string keys and lists of string values.
1214
/// </summary>
15+
[DebuggerDisplay("{ToString(),nq}")]
1316
public sealed partial class HyperHeaderCollection : IList<KeyValuePair<string, byte[]>>
1417
{
1518
private readonly List<KeyValuePair<string, byte[]>> _headers;
@@ -548,5 +551,19 @@ public bool TryGetValues(string key, [NotNullWhen(true)] out List<byte[]>? value
548551
/// <inheritdoc />
549552
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator() => _headers.GetEnumerator();
550553
IEnumerator IEnumerable.GetEnumerator() => _headers.GetEnumerator();
554+
555+
public override string ToString()
556+
{
557+
StringBuilder stringBuilder = new();
558+
foreach (KeyValuePair<string, byte[]> header in _headers.OrderBy(x => x.Key))
559+
{
560+
stringBuilder.Append(header.Key);
561+
stringBuilder.Append(": ");
562+
stringBuilder.Append(Encoding.ASCII.GetString(header.Value));
563+
stringBuilder.Append("\r\n");
564+
}
565+
566+
return stringBuilder.ToString();
567+
}
551568
}
552569
}

src/HyperSharp/Protocol/HyperHeaderName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated/>
2-
// Last modified at 2024-06-28.
2+
// Last modified at 2024-07-30.
33

44
namespace HyperSharp.Protocol
55
{

src/HyperSharp/Protocol/HyperSerializers/JsonAsync.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text;
34
using System.Text.Json;
45
using System.Threading;
@@ -9,7 +10,7 @@ namespace HyperSharp.Protocol
910
{
1011
public static partial class HyperSerializers
1112
{
12-
private static readonly byte[] _contentTypeJsonEncodingHeader = "Content-Type: application/json; charset=utf-8\r\nContent-Length: "u8.ToArray();
13+
private static readonly byte[] _contentTypeJsonEncodingHeader = "Content-Type: application/json\r\nContent-Length: "u8.ToArray();
1314

1415
/// <summary>
1516
/// Serializes the body to the client as JSON using the <see cref="JsonSerializer.SerializeToUtf8Bytes{TValue}(TValue, JsonSerializerOptions?)"/> method with the <see cref="HyperConfiguration.JsonSerializerOptions"/> options.
@@ -28,7 +29,7 @@ public static ValueTask<bool> JsonAsync(HyperContext context, HyperStatus status
2829
byte[] body = JsonSerializer.SerializeToUtf8Bytes(status.Body ?? new object(), context.Connection.Server.Configuration.JsonSerializerOptions);
2930

3031
// Finish the Content-Length header
31-
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString()));
32+
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString(CultureInfo.InvariantCulture)));
3233
context.Connection.StreamWriter.Write<byte>(_newLine);
3334

3435
// Write body

src/HyperSharp/Protocol/HyperSerializers/PlainTextAsync.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -29,7 +30,7 @@ public static ValueTask<bool> PlainTextAsync(HyperContext context, HyperStatus s
2930
byte[] body = Encoding.UTF8.GetBytes(status.Body?.ToString() ?? "");
3031

3132
// Write Content-Length header
32-
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString()));
33+
context.Connection.StreamWriter.Write<byte>(Encoding.UTF8.GetBytes(body.Length.ToString(CultureInfo.InvariantCulture)));
3334
context.Connection.StreamWriter.Write<byte>(_newLine);
3435

3536
// Write body

0 commit comments

Comments
 (0)