Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DanTup.BrowserSelector/DanTup.BrowserSelector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegistrySettings.cs" />
<Compile Include="UrlFixes.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
16 changes: 10 additions & 6 deletions DanTup.BrowserSelector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void Main(string[] args)

if (args == null || args.Length == 0)
{
ShowHelpInfo();
ShowHelpInfo("No arguments were passed to BrowserSelector");
return;
}

Expand Down Expand Up @@ -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))
{
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions DanTup.BrowserSelector/UrlFixes.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}