X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FHeaders.hs;h=e6641154adc1d390b1c7cd35bb4d5d5a4c71342d;hb=db4a546d0d462cb94639b1f273bf0b78bccc960c;hp=e56567e7d665b191a96d08b75dfab9bc781f614c;hpb=0678be80d2cab7c670aba82659bde87ba84b926b;p=Lucu.git diff --git a/Network/HTTP/Lucu/Headers.hs b/Network/HTTP/Lucu/Headers.hs index e56567e..e664115 100644 --- a/Network/HTTP/Lucu/Headers.hs +++ b/Network/HTTP/Lucu/Headers.hs @@ -12,7 +12,6 @@ module Network.HTTP.Lucu.Headers ( Headers , HasHeaders(..) - , headers ) where import Control.Applicative hiding (empty) @@ -21,6 +20,8 @@ import Control.Monad import Data.Ascii (Ascii, AsciiBuilder, CIAscii) import qualified Data.Ascii as A import Data.Attoparsec.Char8 +import Data.Attoparsec.Parsable +import Data.ByteString (ByteString) import qualified Data.Collections.Newtype.TH as C import Data.Convertible.Base import Data.Convertible.Instances.Ascii () @@ -57,7 +58,7 @@ class HasHeaders a where getCIHeader ∷ CIAscii → a → Maybe CIAscii {-# INLINE getCIHeader #-} - getCIHeader = ((A.toCIAscii <$>) ∘) ∘ getHeader + getCIHeader = ((cs <$>) ∘) ∘ getHeader deleteHeader ∷ CIAscii → a → a {-# INLINE deleteHeader #-} @@ -138,30 +139,31 @@ deriveAttempts [ ([t| Headers |], [t| Ascii |]) field-value の先頭および末尾にある LWS は全て削除され、それ以外の LWS は單一の SP に變換される。 -} -headers ∷ Parser Headers -{-# INLINEABLE headers #-} -headers = do xs ← many header - crlf - return $ fromFoldable xs - where - header ∷ Parser (CIAscii, Ascii) - header = do name ← A.toCIAscii <$> token - void $ char ':' - skipMany lws - values ← content `sepBy` try lws - skipMany (try lws) - crlf - return (name, joinValues values) - - content ∷ Parser Ascii - {-# INLINE content #-} - content = A.unsafeFromByteString - <$> - takeWhile1 (\c → isText c ∧ c ≢ '\x20') - - joinValues ∷ [Ascii] → Ascii - {-# INLINE joinValues #-} - joinValues = A.fromAsciiBuilder - ∘ mconcat - ∘ intersperse (A.toAsciiBuilder "\x20") - ∘ (A.toAsciiBuilder <$>) +instance Parsable ByteString Headers where + {-# INLINEABLE parser #-} + parser = do xs ← many header + crlf + return $ fromFoldable xs + where + header ∷ Parser (CIAscii, Ascii) + {-# INLINEABLE header #-} + header = do name ← cs <$> token + void $ char ':' + skipMany lws + values ← content `sepBy` try lws + skipMany (try lws) + crlf + return (name, joinValues values) + + content ∷ Parser Ascii + {-# INLINEABLE content #-} + content = A.unsafeFromByteString + <$> + takeWhile1 (\c → isText c ∧ c ≢ '\x20') + + joinValues ∷ [Ascii] → Ascii + {-# INLINEABLE joinValues #-} + joinValues = cs + ∘ mconcat + ∘ intersperse (cs ("\x20" ∷ Ascii) ∷ AsciiBuilder) + ∘ (cs <$>)