{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Network.Gitit.Server
( module Happstack.Server
, withExpiresHeaders
, setContentType
, setFilename
, lookupIPAddr
, getHost
, compressedResponseFilter
)
where
import Happstack.Server
import Happstack.Server.Compression (compressedResponseFilter)
import Network.Socket (getAddrInfo, defaultHints, addrAddress)
import Control.Monad.Reader
import Data.ByteString.UTF8 as U hiding (lines)
withExpiresHeaders :: ServerMonad m => m Response -> m Response
withExpiresHeaders = liftM (setHeader "Cache-Control" "max-age=21600")
setContentType :: String -> Response -> Response
setContentType = setHeader "Content-Type"
setFilename :: String -> Response -> Response
setFilename = setHeader "Content-Disposition" . \fname -> "attachment; filename=\"" ++ fname ++ "\""
lookupIPAddr :: String -> IO (Maybe String)
lookupIPAddr hostname = do
addrs <- getAddrInfo (Just defaultHints) (Just hostname) Nothing
if null addrs
then return Nothing
else return $ Just $ takeWhile (/=':') $ show $ addrAddress $ case addrs of
[] -> error "lookupIPAddr, no addrs"
(x:_) -> x
getHost :: ServerMonad m => m (Maybe String)
getHost = liftM (maybe Nothing (Just . U.toString)) $ getHeaderM "Host"