summaryrefslogtreecommitdiffhomepage
path: root/SessionID.hs
blob: a47de8f04323d074fead61d2c3a96d4f655b91e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module SessionID (SessionID, mkSessionID, sessionLogFile, withSessionID) where

import System.FilePath
import Data.Text
import System.IO

-- | A SessionID is the base name of the log file to use,
-- and may not contain any path information.
newtype SessionID = SessionID FilePath
	deriving (Show, Eq, Ord)

-- | Smart constructor that enforces security requirements.
mkSessionID :: Text -> Maybe SessionID
mkSessionID t = 
	let f = unpack t
	in if takeFileName f == f
		then Just (SessionID f)
		else Nothing

sessionLogFile :: FilePath -> SessionID -> FilePath
sessionLogFile dir (SessionID f) = dir </> "debug-me." ++ f ++ ".log"

-- | Allocate a new SessionID and return an open Handle to its log file.
withSessionID :: FilePath -> ((Handle, SessionID) -> IO a) -> IO a
withSessionID dir a = do
	-- TODO find an unused log file and open it
	let sid = SessionID "1"
	withFile "debug-me-server.log" WriteMode $ \h -> a (h, sid)