Explorar el Código

2009-02-21 Gonzalo Paniagua Javier <[email protected]>

	* FtpWebResponse.cs: return a Null stream when there's no response to
	be read.
	* FtpWebRequest.cs: provide the error returned by the server in the
	exception.
	* WebClient.cs: set the correct method when doing a FTP upload.
	Fixes bug #478451.


svn path=/trunk/mcs/; revision=127640
Gonzalo Paniagua Javier hace 17 años
padre
commit
f65e2cc9cc

+ 9 - 0
mcs/class/System/System.Net/ChangeLog

@@ -1,3 +1,12 @@
+2009-02-21 Gonzalo Paniagua Javier <[email protected]>
+
+	* FtpWebResponse.cs: return a Null stream when there's no response to
+	be read.
+	* FtpWebRequest.cs: provide the error returned by the server in the
+	exception.
+	* WebClient.cs: set the correct method when doing a FTP upload.
+	Fixes bug #478451.
+
 2009-02-19 Gonzalo Paniagua Javier <[email protected]>
 
 	* FtpWebRequest.cs: avoid errors when creating a temporary Uri to get

+ 4 - 2
mcs/class/System/System.Net/FtpWebRequest.cs

@@ -741,6 +741,7 @@ namespace System.Net
 
 		void OpenControlConnection ()
 		{
+			Exception exception = null;
 			Socket sock = null;
 			foreach (IPAddress address in hostEntry.AddressList) {
 				sock = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
@@ -755,7 +756,8 @@ namespace System.Net
 						sock.Connect (remote);
 						localEndPoint = (IPEndPoint) sock.LocalEndPoint;
 						break;
-					} catch (SocketException) {
+					} catch (SocketException exc) {
+						exception = exc;
 						sock.Close ();
 						sock = null;
 					}
@@ -764,7 +766,7 @@ namespace System.Net
 
 			// Couldn't connect to any address
 			if (sock == null)
-				throw new WebException ("Unable to connect to remote server", null,
+				throw new WebException ("Unable to connect to remote server", exception,
 						WebExceptionStatus.UnknownError, ftpResponse);
 
 			controlStream = new NetworkStream (sock);

+ 2 - 3
mcs/class/System/System.Net/FtpWebResponse.cs

@@ -135,9 +135,8 @@ namespace System.Net
 
 		public override Stream GetResponseStream ()
 		{
-			if (stream == null) {
-				throw new InvalidOperationException ();
-			}
+			if (stream == null)
+				return Stream.Null; // After a STOR we get this
 			
 			if (method != WebRequestMethods.Ftp.DownloadFile &&
 					method != WebRequestMethods.Ftp.ListDirectory)

+ 12 - 10
mcs/class/System/System.Net/WebClient.cs

@@ -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 ();
@@ -937,10 +939,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 +1236,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]));