2 // HttpRequestHeaders.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System
.Collections
.Generic
;
31 namespace System
.Net
.Http
.Headers
33 public sealed class HttpRequestHeaders
: HttpHeaders
37 internal HttpRequestHeaders ()
38 : base (HttpHeaderKind
.Request
)
42 public HttpHeaderValueCollection
<MediaTypeWithQualityHeaderValue
> Accept
{
44 return GetValues
<MediaTypeWithQualityHeaderValue
> ("Accept");
48 public HttpHeaderValueCollection
<StringWithQualityHeaderValue
> AcceptCharset
{
50 return GetValues
<StringWithQualityHeaderValue
> ("Accept-Charset");
54 public HttpHeaderValueCollection
<StringWithQualityHeaderValue
> AcceptEncoding
{
56 return GetValues
<StringWithQualityHeaderValue
> ("Accept-Encoding");
60 public HttpHeaderValueCollection
<StringWithQualityHeaderValue
> AcceptLanguage
{
62 return GetValues
<StringWithQualityHeaderValue
> ("Accept-Language");
66 public AuthenticationHeaderValue Authorization
{
68 return GetValue
<AuthenticationHeaderValue
> ("Authorization");
71 AddOrRemove ("Authorization", value);
75 public CacheControlHeaderValue CacheControl
{
77 return GetValue
<CacheControlHeaderValue
> ("Cache-Control");
80 AddOrRemove ("Cache-Control", value);
84 public HttpHeaderValueCollection
<string> Connection
{
86 return GetValues
<string> ("Connection");
90 public bool? ConnectionClose
{
92 if (connectionclose
== true || Connection
.Find (l
=> string.Equals (l
, "close", StringComparison
.OrdinalIgnoreCase
)) != null)
95 return connectionclose
;
98 if (connectionclose
== value)
101 Connection
.Remove ("close");
103 Connection
.Add ("close");
105 connectionclose
= value;
109 internal bool ConnectionKeepAlive
{
111 return Connection
.Find (l
=> string.Equals (l
, "Keep-Alive", StringComparison
.OrdinalIgnoreCase
)) != null;
115 public DateTimeOffset
? Date
{
117 return GetValue
<DateTimeOffset
?> ("Date");
120 AddOrRemove ("Date", value, Parser
.DateTime
.ToString
);
124 public HttpHeaderValueCollection
<NameValueWithParametersHeaderValue
> Expect
{
126 return GetValues
<NameValueWithParametersHeaderValue
> ("Expect");
130 public bool? ExpectContinue
{
132 if (expectContinue
.HasValue
)
133 return expectContinue
;
135 var found
= TransferEncoding
.Find (l
=> string.Equals (l
.Value
, "100-continue", StringComparison
.OrdinalIgnoreCase
));
136 return found
!= null ? true : (bool?) null;
139 if (expectContinue
== value)
142 Expect
.Remove (l
=> l
.Name
== "100-continue");
145 Expect
.Add (new NameValueWithParametersHeaderValue ("100-continue"));
147 expectContinue
= value;
153 return GetValue
<string> ("From");
156 if (!string.IsNullOrEmpty (value) && !Parser
.EmailAddress
.TryParse (value, out value))
157 throw new FormatException ();
159 AddOrRemove ("From", value);
165 return GetValue
<string> ("Host");
168 AddOrRemove ("Host", value);
172 public HttpHeaderValueCollection
<EntityTagHeaderValue
> IfMatch
{
174 return GetValues
<EntityTagHeaderValue
> ("If-Match");
178 public DateTimeOffset
? IfModifiedSince
{
180 return GetValue
<DateTimeOffset
?> ("If-Modified-Since");
183 AddOrRemove ("If-Modified-Since", value, Parser
.DateTime
.ToString
);
187 public HttpHeaderValueCollection
<EntityTagHeaderValue
> IfNoneMatch
{
189 return GetValues
<EntityTagHeaderValue
> ("If-None-Match");
193 public RangeConditionHeaderValue IfRange
{
195 return GetValue
<RangeConditionHeaderValue
> ("If-Range");
198 AddOrRemove ("If-Range", value);
202 public DateTimeOffset
? IfUnmodifiedSince
{
204 return GetValue
<DateTimeOffset
?> ("If-Unmodified-Since");
207 AddOrRemove ("If-Unmodified-Since", value, Parser
.DateTime
.ToString
);
211 public int? MaxForwards
{
213 return GetValue
<int?> ("Max-Forwards");
216 AddOrRemove ("Max-Forwards", value);
220 public HttpHeaderValueCollection
<NameValueHeaderValue
> Pragma
{
222 return GetValues
<NameValueHeaderValue
> ("Pragma");
226 public AuthenticationHeaderValue ProxyAuthorization
{
228 return GetValue
<AuthenticationHeaderValue
> ("Proxy-Authorization");
231 AddOrRemove ("Proxy-Authorization", value);
235 public RangeHeaderValue Range
{
237 return GetValue
<RangeHeaderValue
> ("Range");
240 AddOrRemove ("Range", value);
244 public Uri Referrer
{
246 return GetValue
<Uri
> ("Referer");
249 AddOrRemove ("Referer", value);
253 public HttpHeaderValueCollection
<TransferCodingWithQualityHeaderValue
> TE
{
255 return GetValues
<TransferCodingWithQualityHeaderValue
> ("TE");
259 public HttpHeaderValueCollection
<string> Trailer
{
261 return GetValues
<string> ("Trailer");
265 public HttpHeaderValueCollection
<TransferCodingHeaderValue
> TransferEncoding
{
267 return GetValues
<TransferCodingHeaderValue
> ("Transfer-Encoding");
271 public bool? TransferEncodingChunked
{
273 if (transferEncodingChunked
.HasValue
)
274 return transferEncodingChunked
;
276 var found
= TransferEncoding
.Find (l
=> string.Equals (l
.Value
, "chunked", StringComparison
.OrdinalIgnoreCase
));
277 return found
!= null ? true : (bool?) null;
280 if (value == transferEncodingChunked
)
283 TransferEncoding
.Remove (l
=> l
.Value
== "chunked");
285 TransferEncoding
.Add (new TransferCodingHeaderValue ("chunked"));
287 transferEncodingChunked
= value;
291 public HttpHeaderValueCollection
<ProductHeaderValue
> Upgrade
{
293 return GetValues
<ProductHeaderValue
> ("Upgrade");
297 public HttpHeaderValueCollection
<ProductInfoHeaderValue
> UserAgent
{
299 return GetValues
<ProductInfoHeaderValue
> ("User-Agent");
303 public HttpHeaderValueCollection
<ViaHeaderValue
> Via
{
305 return GetValues
<ViaHeaderValue
> ("Via");
309 public HttpHeaderValueCollection
<WarningHeaderValue
> Warning
{
311 return GetValues
<WarningHeaderValue
> ("Warning");
315 internal void AddHeaders (HttpRequestHeaders headers
)
317 foreach (var header
in headers
) {
318 TryAddWithoutValidation (header
.Key
, header
.Value
);