[System] Suppress a few instances of CS0618 obsolete warnings
[mono.git] / mcs / class / System / System.Net / EndPointManager.cs
index b3919d4258d3ab633bea6a09b01dbca10cbff0e1..9a103f45cd72898eaec07552c2b693cde4f9816f 100644 (file)
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-#if NET_2_0
+
 using System.Collections;
 using System.Collections.Generic;
 namespace System.Net {
        sealed class EndPointManager
        {
-               static Dictionary<IPAddress, Dictionary<int, EndPointListener>> ip_to_endpoints =
-                               new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
+               // Dictionary<IPAddress, Dictionary<int, EndPointListener>>
+               static Hashtable ip_to_endpoints = new Hashtable ();
                
                private EndPointManager ()
                {
@@ -40,7 +40,7 @@ namespace System.Net {
 
                public static void AddListener (HttpListener listener)
                {
-                       List<string> added = new List<string> ();
+                       ArrayList added = new ArrayList ();
                        try {
                                lock (ip_to_endpoints) {
                                        foreach (string prefix in listener.Prefixes) {
@@ -50,7 +50,7 @@ namespace System.Net {
                                }
                        } catch {
                                foreach (string prefix in added) {
-                                       RemovePrefixInternal (prefix, listener);
+                                       RemovePrefix (prefix, listener);
                                }
                                throw;
                        }
@@ -69,29 +69,45 @@ namespace System.Net {
                        if (lp.Path.IndexOf ('%') != -1)
                                throw new HttpListenerException (400, "Invalid path.");
 
-                       if (lp.Path.IndexOf ("//") != -1) // TODO: Code?
+                       if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
                                throw new HttpListenerException (400, "Invalid path.");
 
-                       // Always listens on all the interfaces, no matter the host name/ip used.
-                       EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
+                       // listens on all the interfaces if host name cannot be parsed by IPAddress.
+                       EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
                        epl.AddPrefix (lp, listener);
                }
 
-               static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
+               static EndPointListener GetEPListener (string host, int port, HttpListener listener, bool secure)
                {
-                       Dictionary<int, EndPointListener> p = null;
+                       IPAddress addr;
+                       if (host == "*")
+                               addr = IPAddress.Any;
+                       else if (IPAddress.TryParse(host, out addr) == false){
+                               try {
+#pragma warning disable 618
+                                       IPHostEntry iphost = Dns.GetHostByName(host);
+#pragma warning restore 618
+                                       if (iphost != null)
+                                               addr = iphost.AddressList[0];
+                                       else
+                                               addr = IPAddress.Any;
+                               } catch {
+                                       addr = IPAddress.Any;
+                               } 
+                       }
+                       Hashtable p = null;  // Dictionary<int, EndPointListener>
                        if (ip_to_endpoints.ContainsKey (addr)) {
-                               p = ip_to_endpoints [addr];
+                               p = (Hashtable) ip_to_endpoints [addr];
                        } else {
-                               p = new Dictionary<int, EndPointListener> ();
+                               p = new Hashtable ();
                                ip_to_endpoints [addr] = p;
                        }
 
                        EndPointListener epl = null;
                        if (p.ContainsKey (port)) {
-                               epl = p [port];
+                               epl = (EndPointListener) p [port];
                        } else {
-                               epl = new EndPointListener (addr, port, secure);
+                               epl = new EndPointListener (listener, addr, port, secure);
                                p [port] = epl;
                        }
 
@@ -101,9 +117,13 @@ namespace System.Net {
                public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
                {
                        lock (ip_to_endpoints) {
-                               Dictionary<int, EndPointListener> p = null;
-                               p = ip_to_endpoints [ep.Address];
+                               // Dictionary<int, EndPointListener> p
+                               Hashtable p = null;
+                               p = (Hashtable) ip_to_endpoints [ep.Address];
                                p.Remove (ep.Port);
+                               if (p.Count == 0) {
+                                       ip_to_endpoints.Remove (ep.Address);
+                               }
                                epl.Close ();
                        }
                }
@@ -130,13 +150,12 @@ namespace System.Net {
                        if (lp.Path.IndexOf ('%') != -1)
                                return;
 
-                       if (lp.Path.IndexOf ("//") != -1)
+                       if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
                                return;
 
-                       EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
+                       EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
                        epl.RemovePrefix (lp, listener);
                }
        }
 }
-#endif