CustomHttpHeaders.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Rssdp
  7. {
  8. /// <summary>
  9. /// Represents a custom HTTP header sent on device search response or notification messages.
  10. /// </summary>
  11. public sealed class CustomHttpHeader
  12. {
  13. #region Fields
  14. private string _Name;
  15. private string _Value;
  16. #endregion
  17. #region Constructors
  18. /// <summary>
  19. /// Full constructor.
  20. /// </summary>
  21. /// <param name="name">The field name of the header.</param>
  22. /// <param name="value">The value of the header</param>
  23. /// <remarks>
  24. /// <para>As per RFC 822 and 2616, the name must contain only printable ASCII characters (33-126) excluding colon (:). The value may contain any ASCII characters except carriage return or line feed.</para>
  25. /// </remarks>
  26. /// <exception cref="System.ArgumentNullException">Thrown if the name is null.</exception>
  27. /// <exception cref="System.ArgumentException">Thrown if the name is an empty value, or contains an invalid character. Also thrown if the value contains a \r or \n character.</exception>
  28. public CustomHttpHeader(string name, string value)
  29. {
  30. Name = name;
  31. Value = value;
  32. }
  33. #endregion
  34. #region Public Properties
  35. /// <summary>
  36. /// Return the name of this header.
  37. /// </summary>
  38. public string Name
  39. {
  40. get { return _Name; }
  41. private set
  42. {
  43. EnsureValidName(value);
  44. _Name = value;
  45. }
  46. }
  47. /// <summary>
  48. /// Returns the value of this header.
  49. /// </summary>
  50. public string Value
  51. {
  52. get { return _Value; }
  53. private set
  54. {
  55. EnsureValidValue(value);
  56. _Value = value;
  57. }
  58. }
  59. #endregion
  60. #region Overrides
  61. /// <summary>
  62. /// Returns the header formatted for use in an HTTP message.
  63. /// </summary>
  64. /// <returns>A string representing this header in the format of 'name: value'.</returns>
  65. public override string ToString()
  66. {
  67. return this.Name + ": " + this.Value;
  68. }
  69. #endregion
  70. #region Private Methods
  71. private static void EnsureValidName(string name)
  72. {
  73. if (name == null) throw new ArgumentNullException(nameof(name), "Name cannot be null.");
  74. if (name.Length == 0) throw new ArgumentException("Name cannot be blank.", nameof(name));
  75. foreach (var c in name)
  76. {
  77. var b = (byte)c;
  78. if (c == ':' || b < 33 || b > 126) throw new ArgumentException("Name contains illegal characters.", nameof(name));
  79. }
  80. }
  81. private static void EnsureValidValue(string value)
  82. {
  83. if (String.IsNullOrEmpty(value)) return;
  84. if (value.Contains("\r") || value.Contains("\n")) throw new ArgumentException("Invalid value.", nameof(value));
  85. }
  86. #endregion
  87. }
  88. /// <summary>
  89. /// Represents a collection of custom HTTP headers, keyed by name.
  90. /// </summary>
  91. public class CustomHttpHeadersCollection : IEnumerable<CustomHttpHeader>
  92. {
  93. #region Fields
  94. private IDictionary<string, CustomHttpHeader> _Headers;
  95. #endregion
  96. #region Constructors
  97. /// <summary>
  98. /// Default constructor.
  99. /// </summary>
  100. public CustomHttpHeadersCollection()
  101. {
  102. _Headers = new Dictionary<string, CustomHttpHeader>();
  103. }
  104. /// <summary>
  105. /// Full constructor.
  106. /// </summary>
  107. /// <param name="capacity">Specifies the initial capacity of the collection.</param>
  108. public CustomHttpHeadersCollection(int capacity)
  109. {
  110. _Headers = new Dictionary<string, CustomHttpHeader>(capacity);
  111. }
  112. #endregion
  113. #region Public Methpds
  114. /// <summary>
  115. /// Adds a <see cref="CustomHttpHeader"/> instance to the collection.
  116. /// </summary>
  117. /// <param name="header">The <see cref="CustomHttpHeader"/> instance to add to the collection.</param>
  118. /// <remarks>
  119. /// <para></para>
  120. /// </remarks>
  121. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="header"/> is null.</exception>
  122. public void Add(CustomHttpHeader header)
  123. {
  124. if (header == null) throw new ArgumentNullException(nameof(header));
  125. lock (_Headers)
  126. {
  127. _Headers.Add(header.Name, header);
  128. }
  129. }
  130. #region Remove Overloads
  131. /// <summary>
  132. /// Removes the specified header instance from the collection.
  133. /// </summary>
  134. /// <param name="header">The <see cref="CustomHttpHeader"/> instance to remove from the collection.</param>
  135. /// <remarks>
  136. /// <para>Only removes the specified header if that instance was in the collection, if another header with the same name exists in the collection it is not removed.</para>
  137. /// </remarks>
  138. /// <returns>True if an item was removed from the collection, otherwise false (because it did not exist or was not the same instance).</returns>
  139. /// <seealso cref="Remove(string)"/>
  140. /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="header"/> is null.</exception>
  141. public bool Remove(CustomHttpHeader header)
  142. {
  143. if (header == null) throw new ArgumentNullException(nameof(header));
  144. lock (_Headers)
  145. {
  146. if (_Headers.ContainsKey(header.Name) && _Headers[header.Name] == header)
  147. return _Headers.Remove(header.Name);
  148. }
  149. return false;
  150. }
  151. /// <summary>
  152. /// Removes the property with the specified key (<see cref="CustomHttpHeader.Name"/> from the collection.
  153. /// </summary>
  154. /// <param name="headerName">The name of the <see cref="CustomHttpHeader"/> instance to remove from the collection.</param>
  155. /// <returns>True if an item was removed from the collection, otherwise false (because no item exists in the collection with that key).</returns>
  156. /// <exception cref="System.ArgumentException">Thrown if the <paramref name="headerName"/> argument is null or empty string.</exception>
  157. public bool Remove(string headerName)
  158. {
  159. if (String.IsNullOrEmpty(headerName)) throw new ArgumentException("headerName cannot be null or empty.", nameof(headerName));
  160. lock (_Headers)
  161. {
  162. return _Headers.Remove(headerName);
  163. }
  164. }
  165. #endregion
  166. /// <summary>
  167. /// Returns a boolean indicating whether or not the specified <see cref="CustomHttpHeader"/> instance is in the collection.
  168. /// </summary>
  169. /// <param name="header">An <see cref="CustomHttpHeader"/> instance to check the collection for.</param>
  170. /// <returns>True if the specified instance exists in the collection, otherwise false.</returns>
  171. public bool Contains(CustomHttpHeader header)
  172. {
  173. if (header == null) throw new ArgumentNullException(nameof(header));
  174. lock (_Headers)
  175. {
  176. if (_Headers.ContainsKey(header.Name))
  177. return _Headers[header.Name] == header;
  178. }
  179. return false;
  180. }
  181. /// <summary>
  182. /// Returns a boolean indicating whether or not a <see cref="CustomHttpHeader"/> instance with the specified full name value exists in the collection.
  183. /// </summary>
  184. /// <param name="headerName">A string containing the full name of the <see cref="CustomHttpHeader"/> instance to check for.</param>
  185. /// <returns>True if an item with the specified full name exists in the collection, otherwise false.</returns>
  186. public bool Contains(string headerName)
  187. {
  188. if (String.IsNullOrEmpty(headerName)) throw new ArgumentException("headerName cannot be null or empty.", nameof(headerName));
  189. lock (_Headers)
  190. {
  191. return _Headers.ContainsKey(headerName);
  192. }
  193. }
  194. #endregion
  195. #region Public Properties
  196. /// <summary>
  197. /// Returns the number of items in the collection.
  198. /// </summary>
  199. public int Count
  200. {
  201. get { return _Headers.Count; }
  202. }
  203. /// <summary>
  204. /// Returns the <see cref="CustomHttpHeader"/> instance from the collection that has the specified <see cref="CustomHttpHeader.Name"/> value.
  205. /// </summary>
  206. /// <param name="name">The full name of the property to return.</param>
  207. /// <returns>A <see cref="CustomHttpHeader"/> instance from the collection.</returns>
  208. /// <exception cref="System.Collections.Generic.KeyNotFoundException">Thrown if no item exists in the collection with the specified <paramref name="name"/> value.</exception>
  209. public CustomHttpHeader this[string name]
  210. {
  211. get
  212. {
  213. return _Headers[name];
  214. }
  215. }
  216. #endregion
  217. #region IEnumerable Members
  218. /// <summary>
  219. /// Returns an enumerator of <see cref="CustomHttpHeader"/> instances in this collection.
  220. /// </summary>
  221. /// <returns>An enumerator of <see cref="CustomHttpHeader"/> instances in this collection.</returns>
  222. public IEnumerator<CustomHttpHeader> GetEnumerator()
  223. {
  224. lock (_Headers)
  225. {
  226. return _Headers.Values.GetEnumerator();
  227. }
  228. }
  229. /// <summary>
  230. /// Returns an enumerator of <see cref="CustomHttpHeader"/> instances in this collection.
  231. /// </summary>
  232. /// <returns>An enumerator of <see cref="CustomHttpHeader"/> instances in this collection.</returns>
  233. IEnumerator IEnumerable.GetEnumerator()
  234. {
  235. lock (_Headers)
  236. {
  237. return _Headers.Values.GetEnumerator();
  238. }
  239. }
  240. #endregion
  241. }
  242. }