X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FETag.hs;h=f7ef8387c7044d02acc1451a7fa0e140c3197c60;hb=ac2ff93;hp=c75394f5701e6b8fb92b2382d99e0be1a8787042;hpb=858129cb755aa09da2b7bd758efb8519f2c89103;p=Lucu.git diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index c75394f..f7ef838 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -1,58 +1,87 @@ --- #prune - +{-# LANGUAGE + OverloadedStrings + , UnicodeSyntax + #-} -- |Manipulation of entity tags. module Network.HTTP.Lucu.ETag ( ETag(..) + , parseETag + , printETag + , strongETag , weakETag , eTagP , eTagListP ) where - -import Control.Monad -import Network.HTTP.Lucu.Parser -import Network.HTTP.Lucu.Parser.Http -import Network.HTTP.Lucu.Utils +import Control.Applicative +import Control.Monad +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 { -- |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) - -instance Show ETag where - show (ETag isWeak token) = (if isWeak then - "W/" - else - "") - ++ - quoteStr token - --- |This is equivalent to @'ETag' False@. If you want to generate --- an ETag from a file, try using + , etagToken ∷ !Ascii + } deriving (Eq, Show) + +-- |Convert an 'ETag' to 'AsciiBuilder'. +printETag ∷ ETag → AsciiBuilder +{-# INLINEABLE printETag #-} +printETag 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 +{-# INLINEABLE parseETag #-} +parseETag str + = case parseOnly p $ A.toByteString str of + Right et → et + Left err → error ("unparsable ETag: " ⧺ A.toString str ⧺ ": " ⧺ err) + where + p ∷ Parser ETag + {-# INLINE p #-} + p = do et ← eTagP + endOfInput + return 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 +{-# INLINE strongETag #-} strongETag = ETag False --- |This is equivalent to @'ETag' True@. -weakETag :: String -> ETag +-- |This is equivalent to @'ETag' 'Prelude.True'@. +weakETag ∷ Ascii → ETag +{-# INLINE weakETag #-} weakETag = ETag True - -eTagP :: Parser ETag -eTagP = do isWeak <- option False (string "W/" >> return True) - str <- quotedStr +eTagP ∷ Parser ETag +{-# INLINEABLE eTagP #-} +eTagP = 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] +{-# INLINEABLE eTagListP #-} +eTagListP = do xs ← listOf eTagP + when (null xs) $ + fail "empty list of ETags" + return xs