{- |
Copyright: 2006, Bjorn Bringert
Copyright: 2009, Henning Thielemann
-}
module Network.MoHWS.Module where

import qualified Network.MoHWS.HTTP.Response as Response
import qualified Network.MoHWS.Server.Request as ServerRequest
import Control.Monad.Trans.Maybe (MaybeT, )
import Control.Monad (mzero, )
import Network.Socket (HostName, )


{- |
'isServerHost' allows for advanced checks of the appropriate domain,
e.g. we can catch all subdomains of a certain domain.
-}
data T body = Cons
   {
      T body -> HostName -> Bool
isServerHost  :: HostName -> Bool,
      T body -> HostName -> HostName -> MaybeT IO HostName
translatePath :: String -> String -> MaybeT IO FilePath,
      T body -> T body -> IO (T body)
tweakRequest  :: ServerRequest.T body -> IO (ServerRequest.T body),
      T body -> T body -> MaybeT IO (T body)
handleRequest :: ServerRequest.T body -> MaybeT IO (Response.T body)
   }

empty :: T body
empty :: T body
empty =
   Cons :: forall body.
(HostName -> Bool)
-> (HostName -> HostName -> MaybeT IO HostName)
-> (T body -> IO (T body))
-> (T body -> MaybeT IO (T body))
-> T body
Cons {
      isServerHost :: HostName -> Bool
isServerHost  = \HostName
_   -> Bool
False,
      translatePath :: HostName -> HostName -> MaybeT IO HostName
translatePath = \HostName
_ HostName
_ -> MaybeT IO HostName
forall (m :: * -> *) a. MonadPlus m => m a
mzero,
      tweakRequest :: T body -> IO (T body)
tweakRequest  = \T body
r   -> T body -> IO (T body)
forall (m :: * -> *) a. Monad m => a -> m a
return T body
r,
      handleRequest :: T body -> MaybeT IO (T body)
handleRequest = \T body
_   -> MaybeT IO (T body)
forall (m :: * -> *) a. MonadPlus m => m a
mzero
   }

{- |
We use the type variable 'server'
although it will be always instantiated with 'ServerContext.T'.
However, with this type variable we avoid mutual recursive Haskell modules
for Module and ServerContext.
-}
tweakFilename ::
   (server -> FilePath -> IO FilePath) ->
   server -> ServerRequest.T body -> IO (ServerRequest.T body)
tweakFilename :: (server -> HostName -> IO HostName)
-> server -> T body -> IO (T body)
tweakFilename server -> HostName -> IO HostName
f server
conf T body
req =
    do HostName
filename <- server -> HostName -> IO HostName
f server
conf (T body -> HostName
forall body. T body -> HostName
ServerRequest.serverFilename T body
req)
       T body -> IO (T body)
forall (m :: * -> *) a. Monad m => a -> m a
return (T body -> IO (T body)) -> T body -> IO (T body)
forall a b. (a -> b) -> a -> b
$ T body
req { serverFilename :: HostName
ServerRequest.serverFilename = HostName
filename }