2 // System.Net.WebPermission.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // (based on SocketPermission.cs)
8 // (C) 2003 Andreas Nahr
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.
33 using System
.Collections
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
36 using System
.Text
.RegularExpressions
;
38 namespace System
.Net
{
40 internal enum WebPermissionInfoType
{
46 internal class WebPermissionInfo
{
47 WebPermissionInfoType _type
;
50 public WebPermissionInfo (WebPermissionInfoType type
, string info
)
53 _info
= (string) info
;
56 public WebPermissionInfo (Regex regex
)
58 _type
= WebPermissionInfoType
.InfoRegex
;
59 _info
= (object) regex
;
64 if (_type
== WebPermissionInfoType
.InfoRegex
)
66 return (string) _info
;
71 // (based on SocketPermission.cs - Please look there to implement missing members!)
72 [MonoTODO ("Most private members that include functionallity are not implemented!")]
74 public sealed class WebPermission
: CodeAccessPermission
, IUnrestrictedPermission
77 ArrayList m_acceptList
= new ArrayList ();
78 ArrayList m_connectList
= new ArrayList ();
79 bool m_noRestriction
= false;
82 public WebPermission () : base ()
86 public WebPermission (PermissionState state
) : base ()
88 m_noRestriction
= (state
== PermissionState
.Unrestricted
);
91 public WebPermission (NetworkAccess access
, string uriString
) : base ()
93 AddPermission (access
, uriString
);
96 public WebPermission (NetworkAccess access
, Regex uriRegex
) : base ()
98 AddPermission (access
, uriRegex
);
102 public IEnumerator AcceptList
{
103 get { return m_acceptList.GetEnumerator (); }
106 public IEnumerator ConnectList
{
107 get { return m_connectList.GetEnumerator (); }
112 public void AddPermission (NetworkAccess access
, string uriString
)
114 WebPermissionInfo info
= new WebPermissionInfo (WebPermissionInfoType
.InfoString
, uriString
);
115 AddPermission (access
, info
);
118 public void AddPermission (NetworkAccess access
, Regex uriRegex
)
120 WebPermissionInfo info
= new WebPermissionInfo (uriRegex
);
121 AddPermission (access
, info
);
124 internal void AddPermission (NetworkAccess access
, WebPermissionInfo info
)
127 case NetworkAccess
.Accept
:
128 m_acceptList
.Add (info
);
130 case NetworkAccess
.Connect
:
131 m_connectList
.Add (info
);
134 string msg
= Locale
.GetText ("Unknown NetworkAccess value {0}.");
135 throw new ArgumentException (String
.Format (msg
, access
), "access");
139 public override IPermission
Copy ()
141 WebPermission permission
;
142 permission
= new WebPermission (m_noRestriction
?
143 PermissionState
.Unrestricted
:
144 PermissionState
.None
);
146 // as EndpointPermission's are immutable it's safe to do a shallow copy.
147 permission
.m_connectList
= (ArrayList
)
148 this.m_connectList
.Clone ();
149 permission
.m_acceptList
= (ArrayList
) this.m_acceptList
.Clone ();
153 public override IPermission
Intersect (IPermission target
)
157 WebPermission perm
= target
as WebPermission
;
159 throw new ArgumentException ("Argument not of type WebPermission");
161 return IntersectEmpty (perm
) ? null : perm
.Copy ();
162 if (perm
.m_noRestriction
)
163 return IntersectEmpty (this) ? null : this.Copy ();
164 WebPermission newperm
= new WebPermission (PermissionState
.None
);
165 Intersect (this.m_connectList
, perm
.m_connectList
, newperm
.m_connectList
);
166 Intersect (this.m_acceptList
, perm
.m_acceptList
, newperm
.m_acceptList
);
167 return IntersectEmpty (newperm
) ? null : newperm
;
170 private bool IntersectEmpty (WebPermission permission
)
172 return !permission
.m_noRestriction
&&
173 (permission
.m_connectList
.Count
== 0) &&
174 (permission
.m_acceptList
.Count
== 0);
178 private void Intersect (ArrayList list1
, ArrayList list2
, ArrayList result
)
180 throw new NotImplementedException ();
183 public override bool IsSubsetOf (IPermission target
)
186 return (!m_noRestriction
&& m_connectList
.Count
== 0 && m_acceptList
.Count
== 0);
187 WebPermission perm
= target
as WebPermission
;
189 throw new ArgumentException ("Parameter target must be of type WebPermission");
190 if (perm
.m_noRestriction
)
192 if (this.m_noRestriction
)
194 if (this.m_acceptList
.Count
== 0 && this.m_connectList
.Count
== 0)
196 if (perm
.m_acceptList
.Count
== 0 && perm
.m_connectList
.Count
== 0)
198 return IsSubsetOf (this.m_connectList
, perm
.m_connectList
)
199 && IsSubsetOf (this.m_acceptList
, perm
.m_acceptList
);
204 private bool IsSubsetOf (ArrayList list1
, ArrayList list2
)
206 throw new NotImplementedException ();
209 public bool IsUnrestricted ()
211 return m_noRestriction
;
214 public override SecurityElement
ToXml ()
216 SecurityElement root
= new SecurityElement ("IPermission");
217 root
.AddAttribute ("class", this.GetType ().AssemblyQualifiedName
);
218 root
.AddAttribute ("version", "1");
219 if (m_noRestriction
) {
220 root
.AddAttribute ("Unrestricted", "true");
223 if (this.m_connectList
.Count
> 0)
224 ToXml (root
, "ConnectAccess", m_connectList
.GetEnumerator ());
225 if (this.m_acceptList
.Count
> 0)
226 ToXml (root
, "AcceptAccess", m_acceptList
.GetEnumerator ());
231 private void ToXml (SecurityElement root
, string childName
, IEnumerator enumerator
)
233 throw new NotImplementedException ();
236 public override void FromXml (SecurityElement securityElement
)
238 if (securityElement
== null)
239 throw new ArgumentNullException ("securityElement");
241 // LAMESPEC: it says to throw an ArgumentNullException in this case
242 if (securityElement
.Tag
!= "IPermission")
243 throw new ArgumentException ("securityElement");
245 string unrestricted
= securityElement
.Attribute ("Unrestricted");
246 if (unrestricted
!= null) {
247 this.m_noRestriction
= (String
.Compare (unrestricted
, "true", true) == 0);
248 if (this.m_noRestriction
)
251 this.m_noRestriction
= false;
252 this.m_connectList
= new ArrayList ();
253 this.m_acceptList
= new ArrayList ();
254 ArrayList children
= securityElement
.Children
;
255 foreach (SecurityElement child
in children
) {
256 if (child
.Tag
== "ConnectAccess")
257 FromXml (child
.Children
, NetworkAccess
.Connect
);
258 else if (child
.Tag
== "AcceptAccess")
259 FromXml (child
.Children
, NetworkAccess
.Accept
);
264 private void FromXml (ArrayList endpoints
, NetworkAccess access
)
266 throw new NotImplementedException ();
269 public override IPermission
Union (IPermission target
)
271 // LAMESPEC: according to spec we should throw an
272 // exception when target is null. We'll follow the
273 // behaviour of MS.Net instead of the spec, also
274 // because it matches the Intersect behaviour.
278 // throw new ArgumentNullException ("target");
280 WebPermission perm
= target
as WebPermission
;
282 throw new ArgumentException ("Argument not of type WebPermission");
283 if (this.m_noRestriction
|| perm
.m_noRestriction
)
284 return new WebPermission (PermissionState
.Unrestricted
);
286 WebPermission copy
= (WebPermission
) perm
.Copy ();
287 copy
.m_acceptList
.InsertRange (copy
.m_acceptList
.Count
, this.m_acceptList
);
288 copy
.m_connectList
.InsertRange (copy
.m_connectList
.Count
, this.m_connectList
);