CustomHttpHeaders.cs 8.5 KB

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