diff --git a/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs b/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs index cbd2e0e519..dc8aa15f6a 100644 --- a/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs +++ b/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Net.Http.Headers; using System.Text; using System.Text.RegularExpressions; using System.Web; @@ -36,22 +37,33 @@ namespace Umbraco.Web.Mvc return _applicationContext ?? ApplicationContext.Current; } + public const string AuthorizationType = "AToken"; + /// - /// Used to return the value that needs to go in the Authorization header + /// Used to return the full value that needs to go in the Authorization header /// /// /// public static string GetAuthHeaderTokenVal(ApplicationContext appContext) { - var admin = appContext.Services.UserService.GetUserById(0); + return string.Format("{0} {1}", AuthorizationType, GetAuthHeaderVal(appContext)); + } + public static AuthenticationHeaderValue GetAuthenticationHeaderValue(ApplicationContext appContext) + { + return new AuthenticationHeaderValue(AuthorizationType, GetAuthHeaderVal(appContext)); + } + + private static string GetAuthHeaderVal(ApplicationContext appContext) + { + var admin = appContext.Services.UserService.GetUserById(0); var token = string.Format("{0}u____u{1}u____u{2}", admin.Email, admin.Username, admin.RawPasswordValue); var encrypted = token.EncryptWithMachineKey(); var bytes = Encoding.UTF8.GetBytes(encrypted); var base64 = Convert.ToBase64String(bytes); - return "AToken val=\"" + base64 + "\""; + return string.Format("val=\"{0}\"", base64); } /// diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs index 575845efba..78e5f4ce74 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs @@ -96,6 +96,21 @@ namespace Umbraco.Web.Scheduling StartUp(); } + /// + /// Returns the current cancellation token + /// + public CancellationToken CurrentCancellationToken + { + get + { + if (_tokenSource == null) + { + throw new InvalidOperationException("The token source has not been created which means the task runner has not been started"); + } + return _tokenSource.Token; + } + } + /// /// Gets the number of tasks in the queue. /// diff --git a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index 9db21fba8a..cbc000b7e6 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -1,7 +1,10 @@ using System; using System.Diagnostics; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; +using System.Threading; using System.Threading.Tasks; using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; @@ -16,15 +19,17 @@ namespace Umbraco.Web.Scheduling { private readonly ApplicationContext _appContext; private readonly IUmbracoSettingsSection _settings; + private readonly Func _cancellationToken; private static bool _isPublishingRunning; public ScheduledPublishing(IBackgroundTaskRunner runner, int delayMilliseconds, int periodMilliseconds, - ApplicationContext appContext, IUmbracoSettingsSection settings) + ApplicationContext appContext, IUmbracoSettingsSection settings, Func cancellationToken) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; _settings = settings; + _cancellationToken = cancellationToken; } private ScheduledPublishing(ScheduledPublishing source) @@ -41,6 +46,12 @@ namespace Umbraco.Web.Scheduling public override void PerformRun() { + throw new NotImplementedException(); + } + + public override async Task PerformRunAsync() + { + if (_appContext == null) return; if (ServerEnvironmentHelper.GetStatus(_settings) == CurrentServerEnvironmentStatus.Slave) { @@ -57,7 +68,7 @@ namespace Umbraco.Web.Scheduling var umbracoBaseUrl = ServerEnvironmentHelper.GetCurrentServerUmbracoBaseUrl(_appContext, _settings); try - { + { if (string.IsNullOrWhiteSpace(umbracoBaseUrl)) { @@ -66,13 +77,30 @@ namespace Umbraco.Web.Scheduling else { var url = string.Format("{0}RestServices/ScheduledPublish/Index", umbracoBaseUrl.EnsureEndsWith('/')); - using (var wc = new WebClient()) + using (var wc = new HttpClient()) { + var request = new HttpRequestMessage() + { + RequestUri = new Uri(url), + Method = HttpMethod.Post, + Content = new StringContent(string.Empty) + }; //pass custom the authorization header - wc.Headers.Set("Authorization", AdminTokenAuthorizeAttribute.GetAuthHeaderTokenVal(_appContext)); + request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext); - var result = wc.UploadString(url, ""); - } + var token = new CancellationToken(); + try + { + token = _cancellationToken(); + } + catch (InvalidOperationException) + { + //There is no valid token, so we'll continue with the empty one + } + + var result = await wc.SendAsync(request, token); + + } } } catch (Exception ee) @@ -88,14 +116,9 @@ namespace Umbraco.Web.Scheduling } } - public override Task PerformRunAsync() - { - throw new NotImplementedException(); - } - public override bool IsAsync { - get { return false; } + get { return true; } } public override bool RunsOnShutdown diff --git a/src/Umbraco.Web/Scheduling/Scheduler.cs b/src/Umbraco.Web/Scheduling/Scheduler.cs index 409a67028f..71f2ab59c5 100644 --- a/src/Umbraco.Web/Scheduling/Scheduler.cs +++ b/src/Umbraco.Web/Scheduling/Scheduler.cs @@ -66,7 +66,7 @@ namespace Umbraco.Web.Scheduling // scheduled publishing/unpublishing // install on all, will only run on non-slaves servers // both are delayed recurring tasks - _publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings)); + _publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings, () => _publishingRunner.CurrentCancellationToken)); _tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings)); // log scrubbing