EndPointManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // System.Net.EndPointManager
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. namespace System.Net {
  32. sealed class EndPointManager
  33. {
  34. static Dictionary<IPAddress, Dictionary<int, EndPointListener>> ip_to_endpoints =
  35. new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
  36. private EndPointManager ()
  37. {
  38. }
  39. public static void AddListener (HttpListener listener)
  40. {
  41. List<string> added = new List<string> ();
  42. try {
  43. lock (ip_to_endpoints) {
  44. foreach (string prefix in listener.Prefixes) {
  45. AddPrefixInternal (prefix, listener);
  46. added.Add (prefix);
  47. }
  48. }
  49. } catch {
  50. foreach (string prefix in added) {
  51. RemovePrefixInternal (prefix, listener);
  52. }
  53. throw;
  54. }
  55. }
  56. public static void AddPrefix (string prefix, HttpListener listener)
  57. {
  58. lock (ip_to_endpoints) {
  59. AddPrefixInternal (prefix, listener);
  60. }
  61. }
  62. static void AddPrefixInternal (string p, HttpListener listener)
  63. {
  64. ListenerPrefix lp = new ListenerPrefix (p);
  65. if (lp.Path.IndexOf ('%') != -1)
  66. throw new HttpListenerException (400, "Invalid path.");
  67. if (lp.Path.IndexOf ("//") != -1) // TODO: Code?
  68. throw new HttpListenerException (400, "Invalid path.");
  69. // Always listens on all the interfaces, no matter the host name/ip used.
  70. EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
  71. epl.AddPrefix (lp, listener);
  72. }
  73. static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
  74. {
  75. Dictionary<int, EndPointListener> p = null;
  76. if (ip_to_endpoints.ContainsKey (addr)) {
  77. p = ip_to_endpoints [addr];
  78. } else {
  79. p = new Dictionary<int, EndPointListener> ();
  80. ip_to_endpoints [addr] = p;
  81. }
  82. EndPointListener epl = null;
  83. if (p.ContainsKey (port)) {
  84. epl = p [port];
  85. } else {
  86. epl = new EndPointListener (addr, port, secure);
  87. p [port] = epl;
  88. }
  89. return epl;
  90. }
  91. public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
  92. {
  93. lock (ip_to_endpoints) {
  94. Dictionary<int, EndPointListener> p = null;
  95. p = ip_to_endpoints [ep.Address];
  96. p.Remove (ep.Port);
  97. epl.Close ();
  98. }
  99. }
  100. public static void RemoveListener (HttpListener listener)
  101. {
  102. lock (ip_to_endpoints) {
  103. foreach (string prefix in listener.Prefixes) {
  104. RemovePrefixInternal (prefix, listener);
  105. }
  106. }
  107. }
  108. public static void RemovePrefix (string prefix, HttpListener listener)
  109. {
  110. lock (ip_to_endpoints) {
  111. RemovePrefixInternal (prefix, listener);
  112. }
  113. }
  114. static void RemovePrefixInternal (string prefix, HttpListener listener)
  115. {
  116. ListenerPrefix lp = new ListenerPrefix (prefix);
  117. if (lp.Path.IndexOf ('%') != -1)
  118. return;
  119. if (lp.Path.IndexOf ("//") != -1)
  120. return;
  121. EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
  122. epl.RemovePrefix (lp, listener);
  123. }
  124. }
  125. }
  126. #endif