private bool UrlProcess(UrlException exception) { var url = new UmbracoUrlService().Url(exception.Url); if (url == null) { return(false); } exception.CalculatedUrl = true; if (string.IsNullOrWhiteSpace(url)) { return(false); } var query = url.IndexOf('?'); if (query == 0) { exception.CalculatedUrlWithoutSlash = url; exception.CalculatedUrlWithSlash = "/" + url; return(true); } var point = (query == -1 ? url.Length : query) - 1; var builder = new StringBuilder(url.Length); if (url[point] == '/') { exception.CalculatedUrlWithSlash = url; for (var i = 0; i != url.Length; i++) { if (i != point) { builder.Append(url[i]); } } exception.CalculatedUrlWithoutSlash = builder.ToString(); return(true); } exception.CalculatedUrlWithoutSlash = url; for (var i = 0; i != url.Length; i++) { builder.Append(url[i]); if (i == point) { builder.Append('/'); } } exception.CalculatedUrlWithSlash = builder.ToString(); return(true); }
private bool MakeSureAllUrlExceptionsHaveBeenCalculated() { return(UrlExceptionLock.Write(() => { foreach (var exception in UrlExceptions.Where(x => x.CalculatedUrl == false)) { var url = new UmbracoUrlService().Url(exception.Url); exception.CalculatedUrl = true; if (string.IsNullOrWhiteSpace(url)) { continue; } var query = url.IndexOf('?'); if (query == 0) { exception.CalculatedUrlWithoutSlash = url; exception.CalculatedUrlWithSlash = "/" + url; continue; } var point = (query == -1 ? url.Length : query) - 1; var builder = new StringBuilder(url.Length); if (url[point] == '/') { exception.CalculatedUrlWithSlash = url; for (var i = 0; i != url.Length; i++) { if (i != point) { builder.Append(url[i]); } } exception.CalculatedUrlWithoutSlash = builder.ToString(); continue; } exception.CalculatedUrlWithoutSlash = url; for (var i = 0; i != url.Length; i++) { builder.Append(url[i]); if (i == point) { builder.Append('/'); } } exception.CalculatedUrlWithSlash = builder.ToString(); } })); }
// ReSharper disable once UnusedParameter.Local private WatchResponse.Cycles ExecuteResponse(int environmentId, Watcher watch, WatchResponse response, HttpApplication application) { if (response.Transfer == null) { return(response.Cycle); } if (response.Transfer.TransferType == TransferTypes.PlayDead) { application.Context.Response.Close(); return(WatchResponse.Cycles.Stop); } if (!MakeSureAllUrlExceptionsHaveBeenCalculated()) { return(WatchResponse.Cycles.Error); } var urlExeceptionResult = UrlExceptionLock.Read(() => { foreach (var exception in UrlExceptions.Where(x => x.EnvironmentId == environmentId)) { if (exception.Regex != null && (exception.Regex.IsMatch(application.Context.Request.Url.PathAndQuery) || exception.Regex.IsMatch(application.Context.Request.Url.AbsoluteUri))) { return(new Tuple <bool, WatchResponse.Cycles?>(true, WatchResponse.Cycles.Continue)); } if (exception.CalculatedUrl && (exception.CalculatedUrlWithoutSlash.Equals(application.Context.Request.Url.PathAndQuery, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithSlash.Equals(application.Context.Request.Url.PathAndQuery, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithoutSlash.Equals(application.Context.Request.Url.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithSlash.Equals(application.Context.Request.Url.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))) { return(new Tuple <bool, WatchResponse.Cycles?>(true, WatchResponse.Cycles.Continue)); } } return(new Tuple <bool, WatchResponse.Cycles?>(false, null)); }); if (urlExeceptionResult == null) { return(WatchResponse.Cycles.Error); } if (urlExeceptionResult.Item1 && urlExeceptionResult.Item2 != null) { return((WatchResponse.Cycles)urlExeceptionResult.Item2); } var url = new UmbracoUrlService().Url(response.Transfer.Url); switch (response.Transfer.TransferType) { case TransferTypes.Redirect: application.Context.Response.Redirect(url, true); return(WatchResponse.Cycles.Stop); case TransferTypes.Rewrite: application.Context.RewritePath(url, string.Empty, string.Empty); return(WatchResponse.Cycles.Restart); } return(WatchResponse.Cycles.Error); }
// ReSharper disable once UnusedParameter.Local private WatchResponse.Cycles ExecuteTransfer(int environmentId, Watcher watch, WatchResponse response, HttpApplication application) { if (response.Transfer.TransferType == TransferTypes.PlayDead) { application.Context.Response.Close(); return(WatchResponse.Cycles.Stop); } if (!ExceptionsProcess()) { return(WatchResponse.Cycles.Error); } if (UrlExceptionLock.Read(() => { var requestUrl = application.Context.Request.Url; foreach (var exception in UrlExceptions.Where(x => x.EnvironmentId == environmentId)) { if (exception.Regex != null && (exception.Regex.IsMatch(requestUrl.PathAndQuery) || exception.Regex.IsMatch(requestUrl.AbsoluteUri))) { return(true); } if (exception.CalculatedUrl && (exception.CalculatedUrlWithoutSlash.Equals(requestUrl.PathAndQuery, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithSlash.Equals(requestUrl.PathAndQuery, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithoutSlash.Equals(requestUrl.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase) || exception.CalculatedUrlWithSlash.Equals(requestUrl.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))) { return(true); } } return(false); })) { return(WatchResponse.Cycles.Continue); } var urlService = new UmbracoUrlService(); var url = urlService.Url(response.Transfer.Url); switch (response.Transfer.TransferType) { case TransferTypes.Redirect: application.Context.Response.Redirect(url, true); return(WatchResponse.Cycles.Stop); case TransferTypes.TransferRequest: application.Server.TransferRequest(url, true); return(WatchResponse.Cycles.Stop); case TransferTypes.TransmitFile: // Request is for a css etc. file, transmit the file and set correct mime type var mimeType = MimeMapping.GetMimeMapping(url); application.Response.ContentType = mimeType; application.Response.TransmitFile(application.Server.MapPath(url)); return(WatchResponse.Cycles.Kill); case TransferTypes.Rewrite: if (urlService.IsUmbracoUrl(response.Transfer.Url)) { RewritePage(application, url); return(WatchResponse.Cycles.Kill); } application.Context.RewritePath(url); return(WatchResponse.Cycles.Restart); } return(WatchResponse.Cycles.Error); }