Session -> BackendSession

This commit is contained in:
gregwebs 2012-03-25 13:28:31 -07:00
parent f147e76231
commit 3f0848121c
4 changed files with 29 additions and 19 deletions

View File

@ -33,6 +33,8 @@ module Yesod.Core
, clientSessionBackend , clientSessionBackend
, saveClientSession , saveClientSession
, loadClientSession , loadClientSession
, Header(..)
, BackendSession
-- * JS loaders -- * JS loaders
, loadJsYepnope , loadJsYepnope
, ScriptLoadPosition (..) , ScriptLoadPosition (..)
@ -50,6 +52,7 @@ module Yesod.Core
) where ) where
import Yesod.Internal.Core import Yesod.Internal.Core
import Yesod.Internal (Header(..))
import Yesod.Content import Yesod.Content
import Yesod.Dispatch import Yesod.Dispatch
import Yesod.Handler import Yesod.Handler

View File

@ -31,6 +31,7 @@ module Yesod.Internal.Core
, clientSessionBackend , clientSessionBackend
, saveClientSession , saveClientSession
, loadClientSession , loadClientSession
, BackendSession
-- * jsLoader -- * jsLoader
, ScriptLoadPosition (..) , ScriptLoadPosition (..)
, BottomOfHeadAsync , BottomOfHeadAsync
@ -323,20 +324,6 @@ $doctype 5
key <- CS.getKey CS.defaultKeyFile key <- CS.getKey CS.defaultKeyFile
return $ Just $ clientSessionBackend key 120 return $ Just $ clientSessionBackend key 120
type Session = [(Text, S8.ByteString)]
data SessionBackend master = SessionBackend
{ sbSaveSession :: master
-> W.Request
-> UTCTime -- ^ The current time
-> Session -- ^ The old session (before running handler)
-> Session -- ^ The final session
-> IO [Header]
, sbLoadSession :: master
-> W.Request
-> UTCTime -- ^ The current time
-> IO Session
}
messageLoggerHandler :: Yesod m messageLoggerHandler :: Yesod m
=> Loc -> LogLevel -> Text -> GHandler s m () => Loc -> LogLevel -> Text -> GHandler s m ()
@ -724,7 +711,7 @@ loadClientSession :: Yesod master
-> master -> master
-> W.Request -> W.Request
-> UTCTime -> UTCTime
-> IO Session -> IO BackendSession
loadClientSession key _ req now = return . fromMaybe [] $ do loadClientSession key _ req now = return . fromMaybe [] $ do
raw <- lookup "Cookie" $ W.requestHeaders req raw <- lookup "Cookie" $ W.requestHeaders req
val <- lookup sessionName $ parseCookies raw val <- lookup sessionName $ parseCookies raw
@ -737,12 +724,12 @@ saveClientSession :: Yesod master
-> master -> master
-> W.Request -> W.Request
-> UTCTime -> UTCTime
-> Session -> BackendSession
-> Session -> BackendSession
-> IO [Header] -> IO [Header]
saveClientSession key timeout master _ now _ sess = do saveClientSession key timeout master _ now _ sess = do
-- fixme should we be caching this? -- fixme should we be caching this?
iv <- liftIO $ CS.randomIV iv <- liftIO CS.randomIV
return [AddCookie def return [AddCookie def
{ setCookieName = sessionName { setCookieName = sessionName
, setCookieValue = sessionVal iv , setCookieValue = sessionVal iv

View File

@ -1,8 +1,11 @@
module Yesod.Internal.Session module Yesod.Internal.Session
( encodeClientSession ( encodeClientSession
, decodeClientSession , decodeClientSession
, BackendSession
, SessionBackend(..)
) where ) where
import Yesod.Internal (Header(..))
import qualified Web.ClientSession as CS import qualified Web.ClientSession as CS
import Data.Serialize import Data.Serialize
import Data.Time import Data.Time
@ -12,6 +15,24 @@ import Data.Text (Text, pack, unpack)
import Control.Arrow (first) import Control.Arrow (first)
import Control.Applicative ((<$>)) import Control.Applicative ((<$>))
import qualified Data.ByteString.Char8 as S8
import qualified Network.Wai as W
type BackendSession = [(Text, S8.ByteString)]
data SessionBackend master = SessionBackend
{ sbSaveSession :: master
-> W.Request
-> UTCTime -- ^ The current time
-> BackendSession -- ^ The old session (before running handler)
-> BackendSession -- ^ The final session
-> IO [Header]
, sbLoadSession :: master
-> W.Request
-> UTCTime -- ^ The current time
-> IO BackendSession
}
encodeClientSession :: CS.Key encodeClientSession :: CS.Key
-> CS.IV -> CS.IV
-> UTCTime -- ^ expire time -> UTCTime -- ^ expire time

View File

@ -4,7 +4,6 @@ module YesodCoreTest.WaiSubsite (specs, Widget) where
import YesodCoreTest.YesodTest import YesodCoreTest.YesodTest
import Yesod.Core import Yesod.Core
import qualified Network.HTTP.Types as H import qualified Network.HTTP.Types as H
import Network.Wai
myApp :: Application myApp :: Application
myApp _ = return $ responseLBS H.status200 [("Content-type", "text/plain")] "WAI" myApp _ = return $ responseLBS H.status200 [("Content-type", "text/plain")] "WAI"