/// <summary> /// /// </summary> /// <param name="job"></param> /// <param name="stage"></param> /// <param name="regex"></param> /// <param name="priority"></param> /// <param name="request"></param> /// <returns></returns> public static int Watch(IJob job, PipeLineStages stage, Regex regex, int priority, Func <int, HttpApplication, WatchResponse> request) { Environ environ = null; if (EnvironLock.Write(() => { if (!Environs.TryGetValue(job.Environment.SortOrder, out environ)) { Environs.Add(job.Environment.SortOrder, environ = new Environ(job.Environment)); } })) { return(EnvironLock.Read(() => { return environ.WatchLocks[(int)stage].Write(() => { var watchList = environ.Watchers[(int)stage]; var count = Interlocked.Increment(ref _requestCount); EnvironHasWatches[(int)stage] = true; watchList.Add(new Watcher { Priority = priority, AppId = job.App.Id, Regex = regex, Request = request }); watchList.Sort(new WatchComparer()); return count; }); })); } return(-1); }
void Request(PipeLineStages stage, HttpApplication application) { if (application.Context.Request.Url.AbsolutePath == "/umbraco/ping.aspx" || application.Context.Request.Url.AbsolutePath == "/umbraco/backoffice/UmbracoApi/Authentication/GetRemainingTimeoutSeconds") { return; } int count = 0; if (ProcessRequest(stage, count, application)) { return; } while (++count != RequestRestartLimit) { if (ProcessRequest(PipeLineStages.BeginRequest, count, application) && stage == PipeLineStages.BeginRequest) { return; } if (stage != PipeLineStages.BeginRequest) { if (ProcessRequest(PipeLineStages.AuthenticateRequest, count, application) && stage == PipeLineStages.AuthenticateRequest) { return; } if (stage != PipeLineStages.AuthenticateRequest) { if (ProcessRequest(PipeLineStages.ResolveRequestCache, count, application) && stage == PipeLineStages.ResolveRequestCache) { return; } if (stage != PipeLineStages.ResolveRequestCache) { if (ProcessRequest(PipeLineStages.UpdateRequestCache, count, application) && stage == PipeLineStages.UpdateRequestCache) { return; } if (stage != PipeLineStages.UpdateRequestCache) { if (ProcessRequest(PipeLineStages.EndRequest, count, application) && stage == PipeLineStages.EndRequest) { return; } } } } } } // To many redirects application.Context.Response.StatusCode = 500; application.CompleteRequest(); return; }
/// <summary> /// /// </summary> /// <param name="job"></param> /// <param name="stage"></param> /// <param name="regex"></param> /// <returns></returns> public static int Unwatch(IJob job, PipeLineStages stage, Regex regex = null) { return(EnvironLock.Read(() => { if (!Environs.TryGetValue(job.Environment.SortOrder, out var environ)) { return 0; } return environ.WatchLocks[(int)stage].Write(() => { var regy = regex?.ToString(); return environ.Watchers[(int)stage].RemoveAll(x => x.AppId.Equals(job.App.Id, StringComparison.InvariantCultureIgnoreCase) && (regy == null && x.Regex == null || regy != null && x.Regex != null && regy.Equals(x.Regex.ToString(), StringComparison.InvariantCulture))); }); })); }
private void Request(PipeLineStages stage, HttpApplication application) { if (EnvironHasWatches[(int)stage] == false) { return; } var count = 0; while (true == EnvironLock.Read <bool?>(() => { if (!Environs.Any()) { return(false); } if (count++ > RequestRestartLimit) { application.Context.Response.StatusCode = 500; application.CompleteRequest(); return(false); } var uri = application.Context.Request.Url.AbsoluteUri; string uriWithoutDomain = null; foreach (var environ in Environs) { string filePath; if (environ.Value.Domains == null) { if (uriWithoutDomain == null) { uriWithoutDomain = application.Context.Request.Url.LocalPath; } filePath = uriWithoutDomain; } else { var domain = environ.Value.Domains.FirstOrDefault(x => uri.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)); if (domain != null) { filePath = uri.Substring(domain.Length - 1); } else { continue; } } if (environ.Value.WatchLocks[(int)stage].Read <bool?>(() => { foreach (var watch in environ.Value.Watchers[(int)stage]) { if (watch.Regex != null && !watch.Regex.IsMatch(filePath)) { continue; } #if TRACE var debug = $"{uri}: Watcher({environ.Value.Name}, {watch.AppId}, {watch.Priority}, {watch.Regex}) returned "; #endif switch (ExecuteResponse(environ.Value.Id, watch, watch.Request(count, application), application)) { case WatchResponse.Cycles.Stop: #if TRACE Debug.WriteLine(debug + "Stop"); #endif return(false); case WatchResponse.Cycles.Restart: #if TRACE Debug.WriteLine(debug + "Restart"); #endif return(true); case WatchResponse.Cycles.Error: #if TRACE Debug.WriteLine(debug + "Error"); #endif application.Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; application.CompleteRequest(); break; #if TRACE default: Debug.WriteLine(debug + "Continue"); break; #endif // If WatchCycle.Continue we do nothing } } return(false); }) == true) { return(true); } if (!environ.Value.ContinueProcessing) { break; } } return(false); })) { } }
/// <inheritdoc /> public int UnwatchWebRequests(PipeLineStages stage) => WebRequestHandler.Unwatch(this, stage);
/// <inheritdoc /> public int UnwatchWebRequests(PipeLineStages stage, Regex regex) => WebRequestHandler.Unwatch(this, stage, regex);
/// <inheritdoc /> public int WatchWebRequests(PipeLineStages stage, Regex regex, int priority, Func <int, HttpApplication, WatchResponse> request) => WebRequestHandler.Watch(this, stage, regex, priority, request);
private bool ProcessRequest(PipeLineStages stage, int count, HttpApplication application) { if (EnvironHasWatches[(int)stage] == false) { return(true); } return(EnvironLock.Read(() => { if (!Environs.Any()) { return true; } #if TRACE Debug.WriteLine(stage.ToString() + " : " + application.Request.Url.AbsoluteUri); #endif var uri = application.Context.Request.Url.AbsoluteUri; string uriWithoutDomain = null; foreach (var environ in Environs) { string filePath; if (environ.Value.Domains == null) { if (uriWithoutDomain == null) { uriWithoutDomain = application.Context.Request.Url.LocalPath; } filePath = uriWithoutDomain; } else { var domain = environ.Value.Domains.FirstOrDefault(x => uri.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)); if (domain != null) { filePath = uri.Substring(domain.Length - 1); } else { continue; } } if (environ.Value.WatchLocks[(int)stage].Read <bool?>(() => { var ignores = UrlIgnoresLock.Read(() => { return UrlIgnores.Where(x => x.EnvironmentId == environ.Value.Id && x.Regex.IsMatch(filePath)).Select(x => x.AppId); }); foreach (var watch in environ.Value.Watchers[(int)stage]) { if ((watch.Regex != null && !watch.Regex.IsMatch(filePath)) || ignores.Any(x => x != watch.AppId)) { continue; } #if TRACE var debug = $"{uri}: Watcher({environ.Value.Name}, {watch.AppId}, {watch.Priority}, {watch.Regex}) "; #endif var watchResponse = watch.Request(count, application); if (watchResponse.Transfer != null) { #if TRACE debug += "by transfer then "; #endif watchResponse.Cycle = ExecuteTransfer(environ.Value.Id, watch, watchResponse, application); } switch (watchResponse.Cycle) { case WatchResponse.Cycles.Kill: #if TRACE Debug.WriteLine(debug + "Kill"); #endif application.Response.End(); //application.CompleteRequest(); return true; case WatchResponse.Cycles.Stop: #if TRACE Debug.WriteLine(debug + "Stop"); #endif return true; case WatchResponse.Cycles.Restart: #if TRACE Debug.WriteLine(debug + "Restart"); #endif return false; case WatchResponse.Cycles.Error: #if TRACE Debug.WriteLine(debug + "Error"); #endif application.Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; application.CompleteRequest(); break; #if TRACE default: Debug.WriteLine(debug + "Continue"); break; #endif // If WatchCycle.Continue we do nothing } } return true; }) == false) { return false; } if (!environ.Value.ContinueProcessing) { break; } } return true; })); }