2 // System.Security.Policy.Url.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
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)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
38 namespace System.Security.Policy {
42 public sealed class Url :
46 IIdentityPermissionFactory, IBuiltInEvidence {
48 private string origin_url;
50 public Url (string name)
55 internal Url (string name, bool validated)
57 origin_url = validated ? name : Prepare (name);
64 // dont re-validate the Url
65 return new Url (origin_url, true);
68 public IPermission CreateIdentityPermission (Evidence evidence)
70 return new UrlIdentityPermission (origin_url);
73 public override bool Equals (object o)
75 Url u = (o as System.Security.Policy.Url);
79 string url1 = u.Value;
80 string url2 = origin_url;
81 if (url1.IndexOf (Uri.SchemeDelimiter) < 0)
82 url1 = "file://" + url1;
83 if (url2.IndexOf (Uri.SchemeDelimiter) < 0)
84 url2 = "file://" + url2;
85 return (String.Compare (url1, url2, true, CultureInfo.InvariantCulture) == 0);
88 public override int GetHashCode ()
90 string s = origin_url;
91 if (s.IndexOf (Uri.SchemeDelimiter) < 0)
93 return s.GetHashCode ();
96 public override string ToString ()
98 SecurityElement element = new SecurityElement ("System.Security.Policy.Url");
99 element.AddAttribute ("version", "1");
100 element.AddChild (new SecurityElement ("Url", origin_url));
101 return element.ToString ();
104 public string Value {
105 get { return origin_url; }
108 // interface IBuiltInEvidence
110 int IBuiltInEvidence.GetRequiredSize (bool verbose)
112 return (verbose ? 3 : 1) + origin_url.Length;
115 [MonoTODO ("IBuiltInEvidence")]
116 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
121 [MonoTODO ("IBuiltInEvidence")]
122 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
128 private string Prepare (string url)
131 throw new ArgumentNullException ("Url");
132 if (url == String.Empty)
133 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
135 int protocolPos = url.IndexOf (Uri.SchemeDelimiter); // '://'
136 if (protocolPos > 0) {
137 if (url.StartsWith ("file://")) {
138 // convert file url into uppercase
139 url = "file://" + url.Substring (7);
141 // don't escape and don't reduce (e.g. '.' and '..')
142 Uri uri = new Uri (url, false, false);
143 url = uri.ToString ();
146 int lastpos = url.Length - 1;
147 if (url [lastpos] == '/')
148 url = url.Substring (0, lastpos);