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.Net.Mail;
30 using System.Globalization;
31 using System.Collections.Generic;
33 namespace System.Net.Http.Headers
37 public static class Token
39 public static bool TryParse (string input, out string result)
41 if (input != null && Lexer.IsValidToken (input)) {
50 public static void Check (string s)
53 throw new ArgumentNullException ();
55 if (!Lexer.IsValidToken (s)) {
57 throw new ArgumentException ();
59 throw new FormatException (s);
63 public static bool TryCheck (string s)
68 return Lexer.IsValidToken (s);
71 public static void CheckQuotedString (string s)
74 throw new ArgumentNullException ();
76 var lexer = new Lexer (s);
77 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
81 throw new ArgumentException ();
83 throw new FormatException (s);
86 public static void CheckComment (string s)
89 throw new ArgumentNullException ();
91 var lexer = new Lexer (s);
94 if (!lexer.ScanCommentOptional (out temp)) {
96 throw new ArgumentException ();
98 throw new FormatException (s);
103 public static class DateTime
105 public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
107 public static bool TryParse (string input, out DateTimeOffset result)
109 return Lexer.TryGetDateValue (input, out result);
113 public static class EmailAddress
115 public static bool TryParse (string input, out string result)
118 new MailAddress (input);
128 public static class Host
130 public static bool TryParse (string input, out string result)
135 return System.Uri.TryCreate ("http://u@" + input + "/", UriKind.Absolute, out dummy);
139 public static class Int
141 public static bool TryParse (string input, out int result)
143 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
147 public static class Long
149 public static bool TryParse (string input, out long result)
151 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
155 public static class MD5
157 public new static readonly Func<object, string> ToString = l => Convert.ToBase64String ((byte[]) l);
159 public static bool TryParse (string input, out byte[] result)
162 result = Convert.FromBase64String (input);
171 public static class TimeSpanSeconds
173 public static bool TryParse (string input, out TimeSpan result)
176 if (Int.TryParse (input, out value)) {
177 result = TimeSpan.FromSeconds (value);
181 result = TimeSpan.Zero;
186 public static class Uri
188 public static bool TryParse (string input, out System.Uri result)
190 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
193 public static void Check (string s)
196 throw new ArgumentNullException ();
199 if (!TryParse (s, out uri)) {
201 throw new ArgumentException ();
203 throw new FormatException (s);