From 6359826fd942144badf139a58681883ad0c0f82d Mon Sep 17 00:00:00 2001 From: bao-qian Date: Tue, 10 May 2016 20:26:47 +0100 Subject: [PATCH] use unified http method for plugin installation + add more exceptions #573 #610 --- .../HttpRequest.cs | 34 ------ Plugins/Wox.Plugin.PluginManagement/Main.cs | 111 +++++++++--------- .../Wox.Plugin.PluginManagement.csproj | 1 - .../SuggestionSources/Baidu.cs | 16 ++- .../SuggestionSources/Google.cs | 14 ++- Wox.Core/Updater.cs | 16 ++- Wox.Infrastructure/Http/HttpRequest.cs | 27 ++--- 7 files changed, 111 insertions(+), 108 deletions(-) delete mode 100644 Plugins/Wox.Plugin.PluginManagement/HttpRequest.cs diff --git a/Plugins/Wox.Plugin.PluginManagement/HttpRequest.cs b/Plugins/Wox.Plugin.PluginManagement/HttpRequest.cs deleted file mode 100644 index 27c7acabf5..0000000000 --- a/Plugins/Wox.Plugin.PluginManagement/HttpRequest.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Net; - -namespace Wox.Plugin.PluginManagement -{ - public class HttpRequest - { - private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; - - public static HttpWebResponse CreateGetHttpResponse(string url,IHttpProxy proxy) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentNullException("url"); - } - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server)) - { - if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password)) - { - request.Proxy = new WebProxy(proxy.Server, proxy.Port); - } - else - { - request.Proxy = new WebProxy(proxy.Server, proxy.Port); - request.Proxy.Credentials = new NetworkCredential(proxy.UserName, proxy.Password); - } - } - request.Method = "GET"; - request.UserAgent = DefaultUserAgent; - return request.GetResponse() as HttpWebResponse; - } - } -} diff --git a/Plugins/Wox.Plugin.PluginManagement/Main.cs b/Plugins/Wox.Plugin.PluginManagement/Main.cs index 77599065d3..b484b09344 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Main.cs +++ b/Plugins/Wox.Plugin.PluginManagement/Main.cs @@ -8,6 +8,8 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; using Newtonsoft.Json; +using Wox.Infrastructure.Http; +using Wox.Infrastructure.Logger; namespace Wox.Plugin.PluginManagement { @@ -108,68 +110,71 @@ namespace Wox.Plugin.PluginManagement List results = new List(); string pluginName = query.SecondSearch; if (string.IsNullOrEmpty(pluginName)) return results; - HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy); - Stream s = response.GetResponseStream(); - if (s != null) + string json; + try { - StreamReader reader = new StreamReader(s, Encoding.UTF8); - string json = reader.ReadToEnd(); - List searchedPlugins = null; - try - { - searchedPlugins = JsonConvert.DeserializeObject>(json); - } - catch - { - context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty); - return results; - } + var task = Http.Get(pluginSearchUrl + pluginName, context.Proxy); + task.RunSynchronously(); + json = task.Result; + } + catch (WebException e) + { + Log.Warn("Can't connect to Wox plugin website, check your conenction"); + Log.Error(e); + return new List(); + } + List searchedPlugins; + try + { + searchedPlugins = JsonConvert.DeserializeObject>(json); + } + catch(JsonSerializationException e) + { + context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty); + Log.Error(e); + return results; + } - foreach (WoxPluginResult r in searchedPlugins) + foreach (WoxPluginResult r in searchedPlugins) + { + WoxPluginResult r1 = r; + results.Add(new Result { - WoxPluginResult r1 = r; - results.Add(new Result + Title = r.name, + SubTitle = r.description, + IcoPath = "Images\\plugin.png", + Action = c => { - Title = r.name, - SubTitle = r.description, - IcoPath = "Images\\plugin.png", - Action = e => + MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin", + "Install plugin", MessageBoxButton.YesNo); + + if (result == MessageBoxResult.Yes) { - MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin", - "Install plugin", MessageBoxButton.YesNo); + string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload"); + if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); + string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox"); - if (result == MessageBoxResult.Yes) + context.API.StartLoadingBar(); + string pluginUrl = APIBASE + "/media/" + r1.plugin_file; + + try { - string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload"); - if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); - string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox"); - - context.API.StartLoadingBar(); - Task.Run(() => - { - using (WebClient Client = new WebClient()) - { - try - { - string pluginUrl = APIBASE + "/media/" + r1.plugin_file; - Client.DownloadFile(pluginUrl, filePath); - context.API.InstallPlugin(filePath); - } - catch (Exception exception) - { - MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message); - } - finally - { - context.API.StopLoadingBar(); - } - } - }); + Http.Download(pluginUrl, filePath, context.Proxy); } - return false; + catch (WebException e) + { + var info = "download plugin " + r.name + "failed."; + MessageBox.Show(info); + Log.Warn(info); + Log.Error(e); + return false; + } + context.API.InstallPlugin(filePath); + context.API.StopLoadingBar(); } - }); - } + return false; + } + }); } return results; } diff --git a/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj b/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj index f56d9427f7..a7c361159b 100644 --- a/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj +++ b/Plugins/Wox.Plugin.PluginManagement/Wox.Plugin.PluginManagement.csproj @@ -53,7 +53,6 @@ Properties\SolutionAssemblyInfo.cs - diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs index d05d7287f1..b10bbf379f 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text.RegularExpressions; using System.Threading.Tasks; using Newtonsoft.Json; @@ -18,7 +19,20 @@ namespace Wox.Plugin.WebSearch.SuggestionSources public override async Task> GetSuggestions(string query) { - var result = await HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312"); + string result; + + try + { + const string api = "http://suggestion.baidu.com/su?json=1&wd="; + result = await Http.Get(api + Uri.EscapeUriString(query), Proxy, "GB2312"); + } + catch (WebException e) + { + Log.Warn("Can't get suggestion from baidu"); + Log.Error(e); + return new List(); ; + } + if (string.IsNullOrEmpty(result)) return new List(); Match match = reg.Match(result); diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs index ab569219c3..c62376020e 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -14,7 +15,18 @@ namespace Wox.Plugin.WebSearch.SuggestionSources public override string Domain { get; set; } = "www.google.com"; public override async Task> GetSuggestions(string query) { - var result = await HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy); + string result; + try + { + const string api = "https://www.google.com/complete/search?output=chrome&q="; + result = await Http.Get(api + Uri.EscapeUriString(query), Proxy); + } + catch (WebException e) + { + Log.Warn("Can't get suggestion from google"); + Log.Error(e); + return new List(); ; + } if (string.IsNullOrEmpty(result)) return new List(); JContainer json; try diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index ab07821c00..bfd06219ac 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -19,7 +19,7 @@ namespace Wox.Core public static async void UpdateApp() { - var client = new WebClient {Proxy = HttpRequest.WebProxy(HttpProxy.Instance)}; + var client = new WebClient {Proxy = Http.WebProxy(HttpProxy.Instance)}; var downloader = new FileDownloader(client); try @@ -59,10 +59,22 @@ namespace Wox.Core } } + public static async Task NewVersion() { const string githubAPI = @"https://api.github.com/repos/wox-launcher/wox/releases/latest"; - var response = await HttpRequest.Get(githubAPI, HttpProxy.Instance); + + string response; + try + { + response = await Http.Get(githubAPI, HttpProxy.Instance); + } + catch (WebException e) + { + Log.Warn("Can't connect to github api to check new version"); + Log.Error(e); + return string.Empty; + } if (!string.IsNullOrEmpty(response)) { diff --git a/Wox.Infrastructure/Http/HttpRequest.cs b/Wox.Infrastructure/Http/HttpRequest.cs index c1a95ec113..645d458af7 100644 --- a/Wox.Infrastructure/Http/HttpRequest.cs +++ b/Wox.Infrastructure/Http/HttpRequest.cs @@ -1,17 +1,13 @@ -using System; -using System.IO; +using System.IO; using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using JetBrains.Annotations; -using Wox.Infrastructure.Logger; using Wox.Plugin; namespace Wox.Infrastructure.Http { - public static class HttpRequest + public static class Http { public static WebProxy WebProxy(IHttpProxy proxy) { @@ -37,6 +33,14 @@ namespace Wox.Infrastructure.Http } } + /// Can't download file + public static void Download([NotNull] string url, [NotNull] string filePath, IHttpProxy proxy) + { + var client = new WebClient { Proxy = WebProxy(proxy) }; + client.DownloadFile(url, filePath); + } + + /// Can't get response from http get public static async Task Get([NotNull] string url, IHttpProxy proxy, string encoding = "UTF-8") { @@ -45,16 +49,7 @@ namespace Wox.Infrastructure.Http request.Timeout = 10 * 1000; request.Proxy = WebProxy(proxy); request.UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko"; - HttpWebResponse response; - try - { - response = await request.GetResponseAsync() as HttpWebResponse; - } - catch (WebException e) - { - Log.Error(e); - return string.Empty; - } + var response = await request.GetResponseAsync() as HttpWebResponse; if (response != null) { var stream = response.GetResponseStream();