Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Engine/Internal/Infrastructure/Endpoints/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GenHTTP.Api.Infrastructure;

using GenHTTP.Engine.Internal.Protocol;
using GenHTTP.Engine.Internal.Utilities;
using GenHTTP.Engine.Shared.Infrastructure;

namespace GenHTTP.Engine.Internal.Infrastructure.Endpoints;
Expand Down Expand Up @@ -98,7 +99,7 @@ private void Handle(Socket client)

protected abstract ValueTask Accept(Socket client);

protected ValueTask Handle(Socket client, Stream inputStream, X509Certificate? clientCertificate = null)
protected ValueTask Handle(Socket client, PoolBufferedStream inputStream, X509Certificate? clientCertificate = null)
{
client.NoDelay = true;

Expand Down
47 changes: 24 additions & 23 deletions Engine/Internal/Protocol/ChunkedStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GenHTTP.Modules.IO.Streaming;
using GenHTTP.Engine.Internal.Utilities;

namespace GenHTTP.Engine.Internal.Protocol;

Expand All @@ -11,18 +11,8 @@ namespace GenHTTP.Engine.Internal.Protocol;
/// soon as there is no known content length. To avoid this overhead,
/// specify the length of your content whenever possible.
/// </remarks>
public sealed class ChunkedStream : Stream
public sealed class ChunkedStream(PoolBufferedStream target) : Stream
{
private static readonly string NL = "\r\n";

#region Initialization

public ChunkedStream(Stream target)
{
Target = target;
}

#endregion

#region Get-/Setters

Expand All @@ -36,7 +26,7 @@ public ChunkedStream(Stream target)

public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }

private Stream Target { get; }
private Stream Target { get; } = target;

#endregion

Expand All @@ -59,37 +49,37 @@ public override void Write(byte[] buffer, int offset, int count)

Target.Write(buffer, offset, count);

NL.Write(Target);
Target.Write("\r\n"u8);
}
}

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (count > 0)
{
await WriteAsync(count);
Write(count);

await Target.WriteAsync(buffer.AsMemory(offset, count), cancellationToken);

await WriteAsync(NL);
Target.Write("\r\n"u8);
}
}

public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (!buffer.IsEmpty)
{
await WriteAsync(buffer.Length);
Write(buffer.Length);

await Target.WriteAsync(buffer, cancellationToken);

await WriteAsync(NL);
Target.Write("\r\n"u8);
}
}

public async ValueTask FinishAsync()
public void Finish()
{
await WriteAsync("0\r\n\r\n");
Target.Write("0\r\n\r\n"u8);
}

public override void Flush()
Expand All @@ -99,11 +89,22 @@ public override void Flush()

public override Task FlushAsync(CancellationToken cancellationToken) => Target.FlushAsync(cancellationToken);

private void Write(int value) => $"{value:X}\r\n".Write(Target);
private void Write(int value)
{
Span<byte> buffer = stackalloc byte[8 + 2];

private ValueTask WriteAsync(string text) => text.WriteAsync(Target);
if (value.TryFormat(buffer, out var written, "X"))
{
buffer[written++] = (byte)'\r';
buffer[written++] = (byte)'\n';

private ValueTask WriteAsync(int value) => $"{value:X}\r\n".WriteAsync(Target);
Target.Write(buffer[..written]);
}
else
{
throw new InvalidOperationException("Failed to format chunk size");
}
}

#endregion

Expand Down
5 changes: 3 additions & 2 deletions Engine/Internal/Protocol/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GenHTTP.Api.Protocol;

using GenHTTP.Engine.Internal.Protocol.Parser;
using GenHTTP.Engine.Internal.Utilities;
using GenHTTP.Engine.Shared.Infrastructure;
using GenHTTP.Engine.Shared.Types;

Expand Down Expand Up @@ -38,7 +39,7 @@

internal X509Certificate? ClientCertificate { get; set; }

internal Stream Stream { get; }
internal PoolBufferedStream Stream { get; }

private bool? KeepAlive { get; set; }

Expand All @@ -48,7 +49,7 @@

#region Initialization

internal ClientHandler(Socket socket, Stream stream, X509Certificate? clientCertificate, IServer server, IEndPoint endPoint, NetworkConfiguration config)
internal ClientHandler(Socket socket, PoolBufferedStream stream, X509Certificate? clientCertificate, IServer server, IEndPoint endPoint, NetworkConfiguration config)
{
Server = server;
EndPoint = endPoint;
Expand Down Expand Up @@ -92,7 +93,7 @@
Server.Companion?.OnServerError(ServerErrorScope.ClientConnection, Connection.GetAddress(), e);
}

try

Check warning on line 96 in Engine/Internal/Protocol/ClientHandler.cs

View workflow job for this annotation

GitHub Actions / Test & Coverage

Combine this 'try' with the one starting on line 87. (https://rules.sonarsource.com/csharp/RSPEC-2327)

Check warning on line 96 in Engine/Internal/Protocol/ClientHandler.cs

View workflow job for this annotation

GitHub Actions / Test & Coverage

Combine this 'try' with the one starting on line 87. (https://rules.sonarsource.com/csharp/RSPEC-2327)

Check warning on line 96 in Engine/Internal/Protocol/ClientHandler.cs

View workflow job for this annotation

GitHub Actions / Test & Coverage

Combine this 'try' with the one starting on line 87. (https://rules.sonarsource.com/csharp/RSPEC-2327)

Check warning on line 96 in Engine/Internal/Protocol/ClientHandler.cs

View workflow job for this annotation

GitHub Actions / Test & Coverage

Combine this 'try' with the one starting on line 87. (https://rules.sonarsource.com/csharp/RSPEC-2327)
{
Connection.Shutdown(SocketShutdown.Both);
await Connection.DisconnectAsync(false);
Expand Down
Loading