2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Policy / UrlMembershipCondition.cs
blob2098239bcb87b73000db0b68cab9cc916dc503aa
1 //
2 // System.Security.Policy.UrlMembershipCondition.cs
3 //
4 // Authors:
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2003, Ximian Inc.
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:
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.
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
36 using Mono.Security;
38 namespace System.Security.Policy {
40 [Serializable]
41 [ComVisible (true)]
42 public sealed class UrlMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
44 private readonly int version = 1;
46 private Url url;
47 private string userUrl;
49 public UrlMembershipCondition (string url)
51 if (url == null)
52 throw new ArgumentNullException ("url");
53 CheckUrl (url);
54 userUrl = url;
55 this.url = new Url (url);
58 internal UrlMembershipCondition (Url url, string userUrl)
60 // as the Url object has already been validated there's no
61 // need to restart the whole process by converting to string
62 this.url = (Url) url.Copy ();
63 this.userUrl = userUrl;
66 // properties
68 public string Url {
69 get {
70 if (userUrl == null)
71 userUrl = url.Value;
72 return userUrl;
74 set { url = new Url (value); }
77 // methods
79 public bool Check (Evidence evidence)
81 if (evidence == null)
82 return false;
84 string u = url.Value;
85 int wildcard = u.LastIndexOf ("*"); // partial match with a wildcard at the end
86 if (wildcard == -1)
87 wildcard = u.Length; // exact match
89 IEnumerator e = evidence.GetHostEnumerator ();
90 while (e.MoveNext ()) {
91 if (e.Current is Url) {
92 // note: there shouldn't be more than one Url evidence
93 if (String.Compare (u, 0, (e.Current as Url).Value, 0, wildcard,
94 true, CultureInfo.InvariantCulture) == 0) {
95 return true;
97 // but we must check for all of them!
100 return false;
103 public IMembershipCondition Copy ()
105 return new UrlMembershipCondition (url, userUrl);
108 public override bool Equals (object o)
110 UrlMembershipCondition umc = (o as UrlMembershipCondition);
111 if (o == null)
112 return false;
114 string u = url.Value;
115 int length = u.Length; // exact match
117 // partial match with a wildcard at the end
118 if (u [length - 1] == '*') {
119 length--;
120 // in this case the last / could be ommited
121 if (u [length - 1] == '/')
122 length--;
125 return (String.Compare (u, 0, umc.Url, 0, length, true, CultureInfo.InvariantCulture) == 0);
128 public void FromXml (SecurityElement e)
130 FromXml (e, null);
133 public void FromXml (SecurityElement e, PolicyLevel level)
135 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
137 string u = e.Attribute ("Url");
138 if (u != null) {
139 CheckUrl (u);
140 url = new Url (u);
141 } else {
142 url = null;
144 userUrl = u;
147 public override int GetHashCode ()
149 return url.GetHashCode ();
152 public override string ToString ()
154 return "Url - " + Url;
157 public SecurityElement ToXml ()
159 return ToXml (null);
162 public SecurityElement ToXml (PolicyLevel level)
164 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
165 SecurityElement se = MembershipConditionHelper.Element (typeof (UrlMembershipCondition), version);
166 se.AddAttribute ("Url", userUrl);
167 return se;
170 // internal stuff
172 internal void CheckUrl (string url)
174 // In .NET 1.x Url class checked the validity of the
175 // URL but that's no more the case in 2.x - but we
176 // still need the check done here
177 int protocolPos = url.IndexOf (Uri.SchemeDelimiter);
178 string u = (protocolPos < 0) ? "file://" + url : url;
180 Uri uri = new Uri (u, false, false);
181 // no * except for the "lone star" case
182 if (uri.Host.IndexOf ('*') >= 1) {
183 string msg = Locale.GetText ("Invalid * character in url");
184 throw new ArgumentException (msg, "name");