DoAndIfThenElse
, OverloadedStrings
, ScopedTypeVariables
- , UnboxedTuples
, UnicodeSyntax
#-}
module Network.HTTP.Lucu.MultipartForm
import Data.Attoparsec.Char8
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as LS
-import Data.Char
-import Data.List
+import Data.Char
+import Data.List
+import Data.Map (Map)
import Data.Maybe
import Data.Monoid.Unicode
import Data.Text (Text)
-import Network.HTTP.Lucu.Headers
-import Network.HTTP.Lucu.Parser.Http
-import Network.HTTP.Lucu.Response
-import Network.HTTP.Lucu.Utils
+import Network.HTTP.Lucu.Headers
+import Network.HTTP.Lucu.Parser.Http
+import Network.HTTP.Lucu.RFC2231
+import Network.HTTP.Lucu.Response
+import Network.HTTP.Lucu.Utils
import Prelude.Unicode
-- |This data type represents a form value and possibly an uploaded
data ContDispo
= ContDispo {
dType ∷ !CIAscii
- , dParams ∷ ![(CIAscii, Ascii)]
+ , dParams ∷ !(Map CIAscii Text)
}
printContDispo ∷ ContDispo → Ascii
= A.fromAsciiBuilder $
( A.toAsciiBuilder (A.fromCIAscii $ dType d)
⊕
- ( if null $ dParams d then
- (∅)
- else
- A.toAsciiBuilder "; " ⊕
- joinWith "; " (map printPair $ dParams d) ) )
- where
- printPair ∷ (CIAscii, Ascii) → AsciiBuilder
- printPair (name, value)
- = A.toAsciiBuilder (A.fromCIAscii name) ⊕
- A.toAsciiBuilder "=" ⊕
- ( if BS.any ((¬) ∘ isToken) $ A.toByteString value then
- quoteStr value
- else
- A.toAsciiBuilder value )
+ printParams (dParams d) )
multipartFormP ∷ Ascii → Parser [(Text, FormData)]
multipartFormP boundary
- = try $
- do parts ← many (partP boundary)
+ = do parts ← many $ try $ partP boundary
_ ← string "--"
_ ← string $ A.toByteString boundary
_ ← string "--"
partP ∷ Ascii → Parser Part
partP boundary
- = try $
- do _ ← string "--"
+ = do _ ← string "--"
_ ← string $ A.toByteString boundary
crlf
hs ← headersP
bodyP ∷ Ascii → Parser LS.ByteString
bodyP boundary
- = try $
- do body ← manyCharsTill anyChar $
+ = do body ← manyCharsTill anyChar $
try $
do crlf
_ ← string "--"
])
contDispoP ∷ Parser ContDispo
-contDispoP = try $
- do dispoType ← A.toCIAscii <$> token
+contDispoP = do dispoType ← A.toCIAscii <$> token
params ← many paramP
return $ ContDispo dispoType params
where