From b1071ac450439ed5669ec3a9865eb795247a8af9 Mon Sep 17 00:00:00 2001 From: Dave Glick Date: Fri, 10 Mar 2023 14:59:47 -0500 Subject: [PATCH] One more fix for ensuring valid preview server custom headers --- .../Middleware/CustomHeadersMiddleware.cs | 2 +- src/Statiq.Web/Commands/PreviewCommand.cs | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Statiq.Web.Hosting/Middleware/CustomHeadersMiddleware.cs b/src/Statiq.Web.Hosting/Middleware/CustomHeadersMiddleware.cs index 0ea10158..874fc67e 100644 --- a/src/Statiq.Web.Hosting/Middleware/CustomHeadersMiddleware.cs +++ b/src/Statiq.Web.Hosting/Middleware/CustomHeadersMiddleware.cs @@ -34,7 +34,7 @@ public async Task InvokeAsync(HttpContext context) foreach (KeyValuePair header in _customHeaders) { - context.Response.Headers.Append(header.Key, header.Value); + context.Response.Headers.Append(header.Key, header.Value ?? string.Empty); } } } diff --git a/src/Statiq.Web/Commands/PreviewCommand.cs b/src/Statiq.Web/Commands/PreviewCommand.cs index 3b8f6583..77ee27b3 100644 --- a/src/Statiq.Web/Commands/PreviewCommand.cs +++ b/src/Statiq.Web/Commands/PreviewCommand.cs @@ -224,17 +224,27 @@ internal static void GetKeyValues( } } - private static Dictionary GetKeyValues(IEnumerable keysAndValues, string optionName) + private static Dictionary GetKeyValues( + IEnumerable keysAndValues, + string optionName) { Dictionary dictionary = new Dictionary(); foreach (string keyAndValue in keysAndValues) { - string[] split = keyAndValue.Split('='); - if (split.Length < 2) + int equalsLocation = keyAndValue.IndexOf("="); + if (equalsLocation == 0) + { + throw new ArgumentException($"Invalid {optionName} (no key): {keyAndValue}"); + } + if (equalsLocation >= keyAndValue.Length - 1) + { + throw new ArgumentException($"Invalid {optionName} (no value): {keyAndValue}"); + } + if (equalsLocation < 0) { - throw new ArgumentException($"Invalid {optionName} {keyAndValue} specified."); + throw new ArgumentException($"Invalid {optionName} (no equals sign): {keyAndValue}"); } - dictionary[split[0].Trim().Trim('\"')] = split[1].Trim().Trim('\"'); + dictionary[keyAndValue.Substring(0, equalsLocation)] = keyAndValue.Substring(equalsLocation + 1); } return dictionary; }