2009-02-06 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System / System.Net / FtpWebRequest.cs
index 95f5a5949c41db1878e6684955700ea5b47a3c66..c132438489af98a5458776e904a174510e0d5077 100644 (file)
@@ -580,25 +580,20 @@ namespace System.Net
                        ResolveHost ();
 
                        OpenControlConnection ();
+                       CWDAndSetFileName (requestUri);
+                       SetType ();
 
                        switch (method) {
                        // Open data connection and receive data
                        case WebRequestMethods.Ftp.DownloadFile:
-                               CWDAndSetFileName (requestUri);
-                               SetType ();
-                               DownloadData ();
-                               break;
                        case WebRequestMethods.Ftp.ListDirectory:
                        case WebRequestMethods.Ftp.ListDirectoryDetails:
-                               SetType ();
                                DownloadData ();
                                break;
                        // Open data connection and send data
                        case WebRequestMethods.Ftp.AppendFile:
                        case WebRequestMethods.Ftp.UploadFile:
                        case WebRequestMethods.Ftp.UploadFileWithUniqueName:
-                               CWDAndSetFileName (requestUri);
-                               SetType ();
                                UploadData ();
                                break;
                        // Get info from control connection
@@ -644,7 +639,7 @@ namespace System.Net
                        if (method == WebRequestMethods.Ftp.Rename)
                                method = RenameFromCommand;
                        
-                       status = SendCommand (method, requestUri.LocalPath);
+                       status = SendCommand (method, file_name);
 
                        ftpResponse.Stream = new EmptyStream ();
                        
@@ -699,8 +694,9 @@ namespace System.Net
                                        throw CreateExceptionFromResponse (status);
                                break;
                        case WebRequestMethods.Ftp.DeleteFile:
-                               if (status.StatusCode != FtpStatusCode.FileActionOK) 
+                               if (status.StatusCode != FtpStatusCode.FileActionOK)  {
                                        throw CreateExceptionFromResponse (status);
+                               }
                                break;
                        }
 
@@ -790,8 +786,14 @@ namespace System.Net
                                                WebExceptionStatus.UnknownError, null);
 
                        string msg = status.StatusDescription.Substring (4);
-                       if (msg [0] == '"')
-                               msg = msg.Substring (1, msg.Length - 2);
+                       if (msg [0] == '"') {
+                               int next_quote = msg.IndexOf ('\"', 1);
+                               if (next_quote == -1)
+                                       throw new WebException ("Error getting current directory: PWD -> " + status.StatusDescription, null,
+                                                               WebExceptionStatus.UnknownError, null);
+
+                               msg = msg.Substring (1, next_quote - 1);
+                       }
 
                        if (!msg.EndsWith ("/"))
                                msg += "/";
@@ -911,7 +913,7 @@ namespace System.Net
                        }
 
                        IPEndPoint ep = (IPEndPoint) sock.LocalEndPoint;
-                       string ipString = ep.Address.ToString ().Replace (".", ",");
+                       string ipString = ep.Address.ToString ().Replace ('.', ',');
                        int h1 = ep.Port >> 8; // ep.Port / 256
                        int h2 = ep.Port % 256;
 
@@ -932,7 +934,8 @@ namespace System.Net
                        
                        Socket s = InitDataConnection ();
 
-                       if(method != WebRequestMethods.Ftp.UploadFileWithUniqueName) {
+                       if (method != WebRequestMethods.Ftp.ListDirectory && method != WebRequestMethods.Ftp.ListDirectoryDetails &&
+                           method != WebRequestMethods.Ftp.UploadFileWithUniqueName) {
                                status = SendCommand (method, file_name);
                        } else {
                                status = SendCommand (method);