diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index 3a3e770a56d..b45b6adcde1 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -9,6 +9,8 @@ using System; using System.ComponentModel; using System.Threading; +using System.Windows.Interop; +using Flow.Launcher.Plugin; namespace Flow.Launcher.Infrastructure.Http { @@ -18,6 +20,8 @@ public static class Http private static HttpClient client = new HttpClient(); + public static IPublicAPI API { get; set; } + static Http() { // need to be added so it would work on a win10 machine @@ -50,25 +54,36 @@ public static HttpProxy Proxy /// public static void UpdateProxy(ProxyProperty property) { - (WebProxy.Address, WebProxy.Credentials) = property switch + if (string.IsNullOrEmpty(Proxy.Server)) + return; + + try { - ProxyProperty.Enabled => Proxy.Enabled switch + (WebProxy.Address, WebProxy.Credentials) = property switch { - true => Proxy.UserName switch + ProxyProperty.Enabled => Proxy.Enabled switch { - var userName when !string.IsNullOrEmpty(userName) => - (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null), - _ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), - new NetworkCredential(Proxy.UserName, Proxy.Password)) + true when !string.IsNullOrEmpty(Proxy.Server) => Proxy.UserName switch + { + var userName when string.IsNullOrEmpty(userName) => + (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null), + _ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), + new NetworkCredential(Proxy.UserName, Proxy.Password)) + }, + _ => (null, null) }, - false => (null, null) - }, - ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), - ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), - ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), - ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), - _ => throw new ArgumentOutOfRangeException() - }; + ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), + ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials), + ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), + ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)), + _ => throw new ArgumentOutOfRangeException() + }; + } + catch(UriFormatException e) + { + API.ShowMsg("Please try again", "Unable to parse Http Proxy"); + Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e); + } } public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, CancellationToken token = default) diff --git a/Flow.Launcher.Test/HttpTest.cs b/Flow.Launcher.Test/HttpTest.cs new file mode 100644 index 00000000000..637747a0785 --- /dev/null +++ b/Flow.Launcher.Test/HttpTest.cs @@ -0,0 +1,33 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Text; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Infrastructure.Http; + +namespace Flow.Launcher.Test +{ + [TestFixture] + class HttpTest + { + [Test] + public void GivenHttpProxy_WhenUpdated_ThenWebProxyShouldAlsoBeUpdatedToTheSame() + { + HttpProxy proxy = new HttpProxy(); + Http.Proxy = proxy; + + proxy.Enabled = true; + proxy.Server = "127.0.0.1"; + Assert.AreEqual(Http.WebProxy.Address, new Uri($"http://{proxy.Server}:{proxy.Port}")); + Assert.IsNull(Http.WebProxy.Credentials); + + proxy.UserName = "test"; + Assert.NotNull(Http.WebProxy.Credentials); + Assert.AreEqual(Http.WebProxy.Credentials.GetCredential(Http.WebProxy.Address, "Basic").UserName, proxy.UserName); + Assert.AreEqual(Http.WebProxy.Credentials.GetCredential(Http.WebProxy.Address, "Basic").Password, ""); + + proxy.Password = "test password"; + Assert.AreEqual(Http.WebProxy.Credentials.GetCredential(Http.WebProxy.Address, "Basic").Password, proxy.Password); + } + } +} diff --git a/Flow.Launcher.Test/UrlPluginTest.cs b/Flow.Launcher.Test/Plugins/UrlPluginTest.cs similarity index 100% rename from Flow.Launcher.Test/UrlPluginTest.cs rename to Flow.Launcher.Test/Plugins/UrlPluginTest.cs diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 7c4c6a36716..a2907c927e2 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -61,7 +61,6 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () => _settingsVM = new SettingWindowViewModel(_updater, _portable); _settings = _settingsVM.Settings; - Http.Proxy = _settings.Proxy; _alphabet.Initialize(_settings); _stringMatcher = new StringMatcher(_alphabet); @@ -71,6 +70,10 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () => PluginManager.LoadPlugins(_settings.PluginSettings); _mainVM = new MainViewModel(_settings); API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet); + + Http.API = API; + Http.Proxy = _settings.Proxy; + await PluginManager.InitializePlugins(API); var window = new MainWindow(_settings, _mainVM);