fix prev patch
[mono.git] / mcs / class / System / System.Net / WebClient.cs
index a5b8d99d78ab488874e3957a23a3139846829b72..ab04d822b87ace1ea334d352a843ad4cd613b744 100644 (file)
@@ -444,7 +444,7 @@ namespace System.Net
 #if NET_2_0                            
                                async = false;
 #endif                         
-                               WebRequest request = SetupRequest (address, method);
+                               WebRequest request = SetupRequest (address, method, true);
                                return request.GetRequestStream ();
                        } catch (Exception ex) {
                                throw new WebException ("An error occurred " +
@@ -454,16 +454,16 @@ namespace System.Net
                        }
                }
 
-               private string DetermineMethod (Uri address, string method)
+               private string DetermineMethod (Uri address, string method, bool is_upload)
                {
                        if (method != null)
                                return method;
 
 #if NET_2_0
                        if (address.Scheme == Uri.UriSchemeFtp)
-                               return "RETR";
+                               return (is_upload) ? "STOR" : "RETR";
 #endif
-                       return "POST";
+                       return (is_upload) ? "POST" : "GET";
                }
 
                //   UploadData
@@ -514,6 +514,8 @@ namespace System.Net
                                async = false;
 #endif                         
                                return UploadDataCore (address, method, data, null);
+                       } catch (WebException) {
+                               throw;
                        } catch (Exception ex) {
                                throw new WebException ("An error occurred " +
                                        "performing a WebClient request.", ex);
@@ -531,7 +533,7 @@ namespace System.Net
                                throw new ArgumentNullException ("data");
 #endif
 
-                       WebRequest request = SetupRequest (address, method);
+                       WebRequest request = SetupRequest (address, method, true);
                        try {
                                int contentLength = data.Length;
                                request.ContentLength = contentLength;
@@ -628,7 +630,7 @@ namespace System.Net
                        WebRequest request = null;
                        try {
                                fStream = File.OpenRead (fileName);
-                               request = SetupRequest (address, method);
+                               request = SetupRequest (address, method, true);
                                reqStream = request.GetRequestStream ();
                                byte [] realBoundary = Encoding.ASCII.GetBytes ("--" + boundary + "\r\n");
                                reqStream.Write (realBoundary, 0, realBoundary.Length);
@@ -734,7 +736,7 @@ namespace System.Net
                                                        "value for this request.");
 
                        Headers ["Content-Type"] = urlEncodedCType;
-                       WebRequest request = SetupRequest (uri, method);
+                       WebRequest request = SetupRequest (uri, method, true);
                        try {
                                Stream rqStream = request.GetRequestStream ();
                                MemoryStream tmpStream = new MemoryStream ();
@@ -768,12 +770,18 @@ namespace System.Net
 #if NET_2_0
                public string DownloadString (string address)
                {
-                       return encoding.GetString (DownloadData (address));
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
+
+                       return encoding.GetString (DownloadData (CreateUri (address)));
                }
 
                public string DownloadString (Uri address)
                {
-                       return encoding.GetString (DownloadData (address));
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
+
+                       return encoding.GetString (DownloadData (CreateUri (address)));
                }
 
                public string UploadString (string address, string data)
@@ -847,23 +855,51 @@ namespace System.Net
 #endif
                }
 
-               Uri MakeUri (string path)
+#if NET_2_0
+               Uri CreateUri (Uri address)
                {
-                       string query = null;
-                       if (queryString != null && queryString.Count != 0) {
-                               // This is not the same as UploadValues, because these 'keys' are not
-                               // urlencoded here.
-                               StringBuilder sb = new StringBuilder ();
+                       string query = address.Query;
+                       if (String.IsNullOrEmpty (query))
+                               query = GetQueryString (true);
+
+                       if (baseAddress == null && query == null)
+                               return address;
+
+                       if (baseAddress == null)
+                               return new Uri (address.ToString () + query, (query != null));
+
+                       if (query == null)
+                               return new Uri (baseAddress, address.ToString ());
+
+                       return new Uri (baseAddress, address.ToString () + query, (query != null));
+
+               }
+#endif
+
+               string GetQueryString (bool add_qmark)
+               {
+                       if (queryString == null || queryString.Count == 0)
+                               return null;
+
+                       StringBuilder sb = new StringBuilder ();
+                       if (add_qmark)
                                sb.Append ('?');
-                               foreach (string key in queryString)
-                                       sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
 
-                               if (sb.Length != 0) {
-                                       sb.Length--; // remove trailing '&'
-                                       query = sb.ToString ();
-                               }
-                       }
+                       foreach (string key in queryString)
+                               sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
+
+                       if (sb.Length != 0)
+                               sb.Length--; // removes last '&' or the '?' if empty.
 
+                       if (sb.Length == 0)
+                               return null;
+
+                       return sb.ToString ();
+               }
+
+               Uri MakeUri (string path)
+               {
+                       string query = GetQueryString (true);
                        if (baseAddress == null && query == null) {
                                try {
                                        return new Uri (path);
@@ -937,10 +973,10 @@ namespace System.Net
                        return request;
                }
 
-               WebRequest SetupRequest (Uri uri, string method)
+               WebRequest SetupRequest (Uri uri, string method, bool is_upload)
                {
                        WebRequest request = SetupRequest (uri);
-                       request.Method = DetermineMethod (uri, method);
+                       request.Method = DetermineMethod (uri, method, is_upload);
                        return request;
                }
 
@@ -1234,7 +1270,7 @@ namespace System.Net
                                        object [] args = (object []) state;
                                        WebRequest request = null;
                                        try {
-                                               request = SetupRequest ((Uri) args [0], (string) args [1]);
+                                               request = SetupRequest ((Uri) args [0], (string) args [1], true);
                                                Stream stream = request.GetRequestStream ();
                                                OnOpenWriteCompleted (
                                                        new OpenWriteCompletedEventArgs (stream, null, false, args [2]));