From 50237c816718e921fb249354053773de7770ee2a Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Thu, 15 Apr 2021 18:39:55 -0700 Subject: [PATCH] Trim trailing slashes from server URL. --- Desktop.Core/Services/CasterSocket.cs | 2 +- .../ViewModels/MainWindowViewModel.cs | 22 ++++++++----------- Desktop.Win/ViewModels/MainWindowViewModel.cs | 22 ++++++++++--------- Shared/Models/DesktopAppConfig.cs | 2 +- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Desktop.Core/Services/CasterSocket.cs b/Desktop.Core/Services/CasterSocket.cs index 21c3f689..d0eb4d02 100644 --- a/Desktop.Core/Services/CasterSocket.cs +++ b/Desktop.Core/Services/CasterSocket.cs @@ -69,7 +69,7 @@ namespace Remotely.Desktop.Core.Services catch { } } Connection = new HubConnectionBuilder() - .WithUrl($"{host}/CasterHub") + .WithUrl($"{host.Trim().TrimEnd('/')}/CasterHub") .AddMessagePackProtocol() .WithAutomaticReconnect() .Build(); diff --git a/Desktop.Linux/ViewModels/MainWindowViewModel.cs b/Desktop.Linux/ViewModels/MainWindowViewModel.cs index 21db6e9a..c1727d55 100644 --- a/Desktop.Linux/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Linux/ViewModels/MainWindowViewModel.cs @@ -214,24 +214,20 @@ namespace Remotely.Desktop.Linux.ViewModels } await prompt.ShowDialog(MainWindow.Current); - var result = prompt.ViewModel.Host?.Trim(); + var result = prompt.ViewModel.Host?.Trim()?.TrimEnd('/'); - if (result is null) + if (!Uri.TryCreate(result, UriKind.Absolute, out var serverUri) || + (serverUri.Scheme != Uri.UriSchemeHttp && serverUri.Scheme != Uri.UriSchemeHttps)) { + Logger.Write("Server URL is not valid."); + await MessageBox.Show("Server URL must be a valid Uri (e.g. https://app.remotely.one).", "Invalid Server URL", MessageBoxType.OK); return; } - if (!result.StartsWith("https://") && !result.StartsWith("http://")) - { - result = $"https://{result}"; - } - if (result != Host) - { - Host = result; - var config = _configService.GetConfig(); - config.Host = Host; - _configService.Save(config); - } + Host = result; + var config = _configService.GetConfig(); + config.Host = Host; + _configService.Save(config); } diff --git a/Desktop.Win/ViewModels/MainWindowViewModel.cs b/Desktop.Win/ViewModels/MainWindowViewModel.cs index 1cc6cfa1..ded3ea87 100644 --- a/Desktop.Win/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Win/ViewModels/MainWindowViewModel.cs @@ -257,18 +257,20 @@ namespace Remotely.Desktop.Win.ViewModels prompt.Owner = App.Current?.MainWindow; prompt.ShowDialog(); - var result = prompt.ViewModel.Host?.Trim(); - if (!result.StartsWith("https://") && !result.StartsWith("http://")) + var result = prompt.ViewModel.Host?.Trim()?.TrimEnd('/'); + + if (!Uri.TryCreate(result, UriKind.Absolute, out var serverUri) || + (serverUri.Scheme != Uri.UriSchemeHttp && serverUri.Scheme != Uri.UriSchemeHttps)) { - result = $"https://{result}"; - } - if (result != Host) - { - Host = result; - var config = _configService.GetConfig(); - config.Host = Host; - _configService.Save(config); + Logger.Write("Server URL is not valid."); + MessageBox.Show("Server URL must be a valid Uri (e.g. https://app.remotely.one).", "Invalid Server URL", MessageBoxButton.OK, MessageBoxImage.Warning); + return; } + + Host = result; + var config = _configService.GetConfig(); + config.Host = Host; + _configService.Save(config); } public void ShutdownApp() diff --git a/Shared/Models/DesktopAppConfig.cs b/Shared/Models/DesktopAppConfig.cs index ea11c20f..81435d6a 100644 --- a/Shared/Models/DesktopAppConfig.cs +++ b/Shared/Models/DesktopAppConfig.cs @@ -12,7 +12,7 @@ namespace Remotely.Shared.Models public string Host { - get => _host; + get => _host.TrimEnd('/'); set { _host = value?.TrimEnd('/');