module Network.Web.Server.Params where
import Control.Applicative ((<$>))
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Time
import Data.Time.Clock.POSIX
import Network.TCPInfo
import Network.Web.HTTP
import System.Posix.Files
data BasicConfig = BasicConfig {
mapper :: Request -> IO Path
, obtain :: FilePath -> Maybe (Integer,Integer) -> IO L.ByteString
, info :: FilePath -> IO (Maybe (Integer, UTCTime))
, serverName :: S.ByteString
, tcpInfo :: TCPInfo
}
defaultConfig :: BasicConfig
defaultConfig = BasicConfig
{ mapper = error "BasicConfig: no mapper defined"
, obtain = defaultObtain
, info = defaultInfo
, serverName = "BasicConfig: no server name"
, tcpInfo = error "BasicConfig: no TCPInfo"
}
data Path =
None
| File FilePath
| PathCGI CGI
| Handler (IO Response)
instance Eq Path where
(File fp1) == (File fp2) = fp1 == fp2
(PathCGI cgi1) == (PathCGI cgi2) = cgi1 == cgi2
None == None = True
_ == _ = False
instance Show Path where
show None = "None"
show (File fp) = "File " ++ show fp
show (PathCGI cgi) = "PathCGI (" ++ show cgi ++ ")"
show (Handler _) = "Handler"
data CGI = CGI {
progPath :: FilePath
, scriptName :: String
, pathInfo :: String
, queryString :: String
} deriving (Eq,Show)
defaultInfo :: FilePath -> IO (Maybe (Integer, UTCTime))
defaultInfo fp = do
exists <- fileExist fp
if exists
then do
status <- getFileStatus fp
let size = fromIntegral $ fileSize status
mt = posixSecondsToUTCTime . realToFrac $ modificationTime status
return $ Just (size,mt)
else return Nothing
defaultObtain :: FilePath -> Maybe (Integer,Integer) -> IO L.ByteString
defaultObtain fp Nothing = L.readFile fp
defaultObtain fp (Just (offset,numBytes)) = L.take nb . L.drop ofs <$> L.readFile fp
where
nb = fromIntegral numBytes
ofs = fromIntegral offset