Skip to content

Commit

Permalink
Generalized cookie handling.
Browse files Browse the repository at this point in the history
Signatures for setCookie & deleteCookie changed.
Helper function getExpires added to API.
  • Loading branch information
paronsson committed Jan 29, 2012
1 parent 6f1ab6d commit 4796ad7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
43 changes: 16 additions & 27 deletions yesod-core/Yesod/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,19 @@ invalidArgsI msg = do

------- Headers
-- | Set the cookie on the client.
--
-- Note: although the value used for key and value is 'Text', you should only
-- use ASCII values to be HTTP compliant.
setCookie :: Int -- ^ minutes to timeout
-> Text -- ^ key
-> Text -- ^ value

setCookie :: SetCookie
-> GHandler sub master ()
setCookie a b = addHeader . AddCookie a (encodeUtf8 b) . encodeUtf8
setCookie = addHeader . AddCookie

-- | Unset the cookie on the client.
deleteCookie :: Text -> GHandler sub master ()
deleteCookie = addHeader . DeleteCookie . encodeUtf8
--
-- Note: although the value used for key and path is 'Text', you should only
-- use ASCII values to be HTTP compliant.
deleteCookie :: Text -- ^ key
-> Text -- ^ path
-> GHandler sub master ()
deleteCookie a = addHeader . DeleteCookie (encodeUtf8 a) . encodeUtf8

-- | Set the language in the user session. Will show up in 'languages' on the
-- next request.
Expand Down Expand Up @@ -809,32 +810,20 @@ httpAccept = parseHttpAccept
. W.requestHeaders

-- | Convert Header to a key/value pair.
headerToPair :: S.ByteString -- ^ cookie path
-> (Int -> UTCTime) -- ^ minutes -> expiration time
-> Header
headerToPair :: Header
-> (CI H.Ascii, H.Ascii)
headerToPair cp getExpires (AddCookie minutes key value) =
("Set-Cookie", toByteString $ renderSetCookie $ SetCookie
{ setCookieName = key
, setCookieValue = value
, setCookiePath = Just cp
, setCookieExpires =
if minutes == 0
then Nothing
else Just $ getExpires minutes
, setCookieDomain = Nothing
, setCookieHttpOnly = True
})
headerToPair cp _ (DeleteCookie key) =
headerToPair (AddCookie sc) =
("Set-Cookie", toByteString $ renderSetCookie $ sc)
headerToPair (DeleteCookie key path) =
( "Set-Cookie"
, S.concat
[ key
, "=; path="
, cp
, path
, "; expires=Thu, 01-Jan-1970 00:00:00 GMT"
]
)
headerToPair _ _ (Header key value) = (CI.mk key, value)
headerToPair (Header key value) = (CI.mk key, value)

-- | Get a unique identifier.
newIdent :: GHandler sub master Text
Expand Down
5 changes: 3 additions & 2 deletions yesod-core/Yesod/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Data.String (IsString)
import qualified Data.Map as Map
import Data.Text.Lazy.Builder (Builder)
import Network.HTTP.Types (Ascii)
import Web.Cookie (SetCookie (..))

#if GHC7
#define HAMLET hamlet
Expand All @@ -64,8 +65,8 @@ instance Exception ErrorResponse
----- header stuff
-- | Headers to be added to a 'Result'.
data Header =
AddCookie Int Ascii Ascii
| DeleteCookie Ascii
AddCookie SetCookie
| DeleteCookie Ascii Ascii
| Header Ascii Ascii
deriving (Eq, Show)

Expand Down
15 changes: 10 additions & 5 deletions yesod-core/Yesod/Internal/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import Blaze.ByteString.Builder (Builder, toByteString)
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
import Data.List (foldl')
import qualified Network.HTTP.Types as H
import Web.Cookie (SetCookie (..))
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO
import qualified Data.Text.Lazy.Builder as TB
Expand Down Expand Up @@ -407,12 +408,16 @@ defaultYesodRunner handler master sub murl toMasterRoute mkey req = do
hs' =
case mkey of
Nothing -> hs
Just _ -> AddCookie
(clientSessionDuration master)
sessionName
sessionVal
Just _ -> AddCookie SetCookie
{ setCookieName = sessionName
, setCookieValue = sessionVal
, setCookiePath = Just (cookiePath master)
, setCookieExpires = Just $ getExpires (clientSessionDuration master)
, setCookieDomain = Nothing
, setCookieHttpOnly = True
}
: hs
hs'' = map (headerToPair (cookiePath master) getExpires) hs'
hs'' = map headerToPair hs'
hs''' = ("Content-Type", ct) : hs''

data AuthResult = Authorized | AuthenticationRequired | Unauthorized Text
Expand Down

0 comments on commit 4796ad7

Please sign in to comment.