WWWAuthenticateHeaderParser.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using Best.HTTP.Shared.Extensions;
  3. namespace Best.HTTP.Request.Authentication
  4. {
  5. /// <summary>
  6. /// Used for parsing WWW-Authenticate headers:
  7. /// `Digest realm="my realm", nonce="4664b327a2963503ba58bbe13ad672c0", qop=auth, opaque="f7e38bdc1c66fce214f9019ffe43117c"`
  8. /// </summary>
  9. public sealed class WWWAuthenticateHeaderParser : KeyValuePairList
  10. {
  11. public WWWAuthenticateHeaderParser(string headerValue)
  12. {
  13. Values = ParseQuotedHeader(headerValue);
  14. }
  15. private List<HeaderValue> ParseQuotedHeader(string str)
  16. {
  17. List<HeaderValue> result = new List<HeaderValue>();
  18. if (str != null)
  19. {
  20. int idx = 0;
  21. // Read Type (Basic|Digest)
  22. string type = str.Read(ref idx, (ch) => !char.IsWhiteSpace(ch) && !char.IsControl(ch)).TrimAndLower();
  23. result.Add(new HeaderValue(type));
  24. // process the rest of the text
  25. while (idx < str.Length)
  26. {
  27. // Read key
  28. string key = str.Read(ref idx, '=').TrimAndLower();
  29. HeaderValue qp = new HeaderValue(key);
  30. // Skip any white space
  31. str.SkipWhiteSpace(ref idx);
  32. qp.Value = str.ReadPossibleQuotedText(ref idx);
  33. result.Add(qp);
  34. }
  35. }
  36. return result;
  37. }
  38. }
  39. }