From a1224da5e16f948962600c975a90ff98f386da67 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Wed, 26 Sep 2018 13:27:16 +0200 Subject: [PATCH] Fixes #3036 Removes WebClient completely except for in an old legacy class that is not used for anything any more (umbraco.cms.businesslogic.packager.Installer.Fetch). --- .../Editors/CanvasDesignerController.cs | 31 +++------- src/Umbraco.Web/Install/InstallHelper.cs | 5 +- .../Install/InstallSteps/NewInstallStep.cs | 40 +++++++------ .../EmbedProviders/AbstractOEmbedProvider.cs | 13 ++-- .../umbraco.presentation/keepAliveService.cs | 16 ++--- .../umbraco/dashboard/FeedProxy.aspx.cs | 60 +++++++++---------- 6 files changed, 79 insertions(+), 86 deletions(-) diff --git a/src/Umbraco.Web/Editors/CanvasDesignerController.cs b/src/Umbraco.Web/Editors/CanvasDesignerController.cs index 8284bf0b12..e0645b3c23 100644 --- a/src/Umbraco.Web/Editors/CanvasDesignerController.cs +++ b/src/Umbraco.Web/Editors/CanvasDesignerController.cs @@ -1,29 +1,17 @@ using System.Collections.Generic; -using System.Net; using System.Net.Http; -using System.Text; using System.Web.Http; -using AutoMapper; -using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Mvc; -using umbraco; using Umbraco.Web.WebApi; using System; using System.Net.Http.Headers; using System.Web; -using System.IO; -using Umbraco.Core.Models; -using System.Text.RegularExpressions; -using System.Linq; -using Umbraco.Core; -using Umbraco.Core.Services; namespace Umbraco.Web.Editors { public class CanvasDesignerController : UmbracoApiController { - private static WebClient _webClient; + private static HttpClient _httpClient; [HttpGet] public HttpResponseMessage GetGoogleFont() @@ -35,17 +23,14 @@ namespace Umbraco.Web.Editors // Google Web Font JSON URL var googleWebFontAPIURL = string.Format("https://www.googleapis.com/webfonts/v1/webfonts?key={0}", APIKey); - var response = "{}"; - if (_webClient == null) - _webClient = new WebClient(); - - response = _webClient.DownloadString(new Uri(googleWebFontAPIURL)); - - var resp = Request.CreateResponse(); - resp.Content = new StringContent(response); - resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return resp; + if (_httpClient == null) + _httpClient = new HttpClient(); + using (var request = new HttpRequestMessage(HttpMethod.Get, googleWebFontAPIURL)) + { + var response = _httpClient.SendAsync(request).Result; + return response; + } } [HttpGet] diff --git a/src/Umbraco.Web/Install/InstallHelper.cs b/src/Umbraco.Web/Install/InstallHelper.cs index 5b6e8a0c60..50324bcfd2 100644 --- a/src/Umbraco.Web/Install/InstallHelper.cs +++ b/src/Umbraco.Web/Install/InstallHelper.cs @@ -198,12 +198,11 @@ namespace Umbraco.Web.Install var packages = new List(); try { - var requestUri = string.Format("https://our.umbraco.com/webapi/StarterKit/Get/?umbracoVersion={0}", - UmbracoVersion.Current); + var requestUri = $"https://our.umbraco.com/webapi/StarterKit/Get/?umbracoVersion={UmbracoVersion.Current}"; using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) - using (var response = _httpClient.SendAsync(request).Result) { + var response = _httpClient.SendAsync(request).Result; packages = response.Content.ReadAsAsync>().Result.ToList(); } } diff --git a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs index 22f5f0ff7e..2d6fd89c46 100644 --- a/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/NewInstallStep.cs @@ -1,13 +1,14 @@ using System; -using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; using System.Net; +using System.Net.Http; +using System.Text; using System.Web; using System.Web.Security; +using Newtonsoft.Json; using Umbraco.Core; using Umbraco.Core.Configuration; -using Umbraco.Core.Persistence; using Umbraco.Web.Install.Models; namespace Umbraco.Web.Install.InstallSteps @@ -20,13 +21,12 @@ namespace Umbraco.Web.Install.InstallSteps /// error, etc... and the end-user refreshes the installer then we cannot show the user screen because they've already entered that information so instead we'll /// display a simple continue installation view. /// - [InstallSetupStep(InstallationType.NewInstall, - "User", 20, "")] + [InstallSetupStep(InstallationType.NewInstall, "User", 20, "")] internal class NewInstallStep : InstallSetupStep { private readonly HttpContextBase _http; private readonly ApplicationContext _applicationContext; - private static WebClient _webClient; + private static HttpClient _httpClient; public NewInstallStep(HttpContextBase http, ApplicationContext applicationContext) { @@ -76,17 +76,18 @@ namespace Umbraco.Web.Install.InstallSteps admin.Username = user.Email.Trim(); _applicationContext.Services.UserService.Save(admin); - - + if (user.SubscribeToNewsLetter) { + if (_httpClient == null) + _httpClient = new HttpClient(); + + var values = new NameValueCollection { { "name", admin.Name }, { "email", admin.Email } }; + var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json"); + try { - if (_webClient == null) - _webClient = new WebClient(); - - var values = new NameValueCollection { { "name", admin.Name }, { "email", admin.Email} }; - _webClient.UploadValues("https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx", values); + var response = _httpClient.PostAsync("https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx", content).Result; } catch { /* fail in silence */ } } @@ -113,13 +114,16 @@ namespace Umbraco.Web.Install.InstallSteps public override string View { - get { return RequiresExecution(null) - //the user UI - ? "user" - //the continue install UI - : "continueinstall"; } + get + { + return RequiresExecution(null) + //the user UI + ? "user" + //the continue install UI + : "continueinstall"; + } } - + public override bool RequiresExecution(UserModel model) { //now we have to check if this is really a new install, the db might be configured and might contain data diff --git a/src/Umbraco.Web/Media/EmbedProviders/AbstractOEmbedProvider.cs b/src/Umbraco.Web/Media/EmbedProviders/AbstractOEmbedProvider.cs index 737b04fc83..fa1ef9fb93 100644 --- a/src/Umbraco.Web/Media/EmbedProviders/AbstractOEmbedProvider.cs +++ b/src/Umbraco.Web/Media/EmbedProviders/AbstractOEmbedProvider.cs @@ -14,7 +14,7 @@ namespace Umbraco.Web.Media.EmbedProviders public abstract class AbstractOEmbedProvider : IEmbedProvider { - private static WebClient _webClient; + private static HttpClient _httpClient; public virtual bool SupportsDimensions { @@ -53,9 +53,14 @@ namespace Umbraco.Web.Media.EmbedProviders public virtual string DownloadResponse(string url) { - if (_webClient == null) - _webClient = new WebClient(); - return _webClient.DownloadString(url); + if (_httpClient == null) + _httpClient = new HttpClient(); + + using (var request = new HttpRequestMessage(HttpMethod.Get, url)) + { + var response = _httpClient.SendAsync(request).Result; + return response.Content.ReadAsStringAsync().Result; + } } public virtual T GetJsonResponse(string url) where T : class diff --git a/src/Umbraco.Web/umbraco.presentation/keepAliveService.cs b/src/Umbraco.Web/umbraco.presentation/keepAliveService.cs index 192c35a233..21511bb1dc 100644 --- a/src/Umbraco.Web/umbraco.presentation/keepAliveService.cs +++ b/src/Umbraco.Web/umbraco.presentation/keepAliveService.cs @@ -1,7 +1,5 @@ using System; -using System.Diagnostics; -using System.Net; -using System.Web; +using System.Net.Http; using Umbraco.Core; using Umbraco.Core.Logging; @@ -10,7 +8,7 @@ namespace umbraco.presentation [Obsolete("This is no longer used and will be removed in future versions")] public class keepAliveService { - private static WebClient _webClient; + private static HttpClient _httpClient; //NOTE: sender will be the umbraco ApplicationContext public static void PingUmbraco(object sender) { @@ -22,9 +20,13 @@ namespace umbraco.presentation var url = appContext.UmbracoApplicationUrl + "/ping.aspx"; try { - if (_webClient == null) - _webClient = new WebClient(); - _webClient.DownloadString(url); + if (_httpClient == null) + _httpClient = new HttpClient(); + + using (var request = new HttpRequestMessage(HttpMethod.Get, url)) + { + var response = _httpClient.SendAsync(request).Result; + } } catch(Exception ee) { diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/dashboard/FeedProxy.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/dashboard/FeedProxy.aspx.cs index 488e4f06c2..49539b0264 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/dashboard/FeedProxy.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/dashboard/FeedProxy.aspx.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Web; @@ -6,54 +7,51 @@ namespace dashboardUtilities { using System; using System.Linq; - using System.Net; using System.Net.Mime; - using umbraco; using umbraco.BasePages; - using umbraco.BusinessLogic; using Umbraco.Core.IO; public partial class FeedProxy : UmbracoEnsuredPage { - private static WebClient _webClient; + private static HttpClient _httpClient; protected void Page_Load(object sender, EventArgs e) { try { - if (Request.QueryString.AllKeys.Contains("url") && Request.QueryString["url"] != null) + if (Request.QueryString.AllKeys.Contains("url") == false || Request.QueryString["url"] == null) + return; + + var url = Request.QueryString["url"]; + if (string.IsNullOrWhiteSpace(url) || url.StartsWith("/")) + return; + + if (Uri.TryCreate(url, UriKind.Absolute, out var requestUri) == false) + return; + + var feedProxyXml = XmlHelper.OpenAsXmlDocument(IOHelper.MapPath(SystemFiles.FeedProxyConfig)); + if (feedProxyXml?.SelectSingleNode($"//allow[@host = '{requestUri.Host}']") != null && requestUri.Port == 80) { - var url = Request.QueryString["url"]; - if (!string.IsNullOrWhiteSpace(url) && !url.StartsWith("/")) + if (_httpClient == null) + _httpClient = new HttpClient(); + + using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) { - Uri requestUri; - if (Uri.TryCreate(url, UriKind.Absolute, out requestUri)) - { - var feedProxyXml = xmlHelper.OpenAsXmlDocument(IOHelper.MapPath(SystemFiles.FeedProxyConfig)); - if (feedProxyXml != null - && feedProxyXml.SelectSingleNode(string.Concat("//allow[@host = '", requestUri.Host, "']")) != null - && requestUri.Port == 80) - { - if (_webClient == null) - _webClient = new WebClient(); - - var response = _webClient.DownloadString(requestUri); + var response = _httpClient.SendAsync(request).Result; + var result = response.Content.ReadAsStringAsync().Result; - if (string.IsNullOrEmpty(response) == false) - { - Response.Clear(); - Response.ContentType = Request.CleanForXss("type") ?? MediaTypeNames.Text.Xml; - Response.Write(response); - } + if (string.IsNullOrEmpty(result)) + return; - } - else - { - LogHelper.Debug(string.Format("Access to unallowed feedproxy attempted: {0}", requestUri)); - } - } + Response.Clear(); + Response.ContentType = Request.CleanForXss("type") ?? MediaTypeNames.Text.Xml; + Response.Write(result); } } + else + { + LogHelper.Debug($"Access to unallowed feedproxy attempted: {requestUri}"); + } } catch (Exception ex) {