Skip to content

Commit b1071ac

Browse files
committed
One more fix for ensuring valid preview server custom headers
1 parent 50804a4 commit b1071ac

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Statiq.Web.Hosting/Middleware/CustomHeadersMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task InvokeAsync(HttpContext context)
3434

3535
foreach (KeyValuePair<string, string> header in _customHeaders)
3636
{
37-
context.Response.Headers.Append(header.Key, header.Value);
37+
context.Response.Headers.Append(header.Key, header.Value ?? string.Empty);
3838
}
3939
}
4040
}

src/Statiq.Web/Commands/PreviewCommand.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,27 @@ internal static void GetKeyValues(
224224
}
225225
}
226226

227-
private static Dictionary<string, string> GetKeyValues(IEnumerable<string> keysAndValues, string optionName)
227+
private static Dictionary<string, string> GetKeyValues(
228+
IEnumerable<string> keysAndValues,
229+
string optionName)
228230
{
229231
Dictionary<string, string> dictionary = new Dictionary<string, string>();
230232
foreach (string keyAndValue in keysAndValues)
231233
{
232-
string[] split = keyAndValue.Split('=');
233-
if (split.Length < 2)
234+
int equalsLocation = keyAndValue.IndexOf("=");
235+
if (equalsLocation == 0)
236+
{
237+
throw new ArgumentException($"Invalid {optionName} (no key): {keyAndValue}");
238+
}
239+
if (equalsLocation >= keyAndValue.Length - 1)
240+
{
241+
throw new ArgumentException($"Invalid {optionName} (no value): {keyAndValue}");
242+
}
243+
if (equalsLocation < 0)
234244
{
235-
throw new ArgumentException($"Invalid {optionName} {keyAndValue} specified.");
245+
throw new ArgumentException($"Invalid {optionName} (no equals sign): {keyAndValue}");
236246
}
237-
dictionary[split[0].Trim().Trim('\"')] = split[1].Trim().Trim('\"');
247+
dictionary[keyAndValue.Substring(0, equalsLocation)] = keyAndValue.Substring(equalsLocation + 1);
238248
}
239249
return dictionary;
240250
}

0 commit comments

Comments
 (0)