Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
setting Expires header in snap
-- | Set an Expires reponse header to this number of days from now.
expiresInDays :: Integer -> Snap ()
expiresInDays n = do
now <- liftIO getCurrentTime
let later = addUTCTime (fromInteger $ n * 60 * 60 * 24) now
getResponse >>= return . addHeader "Expires" (formatRFC1123 later) >>= putResponse
formatRFC1123 :: UTCTime -> BS.ByteString
formatRFC1123 = BS8.pack . formatTime defaultTimeLocale "%a, %d %b %Y %X %Z"
Example:
route
[ ("public/images", expiresInDays 7 >> (serveDirectory $ cwd </> "public"))
, ("public", serveDirectory $ cwd </> "public")
...

I would suggest the more general

expiresOn :: UTCTime -> Snap ()
expiresIn :: NominalDiffTime -> Snap ()
expiresInDays :: Int -> Snap ()

where expiresIn (operating in seconds, this has a proper Num instance also) and expiresInDays are expressed in terms of expiresOn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment