GlobalProxySelection.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. volatile static IWebProxy proxy;
  37. static readonly object lockobj = new object ();
  38. // Constructors
  39. public GlobalProxySelection() { }
  40. // Properties
  41. static IWebProxy GetProxy ()
  42. {
  43. lock (lockobj) {
  44. if (proxy != null)
  45. return proxy;
  46. object p = ConfigurationSettings.GetConfig ("system.net/defaultProxy");
  47. if (p == null)
  48. p = new EmptyWebProxy ();
  49. proxy = (IWebProxy) p;
  50. }
  51. return proxy;
  52. }
  53. public static IWebProxy Select {
  54. get { return GetProxy (); }
  55. set {
  56. if (value == null)
  57. throw new ArgumentNullException ("GlobalProxySelection.Select",
  58. "null IWebProxy not allowed. Use GetEmptyWebProxy ()");
  59. proxy = value;
  60. }
  61. }
  62. // Methods
  63. public static IWebProxy GetEmptyWebProxy()
  64. {
  65. // must return a new one each time, as the credentials
  66. // can be set
  67. return new EmptyWebProxy ();
  68. }
  69. // Internal Classes
  70. internal class EmptyWebProxy : IWebProxy {
  71. private ICredentials credentials = null;
  72. internal EmptyWebProxy () { }
  73. public ICredentials Credentials {
  74. get { return credentials; }
  75. set { credentials = value; }
  76. }
  77. public Uri GetProxy (Uri destination)
  78. {
  79. return destination;
  80. }
  81. public bool IsBypassed (Uri host)
  82. {
  83. return true; // pass directly to host
  84. }
  85. }
  86. }
  87. }