Skip to content

Commit d06db20

Browse files
committed
Refactor AddYllibedHttpServer to avoid recursion and simplify handler registrations
1 parent 1303e76 commit d06db20

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

Yllibed.HttpServer/Extensions/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ public static IServiceCollection AddYllibedHttpServer(this IServiceCollection se
3939
public static IServiceCollection AddYllibedHttpServer(this IServiceCollection services, Action<ServerOptions> configureOptions)
4040
{
4141
services.Configure(configureOptions);
42-
return services.AddYllibedHttpServer();
42+
43+
// Avoid calling the parameterless overload to prevent confusion/recursion; register the same singletons directly.
44+
services.TryAddSingleton<HandlerRegistrationService>();
45+
services.TryAddSingleton<Server>(sp =>
46+
{
47+
var server = new Server(sp.GetRequiredService<IOptions<ServerOptions>>());
48+
var registrationService = sp.GetRequiredService<HandlerRegistrationService>();
49+
registrationService.RegisterHandlers(server, sp);
50+
return server;
51+
});
52+
53+
return services;
4354
}
4455

4556
/// <summary>

Yllibed.HttpServer/Handlers/GuardHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public GuardHandler(
6060
MaxHeadersCount = maxHeadersCount;
6161
MaxHeadersTotalSize = maxHeadersTotalSize;
6262
MaxBodyBytes = maxBodyBytes;
63-
_allowedMethods = allowedMethods != null ? new HashSet<string>(allowedMethods.Select(m => m.ToUpperInvariant()), StringComparer.OrdinalIgnoreCase) : null;
63+
_allowedMethods = allowedMethods != null ? new HashSet<string>(allowedMethods, StringComparer.OrdinalIgnoreCase) : null;
6464
_allowedHosts = allowedHosts != null ? new HashSet<string>(allowedHosts, StringComparer.OrdinalIgnoreCase) : null;
6565
RequireHostHeader = requireHostHeader;
6666
_inner = inner;
@@ -138,11 +138,11 @@ public async Task HandleRequest(CancellationToken ct, IHttpServerRequest request
138138
{
139139
// Approximate header size as key length + sum of values length
140140
total += kvp.Key.Length;
141-
foreach (var v in kvp.Value)
141+
total += kvp.Value.Sum(v => v?.Length ?? 0);
142+
if (total > maxSize)
142143
{
143-
total += v?.Length ?? 0;
144+
break;
144145
}
145-
if (total > maxSize) break;
146146
}
147147
if (total > maxSize)
148148
{
@@ -162,7 +162,7 @@ public async Task HandleRequest(CancellationToken ct, IHttpServerRequest request
162162
// 4) If wrapping another handler, pass-through
163163
if (_inner is not null)
164164
{
165-
await _inner.HandleRequest(ct, request, relativePath).ConfigureAwait(true);
165+
await _inner.HandleRequest(ct, request, relativePath);
166166
}
167167
}
168168

0 commit comments

Comments
 (0)