Merge pull request #4433 from kumpera/android-fixes
[mono.git] / mcs / class / System / System.Net / HttpConnection.cs
index 38d7b91769ba6cc146cfc7effb558c06bdd657b0..b9940eb07d89585b683f2904aa1b0fb49aa84157 100644 (file)
 #if MONO_SECURITY_ALIAS
 extern alias MonoSecurity;
 #endif
-#if MONO_X509_ALIAS
-extern alias PrebuiltSystem;
-#endif
 
 #if MONO_SECURITY_ALIAS
 using MSI = MonoSecurity::Mono.Security.Interface;
 #else
 using MSI = Mono.Security.Interface;
 #endif
-#if MONO_X509_ALIAS
-using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
-#else
-using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
-#endif
 
 using System.IO;
 using System.Net.Sockets;
@@ -74,7 +66,7 @@ namespace System.Net {
                int reuses;
                bool context_bound;
                bool secure;
-               X509Certificate2 cert;
+               X509Certificate cert;
                int s_timeout = 90000; // 90k ms for first request, 15k ms from then on
                Timer timer;
                IPEndPoint local_ep;
@@ -83,7 +75,7 @@ namespace System.Net {
                X509Certificate2 client_cert;
                IMonoSslStream ssl_stream;
 
-               public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert)
+               public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
                {
                        this.sock = sock;
                        this.epl = epl;
@@ -92,9 +84,7 @@ namespace System.Net {
                        if (secure == false) {
                                stream = new NetworkStream (sock, false);
                        } else {
-                               var tlsProvider = MonoTlsProviderFactory.GetProviderInternal ();
-                               var settings = new MSI.MonoTlsSettings ();
-                               settings.RemoteCertificateValidationCallback = (t, c, ch, e) => {
+                               ssl_stream = epl.Listener.CreateSslStream (new NetworkStream (sock, false), false, (t, c, ch, e) => {
                                        if (c == null)
                                                return true;
                                        var c2 = c as X509Certificate2;
@@ -103,8 +93,7 @@ namespace System.Net {
                                        client_cert = c2;
                                        client_cert_errors = new int[] { (int)e };
                                        return true;
-                               };
-                               ssl_stream = tlsProvider.CreateSslStream (new NetworkStream (sock, false), false, settings);
+                               });
                                stream = ssl_stream.AuthenticatedStream;
                        }
                        timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
@@ -212,8 +201,11 @@ namespace System.Net {
                        // TODO: can we get this stream before reading the input?
                        if (o_stream == null) {
                                HttpListener listener = context.Listener;
-                               bool ign = (listener == null) ? true : listener.IgnoreWriteExceptions;
-                               o_stream = new ResponseStream (stream, context.Response, ign);
+                               
+                               if(listener == null)
+                                       return new ResponseStream (stream, context.Response, true);
+
+                               o_stream = new ResponseStream (stream, context.Response, listener.IgnoreWriteExceptions);
                        }
                        return o_stream;
                }
@@ -315,18 +307,25 @@ namespace System.Net {
                        int used = 0;
                        string line;
 
-                       try {
-                               line = ReadLine (buffer, position, len - position, ref used);
-                               position += used;
-                       } catch {
-                               context.ErrorMessage = "Bad request";
-                               context.ErrorStatus = 400;
-                               return true;
-                       }
+                       while (true) {
+                               if (context.HaveError)
+                                       return true;
+
+                               if (position >= len)
+                                       break;
+
+                               try {
+                                       line = ReadLine (buffer, position, len - position, ref used);
+                                       position += used;
+                               } catch {
+                                       context.ErrorMessage = "Bad request";
+                                       context.ErrorStatus = 400;
+                                       return true;
+                               }
 
-                       do {
                                if (line == null)
                                        break;
+
                                if (line == "") {
                                        if (input_state == InputState.RequestLine)
                                                continue;
@@ -347,21 +346,7 @@ namespace System.Net {
                                                return true;
                                        }
                                }
-
-                               if (context.HaveError)
-                                       return true;
-
-                               if (position >= len)
-                                       break;
-                               try {
-                                       line = ReadLine (buffer, position, len - position, ref used);
-                                       position += used;
-                               } catch {
-                                       context.ErrorMessage = "Bad request";
-                                       context.ErrorStatus = 400;
-                                       return true;
-                               }
-                       } while (line != null);
+                       }
 
                        if (used == len) {
                                ms.SetLength (0);
@@ -404,7 +389,7 @@ namespace System.Net {
                                HttpListenerResponse response = context.Response;
                                response.StatusCode = status;
                                response.ContentType = "text/html";
-                               string description = HttpListenerResponse.GetStatusDescription (status);
+                               string description = HttpListenerResponseHelper.GetStatusDescription (status);
                                string str;
                                if (msg != null)
                                        str = String.Format ("<h1>{0} ({1})</h1>", description, msg);