-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.hs
86 lines (67 loc) · 2.57 KB
/
Utils.hs
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE CPP #-}
#if PRODUCTION
{-# LANGUAGE Safe #-}
#endif
{-# LANGUAGE OverloadedStrings #-}
module Utils where
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy.Char8 as L8
import Data.Maybe (listToMaybe, fromJust)
import Data.IterIO.Http (reqCookies, respAddHeader)
import Data.IterIO.Http.Support
import Data.Bson (genObjectId)
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.State
-- | Force get parameter value
getParamVal :: Monad m => S8.ByteString -> Action t b m String
getParamVal n = (L8.unpack . paramValue . fromJust) `liftM` param n
maybeRead :: Read a => String -> Maybe a
maybeRead = fmap fst . listToMaybe . reads
with404orJust :: Monad m => Maybe a -> (a -> Action t b m ()) -> Action t b m ()
with404orJust mval act = case mval of
Nothing -> respond404
Just val -> act val
-- | Set the referer cookie (to referer) if unset.
saveRefererIfNone :: Action t b IO ()
saveRefererIfNone = do
mref <- getCookie "_hails_referer"
mhdr <- fmap S8.unpack `liftM` requestHeader "referer"
case (mref,mhdr) of
(Nothing, Just u) -> setCookie "_hails_referer" (show u)
_ -> return ()
-- | Redirect to the set refer, if set; or given URL.
redirectToSavedRefererOrTo :: String -> Action t b IO ()
redirectToSavedRefererOrTo url = do
mref <- getCookie "_hails_referer"
redirectTo $ maybe url S8.unpack mref
delCookie "_hails_referer"
--
-- Flash notifications
--
-- | This sets the @_flash-*@ cookie value to the given message, with
-- a unique message ID.
flash :: String -> String -> Action t b IO ()
flash n msg = do
oid <- liftIO genObjectId
setCookie ("_flash-" ++ n) (show (show oid ++ "|" ++ msg))
flashInfo :: String -> Action t b IO ()
flashInfo = flash "info"
flashError :: String -> Action t b IO ()
flashError = flash "error"
flashSuccess :: String -> Action t b IO ()
flashSuccess = flash "success"
getCookie :: String -> Action t b IO (Maybe S8.ByteString)
getCookie n = do
req <- getHttpReq
return $ lookup (S8.pack n) $ reqCookies req
setCookie :: String -> String -> Action t b IO ()
setCookie n v = modify $ \s ->
let cHeader = ( S8.pack "Set-Cookie"
, S8.pack $ n ++ "=" ++ v ++ ";path=/;")
in s { actionResp = respAddHeader cHeader (actionResp s)}
delCookie :: String -> Action t b IO ()
delCookie n = modify $ \s ->
let cHeader = ( S8.pack "Set-Cookie", S8.pack $
n ++ "=; path=/; expires=Thu, Jan 01 1970 00:00:00 UTC;")
in s { actionResp = respAddHeader cHeader (actionResp s)}