* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / System.Security.Policy / Url.cs
1 //
2 // System.Security.Policy.Url.cs
3 //
4 // Author
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
35
36 using Mono.Security;
37
38 namespace System.Security.Policy {
39
40         [Serializable]
41 #if NET_2_0
42         [ComVisible (true)]
43 #endif
44         public sealed class Url: IIdentityPermissionFactory, IBuiltInEvidence {
45
46                 private string origin_url;
47                 
48                 public Url (string name)
49                         : this (name, false)
50                 {
51                 }
52
53                 internal Url (string name, bool validated) 
54                 {
55                         origin_url = validated ? name : Prepare (name);
56                 }
57
58                 // methods
59
60                 public object Copy ()
61                 {
62                         // dont re-validate the Url
63                         return new Url (origin_url, true);
64                 }
65
66                 public IPermission CreateIdentityPermission (Evidence evidence)
67                 {
68                         return new UrlIdentityPermission (origin_url);
69                 }
70
71                 public override bool Equals (object o)
72                 {
73                         Url u = (o as System.Security.Policy.Url);
74                         if (u == null)
75                                 return false;
76
77                         string url1 = u.Value;
78                         string url2 = origin_url;
79 #if NET_2_0
80                         if (url1.IndexOf (Uri.SchemeDelimiter) < 0)
81                                 url1 = "file://" + url1;
82                         if (url2.IndexOf (Uri.SchemeDelimiter) < 0)
83                                 url2 = "file://" + url2;
84 #endif
85                         return (String.Compare (url1, url2, true, CultureInfo.InvariantCulture) == 0);
86                 }
87
88                 public override int GetHashCode ()
89                 {
90                         string s = origin_url;
91 #if NET_2_0
92                         if (s.IndexOf (Uri.SchemeDelimiter) < 0)
93                                 s = "file://" + s;
94 #endif
95                         return s.GetHashCode ();
96                 }
97
98                 public override string ToString ()
99                 {
100                         SecurityElement element = new SecurityElement ("System.Security.Policy.Url");
101                         element.AddAttribute ("version", "1");
102                         element.AddChild (new SecurityElement ("Url", origin_url));
103                         return element.ToString ();
104                 }
105
106                 public string Value {
107                         get { return origin_url; }
108                 }
109
110                 // interface IBuiltInEvidence
111
112                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
113                 {
114                         return (verbose ? 3 : 1) + origin_url.Length;
115                 }
116
117                 [MonoTODO ("IBuiltInEvidence")]
118                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
119                 {
120                         return 0;
121                 }
122
123                 [MonoTODO ("IBuiltInEvidence")]
124                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
125                 {
126                         return 0;
127                 }
128
129                 // internal
130 #if NET_2_0
131                 private string Prepare (string url) 
132                 {
133                         if (url == null)
134                                 throw new ArgumentNullException ("Url");
135                         if (url == String.Empty)
136                                 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
137
138                         int protocolPos = url.IndexOf (Uri.SchemeDelimiter);    // '://'
139                         if (protocolPos > 0) {
140                                 if (url.StartsWith ("file://")) {
141                                         // convert file url into uppercase
142                                         url = "file://" + url.Substring (7);
143                                 }
144                                 // don't escape and don't reduce (e.g. '.' and '..')
145                                 Uri uri = new Uri (url, false, false);
146                                 url = uri.ToString ();
147                         }
148
149                         int lastpos = url.Length - 1;
150                         if (url [lastpos] == '/')
151                                 url = url.Substring (0, lastpos);
152
153                         return url;
154                 }
155 #else
156                 private string Prepare (string url) 
157                 {
158                         if (url == null)
159                                 throw new ArgumentNullException ("Url");
160                         if (url == String.Empty)
161                                 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
162
163                         int protocolPos = url.IndexOf (Uri.SchemeDelimiter);    // '://'
164                         if (protocolPos > 0) {
165                                 if (url.StartsWith ("file://")) {
166                                         // convert file url into uppercase
167                                         url = "file://" + url.Substring (7).ToUpperInvariant ();
168                                 } else {
169                                         // add a trailing slash if none (lonely one) is present
170                                         if (url.LastIndexOf ("/") == protocolPos + 2)
171                                                 url += "/";
172                                 }
173                         } else {
174                                 // add file scheme (default) and convert url to uppercase
175                                 url = "file://" + url.ToUpperInvariant ();
176                         }
177
178                         // don't escape and don't reduce (e.g. '.' and '..')
179                         Uri uri = new Uri (url, false, false);
180                         if ((uri.Host.IndexOf ('*') < 0) || (uri.Host.Length < 2)) // lone star case
181                                 url = uri.ToString ();
182                         else {
183                                 string msg = Locale.GetText ("Invalid * character in url");
184                                 throw new ArgumentException (msg, "name");
185                         }
186
187                         return url;
188                 }
189 #endif
190        }
191 }