DefaultProxyHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // System.Net.Configuration.DefaultProxyHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Configuration;
  11. using System.Xml;
  12. namespace System.Net.Configuration
  13. {
  14. class DefaultProxyHandler : IConfigurationSectionHandler
  15. {
  16. public virtual object Create (object parent, object configContext, XmlNode section)
  17. {
  18. IWebProxy result = parent as IWebProxy;
  19. if (section.Attributes != null && section.Attributes.Count != 0)
  20. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  21. XmlNodeList nodes = section.ChildNodes;
  22. foreach (XmlNode child in nodes) {
  23. XmlNodeType ntype = child.NodeType;
  24. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  25. continue;
  26. if (ntype != XmlNodeType.Element)
  27. HandlersUtil.ThrowException ("Only elements allowed", child);
  28. string name = child.Name;
  29. if (name == "proxy") {
  30. string deflt = HandlersUtil.ExtractAttributeValue ("usesystemdefault", child, true);
  31. string bypass = HandlersUtil.ExtractAttributeValue ("bypassonlocal", child, true);
  32. string address = HandlersUtil.ExtractAttributeValue ("proxyaddress", child, true);
  33. if (child.Attributes != null && child.Attributes.Count != 0) {
  34. foreach (XmlAttribute n in child.Attributes)
  35. Console.WriteLine ("att: {0}", n.Name);
  36. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  37. }
  38. result = new WebProxy ();
  39. bool bp = (bypass != null && String.Compare (bypass, "true", true) == 0);
  40. if (bp == false) {
  41. if (bypass != null && String.Compare (bypass, "false", true) != 0)
  42. HandlersUtil.ThrowException ("Invalid boolean value", child);
  43. }
  44. if (!(result is WebProxy))
  45. continue;
  46. ((WebProxy) result).BypassProxyOnLocal = bp;
  47. if (address == null)
  48. continue;
  49. try {
  50. ((WebProxy) result).Address = new Uri (address);
  51. } catch (Exception) {
  52. HandlersUtil.ThrowException ("invalid uri", child);
  53. }
  54. continue;
  55. }
  56. if (name == "bypasslist") {
  57. if (!(result is WebProxy))
  58. continue;
  59. FillByPassList (child, (WebProxy) result);
  60. continue;
  61. }
  62. if (name == "module") {
  63. Console.WriteLine ("WARNING: module not implemented yet");
  64. continue;
  65. }
  66. HandlersUtil.ThrowException ("Unexpected element", child);
  67. }
  68. return result;
  69. }
  70. static void FillByPassList (XmlNode node, WebProxy proxy)
  71. {
  72. ArrayList bypass = new ArrayList (proxy.BypassArrayList);
  73. if (node.Attributes != null && node.Attributes.Count != 0)
  74. HandlersUtil.ThrowException ("Unrecognized attribute", node);
  75. XmlNodeList nodes = node.ChildNodes;
  76. foreach (XmlNode child in nodes) {
  77. XmlNodeType ntype = child.NodeType;
  78. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  79. continue;
  80. if (ntype != XmlNodeType.Element)
  81. HandlersUtil.ThrowException ("Only elements allowed", child);
  82. string name = child.Name;
  83. if (name == "add") {
  84. string address = HandlersUtil.ExtractAttributeValue ("address", child);
  85. if (!bypass.Contains (address)) {
  86. Console.WriteLine ("added: {0}", address);
  87. bypass.Add (address);
  88. }
  89. continue;
  90. }
  91. if (name == "remove") {
  92. string address = HandlersUtil.ExtractAttributeValue ("address", child);
  93. bypass.Remove (address);
  94. Console.WriteLine ("removed: {0}", address);
  95. continue;
  96. }
  97. if (name == "clear") {
  98. if (node.Attributes != null && node.Attributes.Count != 0)
  99. HandlersUtil.ThrowException ("Unrecognized attribute", node);
  100. bypass.Clear ();
  101. Console.WriteLine ("clear");
  102. continue;
  103. }
  104. HandlersUtil.ThrowException ("Unexpected element", child);
  105. }
  106. proxy.BypassList = (string []) bypass.ToArray (typeof (string));
  107. }
  108. }
  109. }