2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 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.Security.Permissions;
34
35 using Mono.Security;
36
37 namespace System.Security.Policy {
38
39         [Serializable]
40         public sealed class Url: IIdentityPermissionFactory, IBuiltInEvidence {
41
42                 private string origin_url;
43                 
44                 public Url (string name)
45                         : this (name, false)
46                 {
47                 }
48
49                 internal Url (string name, bool validated) 
50                 {
51                         origin_url = validated ? name : Prepare (name);
52                 }
53
54                 // methods
55
56                 public object Copy ()
57                 {
58                         // dont re-validate the Url
59                         return new Url (origin_url, true);
60                 }
61
62                 public IPermission CreateIdentityPermission (Evidence evidence)
63                 {
64                         return new UrlIdentityPermission (origin_url);
65                 }
66
67                 public override bool Equals (object o)
68                 {
69                         if (o is System.Security.Policy.Url)
70                                 return (String.Compare (((Url) o).Value, Value, true, CultureInfo.InvariantCulture) == 0);
71                         return false;
72                 }
73
74                 public override int GetHashCode ()
75                 {
76                         return origin_url.GetHashCode ();
77                 }
78
79                 public override string ToString ()
80                 {
81                         SecurityElement element = new SecurityElement ("System.Security.Policy.Url");
82                         element.AddAttribute ("version", "1");
83                         element.AddChild (new SecurityElement ("Url", origin_url));
84                         return element.ToString ();
85                 }
86
87                 public string Value {
88                         get { return origin_url; }
89                 }
90
91                 // interface IBuiltInEvidence
92
93                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
94                 {
95                         return (verbose ? 3 : 1) + origin_url.Length;
96                 }
97
98                 [MonoTODO]
99                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
100                 {
101                         return 0;
102                 }
103
104                 [MonoTODO]
105                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
106                 {
107                         return 0;
108                 }
109
110                 // internal
111
112                 internal string Prepare (string url) 
113                 {
114                         if (url == null)
115                                 throw new ArgumentNullException ("Url");
116                         if (url == String.Empty)
117                                 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
118
119                         // is a protocol specified
120                         int protocolPos = url.IndexOf (Uri.SchemeDelimiter);    // '://'
121                         if (protocolPos > 0) {
122                                 if (url.StartsWith ("file://")) {
123                                         // convert file url into uppercase
124                                         url = "file://" + url.Substring (7).ToUpperInvariant ();
125                                 }
126                                 else {
127                                         // add a trailing slash if none (lonely one) is present
128                                         if (url.LastIndexOf ("/") == protocolPos + 2)
129                                                 url += "/";
130                                 }
131                         }
132                         else {
133                                 // add file scheme (default) and convert url to uppercase
134                                 url = "file://" + url.ToUpperInvariant ();
135                         }
136
137                         // don't escape and don't reduce (e.g. '.' and '..')
138                         Uri uri = new Uri (url, false, false);
139                         if ((uri.Host.IndexOf ('*') < 0) || (uri.Host.Length < 2)) // lone star case
140                                 url = uri.ToString ();
141                         else {
142                                 string msg = Locale.GetText ("Invalid * character in url");
143                                 throw new ArgumentException (msg, "name");
144                         }
145
146                         return url;
147                 }
148        }
149 }