summaryrefslogtreecommitdiffhomepage
path: root/SessionID.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2017-04-21 21:06:04 -0400
committerJoey Hess <joeyh@joeyh.name>2017-04-21 21:06:04 -0400
commitfe3c26650bb1e267cce756831fdb9cde230cafd5 (patch)
tree5482e3b0f600fd6bb79087e9b26c4539d7530d6c /SessionID.hs
parent378770cde6fb9fd85983c05eab9eeff2e34398c2 (diff)
downloaddebug-me-fe3c26650bb1e267cce756831fdb9cde230cafd5.tar.gz
use UUID to generate a unique SessionID
Diffstat (limited to 'SessionID.hs')
-rw-r--r--SessionID.hs19
1 files changed, 16 insertions, 3 deletions
diff --git a/SessionID.hs b/SessionID.hs
index 71f2150..3827e1d 100644
--- a/SessionID.hs
+++ b/SessionID.hs
@@ -13,10 +13,13 @@ import Serialization
import System.FilePath
import Data.Text
import System.IO
+import System.Directory
import Network.Wai.Handler.Warp (Port)
import Network.WebSockets hiding (Message)
import qualified Data.Aeson
import Data.Maybe
+import Data.UUID
+import Data.UUID.V4
-- | A SessionID is the base name of the log file to use,
-- and may not contain any path information.
@@ -48,11 +51,21 @@ 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.
+--
+-- A UUID is used, to avoid ever generating a SessionID that has been used
+-- before.
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)
+ createDirectoryIfMissing False dir
+ sid <- SessionID . toString <$> nextRandom
+ let f = sessionLogFile dir sid
+ -- File should not already exist, but just in case we get
+ -- spectacularly unlucky (or the RNG is broken..),
+ -- avoid overwriting a log, and try again.
+ exists <- doesFileExist f
+ if exists
+ then withSessionID dir a
+ else withFile f WriteMode $ \h -> a (h, sid)
type UrlString = String