Merge pull request #2778 from xmcclure/checked-build-updates-2
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / HttpRequestHeaders.cs
index 8fed95d80839785f936e21c843e51e3b2e69be85..4f6ceeef8e96102c57d94a4f3714ec3ea9fd6b3c 100644 (file)
@@ -32,28 +32,83 @@ namespace System.Net.Http.Headers
 {
        public sealed class HttpRequestHeaders : HttpHeaders
        {
+               bool? expectContinue;
+
                internal HttpRequestHeaders ()
+                       : base (HttpHeaderKind.Request)
                {
                }
 
-               //public HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue> Accept { get; }
-               //public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptCharset { get; }
-               //public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptEncoding { get; }
-               //public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptLanguage { get; }
-               //public AuthenticationHeaderValue Authorization { get; set; }
-               //public CacheControlHeaderValue CacheControl { get; set; }
+               public HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue> Accept {
+                       get {
+                               return GetValues<MediaTypeWithQualityHeaderValue> ("Accept");
+                       }
+               }
+
+               public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptCharset {
+                       get {
+                               return GetValues<StringWithQualityHeaderValue> ("Accept-Charset");
+                       }
+               }
+
+               public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptEncoding {
+                       get {
+                               return GetValues<StringWithQualityHeaderValue> ("Accept-Encoding");
+                       }
+               }
+
+               public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptLanguage {
+                       get {
+                               return GetValues<StringWithQualityHeaderValue> ("Accept-Language");
+                       }
+               }
+
+               public AuthenticationHeaderValue Authorization {
+                       get {
+                               return GetValue<AuthenticationHeaderValue> ("Authorization");
+                       }
+                       set {
+                               AddOrRemove ("Authorization", value);
+                       }
+               }
+
+               public CacheControlHeaderValue CacheControl {
+                       get {
+                               return GetValue<CacheControlHeaderValue> ("Cache-Control");
+                       }
+                       set {
+                               AddOrRemove ("Cache-Control", value);
+                       }
+               }
+
                public HttpHeaderValueCollection<string> Connection {
                        get {
-                               return Connection.GetValue<HttpHeaderValueCollection<string>> ("Connection");
+                               return GetValues<string> ("Connection");
                        }
                }
 
                public bool? ConnectionClose {
                        get {
-                               return Connection.GetValue<bool?> ("close");
+                               if (connectionclose == true || Connection.Find (l => string.Equals (l, "close", StringComparison.OrdinalIgnoreCase)) != null)
+                                       return true;
+
+                               return connectionclose;
                        }
                        set {
-                               return Connection.SetValue ("close", value);
+                               if (connectionclose == value)
+                                       return;
+
+                               Connection.Remove ("close");
+                               if (value == true)
+                                       Connection.Add ("close");
+
+                               connectionclose = value;
+                       }
+               }
+
+               internal bool ConnectionKeepAlive {
+                       get {
+                               return Connection.Find (l => string.Equals (l, "Keep-Alive", StringComparison.OrdinalIgnoreCase)) != null;
                        }
                }
 
@@ -62,17 +117,46 @@ namespace System.Net.Http.Headers
                                return GetValue<DateTimeOffset?> ("Date");
                        }
                        set {
-                               SetValue ("Date", value);
+                               AddOrRemove ("Date", value, Parser.DateTime.ToString);
+                       }
+               }
+
+               public HttpHeaderValueCollection<NameValueWithParametersHeaderValue> Expect {
+                       get {
+                               return GetValues<NameValueWithParametersHeaderValue> ("Expect");
+                       }
+               }
+
+               public bool? ExpectContinue { 
+                       get {
+                               if (expectContinue.HasValue)
+                                       return expectContinue;
+
+                               var found = TransferEncoding.Find (l => string.Equals (l.Value, "100-continue", StringComparison.OrdinalIgnoreCase));
+                               return found != null ? true : (bool?) null;
+                       }
+                       set {
+                               if (expectContinue == value)
+                                       return;
+
+                               Expect.Remove (l => l.Name == "100-continue");
+
+                               if (value == true)
+                                       Expect.Add (new NameValueWithParametersHeaderValue ("100-continue"));
+
+                               expectContinue = value;
                        }
                }
-               //public HttpHeaderValueCollection<NameValueWithParametersHeaderValue> Expect { get; }
-               //public bool? ExpectContinue { get; set; }
+
                public string From {
                        get {
                                return GetValue<string> ("From");
                        }
                        set {
-                               // TODO: error checks
+                               if (!string.IsNullOrEmpty (value) && !Parser.EmailAddress.TryParse (value, out value))
+                                       throw new FormatException ();
+
+                               AddOrRemove ("From", value);
                        }
                }
 
