66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
namespace Uwaa.HTTP;
|
|
|
|
/// <summary>
|
|
/// Helper methods for HTTP.
|
|
/// </summary>
|
|
public static class HttpHelpers
|
|
{
|
|
/// <summary>
|
|
/// Parses an array of MIME types from an Accept field.
|
|
/// </summary>
|
|
public static MIMEType[] ParseAccept(string accept)
|
|
{
|
|
int count = 1;
|
|
for (int i = 0; i < accept.Length; i++)
|
|
if (accept[i] == ',')
|
|
count++;
|
|
|
|
MIMEType[] result = new MIMEType[count];
|
|
int resultIndex = 0;
|
|
int splStart = 0;
|
|
int splEnd = 0;
|
|
void flush()
|
|
{
|
|
try
|
|
{
|
|
result[resultIndex++] = new MIMEType(accept.AsSpan(splStart..(splEnd + 1)));
|
|
}
|
|
catch (FormatException e)
|
|
{
|
|
throw new HttpException(e.Message, e);
|
|
}
|
|
}
|
|
for (int i = 0; i < accept.Length; i++)
|
|
{
|
|
switch (accept[i])
|
|
{
|
|
case ';':
|
|
flush();
|
|
|
|
//Another stupid idea by the W3C. Not interested in implementing it - skip!!!
|
|
while (i < accept.Length && accept[i] != ',')
|
|
i++;
|
|
splStart = i + 1;
|
|
continue;
|
|
|
|
case ',':
|
|
flush();
|
|
splStart = i + 1;
|
|
continue;
|
|
|
|
case ' ':
|
|
if (splEnd <= splStart)
|
|
splStart = i + 1; //Trim start
|
|
continue; //Trim end
|
|
|
|
default:
|
|
splEnd = i;
|
|
continue;
|
|
}
|
|
}
|
|
if (splStart < splEnd)
|
|
flush(); //Flush remaining
|
|
|
|
return result;
|
|
}
|
|
}
|