HttpRequestHeaders.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // HttpRequestHeaders.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Generic;
  29. namespace System.Net.Http.Headers
  30. {
  31. public sealed class HttpRequestHeaders : HttpHeaders
  32. {
  33. bool? expectContinue;
  34. internal HttpRequestHeaders ()
  35. : base (HttpHeaderKind.Request)
  36. {
  37. }
  38. public HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue> Accept {
  39. get {
  40. return GetValues<MediaTypeWithQualityHeaderValue> ("Accept");
  41. }
  42. }
  43. public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptCharset {
  44. get {
  45. return GetValues<StringWithQualityHeaderValue> ("Accept-Charset");
  46. }
  47. }
  48. public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptEncoding {
  49. get {
  50. return GetValues<StringWithQualityHeaderValue> ("Accept-Encoding");
  51. }
  52. }
  53. public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptLanguage {
  54. get {
  55. return GetValues<StringWithQualityHeaderValue> ("Accept-Language");
  56. }
  57. }
  58. public AuthenticationHeaderValue Authorization {
  59. get {
  60. return GetValue<AuthenticationHeaderValue> ("Authorization");
  61. }
  62. set {
  63. AddOrRemove ("Authorization", value);
  64. }
  65. }
  66. public CacheControlHeaderValue CacheControl {
  67. get {
  68. return GetValue<CacheControlHeaderValue> ("Cache-Control");
  69. }
  70. set {
  71. AddOrRemove ("Cache-Control", value);
  72. }
  73. }
  74. public HttpHeaderValueCollection<string> Connection {
  75. get {
  76. return GetValues<string> ("Connection");
  77. }
  78. }
  79. public bool? ConnectionClose {
  80. get {
  81. if (connectionclose == true || Connection.Find (l => string.Equals (l, "close", StringComparison.OrdinalIgnoreCase)) != null)
  82. return true;
  83. return connectionclose;
  84. }
  85. set {
  86. if (connectionclose == value)
  87. return;
  88. Connection.Remove ("close");
  89. if (value == true)
  90. Connection.Add ("close");
  91. connectionclose = value;
  92. }
  93. }
  94. internal bool ConnectionKeepAlive {
  95. get {
  96. return Connection.Find (l => string.Equals (l, "Keep-Alive", StringComparison.OrdinalIgnoreCase)) != null;
  97. }
  98. }
  99. public DateTimeOffset? Date {
  100. get {
  101. return GetValue<DateTimeOffset?> ("Date");
  102. }
  103. set {
  104. AddOrRemove ("Date", value, Parser.DateTime.ToString);
  105. }
  106. }
  107. public HttpHeaderValueCollection<NameValueWithParametersHeaderValue> Expect {
  108. get {
  109. return GetValues<NameValueWithParametersHeaderValue> ("Expect");
  110. }
  111. }
  112. public bool? ExpectContinue {
  113. get {
  114. if (expectContinue.HasValue)
  115. return expectContinue;
  116. var found = TransferEncoding.Find (l => string.Equals (l.Value, "100-continue", StringComparison.OrdinalIgnoreCase));
  117. return found != null ? true : (bool?) null;
  118. }
  119. set {
  120. if (expectContinue == value)
  121. return;
  122. Expect.Remove (l => l.Name == "100-continue");
  123. if (value == true)
  124. Expect.Add (new NameValueWithParametersHeaderValue ("100-continue"));
  125. expectContinue = value;
  126. }
  127. }
  128. public string From {
  129. get {
  130. return GetValue<string> ("From");
  131. }
  132. set {
  133. if (!string.IsNullOrEmpty (value) && !Parser.EmailAddress.TryParse (value, out value))
  134. throw new FormatException ();
  135. AddOrRemove ("From", value);
  136. }
  137. }
  138. public string Host {
  139. get {
  140. return GetValue<string> ("Host");
  141. }
  142. set {
  143. AddOrRemove ("Host", value);
  144. }
  145. }
  146. public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch {
  147. get {
  148. return GetValues<EntityTagHeaderValue> ("If-Match");
  149. }
  150. }
  151. public DateTimeOffset? IfModifiedSince {
  152. get {
  153. return GetValue<DateTimeOffset?> ("If-Modified-Since");
  154. }
  155. set {
  156. AddOrRemove ("If-Modified-Since", value, Parser.DateTime.ToString);
  157. }
  158. }
  159. public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch {
  160. get {
  161. return GetValues<EntityTagHeaderValue> ("If-None-Match");
  162. }
  163. }
  164. public RangeConditionHeaderValue IfRange {
  165. get {
  166. return GetValue<RangeConditionHeaderValue> ("If-Range");
  167. }
  168. set {
  169. AddOrRemove ("If-Range", value);
  170. }
  171. }
  172. public DateTimeOffset? IfUnmodifiedSince {
  173. get {
  174. return GetValue<DateTimeOffset?> ("If-Unmodified-Since");
  175. }
  176. set {
  177. AddOrRemove ("If-Unmodified-Since", value, Parser.DateTime.ToString);
  178. }
  179. }
  180. public int? MaxForwards {
  181. get {
  182. return GetValue<int?> ("Max-Forwards");
  183. }
  184. set {
  185. AddOrRemove ("Max-Forwards", value);
  186. }
  187. }
  188. public HttpHeaderValueCollection<NameValueHeaderValue> Pragma {
  189. get {
  190. return GetValues<NameValueHeaderValue> ("Pragma");
  191. }
  192. }
  193. public AuthenticationHeaderValue ProxyAuthorization {
  194. get {
  195. return GetValue<AuthenticationHeaderValue> ("Proxy-Authorization");
  196. }
  197. set {
  198. AddOrRemove ("Proxy-Authorization", value);
  199. }
  200. }
  201. public RangeHeaderValue Range {
  202. get {
  203. return GetValue<RangeHeaderValue> ("Range");
  204. }
  205. set {
  206. AddOrRemove ("Range", value);
  207. }
  208. }
  209. public Uri Referrer {
  210. get {
  211. return GetValue<Uri> ("Referer");
  212. }
  213. set {
  214. AddOrRemove ("Referer", value);
  215. }
  216. }
  217. public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE {
  218. get {
  219. return GetValues<TransferCodingWithQualityHeaderValue> ("TE");
  220. }
  221. }
  222. public HttpHeaderValueCollection<string> Trailer {
  223. get {
  224. return GetValues<string> ("Trailer");
  225. }
  226. }
  227. public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding {
  228. get {
  229. return GetValues<TransferCodingHeaderValue> ("Transfer-Encoding");
  230. }
  231. }
  232. public bool? TransferEncodingChunked {
  233. get {
  234. if (transferEncodingChunked.HasValue)
  235. return transferEncodingChunked;
  236. var found = TransferEncoding.Find (l => string.Equals (l.Value, "chunked", StringComparison.OrdinalIgnoreCase));
  237. return found != null ? true : (bool?) null;
  238. }
  239. set {
  240. if (value == transferEncodingChunked)
  241. return;
  242. TransferEncoding.Remove (l => l.Value == "chunked");
  243. if (value == true)
  244. TransferEncoding.Add (new TransferCodingHeaderValue ("chunked"));
  245. transferEncodingChunked = value;
  246. }
  247. }
  248. public HttpHeaderValueCollection<ProductHeaderValue> Upgrade {
  249. get {
  250. return GetValues<ProductHeaderValue> ("Upgrade");
  251. }
  252. }
  253. public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent {
  254. get {
  255. return GetValues<ProductInfoHeaderValue> ("User-Agent");
  256. }
  257. }
  258. public HttpHeaderValueCollection<ViaHeaderValue> Via {
  259. get {
  260. return GetValues<ViaHeaderValue> ("Via");
  261. }
  262. }
  263. public HttpHeaderValueCollection<WarningHeaderValue> Warning {
  264. get {
  265. return GetValues<WarningHeaderValue> ("Warning");
  266. }
  267. }
  268. internal void AddHeaders (HttpRequestHeaders headers)
  269. {
  270. foreach (var header in headers) {
  271. TryAddWithoutValidation (header.Key, header.Value);
  272. }
  273. }
  274. }
  275. }