5 -- |Manipulation of entity tags.
6 module Network.HTTP.Lucu.ETag
17 import Control.Applicative
19 import Data.Ascii (Ascii, AsciiBuilder)
20 import qualified Data.Ascii as A
21 import Data.Attoparsec.Char8
22 import Data.Monoid.Unicode
23 import Network.HTTP.Lucu.Parser.Http hiding (token)
24 import Network.HTTP.Lucu.Utils
25 import Prelude.Unicode
27 -- |An entity tag is made of a weakness flag and a opaque string.
29 -- |The weakness flag. Weak tags looks like W\/\"blahblah\" and
30 -- strong tags are like \"blahblah\".
32 -- |An opaque string. Only characters from 0x20 (sp) to 0x7e (~)
37 -- |Convert an 'ETag' to 'AsciiBuilder'.
38 printETag ∷ ETag → AsciiBuilder
39 {-# INLINEABLE printETag #-}
41 = ( if etagIsWeak et then
47 quoteStr (etagToken et)
49 -- |Parse 'Etag' from an 'Ascii'. This functions throws an exception
51 parseETag ∷ Ascii → ETag
52 {-# INLINEABLE parseETag #-}
54 = case parseOnly p $ A.toByteString str of
56 Left err → error ("unparsable ETag: " ⧺ A.toString str ⧺ ": " ⧺ err)
64 -- |This is equivalent to @'ETag' 'Prelude.False'@. If you want to
65 -- generate an ETag from a file, try using
66 -- 'Network.HTTP.Lucu.StaticFile.generateETagFromFile'.
67 strongETag ∷ Ascii → ETag
68 {-# INLINE strongETag #-}
69 strongETag = ETag False
71 -- |This is equivalent to @'ETag' 'Prelude.True'@.
72 weakETag ∷ Ascii → ETag
73 {-# INLINE weakETag #-}
77 {-# INLINEABLE eTagP #-}
78 eTagP = do isWeak ← option False (string "W/" *> return True)
80 return $ ETag isWeak str
82 eTagListP ∷ Parser [ETag]
83 {-# INLINEABLE eTagListP #-}
84 eTagListP = do xs ← listOf eTagP
86 fail "empty list of ETags"