From a2ad8fe704f09e364c0bbfc8a7ebfc941810e994 Mon Sep 17 00:00:00 2001 From: "David L. L. Thomas" Date: Wed, 21 Nov 2012 22:39:53 -0800 Subject: [PATCH 1/3] Prevent caching of redirect Dynamic redirects shouldn't be cached. This especially causes a problem with redirection to the login page, since trying to actually access the page the user logged in to see can get them redirected back to the login page on some browsers. This patch adds headers to prevent caching of all redirects, which isn't ideal, but better than allowing caching of all of them. --- yesod-core/Yesod/Handler.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yesod-core/Yesod/Handler.hs b/yesod-core/Yesod/Handler.hs index 493d7f0d..226c10a4 100644 --- a/yesod-core/Yesod/Handler.hs +++ b/yesod-core/Yesod/Handler.hs @@ -522,7 +522,11 @@ runHandler handler mrender sroute tomr master sub upload log' = Right c' -> return $ YARPlain status (appEndo headers []) ct c' finalSession HCError e -> handleError e HCRedirect status loc -> do - let hs = Header "Location" (encodeUtf8 loc) : appEndo headers [] + let hs = + [ Header "Cache-Control" "no-cache, must-revalidate" + , Header "Expires" "Thu, 01 Jan 1970 05:05:05 GMT" + , Header "Location" (encodeUtf8 loc) + ] ++ appEndo headers [] return $ YARPlain status hs typePlain emptyContent finalSession From 246a024dce1db84060a0efe518addc8ef3dc9097 Mon Sep 17 00:00:00 2001 From: "David L. L. Thomas" Date: Fri, 23 Nov 2012 13:44:31 -0800 Subject: [PATCH 2/3] Allowing cache on 301 redirects --- yesod-core/Yesod/Handler.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/yesod-core/Yesod/Handler.hs b/yesod-core/Yesod/Handler.hs index 226c10a4..651ac200 100644 --- a/yesod-core/Yesod/Handler.hs +++ b/yesod-core/Yesod/Handler.hs @@ -522,11 +522,12 @@ runHandler handler mrender sroute tomr master sub upload log' = Right c' -> return $ YARPlain status (appEndo headers []) ct c' finalSession HCError e -> handleError e HCRedirect status loc -> do - let hs = + let disable_caching = [ Header "Cache-Control" "no-cache, must-revalidate" , Header "Expires" "Thu, 01 Jan 1970 05:05:05 GMT" - , Header "Location" (encodeUtf8 loc) - ] ++ appEndo headers [] + ] + hs = (if status == H.movedPermanently301 then disable_caching else []) + ++ Header "Location" (encodeUtf8 loc) : appEndo headers [] return $ YARPlain status hs typePlain emptyContent finalSession From 513d6ed4f9cb970be4ce5d90f52caaf1cc05e871 Mon Sep 17 00:00:00 2001 From: "David L. L. Thomas" Date: Sun, 25 Nov 2012 11:12:39 -0800 Subject: [PATCH 3/3] Bugfix - Caching redirect *only* for 301 redirects Previous commit got it backwards. --- yesod-core/Yesod/Handler.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yesod-core/Yesod/Handler.hs b/yesod-core/Yesod/Handler.hs index 651ac200..9a952752 100644 --- a/yesod-core/Yesod/Handler.hs +++ b/yesod-core/Yesod/Handler.hs @@ -526,7 +526,7 @@ runHandler handler mrender sroute tomr master sub upload log' = [ Header "Cache-Control" "no-cache, must-revalidate" , Header "Expires" "Thu, 01 Jan 1970 05:05:05 GMT" ] - hs = (if status == H.movedPermanently301 then disable_caching else []) + hs = (if status /= H.movedPermanently301 then disable_caching else []) ++ Header "Location" (encodeUtf8 loc) : appEndo headers [] return $ YARPlain status hs typePlain emptyContent