WebPermission.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // System.Net.WebPermission.cs
  3. //
  4. // Author:
  5. // Andreas Nahr ([email protected])
  6. // (based on SocketPermission.cs)
  7. //
  8. // (C) 2003 Andreas Nahr
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Security;
  13. using System.Security.Permissions;
  14. using System.Text.RegularExpressions;
  15. namespace System.Net
  16. {
  17. // (based on SocketPermission.cs - Please look there to implement missing members!)
  18. [MonoTODO ("Most private members that include functionallity are not implemented!")]
  19. [Serializable]
  20. public sealed class WebPermission : CodeAccessPermission, IUnrestrictedPermission
  21. {
  22. // Fields
  23. ArrayList m_acceptList = new ArrayList ();
  24. ArrayList m_connectList = new ArrayList ();
  25. bool m_noRestriction = false;
  26. // Constructors
  27. public WebPermission () : base ()
  28. {
  29. }
  30. public WebPermission (PermissionState state) : base ()
  31. {
  32. m_noRestriction = (state == PermissionState.Unrestricted);
  33. }
  34. public WebPermission (NetworkAccess access, string uriString) : base ()
  35. {
  36. AddPermission (access, uriString);
  37. }
  38. public WebPermission (NetworkAccess access, Regex uriRegex) : base ()
  39. {
  40. AddPermission (access, uriRegex);
  41. }
  42. // Properties
  43. public IEnumerator AcceptList {
  44. get { return m_acceptList.GetEnumerator (); }
  45. }
  46. public IEnumerator ConnectList {
  47. get { return m_connectList.GetEnumerator (); }
  48. }
  49. // Methods
  50. [MonoTODO]
  51. public void AddPermission (NetworkAccess access, string uriString)
  52. {
  53. throw new NotImplementedException ();
  54. }
  55. [MonoTODO]
  56. public void AddPermission (NetworkAccess access, Regex uriRegex)
  57. {
  58. throw new NotImplementedException ();
  59. }
  60. public override IPermission Copy ()
  61. {
  62. WebPermission permission;
  63. permission = new WebPermission (m_noRestriction ?
  64. PermissionState.Unrestricted :
  65. PermissionState.None);
  66. // as EndpointPermission's are immutable it's safe to do a shallow copy.
  67. permission.m_connectList = (ArrayList)
  68. this.m_connectList.Clone ();
  69. permission.m_acceptList = (ArrayList) this.m_acceptList.Clone ();
  70. return permission;
  71. }
  72. public override IPermission Intersect (IPermission target)
  73. {
  74. if (target == null)
  75. return null;
  76. WebPermission perm = target as WebPermission;
  77. if (perm == null)
  78. throw new ArgumentException ("Argument not of type WebPermission");
  79. if (m_noRestriction)
  80. return IntersectEmpty (perm) ? null : perm.Copy ();
  81. if (perm.m_noRestriction)
  82. return IntersectEmpty (this) ? null : this.Copy ();
  83. WebPermission newperm = new WebPermission (PermissionState.None);
  84. Intersect (this.m_connectList, perm.m_connectList, newperm.m_connectList);
  85. Intersect (this.m_acceptList, perm.m_acceptList, newperm.m_acceptList);
  86. return IntersectEmpty (newperm) ? null : newperm;
  87. }
  88. private bool IntersectEmpty (WebPermission permission)
  89. {
  90. return !permission.m_noRestriction &&
  91. (permission.m_connectList.Count == 0) &&
  92. (permission.m_acceptList.Count == 0);
  93. }
  94. [MonoTODO]
  95. private void Intersect (ArrayList list1, ArrayList list2, ArrayList result)
  96. {
  97. throw new NotImplementedException ();
  98. }
  99. public override bool IsSubsetOf (IPermission target)
  100. {
  101. if (target == null)
  102. return (!m_noRestriction && m_connectList.Count == 0 && m_acceptList.Count == 0);
  103. WebPermission perm = target as WebPermission;
  104. if (perm == null)
  105. throw new ArgumentException ("Parameter target must be of type WebPermission");
  106. if (perm.m_noRestriction)
  107. return true;
  108. if (this.m_noRestriction)
  109. return false;
  110. if (this.m_acceptList.Count == 0 && this.m_connectList.Count == 0)
  111. return true;
  112. if (perm.m_acceptList.Count == 0 && perm.m_connectList.Count == 0)
  113. return false;
  114. return IsSubsetOf (this.m_connectList, perm.m_connectList)
  115. && IsSubsetOf (this.m_acceptList, perm.m_acceptList);
  116. }
  117. [MonoTODO]
  118. private bool IsSubsetOf (ArrayList list1, ArrayList list2)
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. public bool IsUnrestricted ()
  123. {
  124. return m_noRestriction;
  125. }
  126. public override SecurityElement ToXml ()
  127. {
  128. SecurityElement root = new SecurityElement ("IPermission");
  129. root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
  130. root.AddAttribute ("version", "1");
  131. if (m_noRestriction) {
  132. root.AddAttribute ("Unrestricted", "true");
  133. return root;
  134. }
  135. if (this.m_connectList.Count > 0)
  136. ToXml (root, "ConnectAccess", m_connectList.GetEnumerator ());
  137. if (this.m_acceptList.Count > 0)
  138. ToXml (root, "AcceptAccess", m_acceptList.GetEnumerator ());
  139. return root;
  140. }
  141. [MonoTODO]
  142. private void ToXml (SecurityElement root, string childName, IEnumerator enumerator)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. public override void FromXml (SecurityElement securityElement)
  147. {
  148. if (securityElement == null)
  149. throw new ArgumentNullException ("securityElement");
  150. // LAMESPEC: it says to throw an ArgumentNullException in this case
  151. if (securityElement.Tag != "IPermission")
  152. throw new ArgumentException ("securityElement");
  153. string classStr = securityElement.Attribute ("class");
  154. if (classStr == null || !classStr.StartsWith (this.GetType ().FullName + ","))
  155. throw new ArgumentException ("securityElement");
  156. string unrestricted = securityElement.Attribute ("Unrestricted");
  157. if (unrestricted != null) {
  158. this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);
  159. if (this.m_noRestriction)
  160. return;
  161. }
  162. this.m_noRestriction = false;
  163. this.m_connectList = new ArrayList ();
  164. this.m_acceptList = new ArrayList ();
  165. ArrayList children = securityElement.Children;
  166. foreach (SecurityElement child in children) {
  167. if (child.Tag == "ConnectAccess")
  168. FromXml (child.Children, NetworkAccess.Connect);
  169. else if (child.Tag == "AcceptAccess")
  170. FromXml (child.Children, NetworkAccess.Accept);
  171. }
  172. }
  173. [MonoTODO]
  174. private void FromXml (ArrayList endpoints, NetworkAccess access)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. public override IPermission Union (IPermission target)
  179. {
  180. // LAMESPEC: according to spec we should throw an
  181. // exception when target is null. We'll follow the
  182. // behaviour of MS.Net instead of the spec, also
  183. // because it matches the Intersect behaviour.
  184. if (target == null)
  185. return null;
  186. // throw new ArgumentNullException ("target");
  187. WebPermission perm = target as WebPermission;
  188. if (perm == null)
  189. throw new ArgumentException ("Argument not of type WebPermission");
  190. if (this.m_noRestriction || perm.m_noRestriction)
  191. return new WebPermission (PermissionState.Unrestricted);
  192. WebPermission copy = (WebPermission) perm.Copy ();
  193. copy.m_acceptList.InsertRange (copy.m_acceptList.Count, this.m_acceptList);
  194. copy.m_connectList.InsertRange (copy.m_connectList.Count, this.m_connectList);
  195. return copy;
  196. }
  197. }
  198. }