Example #1
0
        // 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);
        }
Example #2
0
        // 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);
        }