X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FETag.hs;h=9bfa9aa29e67555499dc62bfecfa3b8b8f97b04b;hb=5582050;hp=acc496fa2113ef6a0848e570dffd56c453566f4b;hpb=e34910f85f459f049b9e6e6b79db9ef95dfccc13;p=Lucu.git diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index acc496f..9bfa9aa 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -2,12 +2,10 @@ OverloadedStrings , UnicodeSyntax #-} -{-# OPTIONS_HADDOCK prune #-} - -- |Manipulation of entity tags. module Network.HTTP.Lucu.ETag ( ETag(..) - + , parseETag , printETag , strongETag @@ -16,14 +14,15 @@ module Network.HTTP.Lucu.ETag , 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 { @@ -35,17 +34,29 @@ 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 @@ -58,7 +69,7 @@ weakETag ∷ Ascii → ETag 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