From: PHO Date: Sun, 31 Jul 2011 04:29:27 +0000 (+0900) Subject: ETag X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=commitdiff_plain;h=50a37a0;p=Lucu.git ETag Ditz-issue: 8959dadc07db1bd363283dee401073f6e48dc7fa --- diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index 41e99f8..056f14f 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -1,8 +1,15 @@ +{-# LANGUAGE + OverloadedStrings + , UnicodeSyntax + #-} {-# OPTIONS_HADDOCK prune #-} -- |Manipulation of entity tags. module Network.HTTP.Lucu.ETag ( ETag(..) + + , printETag + , strongETag , weakETag , eTagP @@ -10,6 +17,11 @@ module Network.HTTP.Lucu.ETag ) 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 @@ -17,40 +29,42 @@ 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