From 8071a6afe41cbf31343c57a3deaed31d5fde198b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 22 Feb 2021 11:46:31 +0800 Subject: [PATCH 1/9] fix potential uri format issue causing program crash --- Flow.Launcher.Infrastructure/Http/Http.cs | 40 ++++++++++++++--------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index 3a3e770a5..e8cf39880 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -50,25 +50,35 @@ namespace Flow.Launcher.Infrastructure.Http /// 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) + { + Log.Exception("Http", "Unable to parse Uri", e); + } } public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath, CancellationToken token = default) From cfbdf294bbbac33ff5232e6d19f59853df7ebf0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 22 Feb 2021 12:11:02 +0800 Subject: [PATCH 2/9] Add api to Http.cs & message shown up when error thrown --- Flow.Launcher.Infrastructure/Http/Http.cs | 8 +++++++- Flow.Launcher/App.xaml.cs | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index e8cf39880..c9b2a6877 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -9,6 +9,8 @@ using Flow.Launcher.Infrastructure.UserSettings; 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 @@ namespace Flow.Launcher.Infrastructure.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 @@ -77,7 +81,9 @@ namespace Flow.Launcher.Infrastructure.Http } catch(UriFormatException e) { - Log.Exception("Http", "Unable to parse Uri", e); + _api.ShowMsg("Please try again", "Unable to parse Http Proxy"); + Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e); + } } diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 7c4c6a367..54ede7ff7 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -61,7 +61,6 @@ namespace Flow.Launcher _settingsVM = new SettingWindowViewModel(_updater, _portable); _settings = _settingsVM.Settings; - Http.Proxy = _settings.Proxy; _alphabet.Initialize(_settings); _stringMatcher = new StringMatcher(_alphabet); @@ -74,6 +73,10 @@ namespace Flow.Launcher await PluginManager.InitializePlugins(API); var window = new MainWindow(_settings, _mainVM); + Http._api = API; + Http.Proxy = _settings.Proxy; + + Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}"); Current.MainWindow = window; From f940e695a1bb6591cc33f0aaabbb37c9dfec690b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 22 Feb 2021 13:45:28 +0800 Subject: [PATCH 3/9] add Unit test for Http.cs setting --- Flow.Launcher.Test/HttpTest.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Flow.Launcher.Test/HttpTest.cs diff --git a/Flow.Launcher.Test/HttpTest.cs b/Flow.Launcher.Test/HttpTest.cs new file mode 100644 index 000000000..96213aba6 --- /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 TestSettingUpdate() + { + 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); + } + } +} From d41f47ddcfe106946ed2d013b1d836298eafd6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 22 Feb 2021 13:45:40 +0800 Subject: [PATCH 4/9] Migrate UrlPluginTest to Plugins folder --- Flow.Launcher.Test/{ => Plugins}/UrlPluginTest.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Flow.Launcher.Test/{ => Plugins}/UrlPluginTest.cs (100%) 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 From b01e23bd0349af200a86a391b7ad29de567b1a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 22 Feb 2021 16:58:00 +0800 Subject: [PATCH 5/9] move http proxy initialization earlier than Plugin initialization --- Flow.Launcher/App.xaml.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 54ede7ff7..7c9697c07 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -70,11 +70,14 @@ namespace Flow.Launcher PluginManager.LoadPlugins(_settings.PluginSettings); _mainVM = new MainViewModel(_settings); API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet); - await PluginManager.InitializePlugins(API); - var window = new MainWindow(_settings, _mainVM); Http._api = API; Http.Proxy = _settings.Proxy; + + await PluginManager.InitializePlugins(API); + var window = new MainWindow(_settings, _mainVM); + + Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}"); From a507cbdc60296642ba6100fe7a7d168f804db2b5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 22 Feb 2021 20:31:00 +1100 Subject: [PATCH 6/9] fix formatting --- Flow.Launcher.Infrastructure/Http/Http.cs | 2 +- Flow.Launcher/App.xaml.cs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index c9b2a6877..34d3f36f0 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -68,7 +68,7 @@ namespace Flow.Launcher.Infrastructure.Http 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)) + new NetworkCredential(Proxy.UserName, Proxy.Password)) }, _ => (null, null) }, diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 7c9697c07..56cd5b961 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -77,9 +77,6 @@ namespace Flow.Launcher await PluginManager.InitializePlugins(API); var window = new MainWindow(_settings, _mainVM); - - - Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}"); Current.MainWindow = window; From 5f44663bd7dee878154e2596999d54ecb340fd0d Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 22 Feb 2021 20:33:11 +1100 Subject: [PATCH 7/9] remove space --- Flow.Launcher.Infrastructure/Http/Http.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index 34d3f36f0..43f6237ae 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -83,7 +83,6 @@ namespace Flow.Launcher.Infrastructure.Http { _api.ShowMsg("Please try again", "Unable to parse Http Proxy"); Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e); - } } From 9192ab755077c1752cd8808c83cfb67bb7571e4c Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 22 Feb 2021 20:36:06 +1100 Subject: [PATCH 8/9] update variable naming convention --- Flow.Launcher.Infrastructure/Http/Http.cs | 4 ++-- Flow.Launcher/App.xaml.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Http/Http.cs b/Flow.Launcher.Infrastructure/Http/Http.cs index 43f6237ae..b45b6adcd 100644 --- a/Flow.Launcher.Infrastructure/Http/Http.cs +++ b/Flow.Launcher.Infrastructure/Http/Http.cs @@ -20,7 +20,7 @@ namespace Flow.Launcher.Infrastructure.Http private static HttpClient client = new HttpClient(); - public static IPublicAPI _api { get; set; } + public static IPublicAPI API { get; set; } static Http() { @@ -81,7 +81,7 @@ namespace Flow.Launcher.Infrastructure.Http } catch(UriFormatException e) { - _api.ShowMsg("Please try again", "Unable to parse Http Proxy"); + API.ShowMsg("Please try again", "Unable to parse Http Proxy"); Log.Exception("Flow.Launcher.Infrastructure.Http", "Unable to parse Uri", e); } } diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 56cd5b961..a2907c927 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -71,7 +71,7 @@ namespace Flow.Launcher _mainVM = new MainViewModel(_settings); API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet); - Http._api = API; + Http.API = API; Http.Proxy = _settings.Proxy; await PluginManager.InitializePlugins(API); From 77efbffa933bafc7d39e041ef83442acb8d855bb Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 22 Feb 2021 21:27:36 +1100 Subject: [PATCH 9/9] update test name --- Flow.Launcher.Test/HttpTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Test/HttpTest.cs b/Flow.Launcher.Test/HttpTest.cs index 96213aba6..637747a07 100644 --- a/Flow.Launcher.Test/HttpTest.cs +++ b/Flow.Launcher.Test/HttpTest.cs @@ -1,4 +1,4 @@ -using NUnit.Framework; +using NUnit.Framework; using System; using System.Collections.Generic; using System.Text; @@ -11,7 +11,7 @@ namespace Flow.Launcher.Test class HttpTest { [Test] - public void TestSettingUpdate() + public void GivenHttpProxy_WhenUpdated_ThenWebProxyShouldAlsoBeUpdatedToTheSame() { HttpProxy proxy = new HttpProxy(); Http.Proxy = proxy;