WebPermission.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Text.RegularExpressions;
  35. namespace System.Net {
  36. internal enum WebPermissionInfoType {
  37. InfoString,
  38. InfoUnexecutedRegex,
  39. InfoRegex,
  40. }
  41. internal class WebPermissionInfo {
  42. WebPermissionInfoType _type;
  43. object _info;
  44. public WebPermissionInfo (WebPermissionInfoType type, string info)
  45. {
  46. _type = type;
  47. _info = (string) info;
  48. }
  49. public WebPermissionInfo (Regex regex)
  50. {
  51. _type = WebPermissionInfoType.InfoRegex;
  52. _info = (object) regex;
  53. }
  54. public string Info {
  55. get {
  56. if (_type == WebPermissionInfoType.InfoRegex)
  57. return null;
  58. return (string) _info;
  59. }
  60. }
  61. }
  62. // (based on SocketPermission.cs - Please look there to implement missing members!)
  63. [MonoTODO ("Most private members that include functionallity are not implemented!")]
  64. [Serializable]
  65. public sealed class WebPermission : CodeAccessPermission, IUnrestrictedPermission
  66. {
  67. // Fields
  68. ArrayList m_acceptList = new ArrayList ();
  69. ArrayList m_connectList = new ArrayList ();
  70. bool m_noRestriction = false;
  71. // Constructors
  72. public WebPermission () : base ()
  73. {
  74. }
  75. public WebPermission (PermissionState state) : base ()
  76. {
  77. m_noRestriction = (state == PermissionState.Unrestricted);
  78. }
  79. public WebPermission (NetworkAccess access, string uriString) : base ()
  80. {
  81. AddPermission (access, uriString);
  82. }
  83. public WebPermission (NetworkAccess access, Regex uriRegex) : base ()
  84. {
  85. AddPermission (access, uriRegex);
  86. }
  87. // Properties
  88. public IEnumerator AcceptList {
  89. get { return m_acceptList.GetEnumerator (); }
  90. }
  91. public IEnumerator ConnectList {
  92. get { return m_connectList.GetEnumerator (); }
  93. }
  94. // Methods
  95. public void AddPermission (NetworkAccess access, string uriString)
  96. {
  97. WebPermissionInfo info = new WebPermissionInfo (WebPermissionInfoType.InfoString, uriString);
  98. AddPermission (access, info);
  99. }
  100. public void AddPermission (NetworkAccess access, Regex uriRegex)
  101. {
  102. WebPermissionInfo info = new WebPermissionInfo (uriRegex);
  103. AddPermission (access, info);
  104. }
  105. internal void AddPermission (NetworkAccess access, WebPermissionInfo info)
  106. {
  107. switch (access) {
  108. case NetworkAccess.Accept:
  109. m_acceptList.Add (info);
  110. break;
  111. case NetworkAccess.Connect:
  112. m_connectList.Add (info);
  113. break;
  114. default:
  115. string msg = Locale.GetText ("Unknown NetworkAccess value {0}.");
  116. throw new ArgumentException (String.Format (msg, access), "access");
  117. }
  118. }
  119. public override IPermission Copy ()
  120. {
  121. WebPermission permission;
  122. permission = new WebPermission (m_noRestriction ?
  123. PermissionState.Unrestricted :
  124. PermissionState.None);
  125. // as EndpointPermission's are immutable it's safe to do a shallow copy.
  126. permission.m_connectList = (ArrayList)
  127. this.m_connectList.Clone ();
  128. permission.m_acceptList = (ArrayList) this.m_acceptList.Clone ();
  129. return permission;
  130. }
  131. public override IPermission Intersect (IPermission target)
  132. {
  133. if (target == null)
  134. return null;
  135. WebPermission perm = target as WebPermission;
  136. if (perm == null)
  137. throw new ArgumentException ("Argument not of type WebPermission");
  138. if (m_noRestriction)
  139. return IntersectEmpty (perm) ? null : perm.Copy ();
  140. if (perm.m_noRestriction)
  141. return IntersectEmpty (this) ? null : this.Copy ();
  142. WebPermission newperm = new WebPermission (PermissionState.None);
  143. Intersect (this.m_connectList, perm.m_connectList, newperm.m_connectList);
  144. Intersect (this.m_acceptList, perm.m_acceptList, newperm.m_acceptList);
  145. return IntersectEmpty (newperm) ? null : newperm;
  146. }
  147. private bool IntersectEmpty (WebPermission permission)
  148. {
  149. return !permission.m_noRestriction &&
  150. (permission.m_connectList.Count == 0) &&
  151. (permission.m_acceptList.Count == 0);
  152. }
  153. [MonoTODO]
  154. private void Intersect (ArrayList list1, ArrayList list2, ArrayList result)
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. public override bool IsSubsetOf (IPermission target)
  159. {
  160. if (target == null)
  161. return (!m_noRestriction && m_connectList.Count == 0 && m_acceptList.Count == 0);
  162. WebPermission perm = target as WebPermission;
  163. if (perm == null)
  164. throw new ArgumentException ("Parameter target must be of type WebPermission");
  165. if (perm.m_noRestriction)
  166. return true;
  167. if (this.m_noRestriction)
  168. return false;
  169. if (this.m_acceptList.Count == 0 && this.m_connectList.Count == 0)
  170. return true;
  171. if (perm.m_acceptList.Count == 0 && perm.m_connectList.Count == 0)
  172. return false;
  173. return IsSubsetOf (this.m_connectList, perm.m_connectList)
  174. && IsSubsetOf (this.m_acceptList, perm.m_acceptList);
  175. }
  176. [MonoTODO]
  177. private bool IsSubsetOf (ArrayList list1, ArrayList list2)
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. public bool IsUnrestricted ()
  182. {
  183. return m_noRestriction;
  184. }
  185. public override SecurityElement ToXml ()
  186. {
  187. SecurityElement root = new SecurityElement ("IPermission");
  188. root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
  189. root.AddAttribute ("version", "1");
  190. if (m_noRestriction) {
  191. root.AddAttribute ("Unrestricted", "true");
  192. return root;
  193. }
  194. if (this.m_connectList.Count > 0)
  195. ToXml (root, "ConnectAccess", m_connectList.GetEnumerator ());
  196. if (this.m_acceptList.Count > 0)
  197. ToXml (root, "AcceptAccess", m_acceptList.GetEnumerator ());
  198. return root;
  199. }
  200. [MonoTODO]
  201. private void ToXml (SecurityElement root, string childName, IEnumerator enumerator)
  202. {
  203. throw new NotImplementedException ();
  204. }
  205. public override void FromXml (SecurityElement securityElement)
  206. {
  207. if (securityElement == null)
  208. throw new ArgumentNullException ("securityElement");
  209. // LAMESPEC: it says to throw an ArgumentNullException in this case
  210. if (securityElement.Tag != "IPermission")
  211. throw new ArgumentException ("securityElement");
  212. string unrestricted = securityElement.Attribute ("Unrestricted");
  213. if (unrestricted != null) {
  214. this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);
  215. if (this.m_noRestriction)
  216. return;
  217. }
  218. this.m_noRestriction = false;
  219. this.m_connectList = new ArrayList ();
  220. this.m_acceptList = new ArrayList ();
  221. ArrayList children = securityElement.Children;
  222. foreach (SecurityElement child in children) {
  223. if (child.Tag == "ConnectAccess")
  224. FromXml (child.Children, NetworkAccess.Connect);
  225. else if (child.Tag == "AcceptAccess")
  226. FromXml (child.Children, NetworkAccess.Accept);
  227. }
  228. }
  229. [MonoTODO]
  230. private void FromXml (ArrayList endpoints, NetworkAccess access)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. public override IPermission Union (IPermission target)
  235. {
  236. // LAMESPEC: according to spec we should throw an
  237. // exception when target is null. We'll follow the
  238. // behaviour of MS.Net instead of the spec, also
  239. // because it matches the Intersect behaviour.
  240. if (target == null)
  241. return null;
  242. // throw new ArgumentNullException ("target");
  243. WebPermission perm = target as WebPermission;
  244. if (perm == null)
  245. throw new ArgumentException ("Argument not of type WebPermission");
  246. if (this.m_noRestriction || perm.m_noRestriction)
  247. return new WebPermission (PermissionState.Unrestricted);
  248. WebPermission copy = (WebPermission) perm.Copy ();
  249. copy.m_acceptList.InsertRange (copy.m_acceptList.Count, this.m_acceptList);
  250. copy.m_connectList.InsertRange (copy.m_connectList.Count, this.m_connectList);
  251. return copy;
  252. }
  253. }
  254. }