Headers.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 MediaBrowser.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.Compiled | 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 Stream HeaderStream
  42. {
  43. get
  44. {
  45. return new MemoryStream(Encoding.ASCII.GetBytes(HeaderBlock));
  46. }
  47. }
  48. public bool IsReadOnly
  49. {
  50. get
  51. {
  52. return false;
  53. }
  54. }
  55. public ICollection<string> Keys
  56. {
  57. get
  58. {
  59. return _dict.Keys;
  60. }
  61. }
  62. public ICollection<string> Values
  63. {
  64. get
  65. {
  66. return _dict.Values;
  67. }
  68. }
  69. public string this[string key]
  70. {
  71. get
  72. {
  73. return _dict[Normalize(key)];
  74. }
  75. set
  76. {
  77. _dict[Normalize(key)] = value;
  78. }
  79. }
  80. private string Normalize(string header)
  81. {
  82. if (!_asIs)
  83. {
  84. header = header.ToLower();
  85. }
  86. header = header.Trim();
  87. if (!Validator.IsMatch(header))
  88. {
  89. throw new ArgumentException("Invalid header: " + header);
  90. }
  91. return header;
  92. }
  93. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  94. {
  95. return _dict.GetEnumerator();
  96. }
  97. public void Add(KeyValuePair<string, string> item)
  98. {
  99. Add(item.Key, item.Value);
  100. }
  101. public void Add(string key, string value)
  102. {
  103. _dict.Add(Normalize(key), value);
  104. }
  105. public void Clear()
  106. {
  107. _dict.Clear();
  108. }
  109. public bool Contains(KeyValuePair<string, string> item)
  110. {
  111. var p = new KeyValuePair<string, string>(Normalize(item.Key), item.Value);
  112. return _dict.Contains(p);
  113. }
  114. public bool ContainsKey(string key)
  115. {
  116. return _dict.ContainsKey(Normalize(key));
  117. }
  118. public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
  119. {
  120. throw new NotImplementedException();
  121. }
  122. public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
  123. {
  124. return _dict.GetEnumerator();
  125. }
  126. public bool Remove(string key)
  127. {
  128. return _dict.Remove(Normalize(key));
  129. }
  130. public bool Remove(KeyValuePair<string, string> item)
  131. {
  132. return Remove(item.Key);
  133. }
  134. public override string ToString()
  135. {
  136. return string.Format("({0})", string.Join(", ", (from x in _dict
  137. select string.Format("{0}={1}", x.Key, x.Value))));
  138. }
  139. public bool TryGetValue(string key, out string value)
  140. {
  141. return _dict.TryGetValue(Normalize(key), out value);
  142. }
  143. }
  144. }