HttpRequestHeaders.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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, connectionclose, transferEncodingChunked;
  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.Contains ("close"))
  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);
  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 => StringComparer.OrdinalIgnoreCase.Equals (l.Value, "100-continue"));
  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. AddOrRemove ("From", value);
  134. }
  135. }
  136. public string Host {
  137. get {
  138. return GetValue<string> ("Host");
  139. }
  140. set {
  141. AddOrRemove ("Host", value);
  142. }
  143. }
  144. public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch {
  145. get {
  146. return GetValues<EntityTagHeaderValue> ("If-Match");
  147. }
  148. }
  149. public DateTimeOffset? IfModifiedSince {
  150. get {
  151. return GetValue<DateTimeOffset?> ("If-Modified-Since");
  152. }
  153. set {
  154. AddOrRemove ("If-Modified-Since", value);
  155. }
  156. }
  157. public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch {
  158. get {
  159. return GetValues<EntityTagHeaderValue> ("If-None-Match");
  160. }
  161. }
  162. public RangeConditionHeaderValue IfRange {
  163. get {
  164. return GetValue<RangeConditionHeaderValue> ("If-Range");
  165. }
  166. set {
  167. AddOrRemove ("If-Range", value);
  168. }
  169. }
  170. public DateTimeOffset? IfUnmodifiedSince {
  171. get {
  172. return GetValue<DateTimeOffset?> ("If-Unmodified-Since");
  173. }
  174. set {
  175. AddOrRemove ("If-Unmodified-Since", value);
  176. }
  177. }
  178. public int? MaxForwards {
  179. get {
  180. return GetValue<int?> ("Max-Forwards");
  181. }
  182. set {
  183. AddOrRemove ("Max-Forwards", value);
  184. }
  185. }
  186. public HttpHeaderValueCollection<NameValueHeaderValue> Pragma {
  187. get {
  188. return GetValues<NameValueHeaderValue> ("Pragma");
  189. }
  190. }
  191. public AuthenticationHeaderValue ProxyAuthorization {
  192. get {
  193. return GetValue<AuthenticationHeaderValue> ("Proxy-Authorization");
  194. }
  195. set {
  196. AddOrRemove ("Proxy-Authorization", value);
  197. }
  198. }
  199. public RangeHeaderValue Range {
  200. get {
  201. return GetValue<RangeHeaderValue> ("Range");
  202. }
  203. set {
  204. AddOrRemove ("Range", value);
  205. }
  206. }
  207. public Uri Referrer {
  208. get {
  209. return GetValue<Uri> ("Referer");
  210. }
  211. set {
  212. AddOrRemove ("Referer", value);
  213. }
  214. }
  215. public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE {
  216. get {
  217. return GetValues<TransferCodingWithQualityHeaderValue> ("TE");
  218. }
  219. }
  220. public HttpHeaderValueCollection<string> Trailer {
  221. get {
  222. return GetValues<string> ("Trailer");
  223. }
  224. }
  225. public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding {
  226. get {
  227. return GetValues<TransferCodingHeaderValue> ("Transfer-Encoding");
  228. }
  229. }
  230. public bool? TransferEncodingChunked {
  231. get {
  232. if (transferEncodingChunked.HasValue)
  233. return transferEncodingChunked;
  234. var found = TransferEncoding.Find (l => StringComparer.OrdinalIgnoreCase.Equals (l.Value, "chunked"));
  235. return found != null ? true : (bool?) null;
  236. }
  237. set {
  238. if (value == transferEncodingChunked)
  239. return;
  240. TransferEncoding.Remove (l => l.Value == "chunked");
  241. if (value == true)
  242. TransferEncoding.Add (new TransferCodingHeaderValue ("chunked"));
  243. transferEncodingChunked = value;
  244. }
  245. }
  246. public HttpHeaderValueCollection<ProductHeaderValue> Upgrade {
  247. get {
  248. return GetValues<ProductHeaderValue> ("Upgrade");
  249. }
  250. }
  251. public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent {
  252. get {
  253. return GetValues<ProductInfoHeaderValue> ("User-Agent");
  254. }
  255. }
  256. public HttpHeaderValueCollection<ViaHeaderValue> Via {
  257. get {
  258. return GetValues<ViaHeaderValue> ("Via");
  259. }
  260. }
  261. public HttpHeaderValueCollection<WarningHeaderValue> Warning {
  262. get {
  263. return GetValues<WarningHeaderValue> ("Warning");
  264. }
  265. }
  266. }
  267. }