Merge pull request #463 from strawd/concurrent-requests
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Parser.cs
index 1cb42bdb03460da6db8f145fde54a09725699791..8489ae9bf724447a9fbb163afe9563ec281b64c2 100644 (file)
 //
 
 using System.Net.Mail;
+using System.Globalization;
+using System.Collections.Generic;
+
 namespace System.Net.Http.Headers
 {
        static class Parser
        {
                public static class Token
                {
-                       static readonly bool[] map;
+                       public static bool TryParse (string input, out string result)
+                       {
+                               if (input != null && Lexer.IsValidToken (input)) {
+                                       result = input;
+                                       return true;
+                               }
+
+                               result = null;
+                               return false;
+                       }
 
-                       static Token ()
+                       public static void Check (string s)
                        {
-                               map = new bool[127];
-                               for (int i = 0; i < map.Length; ++i)
-                                       map[i] = i > 32;
-
-                               //
-                               // separators
-                               //
-                               map['('] = false;
-                               map[')'] = false;
-                               map['|'] = false;
-                               map['<'] = false;
-                               map['>'] = false;
-                               map['@'] = false;
-                               map[','] = false;
-                               map[';'] = false;
-                               map[':'] = false;
-                               map['"'] = false;
-                               map['/'] = false;
-                               map['['] = false;
-                               map[']'] = false;
-                               map['?'] = false;
-                               map['='] = false;
-                               map['{'] = false;
-                               map['}'] = false;
+                               if (s == null)
+                                       throw new ArgumentNullException ();
+
+                               if (!Lexer.IsValidToken (s)) {
+                                       if (s.Length == 0)
+                                               throw new ArgumentException ();
+
+                                       throw new FormatException (s);
+                               }
                        }
 
-                       public static bool TryParse (string input, out string result)
+                       public static bool TryCheck (string s)
                        {
-                               throw new NotImplementedException ();
+                               if (s == null)
+                                       return false;
+
+                               return Lexer.IsValidToken (s);
                        }
 
-                       public static bool IsValid (string input)
+                       public static void CheckQuotedString (string s)
                        {
-                               //
-                               // any CHAR except CTLs or separator
-                               //
-                               for (int i = 0; i < input.Length; ++i) {
-                                       char s = input[i];
-                                       if (s > map.Length || !map[s])
-                                               return false;
-                               }
+                               if (s == null)
+                                       throw new ArgumentNullException ();
+
+                               var lexer = new Lexer (s);
+                               if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
+                                       return;
 
-                               if (input.Length == 0)
+                               if (s.Length == 0)
                                        throw new ArgumentException ();
 
-                               return true;
+                               throw new FormatException (s);
+                       }
+
+                       public static void CheckComment (string s)
+                       {
+                               if (s == null)
+                                       throw new ArgumentNullException ();
+
+                               var lexer = new Lexer (s);
+
+                               string temp;
+                               if (!lexer.ScanCommentOptional (out temp)) {
+                                       if (s.Length == 0)
+                                               throw new ArgumentException ();
+
+                                       throw new FormatException (s);
+                               }
                        }
                }
 
                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);
                        }
                }
 
@@ -109,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;
+                               }
                        }
                }
 
@@ -121,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;
                        }
                }
 
@@ -129,7 +187,21 @@ namespace System.Net.Http.Headers
                {
                        public static bool TryParse (string input, out System.Uri result)
                        {
-                               throw new NotImplementedException ();
+                               return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
+                       }
+
+                       public static void Check (string s)
+                       {
+                               if (s == null)
+                                       throw new ArgumentNullException ();
+
+                               System.Uri uri;
+                               if (!TryParse (s, out uri)) {
+                                       if (s.Length == 0)
+                                               throw new ArgumentException ();
+
+                                       throw new FormatException (s);
+                               }
                        }
                }
        }