Skip to content

Commit 03f1079

Browse files
committed
Simplify settings by just providing the callback
1 parent 208f098 commit 03f1079

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

src/Components/Server/src/Builder/ServerComponentsEndpointOptions.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ namespace Microsoft.AspNetCore.Components.Server;
1111
/// </summary>
1212
public class ServerComponentsEndpointOptions
1313
{
14-
/// <summary>
15-
/// Gets or sets a value that indicates whether compression is enabled for the WebSocket connections.
16-
/// </summary>
17-
public bool EnableWebSocketCompression { get; set; } = true;
18-
1914
/// <summary>
2015
/// Gets or sets the <c>frame-ancestors</c> <c>Content-Security-Policy</c> to set in the
21-
/// <see cref="HttpResponse"/> when <see cref="EnableWebSocketCompression" /> is enabled.
16+
/// <see cref="HttpResponse"/> when <see cref="ConfigureConnectionOptions" /> is set.
2217
/// </summary>
2318
/// <remarks>
2419
/// <para>Setting this value to <see langword="null" /> will prevent the policy from being
@@ -39,8 +34,11 @@ public class ServerComponentsEndpointOptions
3934

4035
/// <summary>
4136
/// Gets or sets a callback to configure the underlying <see cref="HttpConnectionDispatcherOptions"/>.
42-
/// If set, <see cref="ContentSecurityFrameAncestorPolicy"/> will be applied independent of the value of
43-
/// <see cref="EnableWebSocketCompression"/>.
37+
/// By default, a policy that enables compression and sets a Content Security Policy for the frame ancestors
38+
/// defined in <see cref="ContentSecurityFrameAncestorPolicy"/> will be applied.
4439
/// </summary>
45-
public Action<HttpConnectionDispatcherOptions> ConfigureConnectionOptions { get; set; }
40+
public Action<HttpConnectionDispatcherOptions> ConfigureConnectionOptions { get; set; } = EnableCompressionDefaults;
41+
42+
private static WebSocketAcceptContext EnableCompressionDefaults(HttpContext context) =>
43+
new() { DangerousEnableCompression = true };
4644
}

src/Components/Server/src/Builder/ServerRazorComponentsEndpointConventionBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static RazorComponentsEndpointConventionBuilder AddInteractiveServerRende
3838

3939
ComponentEndpointConventionBuilderHelper.AddRenderMode(builder, new InternalServerRenderMode(options));
4040

41-
if ((options.EnableWebSocketCompression || options.ConfigureConnectionOptions is not null) && options.ContentSecurityFrameAncestorPolicy != null)
41+
if (options.ConfigureConnectionOptions is not null && options.ContentSecurityFrameAncestorPolicy != null)
4242
{
4343
builder.AddEndpointFilter(new RequireCspFilter(options.ContentSecurityFrameAncestorPolicy));
4444
}

src/Components/Server/src/DependencyInjection/ServerRazorComponentsBuilderExtensions.cs

+2-16
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,9 @@ public override IEnumerable<RouteEndpointBuilder> GetEndpointBuilders(
6565
}
6666

6767
var endpointRouteBuilder = new EndpointRouteBuilder(Services, applicationBuilder);
68-
if (renderMode is InternalServerRenderMode { Options: var configureOptions } &&
69-
configureOptions != null)
68+
if (renderMode is InternalServerRenderMode { Options.ConfigureConnectionOptions: { } configureConnection })
7069
{
71-
if (configureOptions.ConfigureConnectionOptions != null)
72-
{
73-
endpointRouteBuilder.MapBlazorHub("/_blazor", configureOptions.ConfigureConnectionOptions);
74-
}
75-
else if (configureOptions.EnableWebSocketCompression)
76-
{
77-
endpointRouteBuilder.MapBlazorHub("/_blazor", options =>
78-
{
79-
options.WebSockets.WebSocketAcceptContextFactory = EnableCompressionDefaults;
80-
});
81-
}
70+
endpointRouteBuilder.MapBlazorHub("/_blazor", configureOptions.ConfigureConnectionOptions);
8271
}
8372
else
8473
{
@@ -88,9 +77,6 @@ public override IEnumerable<RouteEndpointBuilder> GetEndpointBuilders(
8877
return endpointRouteBuilder.GetEndpoints();
8978
}
9079

91-
private static WebSocketAcceptContext EnableCompressionDefaults(HttpContext context) =>
92-
new() { DangerousEnableCompression = true };
93-
9480
public override bool Supports(IComponentRenderMode renderMode)
9581
{
9682
return renderMode switch

0 commit comments

Comments
 (0)