Skip to content

Commit 7a71acd

Browse files
Switch to express, started mapping
1 parent e53f8f2 commit 7a71acd

14 files changed

Lines changed: 71 additions & 385 deletions

Adapters/WiredIO/Adapter.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,24 @@
77
using GenHTTP.Modules.ErrorHandling;
88
using GenHTTP.Modules.IO;
99

10-
using Wired.IO.App;
11-
using Wired.IO.Http11.Context;
10+
using Wired.IO.Builder;
11+
using Wired.IO.Http11Express;
12+
using Wired.IO.Http11Express.Context;
1213

1314
namespace GenHTTP.Adapters.WiredIO;
1415

1516
public static class Adapter
1617
{
1718

18-
/// <summary>
19-
/// Registers the given handler to respond to requests to the specified path.
20-
/// </summary>
21-
/// <param name="app">The application to add the mapping to</param>
22-
/// <param name="path">The path to register the handler for</param>
23-
/// <param name="handler">The handler to be registered</param>
24-
/// <param name="companion">An object that will be informed about handled requests and any error</param>
25-
public static void Map(this WiredApp<Http11Context> app, string path, IHandlerBuilder handler, IServerCompanion? companion = null)
26-
=> Map(app, path, handler.Build(), companion);
19+
// ToDo: IBaseRequest and IBaseResponse do not feature basic access (such as headers), so we cannot be generic here
2720

28-
/// <summary>
29-
/// Registers the given handler to respond to requests to the specified path.
30-
/// </summary>
31-
/// <param name="app">The application to add the mapping to</param>
32-
/// <param name="path">The path to register the handler for</param>
33-
/// <param name="handler">The handler to be registered</param>
34-
/// <param name="companion">An object that will be informed about handled requests and any error</param>
35-
public static void Map(this WiredApp<Http11Context> app, string path, IHandler handler, IServerCompanion? companion = null)
36-
=> app.Map(path + "/{*any}", async context => await Bridge.MapAsync(context, handler, companion: companion, registeredPath: path));
21+
public static void Map(this Builder<WiredHttp11Express<Http11ExpressContext>, Http11ExpressContext> builder, string path, IHandlerBuilder handler, IServerCompanion? companion = null)
22+
=> Map(builder, path, handler.Build(), companion);
3723

38-
public static void BuildPipeline(this WiredApp<Http11Context> app, IHandlerBuilder handler, IServer server)
39-
=> app.BuildPipeline(handler, server);
40-
41-
public static void BuildPipeline(this WiredApp<Http11Context> app, IHandler handler, IServer server)
42-
=> app.BuildPipeline(new List<Func<Http11Context, Func<Http11Context, Task>, Task>>(), context => Bridge.MapAsync(context, handler, server));
24+
public static void Map(this Builder<WiredHttp11Express<Http11ExpressContext>, Http11ExpressContext> builder, string path, IHandler handler, IServerCompanion? companion = null)
25+
{
26+
builder.UseMiddleware(scope => async (c, n) => await Bridge.MapAsync(c, n, handler, companion: companion, registeredPath: path));
27+
}
4328

4429
/// <summary>
4530
/// Enables default features on the given handler. This should be used on the

Adapters/WiredIO/GenHTTP.Adapters.WiredIO.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<ItemGroup>
1313

14-
<PackageReference Include="Wired.IO" Version="9.5.0" />
14+
<PackageReference Include="Wired.IO" Version="9.5.3" />
1515

1616
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj"/>
1717

Adapters/WiredIO/Mapping/Bridge.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
using GenHTTP.Api.Infrastructure;
66
using GenHTTP.Api.Protocol;
77

8-
using Wired.IO.Http11.Context;
8+
using Wired.IO.Http11Express.Context;
9+
10+
using WR = Wired.IO.Protocol.Response;
911

1012
namespace GenHTTP.Adapters.WiredIO.Mapping;
1113

1214
public static class Bridge
1315
{
1416

15-
public static async ValueTask MapAsync(Http11Context context, IHandler handler, IServer? server = null, IServerCompanion? companion = null, string? registeredPath = null)
17+
public static async Task MapAsync(Http11ExpressContext context, Func<Http11ExpressContext, Task> next, IHandler handler, IServerCompanion? companion = null, string? registeredPath = null)
1618
{
17-
var actualServer = server ?? new ImplicitServer(handler, companion);
19+
var server = new ImplicitServer(handler, companion);
1820

1921
try
2022
{
21-
using var request = new Request(actualServer, context);
23+
using var request = new Request(server, context.Request);
2224

2325
if (registeredPath != null)
2426
{
@@ -27,33 +29,34 @@ public static async ValueTask MapAsync(Http11Context context, IHandler handler,
2729

2830
using var response = await handler.HandleAsync(request);
2931

30-
if (response == null)
32+
if (response != null)
3133
{
32-
context.Response.StatusCode = 404;
34+
await WriteAsync(response, context);
35+
36+
server.Companion?.OnRequestHandled(request, response);
3337
}
3438
else
3539
{
36-
await WriteAsync(response, context);
37-
38-
actualServer.Companion?.OnRequestHandled(request, response);
40+
await next(context);
3941
}
4042
}
4143
catch (Exception e)
4244
{
43-
actualServer.Companion?.OnServerError(ServerErrorScope.ServerConnection, context.Connection.RemoteIpAddress, e);
45+
// todo: cannot tell the IP of the client in wired
46+
server.Companion?.OnServerError(ServerErrorScope.ServerConnection, null, e);
4447
throw;
4548
}
4649
}
4750

48-
private static async ValueTask WriteAsync(IResponse response, HttpContext context)
51+
private static async ValueTask WriteAsync(IResponse response, Http11ExpressContext context)
4952
{
50-
var target = context.Response;
53+
var target = context.Respond();
5154

52-
target.StatusCode = response.Status.RawStatus;
55+
target.Status((WR.ResponseStatus)response.Status.RawStatus);
5356

5457
foreach (var header in response.Headers)
5558
{
56-
target.Headers.Append(header.Key, header.Value);
59+
target.Header(header.Key, header.Value);
5760
}
5861

5962
if (response.Modified != null)

Adapters/WiredIO/Server/ImplicitServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public bool Development
1717
{
1818
get
1919
{
20+
// todo: is there something like development mode in wired?
2021
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
2122
return string.Compare(env, "Development", StringComparison.OrdinalIgnoreCase) == 0;
2223
}
@@ -48,7 +49,7 @@ public ImplicitServer(IHandler handler, IServerCompanion? companion)
4849

4950
public ValueTask DisposeAsync() => new();
5051

51-
public ValueTask StartAsync() => throw new InvalidOperationException("Server is managed by ASP.NET Core and cannot be started");
52+
public ValueTask StartAsync() => throw new InvalidOperationException("Server is managed by WiredIO and cannot be started");
5253

5354
#endregion
5455

Adapters/WiredIO/Types/ClientConnection.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44
using GenHTTP.Api.Infrastructure;
55
using GenHTTP.Api.Protocol;
66

7+
using Wired.IO.Http11Express.Request;
8+
79
namespace GenHTTP.Adapters.WiredIO.Types;
810

911
public sealed class ClientConnection : IClientConnection
1012
{
1113

1214
#region Get-/Setters
1315

14-
public IPAddress IPAddress => Info.RemoteIpAddress ?? throw new InvalidOperationException("Remote client IP address is not known");
16+
public IPAddress IPAddress => throw new InvalidOperationException("Remote client IP address is not known");
1517

1618
public ClientProtocol? Protocol { get; }
1719

18-
public string? Host => Request.Host.HasValue ? Request.Host.Value : null;
19-
20-
public X509Certificate? Certificate => Info.ClientCertificate;
20+
public string? Host => Request.Headers.GetValueOrDefault("Host");
2121

22-
private ConnectionInfo Info { get; }
22+
public X509Certificate? Certificate => null;
2323

24-
private HttpRequest Request { get; }
24+
private IExpressRequest Request { get; }
2525

2626
#endregion
2727

2828
#region Initialization
2929

30-
public ClientConnection(ConnectionInfo info, HttpRequest request)
30+
public ClientConnection(IExpressRequest request)
3131
{
32-
Info = info;
3332
Request = request;
3433

35-
Protocol = (request.IsHttps) ? ClientProtocol.Https : ClientProtocol.Http;
34+
// todo: wired does not expose this information
35+
Protocol = ClientProtocol.Http;
3636
}
3737

3838
#endregion

Adapters/WiredIO/Types/Request.cs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using GenHTTP.Api.Infrastructure;
22
using GenHTTP.Api.Protocol;
33
using GenHTTP.Api.Routing;
4-
using GenHTTP.Engine.Shared.Types;
5-
6-
using Wired.IO.Http11.Context;
74

8-
using HttpProtocol = GenHTTP.Api.Protocol.HttpProtocol;
5+
using GenHTTP.Engine.Shared.Types;
96

10-
using WiredRequest = Wired.IO.Http11.Request.IRequest;
7+
using Wired.IO.Http11Express.Request;
118

129
namespace GenHTTP.Adapters.WiredIO.Types;
1310

@@ -23,8 +20,6 @@ public sealed class Request : IRequest
2320

2421
private Headers? _Headers;
2522

26-
private readonly IEndPoint? _EndPoint;
27-
2823
#region Get-/Setters
2924

3025
public IRequestProperties Properties
@@ -34,7 +29,7 @@ public IRequestProperties Properties
3429

3530
public IServer Server { get; }
3631

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

3934
public IClientConnection Client { get; }
4035

@@ -56,65 +51,67 @@ public IRequestProperties Properties
5651

5752
public IRequestQuery Query
5853
{
59-
get { return _Query ??= new Query(Context); }
54+
get { return _Query ??= new Query(InnerRequest); }
6055
}
6156

6257
public ICookieCollection Cookies
6358
{
64-
get { return _Cookies ??= new Cookies(Context); }
59+
get { return _Cookies ??= new Cookies(InnerRequest); }
6560
}
6661

6762
public IForwardingCollection Forwardings => _Forwardings;
6863

6964
public IHeaderCollection Headers
7065
{
71-
get { return _Headers ??= new Headers(Context); }
66+
get { return _Headers ??= new Headers(InnerRequest); }
7267
}
7368

74-
public Stream Content => Context.BodyReader.AsStream(true);
69+
// todo: this is quite inefficient
70+
public Stream Content => (InnerRequest.Content != null) ? new MemoryStream(InnerRequest.Content) : Stream.Null;
7571

76-
public FlexibleContentType? ContentType => (Context.ContentType != null) ? new(Context.ContentType) : null;
72+
public FlexibleContentType? ContentType
73+
{
74+
get
75+
{
76+
if (InnerRequest.Headers.TryGetValue("Content-Type", out var contentType))
77+
{
78+
return FlexibleContentType.Parse(contentType);
79+
}
7780

78-
private WiredRequest Context { get; }
81+
return null;
82+
}
83+
}
84+
85+
private IExpressRequest InnerRequest { get; }
7986

8087
#endregion
8188

8289
#region Initialization
8390

84-
public Request(IServer server, Http11Context context)
91+
public Request(IServer server, IExpressRequest request)
8592
{
8693
Server = server;
87-
Context = context.Request;
94+
InnerRequest = request;
8895

89-
ProtocolType = Context.ConnectionType switch
90-
{
91-
"HTTP/1.0" => HttpProtocol.Http10,
92-
"HTTP/1.1" => HttpProtocol.Http11,
93-
"HTTP/2" => HttpProtocol.Http2,
94-
"HTTP/3" => HttpProtocol.Http3,
95-
_ => HttpProtocol.Http11
96-
};
96+
// todo: no API provided by wired
97+
ProtocolType = HttpProtocol.Http11;
9798

98-
Method = FlexibleRequestMethod.Get(Context.Method);
99-
Target = new RoutingTarget(WebPath.FromString(Context.Path));
99+
Method = FlexibleRequestMethod.Get(request.HttpMethod);
100+
Target = new RoutingTarget(WebPath.FromString(request.Route));
100101

101-
if (context.Request.Headers.TryGetValue("forwarded", out var forwardings))
102+
if (request.Headers.TryGetValue("forwarded", out var entry))
102103
{
103-
foreach (var entry in forwardings)
104-
{
105-
if (entry != null) _Forwardings.Add(entry);
106-
}
104+
_Forwardings.Add(entry);
107105
}
108106
else
109107
{
110108
_Forwardings.TryAddLegacy(Headers);
111109
}
112110

113-
LocalClient = new ClientConnection(context.Connection, context.Request);
114-
115-
Client = _Forwardings.DetermineClient(context.Connection.ClientCertificate) ?? LocalClient;
111+
LocalClient = new ClientConnection(request);
116112

117-
_EndPoint = Server.EndPoints.FirstOrDefault(e => e.Port == context.Connection.LocalPort);
113+
// todo: potential client certificate is not exposed by wired
114+
Client = _Forwardings.DetermineClient(null) ?? LocalClient;
118115
}
119116

120117
#endregion

Engine/WiredIO/GenHTTP.Engine.WiredIO.csproj

Lines changed: 0 additions & 28 deletions
This file was deleted.

Engine/WiredIO/Host.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)