Skip to content

Commit

Permalink
setSimpleCookie1, changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Zocca committed Dec 16, 2023
1 parent 3401e42 commit 42043a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
32 changes: 29 additions & 3 deletions Web/Scotty/Cookie.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module Web.Scotty.Cookie (
-- * Set cookie
setCookie
, setSimpleCookie
-- ** Sanitized values
, setCookie1
, setSimpleCookie1
-- * Get cookie(s)
, getCookie
, getCookies
Expand Down Expand Up @@ -79,7 +82,7 @@ import qualified Data.ByteString.Lazy as BSL (toStrict)
-- cookie
import Web.Cookie (SetCookie, setCookieName , setCookieValue, setCookiePath, setCookieExpires, setCookieMaxAge, setCookieDomain, setCookieHttpOnly, setCookieSecure, setCookieSameSite, renderSetCookie, defaultSetCookie, CookiesText, parseCookiesText, SameSiteOption, sameSiteStrict, sameSiteNone, sameSiteLax)
-- scotty
import Web.Scotty.Action (ActionT, addHeader, header)
import Web.Scotty.Action (ActionT, addHeader, header, addHeader1, setHeader, setHeader1)

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

The import of ‘addHeader, addHeader1’

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The import of ‘addHeader, addHeader1’

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The import of ‘addHeader, addHeader1’

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The import of ‘addHeader, addHeader1’

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

The import of ‘addHeader, addHeader1’

Check warning on line 85 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

The import of ‘addHeader, addHeader1’
-- time
import Data.Time.Clock.POSIX ( posixSecondsToUTCTime )
-- text
Expand All @@ -91,20 +94,43 @@ import Web.Scotty.Util (decodeUtf8Lenient)
setCookie :: (MonadIO m)
=> SetCookie
-> ActionT m ()
setCookie c = addHeader "Set-Cookie"
setCookie = setCookieWith setHeader

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

Check warning on line 97 in Web/Scotty/Cookie.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of ‘setHeader’ (imported from Web.Scotty.Action):

-- | Set a cookie, with full access to its options (see 'SetCookie')
--
-- NB : sanitizes the cookie value by keeping only the first characters before '\r' or '\n'
setCookie1 :: MonadIO m
=> SetCookie
-> ActionT m ()
setCookie1 = setCookieWith setHeader1

-- | Set a cookie, with full access to its options (see 'SetCookie')
setCookieWith :: MonadIO m
=> (Text -> Text -> ActionT m ())
-> SetCookie
-> ActionT m ()
setCookieWith f c = f "Set-Cookie"
$ decodeUtf8Lenient
$ BSL.toStrict
$ toLazyByteString
$ renderSetCookie c


-- | 'makeSimpleCookie' and 'setCookie' combined.
setSimpleCookie :: (MonadIO m)
=> Text -- ^ name
-> Text -- ^ value
-> ActionT m ()
setSimpleCookie n v = setCookie $ makeSimpleCookie n v

-- | 'makeSimpleCookie' and 'setCookie1' combined.
--
-- NB : sanitizes the cookie value by keeping only the first characters before '\r' or '\n'
setSimpleCookie1 :: (MonadIO m)
=> Text -- ^ name
-> Text -- ^ value
-> ActionT m ()
setSimpleCookie1 n v = setCookie1 $ makeSimpleCookie n v

-- | Lookup one cookie name
getCookie :: (Monad m)
=> Text -- ^ name
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* Deprecate `StatusError`, `raise` and `raiseStatus` (#351)
* Add doctest, refactor some inline examples into doctests (#353)
* document "`defaultHandler` only applies to endpoints defined after it" (#237)
* add `setHeader1`, `addHeader1`, deprecate `setHeader`, `addHeader` (#94)
* add `setCookie1`, `setSimpleCookie1` (#94)

Breaking:

* `setCookie` uses `setHeader` rather than `addHeader` (as it should)

## 0.20.1 [2023.10.03]

Expand Down

0 comments on commit 42043a5

Please sign in to comment.