Merge pull request #2171 from lambdageek/dev/fix-marshal
[mono.git] / mcs / class / System / System.Net / FtpWebRequest.cs
index 38058974500b147cb555671c917510942cc6e7ec..bb900979a48ff614732e30717f06f15925b1981f 100644 (file)
@@ -6,7 +6,15 @@
 //
 // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
 //
-#if NET_2_0
+
+#if SECURITY_DEP
+#if MONO_SECURITY_ALIAS
+extern alias MonoSecurity;
+using MSI = MonoSecurity::Mono.Security.Interface;
+#else
+using MSI = Mono.Security.Interface;
+#endif
+#endif
 
 using System;
 using System.IO;
@@ -18,6 +26,7 @@ using System.Security.Cryptography.X509Certificates;
 using System.Net;
 using System.Net.Security;
 using System.Security.Authentication;
+using Mono.Net.Security;
 
 namespace System.Net
 {
@@ -95,6 +104,8 @@ namespace System.Net
                        WebRequestMethods.Ftp.UploadFileWithUniqueName // STUR
                        };
 
+               Encoding dataEncoding = Encoding.UTF8;
+
                internal FtpWebRequest (Uri uri) 
                {
                        this.requestUri = uri;
@@ -174,6 +185,7 @@ namespace System.Net
                        }
                }
 
+#if !NET_2_1
                [MonoTODO]
                public static new RequestCachePolicy DefaultCachePolicy
                {
@@ -184,6 +196,7 @@ namespace System.Net
                                throw GetMustImplement ();
                        }
                }
+#endif
 
                public bool EnableSsl {
                        get {
@@ -248,9 +261,6 @@ namespace System.Net
                        }
                        set {
                                CheckRequestStarted ();
-                               if (value == null)
-                                       throw new ArgumentNullException ();
-
                                proxy = value;
                        }
                }
@@ -551,7 +561,12 @@ namespace System.Net
                                if (local_path [0] == '/')
                                        local_path = local_path.Substring (1);
 
-                               Uri initial = new Uri ("ftp://dummy-host" + initial_path);
+                               UriBuilder initialBuilder = new UriBuilder () {
+                                       Scheme  = "ftp",
+                                       Host    = "dummy-host",
+                                       Path    = initial_path,
+                               };
+                               Uri initial = initialBuilder.Uri;
                                result = new Uri (initial, local_path).LocalPath;
                        }
 
@@ -790,7 +805,11 @@ namespace System.Net
 
                        Authenticate ();
                        FtpStatus status = SendCommand ("OPTS", "utf8", "on");
-                       // ignore status for OPTS
+                       if ((int)status.StatusCode < 200 || (int)status.StatusCode > 300)
+                               dataEncoding = Encoding.Default;
+                       else
+                               dataEncoding = Encoding.UTF8;
+
                        status = SendCommand (WebRequestMethods.Ftp.PrintWorkingDirectory);
                        initial_path = GetInitialPath (status);
                }
@@ -996,7 +1015,7 @@ namespace System.Net
                                }
 
                                s.Close ();
-                               origDataStream = new NetworkStream (s, true);
+                               origDataStream = new NetworkStream (incoming, true);
                                dataStream = origDataStream;
                                if (EnableSsl)
                                        ChangeToSSLSocket (ref dataStream);
@@ -1077,7 +1096,7 @@ namespace System.Net
                                commandString += " " + String.Join (" ", parameters);
 
                        commandString += EOL;
-                       cmd = Encoding.ASCII.GetBytes (commandString);
+                       cmd = dataEncoding.GetBytes (commandString);
                        try {
                                controlStream.Write (cmd, 0, cmd.Length);
                        } catch (IOException) {
@@ -1146,31 +1165,14 @@ namespace System.Net
                        ChangeToSSLSocket (ref stream);
                }
 
-#if SECURITY_DEP
-               RemoteCertificateValidationCallback callback = delegate (object sender,
-                                                                        X509Certificate certificate,
-                                                                        X509Chain chain,
-                                                                        SslPolicyErrors sslPolicyErrors) {
-                       // honor any exciting callback defined on ServicePointManager
-                       if (ServicePointManager.ServerCertificateValidationCallback != null)
-                               return ServicePointManager.ServerCertificateValidationCallback (sender, certificate, chain, sslPolicyErrors);
-                       // otherwise provide our own
-                       if (sslPolicyErrors != SslPolicyErrors.None)
-                               throw new InvalidOperationException ("SSL authentication error: " + sslPolicyErrors);
-                       return true;
-                       };
-#endif
-
                internal bool ChangeToSSLSocket (ref Stream stream) {
-#if TARGET_JVM
-                       stream.ChangeToSSLSocket ();
-                       return true;
-#elif SECURITY_DEP
-                       SslStream sslStream = new SslStream (stream, true, callback, null);
-                       //sslStream.AuthenticateAsClient (Host, this.ClientCertificates, SslProtocols.Default, false);
-                       //TODO: client certificates
+#if SECURITY_DEP
+                       var provider = MonoTlsProviderFactory.GetProviderInternal ();
+                       var settings = new MSI.MonoTlsSettings ();
+                       settings.UseServicePointManagerCallback = true;
+                       var sslStream = provider.CreateSslStream (stream, true, settings);
                        sslStream.AuthenticateAsClient (requestUri.Host, null, SslProtocols.Default, false);
-                       stream = sslStream;
+                       stream = sslStream.AuthenticatedStream;
                        return true;
 #else
                        throw new NotImplementedException ();
@@ -1197,5 +1199,4 @@ namespace System.Net
        }
 }
 
-#endif