{-# LANGUAGE OverloadedStrings #-} module Network.URI.Cache(shouldCacheHTTP) where -- , storeCacheHTTP, writeCacheHTTP) import Network.HTTP.Client import Network.HTTP.Types.Status import Network.HTTP.Types.Header import Data.ByteString (ByteString) import Data.ByteString.Char8 as C import Data.Maybe (mapMaybe, listToMaybe, isJust, isNothing) import Data.Char (isSpace) strip = C.dropWhile isSpace -- FIXME Upgrade bytestring dependency for a real strip function. httpCacheDirective :: Response b -> ByteString -> Maybe ByteString httpCacheDirective response key | Just header <- lookup hCacheControl $ responseHeaders response = let directives = Prelude.map strip $ C.split ',' header in if key `Prelude.elem` directives then Just "" else listToMaybe $ mapMaybe (C.stripPrefix $ C.snoc key '=') directives | otherwise = Nothing shouldCacheHTTP :: Response b -> Bool -- IETF RFC7234 Section 3 shouldCacheHTTP response = -- Assume GET statusCode (responseStatus response) `Prelude.elem` [200, 201, 404] && -- Supported response code isNothing (httpCacheDirective response "no-store") && -- Honor no-store True && -- This is a private cache, don't check for Cache-Control: private (isJust (lookup hExpires $ responseHeaders response) || -- Support Expires: header isJust (httpCacheDirective response "max-age") || isJust (httpCacheDirective response "public")) -- Override directive