3 , GeneralizedNewtypeDeriving
4 , MultiParamTypeClasses
9 -- |An internal module for HTTP headers.
10 module Network.HTTP.Lucu.Headers
18 import Control.Applicative hiding (empty)
19 import Control.Applicative.Unicode hiding ((∅))
22 import Data.Ascii (Ascii, AsciiBuilder, CIAscii)
23 import qualified Data.Ascii as A
24 import Data.Attoparsec.Char8 as P
25 import Data.List (intersperse)
26 import qualified Data.Map as M (Map)
27 import Data.Collections
28 import Data.Collections.BaseInstances ()
30 import Data.Monoid.Unicode
31 import Network.HTTP.Lucu.Parser.Http
32 import Prelude hiding (filter, foldr, lookup, null)
33 import Prelude.Unicode
36 = Headers (M.Map CIAscii Ascii)
39 class HasHeaders a where
40 getHeaders ∷ a → Headers
41 setHeaders ∷ a → Headers → a
43 modifyHeaders ∷ (Headers → Headers) → a → a
44 {-# INLINE modifyHeaders #-}
45 modifyHeaders = (setHeaders ⊛) ∘ (∘ getHeaders)
47 getHeader ∷ CIAscii → a → Maybe Ascii
48 {-# INLINE getHeader #-}
49 getHeader = (∘ getHeaders) ∘ lookup
51 hasHeader ∷ CIAscii → a → Bool
52 {-# INLINE hasHeader #-}
53 hasHeader = (∘ getHeaders) ∘ member
55 getCIHeader ∷ CIAscii → a → Maybe CIAscii
56 {-# INLINE getCIHeader #-}
57 getCIHeader = ((A.toCIAscii <$>) ∘) ∘ getHeader
59 deleteHeader ∷ CIAscii → a → a
60 {-# INLINE deleteHeader #-}
61 deleteHeader = modifyHeaders ∘ delete
63 setHeader ∷ CIAscii → Ascii → a → a
64 {-# INLINE setHeader #-}
65 setHeader = (modifyHeaders ∘) ∘ insertWith const
67 instance HasHeaders Headers where
71 -- |@'insert' (key, val)@ merges @val@ with an existing one if any.
72 instance Unfoldable Headers (CIAscii, Ascii) where
74 insert (key, val) (Headers m)
75 = Headers $ insertWith merge key val m
79 {-# INLINE singleton #-}
81 = Headers $ singleton p
82 {-# INLINE insertMany #-}
83 insertMany f (Headers m)
84 = Headers $ insertMany f m
85 {-# INLINE insertManySorted #-}
86 insertManySorted f (Headers m)
87 = Headers $ insertManySorted f m
89 instance Foldable Headers (CIAscii, Ascii) where
91 null (Headers m) = null m
93 size (Headers m) = size m
95 foldr f b (Headers m) = foldr f b m
97 instance Collection Headers (CIAscii, Ascii) where
99 filter f (Headers m) = Headers $ filter f m
101 instance Indexed Headers CIAscii Ascii where
103 index k (Headers m) = index k m
104 {-# INLINE adjust #-}
105 adjust f k (Headers m) = Headers $ adjust f k m
106 {-# INLINE inDomain #-}
107 inDomain k (Headers m) = inDomain k m
109 instance Monoid Headers where
110 {-# INLINE mempty #-}
112 {-# INLINE mappend #-}
113 mappend (Headers α) (Headers β)
114 = Headers $ insertManySorted β α
116 instance Map Headers CIAscii Ascii where
117 {-# INLINE lookup #-}
118 lookup k (Headers m) = lookup k m
119 {-# INLINE insertWith #-}
120 insertWith f k v (Headers m)
121 = Headers $ insertWith f k v m
122 {-# INLINE mapWithKey #-}
123 mapWithKey f (Headers m)
124 = Headers $ mapWithKey f m
125 {-# INLINE unionWith #-}
126 unionWith f (Headers α) (Headers β)
127 = Headers $ unionWith f α β
128 {-# INLINE intersectionWith #-}
129 intersectionWith f (Headers α) (Headers β)
130 = Headers $ intersectionWith f α β
131 {-# INLINE differenceWith #-}
132 differenceWith f (Headers α) (Headers β)
133 = Headers $ differenceWith f α β
134 {-# INLINE isSubmapBy #-}
135 isSubmapBy f (Headers α) (Headers β)
137 {-# INLINE isProperSubmapBy #-}
138 isProperSubmapBy f (Headers α) (Headers β)
139 = isProperSubmapBy f α β
141 instance SortingCollection Headers (CIAscii, Ascii) where
142 {-# INLINE minView #-}
143 minView (Headers m) = second Headers <$> minView m
145 merge ∷ Ascii → Ascii → Ascii
148 | nullA a ∧ nullA b = (∅)
151 | otherwise = a ⊕ ", " ⊕ b
155 nullA = null ∘ A.toByteString
158 message-header = field-name ":" [ field-value ]
160 field-value = *( field-content | LWS )
161 field-content = <field-value を構成し、*TEXT あるいは
162 token, separators, quoted-string を連結
165 field-value の先頭および末尾にある LWS は全て削除され、それ以外の
168 headers ∷ Parser Headers
169 {-# INLINEABLE headers #-}
170 headers = do xs ← P.many header
172 return $ fromFoldable xs
174 header ∷ Parser (CIAscii, Ascii)
175 header = do name ← A.toCIAscii <$> token
178 values ← content `sepBy` try lws
181 return (name, joinValues values)
183 content ∷ Parser Ascii
184 {-# INLINE content #-}
185 content = A.unsafeFromByteString
187 takeWhile1 (\c → isText c ∧ c ≢ '\x20')
189 joinValues ∷ [Ascii] → Ascii
190 {-# INLINE joinValues #-}
191 joinValues = A.fromAsciiBuilder
193 ∘ intersperse (A.toAsciiBuilder "\x20")
194 ∘ map A.toAsciiBuilder
196 printHeaders ∷ Headers → AsciiBuilder
197 printHeaders (Headers m)
198 = mconcat (map printHeader (fromFoldable m)) ⊕
199 A.toAsciiBuilder "\x0D\x0A"
201 printHeader ∷ (CIAscii, Ascii) → AsciiBuilder
202 printHeader (name, value)
203 = A.toAsciiBuilder (A.fromCIAscii name) ⊕
204 A.toAsciiBuilder ": " ⊕
205 A.toAsciiBuilder value ⊕
206 A.toAsciiBuilder "\x0D\x0A"