Merge pull request #463 from strawd/concurrent-requests
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Parser.cs
index 3ab8eab7bf55ebd851ca6109bbdfca2116b084ec..8489ae9bf724447a9fbb163afe9563ec281b64c2 100644 (file)
@@ -27,6 +27,8 @@
 //
 
 using System.Net.Mail;
+using System.Globalization;
+using System.Collections.Generic;
 
 namespace System.Net.Http.Headers
 {
@@ -36,7 +38,13 @@ namespace System.Net.Http.Headers
                {
                        public static bool TryParse (string input, out string result)
                        {
-                               throw new NotImplementedException ();
+                               if (input != null && Lexer.IsValidToken (input)) {
+                                       result = input;
+                                       return true;
+                               }
+
+                               result = null;
+                               return false;
                        }
 
                        public static void Check (string s)
@@ -52,6 +60,14 @@ namespace System.Net.Http.Headers
                                }
                        }
 
+                       public static bool TryCheck (string s)
+                       {
+                               if (s == null)
+                                       return false;
+
+                               return Lexer.IsValidToken (s);
+                       }
+
                        public static void CheckQuotedString (string s)
                        {
                                if (s == null)
@@ -86,9 +102,11 @@ namespace System.Net.Http.Headers
 
                public static class DateTime
                {
+                       public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
+                       
                        public static bool TryParse (string input, out DateTimeOffset result)
                        {
-                               throw new NotImplementedException ();
+                               return Lexer.TryGetDateValue (input, out result);
                        }
                }
 
@@ -107,11 +125,46 @@ namespace System.Net.Http.Headers
                        }
                }
 
+               public static class Host
+               {
+                       public static bool TryParse (string input, out string result)
+                       {
+                               result = input;
+
+                               System.Uri dummy;
+                               return System.Uri.TryCreate ("http://u@" + input + "/", UriKind.Absolute, out dummy);
+                       }
+               }
+
                public static class Int
                {
                        public static bool TryParse (string input, out int result)
                        {
-                               throw new NotImplementedException ();
+                               return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
+                       }
+               }
+
+               public static class Long
+               {
+                       public static bool TryParse (string input, out long result)
+                       {
+                               return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
+                       }
+               }
+
+               public static class MD5
+               {
+                       public new static readonly Func<object, string> ToString = l => Convert.ToBase64String ((byte[]) l);
+
+                       public static bool TryParse (string input, out byte[] result)
+                       {
+                               try {
+                                       result = Convert.FromBase64String (input);
+                                       return true;
+                               } catch {
+                                       result = null;
+                                       return false;
+                               }
                        }
                }
 
@@ -119,7 +172,14 @@ namespace System.Net.Http.Headers
                {
                        public static bool TryParse (string input, out TimeSpan result)
                        {
-                               throw new NotImplementedException ();
+                               int value;
+                               if (Int.TryParse (input, out value)) {
+                                       result = TimeSpan.FromSeconds (value);
+                                       return true;
+                               }
+
+                               result = TimeSpan.Zero;
+                               return false;
                        }
                }
 
@@ -127,7 +187,7 @@ namespace System.Net.Http.Headers
                {
                        public static bool TryParse (string input, out System.Uri result)
                        {
-                               return System.Uri.TryCreate ("http://" + input + "/", UriKind.Absolute, out result);
+                               return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
                        }
 
                        public static void Check (string s)