@@ -81,52 +165,158 @@ namespace System.Net.Http.Headers
                                return GetValue<string> ("Host");
                        }
                        set {
-                               // TODO: error checks
+                               AddOrRemove ("Host", value);
+                       }
+               }
+
+               public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch {
+                       get {
+                               return GetValues<EntityTagHeaderValue> ("If-Match");
+                       }
+               }
+
+               public DateTimeOffset? IfModifiedSince {
+                       get {
+                               return GetValue<DateTimeOffset?> ("If-Modified-Since");
+                       }
+                       set {
+                               AddOrRemove ("If-Modified-Since", value, Parser.DateTime.ToString);
+                       }
+               }
+
+               public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch {
+                       get {
+                               return GetValues<EntityTagHeaderValue> ("If-None-Match");
+                       }
+               }
+
+               public RangeConditionHeaderValue IfRange {
+                       get {
+                               return GetValue<RangeConditionHeaderValue> ("If-Range");
+                       }
+                       set {
+                               AddOrRemove ("If-Range", value);
+                       }
+               }
+
+               public DateTimeOffset? IfUnmodifiedSince {
+                       get {
+                               return GetValue<DateTimeOffset?> ("If-Unmodified-Since");
+                       }
+                       set {
+                               AddOrRemove ("If-Unmodified-Since", value, Parser.DateTime.ToString);
                        }
                }
 
-               //public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch { get; }
-               public DateTimeOffset? IfModifiedSince { get; set; }
-               //public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch { get; }
-               //public RangeConditionHeaderValue IfRange { get; set; }
-               //public DateTimeOffset? IfUnmodifiedSince { get; set; }
                public int? MaxForwards {
                        get {
                                return GetValue<int?> ("Max-Forwards");
                        }
                        set {
-                               SetValue ("Max-Forwards", value);
+                               AddOrRemove ("Max-Forwards", value);
+                       }
+               }
+
+               public HttpHeaderValueCollection<NameValueHeaderValue> Pragma {
+                       get {
+                               return GetValues<NameValueHeaderValue> ("Pragma");
+                       }
+               }
+
+               public AuthenticationHeaderValue ProxyAuthorization {
+                       get {
+                               return GetValue<AuthenticationHeaderValue> ("Proxy-Authorization");
+                       }
+                       set {
+                               AddOrRemove ("Proxy-Authorization", value);
                        }
                }
 
-               //public HttpHeaderValueCollection<NameValueHeaderValue> Pragma { get; }
-               //public AuthenticationHeaderValue ProxyAuthorization { get; set; }
-               //public RangeHeaderValue Range { get; set; }
+               public RangeHeaderValue Range {
+                       get {
+                               return GetValue<RangeHeaderValue> ("Range");
+                       }
+                       set {
+                               AddOrRemove ("Range", value);
+                       }
+               }
 
                public Uri Referrer {
                        get {
                                return GetValue<Uri> ("Referer");
                        }
                        set {
-                               SetValue ("Referer", value);
+                               AddOrRemove ("Referer", value);
                        }
                }
 
-               //public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE {
-               //    get {
-               //        return GetValue<HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue>> ("TE");
-               //    }
-               //}
+               public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE {
+                   get {
+                       return GetValues<TransferCodingWithQualityHeaderValue> ("TE");
+                   }
+               }
+
                public HttpHeaderValueCollection<string> Trailer {
                        get {
-                               return GetValue<HttpHeaderValueCollection<string>> ("Trailer");
+                               return GetValues<string> ("Trailer");
+                       }
+               }
+
+               public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding {
+                       get {
+                               return GetValues<TransferCodingHeaderValue> ("Transfer-Encoding");
+                       }
+               }
+
+               public bool? TransferEncodingChunked {
+                       get {
+                               if (transferEncodingChunked.HasValue)
+                                       return transferEncodingChunked;
+
+                               var found = TransferEncoding.Find (l => string.Equals (l.Value, "chunked", StringComparison.OrdinalIgnoreCase));
+                               return found != null ? true : (bool?) null;
+                       }
+                       set {
+                               if (value == transferEncodingChunked)
+                                       return;
+
+                               TransferEncoding.Remove (l => l.Value == "chunked");
+                               if (value == true)
+                                       TransferEncoding.Add (new TransferCodingHeaderValue ("chunked"));
+
+                               transferEncodingChunked = value;
+                       }
+               }
+
+               public HttpHeaderValueCollection<ProductHeaderValue> Upgrade {
+                       get {
+                               return GetValues<ProductHeaderValue> ("Upgrade");
+                       }
+               }
+
+               public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent {
+                       get {
+                               return GetValues<ProductInfoHeaderValue> ("User-Agent");
+                       }
+               }
+
+               public HttpHeaderValueCollection<ViaHeaderValue> Via {
+                       get {
+                               return GetValues<ViaHeaderValue> ("Via");
+                       }
+               }
+
+               public HttpHeaderValueCollection<WarningHeaderValue> Warning {
+                       get {
+                               return GetValues<WarningHeaderValue> ("Warning");
+                       }
+               }
+
+               internal void AddHeaders (HttpRequestHeaders headers)
+               {
+                       foreach (var header in headers) {
+                               TryAddWithoutValidation (header.Key, header.Value);
                        }
                }
-               //public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding { get; }
-               //public bool? TransferEncodingChunked { get; set; }
-               //public HttpHeaderValueCollection<ProductHeaderValue> Upgrade { get; }
-               //public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent { get; }
-               //public HttpHeaderValueCollection<ViaHeaderValue> Via { get; }
-               //public HttpHeaderValueCollection<WarningHeaderValue> Warning { get; }
        }
 }