Cache-friendly headers #29

This commit is contained in:
Michael Snoyman 2014-11-15 20:25:58 +02:00
parent 6618b8b4e3
commit 8649e9d97b

View File

@ -2,6 +2,7 @@ module Handler.StackageIndex where
import Import
import Data.BlobStore
import Network.Wai (responseBuilder)
getStackageIndexR :: PackageSetIdent -> Handler TypedContent
getStackageIndexR ident = do
@ -9,9 +10,31 @@ getStackageIndexR ident = do
case msrc of
Nothing -> notFound
Just src -> do
setEtag $ toPathPiece ident
addHeader "content-disposition" "attachment; filename=\"00-index.tar.gz\""
neverExpires
cacheSeconds 31536000
respondSource "application/x-gzip" $ mapOutput (Chunk . toBuilder) src
-- FIXME BEGIN: move into yesod-core
-- | Send a 304 not modified response immediately. This is a short-circuiting
-- action.
notModified :: MonadHandler m => m a
notModified = sendWaiResponse $ responseBuilder status304 [] mempty
-- | Check the if-none-match header and, if it matches the given value, return
-- a 304 not modified response. Otherwise, set the etag header to the given
-- value.
setEtag :: MonadHandler m => Text -> m ()
setEtag etag = do
mmatch <- lookupHeader "if-none-match"
case mmatch of
Just x | encodeUtf8 etag == x -> notModified
_ -> addHeader "etag" etag
-- FIXME END: move into yesod-core
getStackageBundleR :: PackageSetIdent -> Handler TypedContent
getStackageBundleR ident = do
msrc <- storeRead $ SnapshotBundle ident