namespace Uwaa.HTTP; /// /// Helper methods for HTTP. /// public static class HttpHelpers { /// /// Parses an array of MIME types from an Accept field. /// 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; } }