OverloadedStrings
, UnicodeSyntax
#-}
-{-# OPTIONS_HADDOCK prune #-}
-
-- |Manipulation of entity tags.
module Network.HTTP.Lucu.ETag
( ETag(..)
-
+ , parseETag
, printETag
, strongETag
, eTagListP
)
where
+import Control.Applicative
import Control.Monad
-import Control.Monad.Unicode
-import Data.Ascii (Ascii)
+import Data.Ascii (Ascii, AsciiBuilder)
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
+import Prelude.Unicode
-- |An entity tag is made of a weakness flag and a opaque string.
data ETag = ETag {
, etagToken ∷ !Ascii
} deriving (Eq, Show)
--- |Convert an 'ETag' to 'Ascii'.
-printETag ∷ ETag → Ascii
+-- |Convert an 'ETag' to 'AsciiBuilder'.
+printETag ∷ ETag → AsciiBuilder
printETag et
- = A.fromAsciiBuilder $
- ( ( if etagIsWeak et then
- A.toAsciiBuilder "W/"
- else
- (∅)
- )
- ⊕
- quoteStr (etagToken et) )
+ = ( if etagIsWeak et then
+ A.toAsciiBuilder "W/"
+ else
+ (∅)
+ )
+ ⊕
+ quoteStr (etagToken et)
+
+-- |Parse 'Etag' from an 'Ascii'. This functions throws an exception
+-- for parse error.
+parseETag ∷ Ascii → ETag
+parseETag str
+ = let p = do et ← eTagP
+ endOfInput
+ return et
+ bs = A.toByteString str
+ in
+ case parseOnly p bs of
+ Right et → et
+ Left err → error ("unparsable ETag: " ⧺ A.toString str ⧺ ": " ⧺ err)
-- |This is equivalent to @'ETag' 'Prelude.False'@. If you want to
-- generate an ETag from a file, try using
weakETag = ETag True
eTagP ∷ Parser ETag
-eTagP = do isWeak ← option False (string "W/" ≫ return True)
+eTagP = do isWeak ← option False (string "W/" *> return True)
str ← quotedStr
return $ ETag isWeak str