fe6100de4d679214eb23e6e811e46774d2607043
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / Utility.cs
1
2
3 using System;
4 using System.Text;
5
6 namespace MonoHttp
7 {
8         class Utility
9         {
10         
11                 #region from System.Uri
12                 
13                 internal static bool MaybeUri (string s)
14                 {
15                         int p = s.IndexOf (':');
16                         if (p == -1)
17                                 return false;
18
19                         if (p >= 10)
20                                 return false;
21
22                         return IsPredefinedScheme (s.Substring (0, p));
23                 }
24                 
25                 private static bool IsPredefinedScheme (string scheme)
26                 {
27                         switch (scheme) {
28                         case "http":
29                         case "https":
30                         case "file":
31                         case "ftp":
32                         case "nntp":
33                         case "gopher":
34                         case "mailto":
35                         case "news":
36 #if NET_2_0
37                         case "net.pipe":
38                         case "net.tcp":
39 #endif
40                                 return true;
41                         default:
42                                 return false;
43                         }
44                 }
45                 
46                 #endregion
47                 
48                 #region from System.Net.Cookiie
49                 
50                 internal static string ToClientString (System.Net.Cookie cookie) 
51                 {
52                         if (cookie.Name.Length == 0) 
53                                 return String.Empty;
54
55                         StringBuilder result = new StringBuilder (64);
56         
57                         if (cookie.Version > 0) 
58                                 result.Append ("Version=").Append (cookie.Version).Append (";");
59                                 
60                         result.Append (cookie.Name).Append ("=").Append (cookie.Value);
61
62                         if (cookie.Path != null && cookie.Path.Length != 0)
63                                 result.Append (";Path=").Append (QuotedString (cookie, cookie.Path));
64                                 
65                         if (cookie.Domain != null && cookie.Domain.Length != 0)
66                                 result.Append (";Domain=").Append (QuotedString (cookie, cookie.Domain));                       
67         
68                         if (cookie.Port != null && cookie.Port.Length != 0)
69                                 result.Append (";Port=").Append (cookie.Port);  
70                                                 
71                         return result.ToString ();
72                 }
73
74                 // See par 3.6 of RFC 2616
75                 static string QuotedString (System.Net.Cookie cookie, string value)
76                 {
77                         if (cookie.Version == 0 || IsToken (value))
78                                 return value;
79                         else 
80                                 return "\"" + value.Replace("\"", "\\\"") + "\"";
81                 }                                   
82
83                 static bool IsToken (string value) 
84                 {
85                         int len = value.Length;
86                         for (int i = 0; i < len; i++) {
87                                 char c = value [i];
88                                 if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)
89                                         return false;
90                         }
91                         return true;
92                 }
93                 
94                  static string tspecials = "()<>@,;:\\\"/[]?={} \t";   // from RFC 2965, 2068
95                 
96                 #endregion
97         }
98 }