Skip to content

Commit

Permalink
Support list of headers key=value
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed Apr 12, 2024
1 parent 1048277 commit 925838b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Hosting/ResponseHeadersHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ResponseHeadersHandler(ILogger logger, ResponseHeadersOptions responseHea
}

if (responseHeaders.FilterContentType is not { } listContentType
|| !listContentType.Any()
|| listContentType.Count == 0
|| responseHeaders.IsAnyContentType)
{
_matchContentType = _ => true;
Expand Down
2 changes: 2 additions & 0 deletions src/Hosting/ResponseHeadersOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ResponseHeadersOptions

public Dictionary<string, string?> Headers { get; set; } = new Dictionary<string, string?>();

public List<string?> List { get; set; } = new List<string?>();

public List<string?>? FilterContentType { get; set; }

public List<int>? FilterHttpStatusCode { get; set; }
Expand Down
43 changes: 33 additions & 10 deletions src/Hosting/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Microsoft.AspNetCore.Builder;

public static class WebApplicationExtensions
{
private static readonly string[] DefaultHttpMethods = new[] { "GET" };

public static WebApplication UseSpaApp(this WebApplication app, ILogger? logger = null, Action<WebApplication>? action = null)
{
logger ??= LogHelper.LoggerOrDefault;
Expand All @@ -31,7 +33,7 @@ public static WebApplication UseSpaApp(this WebApplication app, ILogger? logger
app.UseHsts();
}
}
logger.LogInformation("Use Hsts: {enalbed}", !app.Environment.IsDevelopment() && appOptions.HstsEnabled);
logger.LogInformation("Use Hsts: {enabled}", !app.Environment.IsDevelopment() && appOptions.HstsEnabled);

if (appOptions.HttpsRedirectionEnabled)
{
Expand All @@ -44,20 +46,41 @@ public static WebApplication UseSpaApp(this WebApplication app, ILogger? logger
var staticFileOptions = new StaticFileOptions();

var responseHeaders = appOptions.ResponseHeaders;

if (responseHeaders.List is { } list && list.Count != 0)
{
foreach (var item in list)
{
if (!string.IsNullOrWhiteSpace(item))
{
var pos = item.IndexOf('=');
if (pos > 0)
{
var hKey = item[..pos];
var hValue = item[(pos + 1)..];
AddHeader(hKey, hValue);
}
}
}

void AddHeader(string key, string value)
{
responseHeaders.Headers ??= new Dictionary<string, string?>();
responseHeaders.Headers.TryAdd(key, value);
}
}

var isResponseHeadersEnabled = responseHeaders != null
&& responseHeaders.Headers is { } headers
&& headers.Any()
&& headers.Count != 0
&& responseHeaders.IsEnabled;

#pragma warning disable S2589 // Boolean expressions should not be gratuitous
if (isResponseHeadersEnabled && responseHeaders != null)
{
var headerNames = responseHeaders.Headers.Keys.ToArray();
logger.LogInformation("ResponseHeadersHandler Setup for {headerNames}", headerNames);
var headerNames = responseHeaders.Headers.Keys.OrderBy(i => i, StringComparer.OrdinalIgnoreCase).ToArray();
logger.LogInformation("ResponseHeadersHandler Setup for {headerNames}", (object)headerNames);
var handler = new ResponseHeadersHandler(logger, responseHeaders);
staticFileOptions.OnPrepareResponse = handler.PrepareResponse;
}
#pragma warning restore S2589 // Boolean expressions should not be gratuitous

app.UseSpaStaticFiles(staticFileOptions);

Expand Down Expand Up @@ -132,7 +155,7 @@ private static WebApplication UseGeneralInfo(this WebApplication app, AppOptions
app.MapControllerRoute(name: string.Empty,
pattern: routeGeneralVersion,
defaults: new { controller = "General", action = nameof(GeneralController.Version) })
.WithMetadata(new HttpMethodMetadata(new[] { "GET" }));
.WithMetadata(new HttpMethodMetadata(DefaultHttpMethods));
}

if (routeGeneralInfo != null)
Expand All @@ -145,7 +168,7 @@ private static WebApplication UseGeneralInfo(this WebApplication app, AppOptions
app.MapControllerRoute(name: string.Empty,
pattern: routeGeneralInfo,
defaults: new { controller = "General", action = nameof(GeneralController.Info) })
.WithMetadata(new HttpMethodMetadata(new[] { "GET" }));
.WithMetadata(new HttpMethodMetadata(DefaultHttpMethods));
}

if (routeGeneralSys != null)
Expand All @@ -158,7 +181,7 @@ private static WebApplication UseGeneralInfo(this WebApplication app, AppOptions
app.MapControllerRoute(name: string.Empty,
pattern: routeGeneralSys,
defaults: new { controller = "General", action = nameof(GeneralController.Sys) })
.WithMetadata(new HttpMethodMetadata(new[] { "GET" }));
.WithMetadata(new HttpMethodMetadata(DefaultHttpMethods));
}

return app;
Expand Down

0 comments on commit 925838b

Please sign in to comment.