Improve get auth header parts using substring

This commit is contained in:
Tommaso Stocchi 2021-01-02 18:18:47 +01:00
parent 7c7f2316fa
commit a03880b687

View File

@ -273,7 +273,7 @@ namespace Emby.Server.Implementations.HttpServer.Security
{ {
var param = item.Trim().Split('=', 2); var param = item.Trim().Split('=', 2);
var value =param[1].Trim('"'); var value = param[1].Trim('"');
result[param[0]] = NormalizeValue(value); result[param[0]] = NormalizeValue(value);
} }
@ -293,42 +293,37 @@ namespace Emby.Server.Implementations.HttpServer.Security
public static string[] GetParts(string authtorizationHeader) public static string[] GetParts(string authtorizationHeader)
{ {
var result = new List<string>(); var result = new List<string>();
var escapeChars = new[] {'"', ','}; var escapeChars = new[] { '"', ',' };
var escaped = false; var escaped = false;
var authtorizationHeaderChars = authtorizationHeader.ToCharArray(); int start = 0;
var value = new List<char>(); int i = 0;
while (i < authtorizationHeader.Length)
for(var i = 0; i < authtorizationHeaderChars.Length; i++)
{ {
if(!escapeChars.Contains(authtorizationHeaderChars[i])) var token = authtorizationHeader[i];
if (escapeChars.Contains(token))
{ {
value = value.Append(authtorizationHeaderChars[i]).ToList(); // Applying a XOR logic to evaluate whether it is opening or closing a value
} escaped = (!escaped) == (token == '"');
else if (token == ',' && !escaped)
{
// Applying a XOR logic to evaluate wether it is opening or closing a value
escaped = (!escaped) == (authtorizationHeaderChars[i] == '"');
if(authtorizationHeaderChars[i] == ',')
{
if(escaped)
{
value = value.Append(authtorizationHeaderChars[i]).ToList();
}
else
{ {
// Meeting a comma after a closing escape char means the value is complete // Meeting a comma after a closing escape char means the value is complete
result.Add(new string(value.ToArray())); if (start < i)
value = new List<char>();
}
}
else
{ {
value = value.Append(authtorizationHeaderChars[i]).ToList(); result.Add(authtorizationHeader[start..(i)]);
}
start = i + 1;
} }
} }
i++;
} }
// Add last value // Add last value
result.Add(new string(value.ToArray())); if (start < i)
{
result.Add(authtorizationHeader[start..(i)]);
}
return result.ToArray(); return result.ToArray();
} }