Skip to content

Commit

Permalink
One more fix for ensuring valid preview server custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Mar 10, 2023
1 parent 50804a4 commit b1071ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task InvokeAsync(HttpContext context)

foreach (KeyValuePair<string, string> header in _customHeaders)
{
context.Response.Headers.Append(header.Key, header.Value);
context.Response.Headers.Append(header.Key, header.Value ?? string.Empty);
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/Statiq.Web/Commands/PreviewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,27 @@ internal static void GetKeyValues(
}
}

private static Dictionary<string, string> GetKeyValues(IEnumerable<string> keysAndValues, string optionName)
private static Dictionary<string, string> GetKeyValues(
IEnumerable<string> keysAndValues,
string optionName)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
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;
}
Expand Down

0 comments on commit b1071ac

Please sign in to comment.