Headers.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace Emby.Dlna.Server
  8. {
  9. public class Headers : IDictionary<string, string>
  10. {
  11. private readonly bool _asIs = false;
  12. private readonly Dictionary<string, string> _dict = new Dictionary<string, string>();
  13. private readonly static Regex Validator = new Regex(@"^[a-z\d][a-z\d_.-]+$", RegexOptions.IgnoreCase);
  14. public Headers(bool asIs)
  15. {
  16. _asIs = asIs;
  17. }
  18. public Headers()
  19. : this(asIs: false)
  20. {
  21. }
  22. public int Count
  23. {
  24. get
  25. {
  26. return _dict.Count;
  27. }
  28. }
  29. public string HeaderBlock
  30. {
  31. get
  32. {
  33. var hb = new StringBuilder();
  34. foreach (var h in this)
  35. {
  36. hb.AppendFormat("{0}: {1}\r\n", h.Key, h.Value);
  37. }
  38. return hb.ToString();
  39. }
  40. }
  41. public bool IsReadOnly
  42. {
  43. get
  44. {
  45. return false;
  46. }
  47. }
  48. public ICollection<string> Keys
  49. {
  50. get
  51. {
  52. return _dict.Keys;
  53. }
  54. }
  55. public ICollection<string> Values
  56. {
  57. get
  58. {
  59. return _dict.Values;
  60. }
  61. }
  62. public string this[string key]
  63. {
  64. get
  65. {
  66. return _dict[Normalize(key)];
  67. }
  68. set
  69. {
  70. _dict[Normalize(key)] = value;
  71. }
  72. }
  73. private string Normalize(string header)
  74. {
  75. if (!_asIs)
  76. {
  77. header = header.ToLower();
  78. }
  79. header = header.Trim();
  80. if (!Validator.IsMatch(header))
  81. {
  82. throw new ArgumentException("Invalid header: " + header);
  83. }
  84. return header;
  85. }
  86. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  87. {
  88. return _dict.GetEnumerator();
  89. }
  90. public void Add(KeyValuePair<string, string> item)
  91. {
  92. Add(item.Key, item.Value);
  93. }
  94. public void Add(string key, string value)
  95. {
  96. _dict.Add(Normalize(key), value);
  97. }
  98. public void Clear()
  99. {
  100. _dict.Clear();
  101. }
  102. public bool Contains(KeyValuePair<string, string> item)
  103. {
  104. var p = new KeyValuePair<string, string>(Normalize(item.Key), item.Value);
  105. return _dict.Contains(p);
  106. }
  107. public bool ContainsKey(string key)
  108. {
  109. return _dict.ContainsKey(Normalize(key));
  110. }
  111. public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
  116. {
  117. return _dict.GetEnumerator();
  118. }
  119. public bool Remove(string key)
  120. {
  121. return _dict.Remove(Normalize(key));
  122. }
  123. public bool Remove(KeyValuePair<string, string> item)
  124. {
  125. return Remove(item.Key);
  126. }
  127. public override string ToString()
  128. {
  129. return string.Format("({0})", string.Join(", ", (from x in _dict
  130. select string.Format("{0}={1}", x.Key, x.Value))));
  131. }
  132. public bool TryGetValue(string key, out string value)
  133. {
  134. return _dict.TryGetValue(Normalize(key), out value);
  135. }
  136. public string GetValueOrDefault(string key, string defaultValue)
  137. {
  138. string val;
  139. if (TryGetValue(key, out val))
  140. {
  141. return val;
  142. }
  143. return defaultValue;
  144. }
  145. }
  146. }