X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FETag.hs;h=b8191a353fd86dd05c325b3d99d872d5a34e3e9b;hb=4e41b11;hp=acc496fa2113ef6a0848e570dffd56c453566f4b;hpb=b923d454928e3d01134b15d6072b6d3edf7a15ca;p=Lucu.git diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index acc496f..b8191a3 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -1,69 +1,98 @@ {-# LANGUAGE - OverloadedStrings + DeriveDataTypeable + , OverloadedStrings + , RecordWildCards + , TemplateHaskell , UnicodeSyntax #-} -{-# OPTIONS_HADDOCK prune #-} - --- |Manipulation of entity tags. +-- |Entity tags module Network.HTTP.Lucu.ETag ( ETag(..) - + , parseETag , printETag , strongETag , weakETag - , eTagP - , eTagListP + , eTag + , eTagList ) 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.Data import Data.Monoid.Unicode +import Language.Haskell.TH.Syntax +import Network.HTTP.Lucu.Parser 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. +-- |An entity tag consists of a weakness flag and an opaque string. data ETag = ETag { -- |The weakness flag. Weak tags looks like W\/\"blahblah\" and - -- strong tags are like \"blahblah\". + -- strong tags are like \"blahblah\". See: + -- etagIsWeak ∷ !Bool -- |An opaque string. Only characters from 0x20 (sp) to 0x7e (~) -- are allowed. , etagToken ∷ !Ascii - } deriving (Eq, Show) + } deriving (Eq, Show, Data, Typeable) --- |Convert an 'ETag' to 'Ascii'. -printETag ∷ ETag → Ascii +instance Lift ETag where + lift (ETag {..}) + = [| ETag { + etagIsWeak = $(lift etagIsWeak) + , etagToken = $(liftAscii etagToken) + } + |] + +-- |Convert an 'ETag' to an 'AsciiBuilder'. +printETag ∷ ETag → AsciiBuilder +{-# INLINEABLE printETag #-} 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 +{-# INLINEABLE parseETag #-} +parseETag str + = case parseOnly (finishOff eTag) $ A.toByteString str 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 +-- |This is equivalent to @'ETag' 'False'@. If you want to generate an +-- ETag from a file, try using -- 'Network.HTTP.Lucu.StaticFile.generateETagFromFile'. strongETag ∷ Ascii → ETag +{-# INLINE strongETag #-} strongETag = ETag False --- |This is equivalent to @'ETag' 'Prelude.True'@. +-- |This is equivalent to @'ETag' 'True'@. weakETag ∷ Ascii → ETag +{-# INLINE weakETag #-} weakETag = ETag True -eTagP ∷ Parser ETag -eTagP = do isWeak ← option False (string "W/" ≫ return True) - str ← quotedStr - return $ ETag isWeak str +-- |'Parser' for an 'ETag'. +eTag ∷ Parser ETag +{-# INLINEABLE eTag #-} +eTag = do isWeak ← option False (string "W/" *> return True) + str ← quotedStr + return $ ETag isWeak str -eTagListP ∷ Parser [ETag] -eTagListP = do xs ← listOf eTagP - when (null xs) $ - fail "empty list of ETags" - return xs +-- |'Parser' for a list of 'ETag's. +eTagList ∷ Parser [ETag] +{-# INLINEABLE eTagList #-} +eTagList = do xs ← listOf eTag + when (null xs) $ + fail "empty list of ETags" + return xs