WWWAuthenticateHeaderParser.cs 1.5 KB

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