diff options
author | Miguel de Icaza <miguel@gnome.org> | 2015-02-02 19:04:37 +0300 |
---|---|---|
committer | Rodrigo Kumpera <kumpera@gmail.com> | 2015-02-18 23:22:01 +0300 |
commit | b8f5055f4eed8daad9b24ff3ebd70a4e91a34237 (patch) | |
tree | 012da28b993388fd296ceb0085f083f95d16e116 | |
parent | de2f33f7b2d390e297e00b2ae33502ef37f79115 (diff) |
[WebClient/FtpWebRequest] Do not use MIME boundaries on FTP uploads (Fixes #26312)mono-3.12.0.77
The existing code only prevented HTTP PUTs from not having MIME-type
boundaries. Apply this also to FTP methods.
Bring a referencesource helper method that determines the default
method based on a Uri.
The existing test case only tested for the attempt to upload a file
not for the contents. Extended the code to check for a bit more
(just that the uplaod is actually 10 bytes long).
Fixes #26312
-rw-r--r-- | mcs/class/System/System.Net/WebClient.cs | 21 | ||||
-rw-r--r-- | mcs/class/System/Test/System.Net/FtpWebRequestTest.cs | 60 |
2 files changed, 76 insertions, 5 deletions
diff --git a/mcs/class/System/System.Net/WebClient.cs b/mcs/class/System/System.Net/WebClient.cs index 38415f05c5f..4c259014d01 100644 --- a/mcs/class/System/System.Net/WebClient.cs +++ b/mcs/class/System/System.Net/WebClient.cs @@ -12,6 +12,7 @@ // Copyright 2003 Ximian, Inc. (http://www.ximian.com) // Copyright 2006, 2010 Novell, Inc. (http://www.novell.com) // Copyright 2012 Xamarin Inc. (http://www.xamarin.com) +// Copyright 2014 Microsoft Inc // // // Permission is hereby granted, free of charge, to any person obtaining @@ -553,6 +554,21 @@ namespace System.Net } } + // From the Microsoft reference source + string MapToDefaultMethod(Uri address) { + Uri uri; + if (!address.IsAbsoluteUri && baseAddress != null) { + uri = new Uri(baseAddress, address); + } else { + uri = address; + } + if (uri.Scheme.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "ftp") { + return WebRequestMethods.Ftp.UploadFile; + } else { + return "POST"; + } + } + byte [] UploadFileCore (Uri address, string method, string fileName, object userToken) { string fileCType = Headers ["Content-Type"]; @@ -565,7 +581,10 @@ namespace System.Net fileCType = "application/octet-stream"; } - bool needs_boundary = (method != "PUT"); // only verified case so far + if (method == null) + method = MapToDefaultMethod (address); + + bool needs_boundary = (method != "PUT" && method != WebRequestMethods.Ftp.UploadFile); // only verified case so far string boundary = null; if (needs_boundary) { boundary = "------------" + DateTime.Now.Ticks.ToString ("x"); diff --git a/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs b/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs index 2fc05090ef4..429a8d59450 100644 --- a/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs +++ b/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs @@ -10,6 +10,7 @@ #if NET_2_0 using NUnit.Framework; using System; +using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Sockets; @@ -23,6 +24,31 @@ namespace MonoTests.System.Net { FtpWebRequest defaultRequest; + private string _tempDirectory; + private string _tempFile; + + [SetUp] + public void SetUp () + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest"); + _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp"); + if (!Directory.Exists (_tempDirectory)) { + Directory.CreateDirectory (_tempDirectory); + } else { + // ensure no files are left over from previous runs + string [] files = Directory.GetFiles (_tempDirectory, "*"); + foreach (string file in files) + File.Delete (file); + } + } + + [TearDown] + public void TearDown () + { + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + } + [TestFixtureSetUp] public void Init () { @@ -183,13 +209,15 @@ namespace MonoTests.System.Net ftp.KeepAlive = false; ftp.Timeout = 5000; ftp.Method = WebRequestMethods.Ftp.UploadFile; - ftp.ContentLength = 1; + ftp.ContentLength = 10; ftp.UseBinary = true; Stream stream = ftp.GetRequestStream (); - stream.WriteByte (0); + for (int i = 0; i < 10; i++) + stream.WriteByte ((byte)i); stream.Close (); FtpWebResponse response = (FtpWebResponse) ftp.GetResponse (); Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01"); + Assert.AreEqual (10, sp.result.Count, "UP#02"); response.Close (); } catch (Exception) { if (!String.IsNullOrEmpty (sp.Where)) @@ -201,6 +229,24 @@ namespace MonoTests.System.Net } [Test] + public void UploadFile_WebClient () + { + ServerPut sp = new ServerPut (); + File.WriteAllText (_tempFile, "0123456789"); + sp.Start (); + + using (WebClient m_WebClient = new WebClient()) + { + string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port); + + m_WebClient.UploadFile(uri, _tempFile); + } + Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01"); + + sp.Stop (); + } + + [Test] public void DownloadFile1 () { DownloadFile (new ServerDownload ()); @@ -464,6 +510,8 @@ namespace MonoTests.System.Net } class ServerPut : FtpServer { + public List<byte> result = new List<byte> (); + protected override void Run () { Socket client = control.Accept (); @@ -511,8 +559,12 @@ namespace MonoTests.System.Net writer.Flush (); Socket data_cnc = data.Accept (); - byte [] dontcare = new byte [1]; - data_cnc.Receive (dontcare, 1, SocketFlags.None); + var datastr = new NetworkStream (data_cnc, false); + int ch; + while ((ch = datastr.ReadByte ()) != -1){ + result.Add ((byte)ch); + + } data_cnc.Close (); writer.WriteLine ("226 File received Ok"); writer.Flush (); |