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
4 changes: 3 additions & 1 deletion Adapters/AspNetCore/Mapping/Bridge.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using GenHTTP.Adapters.AspNetCore.Server;
using GenHTTP.Adapters.AspNetCore.Types;

using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Api.Protocol;

using Microsoft.AspNetCore.Http;

namespace GenHTTP.Adapters.AspNetCore.Mapping;
Expand All @@ -12,7 +14,7 @@ public static class Bridge

public static async ValueTask MapAsync(HttpContext context, IHandler handler, IServer? server = null, IServerCompanion? companion = null, string? registeredPath = null)
{
var actualServer = server ?? new ImplicitServer(handler, companion);
var actualServer = server ?? new ImplicitServer(context, handler, companion);

try
{
Expand Down
5 changes: 0 additions & 5 deletions Adapters/AspNetCore/Server/EmptyEndpoints.cs

This file was deleted.

14 changes: 14 additions & 0 deletions Adapters/AspNetCore/Server/EndpointCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GenHTTP.Api.Infrastructure;
using Microsoft.AspNetCore.Http;

namespace GenHTTP.Adapters.AspNetCore.Server;

public class EndpointCollection : List<IEndPoint>, IEndPointCollection
{

public EndpointCollection(HttpContext context)
{
Add(new ImplicitEndpoint(context));
}

}
23 changes: 23 additions & 0 deletions Adapters/AspNetCore/Server/ImplicitEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net;

using GenHTTP.Api.Infrastructure;

using Microsoft.AspNetCore.Http;

namespace GenHTTP.Adapters.AspNetCore.Server;

public class ImplicitEndpoint(HttpContext context) : IEndPoint
{

public IPAddress? Address => context.Connection.LocalIpAddress;

public ushort Port => (ushort)context.Connection.LocalPort;

public bool Secure => context.Request.IsHttps;

public void Dispose()
{
// nop
}

}
6 changes: 4 additions & 2 deletions Adapters/AspNetCore/Server/ImplicitServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;

using Microsoft.AspNetCore.Http;

namespace GenHTTP.Adapters.AspNetCore.Server;

public sealed class ImplicitServer : IServer
Expand Down Expand Up @@ -33,12 +35,12 @@ public bool Development

#region Initialization

public ImplicitServer(IHandler handler, IServerCompanion? companion)
public ImplicitServer(HttpContext context, IHandler handler, IServerCompanion? companion)
{
Handler = handler;
Companion = companion;

EndPoints = new EmptyEndpoints();
EndPoints = new EndpointCollection(context);

Running = true;
}
Expand Down
42 changes: 20 additions & 22 deletions Adapters/AspNetCore/Types/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@ namespace GenHTTP.Adapters.AspNetCore.Types;

public sealed class Request : IRequest
{
private RequestProperties? _Properties;
private RequestProperties? _properties;

private Query? _Query;
private Query? _query;

private Cookies? _Cookies;
private Cookies? _cookies;

private readonly ForwardingCollection _Forwardings = new();
private readonly ForwardingCollection _forwardings = new();

private Headers? _Headers;

private readonly IEndPoint? _EndPoint;
private Headers? _headers;

#region Get-/Setters

public IRequestProperties Properties
{
get { return _Properties ??= new RequestProperties(); }
get { return _properties ??= new RequestProperties(); }
}

public IServer Server { get; }

public IEndPoint EndPoint => _EndPoint ?? throw new InvalidOperationException("EndPoint is not available as it is managed by ASP.NET Core");

public IEndPoint EndPoint { get; }
public IClientConnection Client { get; }

public IClientConnection LocalClient { get; }
Expand All @@ -55,19 +53,19 @@ public IRequestProperties Properties

public IRequestQuery Query
{
get { return _Query ??= new Query(Context); }
get { return _query ??= new Query(Context); }
}

public ICookieCollection Cookies
{
get { return _Cookies ??= new Cookies(Context); }
get { return _cookies ??= new Cookies(Context); }
}

public IForwardingCollection Forwardings => _Forwardings;
public IForwardingCollection Forwardings => _forwardings;

public IHeaderCollection Headers
{
get { return _Headers ??= new Headers(Context); }
get { return _headers ??= new Headers(Context); }
}

public Stream Content => Context.BodyReader.AsStream(true);
Expand Down Expand Up @@ -101,19 +99,19 @@ public Request(IServer server, HttpContext context)
{
foreach (var entry in forwardings)
{
if (entry != null) _Forwardings.Add(entry);
if (entry != null) _forwardings.Add(entry);
}
}
else
{
_Forwardings.TryAddLegacy(Headers);
_forwardings.TryAddLegacy(Headers);
}

LocalClient = new ClientConnection(context.Connection, context.Request);

Client = _Forwardings.DetermineClient(context.Connection.ClientCertificate) ?? LocalClient;
Client = _forwardings.DetermineClient(context.Connection.ClientCertificate) ?? LocalClient;

_EndPoint = Server.EndPoints.FirstOrDefault(e => e.Port == context.Connection.LocalPort);
EndPoint = Server.EndPoints.First(e => e.Port == context.Connection.LocalPort);
}

#endregion
Expand All @@ -128,15 +126,15 @@ public Request(IServer server, HttpContext context)

#region Lifecycle

private bool _Disposed;
private bool _disposed;

public void Dispose()
{
if (!_Disposed)
if (!_disposed)
{
_Properties?.Dispose();
_properties?.Dispose();

_Disposed = true;
_disposed = true;
}
}

Expand Down
21 changes: 9 additions & 12 deletions Testing/Acceptance/Adapters/AspNetCore/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ public async Task TestImplicitServer()
{
app.Map("/server", Inline.Create().Get(async (IRequest r) =>
{
await r.Server.DisposeAsync(); // nop

await Assert.ThrowsExactlyAsync<InvalidOperationException>(async () => await r.Server.StartAsync());

return r.Server;
var server = r.Server;

await server.DisposeAsync(); // nop

await Assert.ThrowsExactlyAsync<InvalidOperationException>(async () => await server.StartAsync());

Assert.IsTrue(server.Running);
Assert.IsFalse(server.Development);
}));
};

Expand All @@ -110,13 +113,7 @@ public async Task TestImplicitServer()

using var response = await GetResponseAsync(client, "/server", port);

await response.AssertStatusAsync(HttpStatusCode.OK);

var content = await response.GetContentAsync();

AssertX.Contains("\"running\":true", content);
AssertX.Contains("\"development\":false", content);
AssertX.Contains("\"version\"", content);
await response.AssertStatusAsync(HttpStatusCode.NoContent);
}

#endregion
Expand Down
1 change: 1 addition & 0 deletions Testing/Acceptance/Modules/Basics/RedirectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ public async Task TestAbsoluteRoute(TestEngine engine)

Assert.AreEqual("/me/to/", new Uri(response.GetHeader("Location")!).AbsolutePath);
}

}
Loading