From ee60e9cbc6dac37bac2ff1880975a6d8b0e00a14 Mon Sep 17 00:00:00 2001 From: Gregory Date: Tue, 23 Sep 2014 20:17:42 -0400 Subject: [PATCH] Fixes issue that url encodes colons when specifying a web server that doesn't run on port 80 for FTP uploads. Bug #282 --- UploadersLib/FileUploaders/FTP/FTPAccount.cs | 57 ++++++++++---------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/UploadersLib/FileUploaders/FTP/FTPAccount.cs b/UploadersLib/FileUploaders/FTP/FTPAccount.cs index 8cdadbce4..25b95b300 100644 --- a/UploadersLib/FileUploaders/FTP/FTPAccount.cs +++ b/UploadersLib/FileUploaders/FTP/FTPAccount.cs @@ -153,8 +153,6 @@ public string GetHttpHomePath() HttpHomePathAutoAddSubFolderPath = false; } - HttpHomePath = URLHelpers.RemovePrefixes(HttpHomePath); - return NameParser.Parse(NameParserType.URL, HttpHomePath.Replace("%host", Host)); } @@ -179,51 +177,54 @@ public string GetUriPath(string filename, string subFolderPath = null) subFolderPath = URLHelpers.URLPathEncode(subFolderPath); - string httpHomePath = GetHttpHomePath(); - httpHomePath = URLHelpers.URLPathEncode(httpHomePath); - - string path; - + UriBuilder httpHomeUri; + var httpHomePath = GetHttpHomePath(); + if (string.IsNullOrEmpty(httpHomePath)) { - string host = Host; + var host = Host; if (host.StartsWith("ftp.")) { host = host.Substring(4); } - path = URLHelpers.CombineURL(host, subFolderPath, filename); + httpHomeUri = new UriBuilder(URLHelpers.CombineURL(host, subFolderPath, filename)); } else { + //Parse HttpHomePath in to host, port and path components + var firstSlash = httpHomePath.IndexOf('/'); + var httpHome = firstSlash >= 0 ? httpHomePath.Substring(0, firstSlash) : httpHomePath; + var portSpecifiedAt = httpHome.LastIndexOf(':'); + + var httpHomeHost = portSpecifiedAt >= 0 ? httpHome.Substring(0, portSpecifiedAt) : httpHome; + var httpHomePort = -1; + var httpHomeDir = httpHomePath.Substring(firstSlash + 1); + + if (portSpecifiedAt >= 0) + int.TryParse(httpHome.Substring(portSpecifiedAt + 1), out httpHomePort); + + //Build URI + httpHomeUri = new UriBuilder {Host = httpHomeHost, Path = httpHomeDir}; + if (portSpecifiedAt >= 0) + httpHomeUri.Port = httpHomePort; + if (HttpHomePathAutoAddSubFolderPath) + httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, subFolderPath); + + if (httpHomeUri.Query.EndsWith("=")) { - path = URLHelpers.CombineURL(httpHomePath, subFolderPath); + httpHomeUri.Query += filename; } else { - path = httpHomePath; - } - - if (path.EndsWith("=")) - { - path += filename; - } - else - { - path = URLHelpers.CombineURL(path, filename); + httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, filename); } } - string browserProtocol = BrowserProtocol.GetDescription(); - - if (!path.StartsWith(browserProtocol)) - { - path = browserProtocol + path; - } - - return path; + httpHomeUri.Scheme = BrowserProtocol.GetDescription(); + return httpHomeUri.Uri.ToString(); } public string GetFtpPath(string filemame)