Merge pull request #1949 from lewurm/fixtype
[mono.git] / mcs / class / System / System.Net / EndPointListener.cs
index 8fb54a4c6c5b139176620bbda6ea5995424b7127..df73d85428e37786b523f71cb071de1522a4e3e8 100644 (file)
@@ -2,9 +2,10 @@
 // System.Net.EndPointListener
 //
 // Author:
-//     Gonzalo Paniagua Javier (gonzalo@novell.com)
+//     Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
 //
 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2012 Xamarin, Inc. (http://xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0 && SECURITY_DEP
+#if SECURITY_DEP
+
+#if MONOTOUCH || MONODROID
+using Mono.Security.Authenticode;
+#else
+extern alias MonoSecurity;
+using MonoSecurity::Mono.Security.Authenticode;
+#endif
 
 using System.IO;
 using System.Net.Sockets;
 using System.Collections;
+using System.Collections.Generic;
 using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using System.Threading;
-using Mono.Security.Authenticode;
 
 namespace System.Net {
        sealed class EndPointListener
@@ -47,7 +55,7 @@ namespace System.Net {
                X509Certificate2 cert;
                AsymmetricAlgorithm key;
                bool secure;
-               Hashtable unregistered;
+               Dictionary<HttpConnection, HttpConnection> unregistered;
 
                public EndPointListener (IPAddress addr, int port, bool secure)
                {
@@ -65,7 +73,7 @@ namespace System.Net {
                        args.Completed += OnAccept;
                        sock.AcceptAsync (args);
                        prefixes = new Hashtable ();
-                       unregistered = new Hashtable ();
+                       unregistered = new Dictionary<HttpConnection, HttpConnection> ();
                }
 
                void LoadCertificateAndKey (IPAddress addr, int port)
@@ -76,7 +84,11 @@ namespace System.Net {
                                string path = Path.Combine (dirname, ".mono");
                                path = Path.Combine (path, "httplistener");
                                string cert_file = Path.Combine (path, String.Format ("{0}.cer", port));
+                               if (!File.Exists (cert_file))
+                                       return;
                                string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
+                               if (!File.Exists (pvk_file))
+                                       return;
                                cert = new X509Certificate2 (cert_file);
                                key = PrivateKey.CreateFromFile (pvk_file).RSA;
                        } catch {
@@ -156,7 +168,7 @@ namespace System.Net {
 
                        string host = uri.Host;
                        int port = uri.Port;
-                       string path = HttpUtility.UrlDecode (uri.AbsolutePath);
+                       string path = WebUtility.UrlDecode (uri.AbsolutePath);
                        string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
                        
                        HttpListener best_match = null;
@@ -271,7 +283,12 @@ namespace System.Net {
                {
                        sock.Close ();
                        lock (unregistered) {
-                               foreach (HttpConnection c in unregistered.Keys)
+                               //
+                               // Clone the list because RemoveConnection can be called from Close
+                               //
+                               var connections = new List<HttpConnection> (unregistered.Keys);
+
+                               foreach (HttpConnection c in connections)
                                        c.Close (true);
                                unregistered.Clear ();
                        }