Merge branch 'temp2-U4-11570' of https://github.com/raduorleanu/Umbraco-CMS into raduorleanu-temp2-U4-11570

This commit is contained in:
Sebastiaan Janssen
2018-09-25 16:09:58 +02:00
11 changed files with 106 additions and 88 deletions

View File

@@ -49,7 +49,7 @@ namespace Umbraco.Core.Services
private Dictionary<string, IContentType> _importedContentTypes;
private IPackageInstallation _packageInstallation;
private readonly IUserService _userService;
private static HttpClient _httpClient;
public PackagingService(
ILogger logger,
@@ -89,7 +89,6 @@ namespace Umbraco.Core.Services
/// <returns></returns>
public string FetchPackageFile(Guid packageId, Version umbracoVersion, int userId)
{
using (var httpClient = new HttpClient())
using (var uow = _uowProvider.GetUnitOfWork())
{
//includeHidden = true because we don't care if it's hidden we want to get the file regardless
@@ -97,7 +96,11 @@ namespace Umbraco.Core.Services
byte[] bytes;
try
{
bytes = httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult();
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
bytes = _httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult();
}
catch (HttpRequestException ex)
{
@@ -1746,7 +1749,7 @@ namespace Umbraco.Core.Services
internal InstallationSummary InstallPackage(string packageFilePath, int userId = 0, bool raiseEvents = false)
{
var metaData = GetPackageMetaData(packageFilePath);
var metaData = GetPackageMetaData(packageFilePath);
if (raiseEvents)
{

View File

@@ -20,9 +20,10 @@ using Umbraco.Core.Services;
namespace Umbraco.Web.Editors
{
public class CanvasDesignerController : UmbracoApiController
{
private static WebClient _webClient;
[HttpGet]
public HttpResponseMessage GetGoogleFont()
@@ -35,10 +36,10 @@ namespace Umbraco.Web.Editors
var googleWebFontAPIURL = string.Format("https://www.googleapis.com/webfonts/v1/webfonts?key={0}", APIKey);
var response = "{}";
using (var client = new System.Net.WebClient())
{
response = client.DownloadString(new Uri(googleWebFontAPIURL));
}
if (_webClient == null)
_webClient = new WebClient();
response = _webClient.DownloadString(new Uri(googleWebFontAPIURL));
var resp = Request.CreateResponse();
resp.Content = new StringContent(response);
@@ -59,7 +60,7 @@ namespace Umbraco.Web.Editors
// Prepare string parameter result
string[] paramLines = paramBlock.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
IList<string> parameters = new List<string>();
IList<string> parameters = new List<string>();
foreach (var line in paramLines)
{
if (!line.Contains("@import"))
@@ -121,4 +122,4 @@ namespace Umbraco.Web.Editors
}
}
}

View File

@@ -27,6 +27,8 @@ namespace Umbraco.Web.Editors
[WebApi.UmbracoAuthorize]
public class DashboardController : UmbracoApiController
{
//we have just one instance of HttpClient shared for the entire application
private static readonly HttpClient HttpClient = new HttpClient();
//we have baseurl as a param to make previewing easier, so we can test with a dev domain from client side
[ValidateAngularAntiForgeryToken]
public async Task<JObject> GetRemoteDashboardContent(string section, string baseUrl = "https://dashboard.umbraco.org/")
@@ -54,13 +56,10 @@ namespace Umbraco.Web.Editors
//content is null, go get it
try
{
using (var web = new HttpClient())
{
//fetch dashboard json and parse to JObject
var json = await web.GetStringAsync(url);
content = JObject.Parse(json);
result = content;
}
//fetch dashboard json and parse to JObject
var json = await HttpClient.GetStringAsync(url);
content = JObject.Parse(json);
result = content;
ApplicationContext.ApplicationCache.RuntimeCache.InsertCacheItem<JObject>(key, () => result, new TimeSpan(0, 30, 0));
}
@@ -93,17 +92,14 @@ namespace Umbraco.Web.Editors
//content is null, go get it
try
{
using (var web = new HttpClient())
{
//fetch remote css
content = await web.GetStringAsync(url);
//fetch remote css
content = await HttpClient.GetStringAsync(url);
//can't use content directly, modified closure problem
result = content;
//can't use content directly, modified closure problem
result = content;
//save server content for 30 mins
ApplicationContext.ApplicationCache.RuntimeCache.InsertCacheItem<string>(key, () => result, new TimeSpan(0, 30, 0));
}
//save server content for 30 mins
ApplicationContext.ApplicationCache.RuntimeCache.InsertCacheItem<string>(key, () => result, new TimeSpan(0, 30, 0));
}
catch (HttpRequestException ex)
{
@@ -119,12 +115,12 @@ namespace Umbraco.Web.Editors
Content = new StringContent(result, Encoding.UTF8, "text/css")
};
}
[ValidateAngularAntiForgeryToken]
public IEnumerable<Tab<DashboardControl>> GetDashboard(string section)
{
var dashboardHelper = new DashboardHelper(Services.SectionService);
return dashboardHelper.GetDashboard(section, Security.CurrentUser);
return dashboardHelper.GetDashboard(section, Security.CurrentUser);
}
}
}

View File

@@ -7,20 +7,18 @@ using System.Threading.Tasks;
namespace Umbraco.Web.Editors
{
public class HelpController : UmbracoAuthorizedJsonController
{
{
private static readonly HttpClient HttpClient = new HttpClient();
public async Task<List<HelpPage>> GetContextHelpForPage(string section, string tree, string baseUrl = "https://our.umbraco.com")
{
var url = string.Format(baseUrl + "/Umbraco/Documentation/Lessons/GetContextHelpDocs?sectionAlias={0}&treeAlias={1}", section, tree);
using (var web = new HttpClient())
{
//fetch dashboard json and parse to JObject
var json = await web.GetStringAsync(url);
var result = JsonConvert.DeserializeObject<List<HelpPage>>(json);
if (result != null)
return result;
//fetch dashboard json and parse to JObject
var json = await HttpClient.GetStringAsync(url);
var result = JsonConvert.DeserializeObject<List<HelpPage>>(json);
if (result != null)
return result;
return new List<HelpPage>();
}
return new List<HelpPage>();
}
}

View File

@@ -23,6 +23,7 @@ namespace Umbraco.Web.Install
{
internal class InstallHelper
{
private static HttpClient _httpClient;
private readonly UmbracoContext _umbContext;
private InstallationType? _installationType;
@@ -191,16 +192,18 @@ namespace Umbraco.Web.Install
internal IEnumerable<Package> GetStarterKits()
{
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
var packages = new List<Package>();
try
{
var requestUri = string.Format("https://our.umbraco.com/webapi/StarterKit/Get/?umbracoVersion={0}",
UmbracoVersion.Current);
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
using (var httpClient = new HttpClient())
using (var response = httpClient.SendAsync(request).Result)
using (var response = _httpClient.SendAsync(request).Result)
{
packages = response.Content.ReadAsAsync<IEnumerable<Package>>().Result.ToList();
}
@@ -213,4 +216,4 @@ namespace Umbraco.Web.Install
return packages;
}
}
}
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Net;
using System.Web;
using System.Web.Security;
using Umbraco.Core;
@@ -25,6 +26,7 @@ namespace Umbraco.Web.Install.InstallSteps
{
private readonly HttpContextBase _http;
private readonly ApplicationContext _applicationContext;
private static WebClient _webClient;
public NewInstallStep(HttpContextBase http, ApplicationContext applicationContext)
{
@@ -80,9 +82,12 @@ namespace Umbraco.Web.Install.InstallSteps
{
try
{
var client = new System.Net.WebClient();
if (_webClient == null)
{
_webClient = new WebClient();
}
var values = new NameValueCollection { { "name", admin.Name }, { "email", admin.Email} };
client.UploadValues("https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx", values);
_webClient.UploadValues("https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx", values);
}
catch { /* fail in silence */ }
}

View File

@@ -12,18 +12,20 @@ namespace Umbraco.Web.Media.EmbedProviders
{
//TODO: Make all Http calls async
public abstract class AbstractOEmbedProvider: IEmbedProvider
public abstract class AbstractOEmbedProvider : IEmbedProvider
{
private static WebClient _webClient;
public virtual bool SupportsDimensions
{
get { return true; }
}
[ProviderSetting]
public string APIEndpoint{ get;set; }
public string APIEndpoint { get; set; }
[ProviderSetting]
public Dictionary<string, string> RequestParams{ get;set; }
public Dictionary<string, string> RequestParams { get; set; }
public abstract string GetMarkup(string url, int maxWidth, int maxHeight);
@@ -51,10 +53,9 @@ namespace Umbraco.Web.Media.EmbedProviders
public virtual string DownloadResponse(string url)
{
using (var webClient = new WebClient())
{
return webClient.DownloadString(url);
}
if (_webClient == null)
_webClient = new WebClient();
return _webClient.DownloadString(url);
}
public virtual T GetJsonResponse<T>(string url) where T : class
@@ -79,4 +80,4 @@ namespace Umbraco.Web.Media.EmbedProviders
}
}
}
}

View File

@@ -17,11 +17,12 @@ namespace Umbraco.Web.Scheduling
internal class ScheduledTasks : RecurringTaskBase
{
private static HttpClient _httpClient;
private readonly ApplicationContext _appContext;
private readonly IUmbracoSettingsSection _settings;
private static readonly Hashtable ScheduledTaskTimes = new Hashtable();
public ScheduledTasks(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
public ScheduledTasks(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
ApplicationContext appContext, IUmbracoSettingsSection settings)
: base(runner, delayMilliseconds, periodMilliseconds)
{
@@ -61,28 +62,29 @@ namespace Umbraco.Web.Scheduling
private async Task<bool> GetTaskByHttpAync(string url, CancellationToken token)
{
using (var wc = new HttpClient())
if (_httpClient == null)
{
if (Uri.TryCreate(_appContext.UmbracoApplicationUrl, UriKind.Absolute, out var baseUri))
{
wc.BaseAddress = baseUri;
}
var request = new HttpRequestMessage(HttpMethod.Get, url);
//TODO: pass custom the authorization header, currently these aren't really secured!
//request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
try
{
var result = await wc.SendAsync(request, token).ConfigureAwait(false); // ConfigureAwait(false) is recommended? http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
return result.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
LogHelper.Error<ScheduledTasks>("An error occurred calling web task for url: " + url, ex);
}
return false;
_httpClient = new HttpClient();
}
if (Uri.TryCreate(_appContext.UmbracoApplicationUrl, UriKind.Absolute, out var baseUri))
{
_httpClient.BaseAddress = baseUri;
}
var request = new HttpRequestMessage(HttpMethod.Get, url);
//TODO: pass custom the authorization header, currently these aren't really secured!
//request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
try
{
var result = await _httpClient.SendAsync(request, token).ConfigureAwait(false); // ConfigureAwait(false) is recommended? http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
return result.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
LogHelper.Error<ScheduledTasks>("An error occurred calling web task for url: " + url, ex);
}
return false;
}
public override async Task<bool> PerformRunAsync(CancellationToken token)

View File

@@ -10,8 +10,9 @@ namespace umbraco.presentation
[Obsolete("This is no longer used and will be removed in future versions")]
public class keepAliveService
{
private static WebClient _webClient;
//NOTE: sender will be the umbraco ApplicationContext
public static void PingUmbraco(object sender)
public static void PingUmbraco(object sender)
{
if (sender == null || !(sender is ApplicationContext))
return;
@@ -21,10 +22,9 @@ namespace umbraco.presentation
var url = appContext.UmbracoApplicationUrl + "/ping.aspx";
try
{
using (var wc = new WebClient())
{
wc.DownloadString(url);
}
if (_webClient == null)
_webClient = new WebClient();
_webClient.DownloadString(url);
}
catch(Exception ee)
{
@@ -32,4 +32,4 @@ namespace umbraco.presentation
}
}
}
}
}

View File

@@ -1,4 +1,5 @@
using Umbraco.Core.Logging;
using System.Net.Http;
using Umbraco.Core.Logging;
using Umbraco.Web;
namespace dashboardUtilities
@@ -14,6 +15,8 @@ namespace dashboardUtilities
public partial class FeedProxy : UmbracoEnsuredPage
{
private static WebClient _webClient;
protected void Page_Load(object sender, EventArgs e)
{
try
@@ -31,9 +34,10 @@ namespace dashboardUtilities
&& feedProxyXml.SelectSingleNode(string.Concat("//allow[@host = '", requestUri.Host, "']")) != null
&& requestUri.Port == 80)
{
using (var client = new WebClient())
{
var response = client.DownloadString(requestUri);
if (_webClient == null)
_webClient = new WebClient();
var response = _webClient.DownloadString(requestUri);
if (string.IsNullOrEmpty(response) == false)
{
@@ -41,7 +45,7 @@ namespace dashboardUtilities
Response.ContentType = Request.CleanForXss("type") ?? MediaTypeNames.Text.Xml;
Response.Write(response);
}
}
}
else
{
@@ -57,4 +61,4 @@ namespace dashboardUtilities
}
}
}
}
}

View File

@@ -14,6 +14,7 @@ using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using System.Diagnostics;
using System.IO.Compression;
using System.Net;
using umbraco.cms.businesslogic.template;
using umbraco.interfaces;
using Umbraco.Core.Events;
@@ -46,6 +47,7 @@ namespace umbraco.cms.businesslogic.packager
private readonly List<string> _binaryFileErrors = new List<string>();
private int _currentUserId = -1;
private static WebClient _webClient;
public string Name { get; private set; }
@@ -685,9 +687,12 @@ namespace umbraco.cms.businesslogic.packager
if (Directory.Exists(IOHelper.MapPath(SystemDirectories.Packages)) == false)
Directory.CreateDirectory(IOHelper.MapPath(SystemDirectories.Packages));
var wc = new System.Net.WebClient();
if (_webClient == null)
{
_webClient = new WebClient();
}
wc.DownloadFile(
_webClient.DownloadFile(
"http://" + PackageServer + "/fetch?package=" + Package.ToString(),
IOHelper.MapPath(SystemDirectories.Packages + "/" + Package + ".umb"));