packages feed

fastcgi-3001.0.2.1: examples/printinput.hs

-- Prints the values of all CGI variables and inputs, and some 
-- process information.

import Control.Monad (liftM)
import Data.Maybe (fromJust)

import Control.Concurrent
import System.Posix.Process (getProcessID)

import Network.FastCGI

printinput :: CGI CGIResult
printinput = do setHeader "Content-type" "text/plain"
                vs <- getVars
                is <- getInputNames
                i <- mapM prInput is
                pid <- liftIO getProcessID
                threadId <- liftIO myThreadId
                let tid = concat $ drop 1 $ words $ show threadId
                output $ unlines ["Environment:", prVars vs,
	                          "Inputs:", unlines i,
	                          "Process ID: " ++ show pid,
	                          "Thread ID:  " ++ tid]

prVars vs = unlines [k ++ ": " ++ x | (k,x) <- vs ]

prInput :: String -> CGI String
prInput i = do v <- liftM fromJust (getInput i)
               f <- getInputFilename i
               return $ case f of
                          Just n -> i ++ ": File\nfilename=" ++ n
                                    ++ "\ncontents=" ++ v
                          Nothing -> i ++ ": " ++ v 

main = runFastCGIorCGI printinput