diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs index 2a64dece01c..85c3dab6084 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs @@ -288,7 +288,8 @@ public void OnActionExecuting(ActionExecutingContext filterContext) { } public void OnActionExecuted(ActionExecutedContext filterContext) { - + // handle redirections + TransformRedirect(filterContext); } public void OnResultExecuted(ResultExecutedContext filterContext) { @@ -341,10 +342,7 @@ public void OnResultExecuted(ResultExecutedContext filterContext) { } return; } - - // handle redirections - TransformRedirect(filterContext); - + // save the result only if the content can be intercepted if (_filter == null) return; @@ -407,58 +405,67 @@ public void OnResultExecuted(ResultExecutedContext filterContext) { } - private void TransformRedirect(ResultExecutedContext filterContext) { - // todo: look for RedirectToRoute to, or intercept 302s + private bool TransformRedirect(ActionExecutedContext filterContext) { - if (filterContext.HttpContext.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) - && filterContext.Result is RedirectResult) { - Logger.Debug("Redirect on POST"); - var redirectUrl = ((RedirectResult) filterContext.Result).Url; + if (filterContext.Result == null) { + throw new ArgumentNullException(); + } - if (!VirtualPathUtility.IsAbsolute(redirectUrl)) { - var applicationRoot = filterContext.HttpContext.Request.ToRootUrlString(); - if (redirectUrl.StartsWith(applicationRoot, StringComparison.OrdinalIgnoreCase)) { - redirectUrl = redirectUrl.Substring(applicationRoot.Length); - } - } + // status code can't be tested at this point, so test the result type instead + if (!filterContext.HttpContext.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) + || !(filterContext.Result is RedirectResult)) { + return false; + } + + Logger.Debug("Redirect on POST"); + var redirectUrl = ((RedirectResult)(filterContext.Result)).Url ; - // querystring invariant key - var invariantCacheKey = ComputeCacheKey( - _shellSettings.Name, - redirectUrl, - () => _workContext.CurrentCulture, - _themeManager.GetRequestTheme(filterContext.RequestContext).Id, - null - ); - - _cacheService.RemoveByTag(invariantCacheKey); - - // adding a refresh key so that the next request will not be cached - var epIndex = redirectUrl.IndexOf('?'); - var qs = new NameValueCollection(); - if (epIndex > 0) { - qs = HttpUtility.ParseQueryString(redirectUrl.Substring(epIndex)); + if (!VirtualPathUtility.IsAbsolute(redirectUrl)) { + var applicationRoot = filterContext.HttpContext.Request.ToRootUrlString(); + if (redirectUrl.StartsWith(applicationRoot, StringComparison.OrdinalIgnoreCase)) { + redirectUrl = redirectUrl.Substring(applicationRoot.Length); } + } - var refresh = _now.Ticks; - qs.Remove(RefreshKey); + // querystring invariant key + var invariantCacheKey = ComputeCacheKey( + _shellSettings.Name, + redirectUrl, + () => _workContext.CurrentCulture, + _themeManager.GetRequestTheme(filterContext.RequestContext).Id, + null + ); + + _cacheService.RemoveByTag(invariantCacheKey); + + // adding a refresh key so that the next request will not be cached + var epIndex = redirectUrl.IndexOf('?'); + var qs = new NameValueCollection(); + if (epIndex > 0) { + qs = HttpUtility.ParseQueryString(redirectUrl.Substring(epIndex)); + } - qs.Add(RefreshKey, refresh.ToString("x")); - var querystring = "?" + string.Join("&", Array.ConvertAll(qs.AllKeys, k => string.Format("{0}={1}", HttpUtility.UrlEncode(k), HttpUtility.UrlEncode(qs[k])))); + var refresh = _now.Ticks; + qs.Remove(RefreshKey); - if (epIndex > 0) { - redirectUrl = redirectUrl.Substring(0, epIndex) + querystring; - } - else { - redirectUrl = redirectUrl + querystring; - } + qs.Add(RefreshKey, refresh.ToString("x")); + var querystring = "?" + string.Join("&", Array.ConvertAll(qs.AllKeys, k => string.Format("{0}={1}", HttpUtility.UrlEncode(k), HttpUtility.UrlEncode(qs[k])))); - filterContext.Result = new RedirectResult(redirectUrl, ((RedirectResult) filterContext.Result).Permanent); - filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); + if (epIndex > 0) { + redirectUrl = redirectUrl.Substring(0, epIndex) + querystring; + } + else { + redirectUrl = redirectUrl + querystring; } + + filterContext.Result = new RedirectResult(redirectUrl, ((RedirectResult) filterContext.Result).Permanent); + filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); + + return true; } public void OnResultExecuting(ResultExecutingContext filterContext) { + } ///