Skip to content

Commit

Permalink
Fixing redirection support in OutputCache
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Nov 9, 2013
1 parent 82025ba commit 4dd9f0f
Showing 1 changed file with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public void OnActionExecuting(ActionExecutingContext filterContext) {
}

public void OnActionExecuted(ActionExecutedContext filterContext) {

// handle redirections
TransformRedirect(filterContext);
}

public void OnResultExecuted(ResultExecutedContext filterContext) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {

}

/// <summary>
Expand Down

0 comments on commit 4dd9f0f

Please sign in to comment.