diff --git a/DanTup.BrowserSelector/DanTup.BrowserSelector.csproj b/DanTup.BrowserSelector/DanTup.BrowserSelector.csproj index 6fa8c39..25cce2e 100644 --- a/DanTup.BrowserSelector/DanTup.BrowserSelector.csproj +++ b/DanTup.BrowserSelector/DanTup.BrowserSelector.csproj @@ -57,6 +57,7 @@ + diff --git a/DanTup.BrowserSelector/Program.cs b/DanTup.BrowserSelector/Program.cs index ef02daf..4d94b63 100644 --- a/DanTup.BrowserSelector/Program.cs +++ b/DanTup.BrowserSelector/Program.cs @@ -21,7 +21,7 @@ static void Main(string[] args) if (args == null || args.Length == 0) { - ShowHelpInfo(); + ShowHelpInfo("No arguments were passed to BrowserSelector"); return; } @@ -63,9 +63,12 @@ static void Main(string[] args) } else { - if (arg.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || arg.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || arg.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase)) + if (arg.StartsWith("http:", StringComparison.OrdinalIgnoreCase) + || arg.StartsWith("https:", StringComparison.OrdinalIgnoreCase) + || arg.StartsWith("ftp:", StringComparison.OrdinalIgnoreCase)) { - LaunchBrowser(arg, waitForClose); + var url = UrlFixes.AddMissedSlashesAfterProtocol(arg); + LaunchBrowser(url, waitForClose); } else if (arg.EndsWith(".url", StringComparison.InvariantCultureIgnoreCase) || arg.EndsWith(".website", StringComparison.InvariantCultureIgnoreCase)) { @@ -77,16 +80,17 @@ static void Main(string[] args) } else { - ShowHelpInfo(); + ShowHelpInfo("The passed argument wasn't recognized as a valid url: " + arg); return; } } } } - static void ShowHelpInfo() + static void ShowHelpInfo(string noArgumentReasonDescription = null) { - MessageBox.Show(@"Usage: + var preMessage = string.IsNullOrEmpty(noArgumentReasonDescription) ? null : noArgumentReasonDescription + "\r\n\r\n"; + MessageBox.Show(preMessage + @"Usage: BrowserSelector.exe --register Register as web browser diff --git a/DanTup.BrowserSelector/UrlFixes.cs b/DanTup.BrowserSelector/UrlFixes.cs new file mode 100644 index 0000000..f5ee498 --- /dev/null +++ b/DanTup.BrowserSelector/UrlFixes.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DanTup.BrowserSelector +{ + public static class UrlFixes + { + public static string AddMissedSlashesAfterProtocol(string url) + { + var protocolEndPos = url.IndexOf(':'); + if (protocolEndPos == -1) + { + throw new ArgumentException($"Not found a colon ':' after protocol in url: " + url); + } + if (protocolEndPos == url.Length) + { + //The passed url contains only a protocol. Just add slashes and return the result + return url + "//"; + //throw new ArgumentException($"The passed url contains only a protocol: " + url); + } + var protocolLength = protocolEndPos + 1; + var urlAfterProtocol = url.Substring(protocolLength); + string slashesToAdd; + if (urlAfterProtocol[0] == '/') + { + if (urlAfterProtocol.Length == 1) + { + //The passed url contains only a protocol with one slash. Just add one more slash and return the result + return url + '/'; + //throw new ArgumentException($"The passed url contains only a protocol: " + url); + } + if (urlAfterProtocol[1] == '/') + { + //all slashes are present. The passed url is valid so return it as is + return url; + } + else + { + slashesToAdd = "/"; + } + } + else + { + slashesToAdd = "//"; + } + var fixedUrl = url.Substring(0, protocolLength) + slashesToAdd + urlAfterProtocol; + return fixedUrl; + } + } +}