+{-# LANGUAGE
+ OverloadedStrings
+ , UnicodeSyntax
+ #-}
{-# OPTIONS_HADDOCK prune #-}
-- |Manipulation of entity tags.
module Network.HTTP.Lucu.ETag
( ETag(..)
+
+ , printETag
+
, strongETag
, weakETag
, eTagP
)
where
import Control.Monad
+import Control.Monad.Unicode
+import Data.Ascii (Ascii)
+import qualified Data.Ascii as A
+import Data.Attoparsec.Char8
+import Data.Monoid.Unicode
import Network.HTTP.Lucu.Parser.Http hiding (token)
import Network.HTTP.Lucu.Utils
data ETag = ETag {
-- |The weakness flag. Weak tags looks like W\/\"blahblah\" and
-- strong tags are like \"blahblah\".
- etagIsWeak :: !Bool
+ etagIsWeak ∷ !Bool
-- |An opaque string. Only characters from 0x20 (sp) to 0x7e (~)
-- are allowed.
- , etagToken :: !String
- } deriving (Eq)
+ , etagToken ∷ !Ascii
+ } deriving (Eq, Show)
-instance Show ETag where
- show (ETag isWeak token) = (if isWeak then
- "W/"
- else
- "")
- ++
- quoteStr token
+-- |Convert an 'ETag' to 'Ascii'.
+printETag ∷ ETag → Ascii
+printETag et
+ = A.fromAsciiBuilder $
+ ( ( if etagIsWeak et then
+ A.toAsciiBuilder "W/"
+ else
+ (∅)
+ )
+ ⊕
+ quoteStr (etagToken et) )
-- |This is equivalent to @'ETag' 'Prelude.False'@. If you want to
-- generate an ETag from a file, try using
-- 'Network.HTTP.Lucu.StaticFile.generateETagFromFile'.
-strongETag :: String -> ETag
+strongETag ∷ Ascii → ETag
strongETag = ETag False
-- |This is equivalent to @'ETag' 'Prelude.True'@.
-weakETag :: String -> ETag
+weakETag ∷ Ascii → ETag
weakETag = ETag True
-
-eTagP :: Parser ETag
-eTagP = do isWeak <- option False (string "W/" >> return True)
- str <- quotedStr
+eTagP ∷ Parser ETag
+eTagP = try $
+ do isWeak ← option False (string "W/" ≫ return True)
+ str ← quotedStr
return $ ETag isWeak str
-
-eTagListP :: Parser [ETag]
-eTagListP = allowEOF
- $! do xs <- listOf eTagP
- when (null xs)
- $ fail ""
- return xs
+eTagListP ∷ Parser [ETag]
+eTagListP = do xs ← listOf eTagP
+ when (null xs) $
+ fail "empty list of ETags"
+ return xs