Implement some System.Net.Http.Headers
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / HttpRequestHeaders.cs
1 //
2 // HttpRequestHeaders.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
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
29 using System.Collections.Generic;
30
31 namespace System.Net.Http.Headers
32 {
33         public sealed class HttpRequestHeaders : HttpHeaders
34         {
35                 bool? expectContinue, connectionclose, transferEncodingChunked;
36
37                 internal HttpRequestHeaders ()
38                         : base (HttpHeaderKind.Request)
39                 {
40                 }
41
42                 public HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue> Accept {
43                         get {
44                                 return GetValues<MediaTypeWithQualityHeaderValue> ("Accept");
45                         }
46                 }
47
48                 public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptCharset {
49                         get {
50                                 return GetValues<StringWithQualityHeaderValue> ("Accept-Charset");
51                         }
52                 }
53
54                 public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptEncoding {
55                         get {
56                                 return GetValues<StringWithQualityHeaderValue> ("Accept-Encoding");
57                         }
58                 }
59
60                 public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptLanguage {
61                         get {
62                                 return GetValues<StringWithQualityHeaderValue> ("Accept-Language");
63                         }
64                 }
65
66                 public AuthenticationHeaderValue Authorization {
67                         get {
68                                 return GetValue<AuthenticationHeaderValue> ("Authorization");
69                         }
70                         set {
71                                 AddOrRemove ("Authorization", value);
72                         }
73                 }
74
75                 public CacheControlHeaderValue CacheControl {
76                         get {
77                                 return GetValue<CacheControlHeaderValue> ("Cache-Control");
78                         }
79                         set {
80                                 AddOrRemove ("Cache-Control", value);
81                         }
82                 }
83
84                 public HttpHeaderValueCollection<string> Connection {
85                         get {
86                                 return GetValues<string> ("Connection");
87                         }
88                 }
89
90                 public bool? ConnectionClose {
91                         get {
92                                 if (connectionclose == true || Connection.Contains ("close"))
93                                         return true;
94
95                                 return connectionclose;
96                         }
97                         set {
98                                 if (connectionclose == value)
99                                         return;
100
101                                 Connection.Remove ("close");
102                                 if (value == true)
103                                         Connection.Add ("close");
104
105                                 connectionclose = value;
106                         }
107                 }
108
109                 public DateTimeOffset? Date {
110                         get {
111                                 return GetValue<DateTimeOffset?> ("Date");
112                         }
113                         set {
114                                 AddOrRemove ("Date", value);
115                         }
116                 }
117
118                 public HttpHeaderValueCollection<NameValueWithParametersHeaderValue> Expect {
119                         get {
120                                 return GetValues<NameValueWithParametersHeaderValue> ("Expect");
121                         }
122                 }
123
124                 public bool? ExpectContinue { 
125                         get {
126                                 if (expectContinue.HasValue)
127                                         return expectContinue;
128
129                                 var found = TransferEncoding.Find (l => StringComparer.OrdinalIgnoreCase.Equals (l.Value, "100-continue"));
130                                 return found != null ? true : (bool?) null;
131                         }
132                         set {
133                                 if (expectContinue == value)
134                                         return;
135
136                                 Expect.Remove (l => l.Name == "100-continue");
137
138                                 if (value == true)
139                                         Expect.Add (new NameValueWithParametersHeaderValue ("100-continue"));
140
141                                 expectContinue = value;
142                         }
143                 }
144
145                 public string From {
146                         get {
147                                 return GetValue<string> ("From");
148                         }
149                         set {
150                                 AddOrRemove ("From", value);
151                         }
152                 }
153
154                 public string Host {
155                         get {
156                                 return GetValue<string> ("Host");
157                         }
158                         set {
159                                 AddOrRemove ("Host", value);
160                         }
161                 }
162
163                 public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch {
164                         get {
165                                 return GetValues<EntityTagHeaderValue> ("If-Match");
166                         }
167                 }
168
169                 public DateTimeOffset? IfModifiedSince {
170                         get {
171                                 return GetValue<DateTimeOffset?> ("If-Modified-Since");
172                         }
173                         set {
174                                 AddOrRemove ("If-Modified-Since", value);
175                         }
176                 }
177
178                 public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch {
179                         get {
180                                 return GetValues<EntityTagHeaderValue> ("If-None-Match");
181                         }
182                 }
183
184                 public RangeConditionHeaderValue IfRange {
185                         get
186                         {
187                                 return GetValue<RangeConditionHeaderValue> ("If-Range");
188                         }
189                         set {
190                                 AddOrRemove ("If-Range", value);
191                         }
192                 }
193
194                 public DateTimeOffset? IfUnmodifiedSince {
195                         get {
196                                 return GetValue<DateTimeOffset?> ("If-Unmodified-Since");
197                         }
198                         set {
199                                 AddOrRemove ("If-Unmodified-Since", value);
200                         }
201                 }
202
203                 public int? MaxForwards {
204                         get {
205                                 return GetValue<int?> ("Max-Forwards");
206                         }
207                         set {
208                                 AddOrRemove ("Max-Forwards", value);
209                         }
210                 }
211
212                 public HttpHeaderValueCollection<NameValueHeaderValue> Pragma {
213                         get {
214                                 return GetValues<NameValueHeaderValue> ("Pragma");
215                         }
216                 }
217
218                 public AuthenticationHeaderValue ProxyAuthorization {
219                         get {
220                                 return GetValue<AuthenticationHeaderValue> ("Proxy-Authorization");
221                         }
222                         set {
223                                 AddOrRemove ("Proxy-Authorization", value);
224                         }
225                 }
226
227                 public RangeHeaderValue Range {
228                         get {
229                                 return GetValue<RangeHeaderValue> ("Range");
230                         }
231                         set {
232                                 AddOrRemove ("Range", value);
233                         }
234                 }
235
236                 public Uri Referrer {
237                         get {
238                                 return GetValue<Uri> ("Referer");
239                         }
240                         set {
241                                 AddOrRemove ("Referer", value);
242                         }
243                 }
244
245                 public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE {
246                     get {
247                         return GetValues<TransferCodingWithQualityHeaderValue> ("TE");
248                     }
249                 }
250
251                 public HttpHeaderValueCollection<string> Trailer {
252                         get {
253                                 return GetValues<string> ("Trailer");
254                         }
255                 }
256
257                 public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding {
258                         get {
259                                 return GetValues<TransferCodingHeaderValue> ("Transfer-Encoding");
260                         }
261                 }
262
263                 public bool? TransferEncodingChunked {
264                         get {
265                                 if (transferEncodingChunked.HasValue)
266                                         return transferEncodingChunked;
267
268                                 var found = TransferEncoding.Find (l => StringComparer.OrdinalIgnoreCase.Equals (l.Value, "chunked"));
269                                 return found != null ? true : (bool?) null;
270                         }
271                         set {
272                                 if (value == transferEncodingChunked)
273                                         return;
274
275                                 TransferEncoding.Remove (l => l.Value == "chunked");
276                                 if (value == true)
277                                         TransferEncoding.Add (new TransferCodingHeaderValue ("chunked"));
278
279                                 transferEncodingChunked = value;
280                         }
281                 }
282
283                 public HttpHeaderValueCollection<ProductHeaderValue> Upgrade {
284                         get {
285                                 return GetValues<ProductHeaderValue> ("Upgrade");
286                         }
287                 }
288
289                 public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent {
290                         get {
291                                 return GetValues<ProductInfoHeaderValue> ("User-Agent");
292                         }
293                 }
294
295                 public HttpHeaderValueCollection<ViaHeaderValue> Via {
296                         get {
297                                 return GetValues<ViaHeaderValue> ("Via");
298                         }
299                 }
300
301                 public HttpHeaderValueCollection<WarningHeaderValue> Warning {
302                         get {
303                                 return GetValues<WarningHeaderValue> ("Warning");
304                         }
305                 }
306         }
307 }