GlobalProxySelection.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // System.Net.GlobalProxySelection
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Configuration;
  30. using System.IO;
  31. using System.Runtime.Serialization;
  32. namespace System.Net
  33. {
  34. public class GlobalProxySelection
  35. {
  36. private static IWebProxy proxy;
  37. // Constructors
  38. public GlobalProxySelection() { }
  39. // Properties
  40. static IWebProxy GetProxy ()
  41. {
  42. if (proxy != null)
  43. return proxy;
  44. lock (typeof (GlobalProxySelection)) {
  45. if (proxy != null)
  46. return proxy;
  47. object p = ConfigurationSettings.GetConfig ("system.net/defaultProxy");
  48. if (p == null)
  49. p = new EmptyWebProxy ();
  50. proxy = (IWebProxy) p;
  51. }
  52. return proxy;
  53. }
  54. public static IWebProxy Select {
  55. get { return GetProxy (); }
  56. set {
  57. if (value == null)
  58. throw new ArgumentNullException ("GlobalProxySelection.Select",
  59. "null IWebProxy not allowed. Use GetEmptyWebProxy ()");
  60. lock (typeof (GlobalProxySelection))
  61. proxy = value;
  62. }
  63. }
  64. // Methods
  65. public static IWebProxy GetEmptyWebProxy()
  66. {
  67. // must return a new one each time, as the credentials
  68. // can be set
  69. return new EmptyWebProxy ();
  70. }
  71. // Internal Classes
  72. internal class EmptyWebProxy : IWebProxy {
  73. private ICredentials credentials = null;
  74. internal EmptyWebProxy () { }
  75. public ICredentials Credentials {
  76. get { return credentials; }
  77. set { credentials = value; }
  78. }
  79. public Uri GetProxy (Uri destination)
  80. {
  81. return destination;
  82. }
  83. public bool IsBypassed (Uri host)
  84. {
  85. return true; // pass directly to host
  86. }
  87. }
  88. }
  89. }