{-# LANGUAGE OverloadedStrings #-}
module Network.Wai.Middleware.Push.Referer.Types (
URLPath
, MakePushPromise
, defaultMakePushPromise
, Settings(..)
, defaultSettings
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Network.Wai.Handler.Warp (PushPromise(..), defaultPushPromise)
type URLPath = ByteString
type MakePushPromise = URLPath
-> URLPath
-> FilePath
-> IO (Maybe PushPromise)
defaultMakePushPromise :: MakePushPromise
defaultMakePushPromise refPath path file = case getCT path of
Nothing -> return Nothing
Just ct -> do
let pp = defaultPushPromise {
promisedPath = path
, promisedFile = file
, promisedResponseHeaders = [("content-type", ct)
,("x-http2-push", refPath)]
}
return $ Just pp
getCT :: URLPath -> Maybe ByteString
getCT p
| ".js" `BS.isSuffixOf` p = Just "application/javascript"
| ".css" `BS.isSuffixOf` p = Just "text/css"
| otherwise = Nothing
data Settings = Settings {
makePushPromise :: MakePushPromise
, duration :: Int
, keyLimit :: Int
, valueLimit :: Int
}
defaultSettings :: Settings
defaultSettings = Settings {
makePushPromise = defaultMakePushPromise
, duration = 0
, keyLimit = 20
, valueLimit = 20
}