X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FMultipartForm.hs;h=a28a80461ede0d502d4de626121731c232c940cc;hp=16d8f28017425e35fe0273e9c39e7326bea458a1;hb=b1fac0a2cb1cafa008c0efa8ae4e14afbee0927f;hpb=e49345ce5e6c0190c826d130d51ec42ee9f09a67 diff --git a/Network/HTTP/Lucu/MultipartForm.hs b/Network/HTTP/Lucu/MultipartForm.hs index 16d8f28..a28a804 100644 --- a/Network/HTTP/Lucu/MultipartForm.hs +++ b/Network/HTTP/Lucu/MultipartForm.hs @@ -2,13 +2,14 @@ DoAndIfThenElse , FlexibleContexts , OverloadedStrings + , QuasiQuotes , RecordWildCards , ScopedTypeVariables , UnicodeSyntax , ViewPatterns #-} -- |Parse \"multipart/form-data\" based on RFC 2388: --- +-- -- -- You usually don't have to use this module directly. module Network.HTTP.Lucu.MultipartForm @@ -29,7 +30,6 @@ import qualified Data.ByteString.Lazy as LS import Data.ByteString.Lazy.Search import Data.Foldable import Data.List -import Data.Map (Map) import qualified Data.Map as M import Data.Maybe import Data.Monoid.Unicode @@ -38,10 +38,12 @@ import Data.Sequence.Unicode hiding ((∅)) import Data.Text (Text) import qualified Data.Text as T import Network.HTTP.Lucu.Headers -import Network.HTTP.Lucu.MIMEType +import Network.HTTP.Lucu.MIMEParams +import Network.HTTP.Lucu.MIMEType (MIMEType) +import qualified Network.HTTP.Lucu.MIMEType as MT +import Network.HTTP.Lucu.MIMEType.TH import Network.HTTP.Lucu.Parser import Network.HTTP.Lucu.Parser.Http -import Network.HTTP.Lucu.RFC2231 import Prelude.Unicode -- |'FormData' represents a form value and possibly an uploaded file @@ -66,7 +68,7 @@ data Part data ContDispo = ContDispo { dType ∷ !CIAscii - , dParams ∷ !(Map CIAscii Text) + , dParams ∷ !MIMEParams } printContDispo ∷ ContDispo → Ascii @@ -85,10 +87,10 @@ printContDispo d -- -- * \"Content-Transfer-Encoding\" is always ignored. -- --- * RFC 2388 says that non-ASCII field names are encoded according --- to the method in RFC 2047 --- , but they won't be --- decoded. +-- * RFC 2388 () says +-- that non-ASCII field names are encoded according to the method in +-- RFC 2047 (), but they won't +-- be decoded. parseMultipartFormData ∷ Ascii -- ^boundary → LS.ByteString -- ^input → Either String [(Ascii, FormData)] @@ -156,7 +158,7 @@ parsePart boundary src ⧺ e where defaultCType ∷ MIMEType - defaultCType = parseMIMEType "text/plain" + defaultCType = [mimeType| text/plain |] partHeader ∷ Parser Headers partHeader = crlf *> headers @@ -188,7 +190,7 @@ getContType hdrs Nothing → return Nothing Just str - → case parseOnly (finishOff mimeType) $ A.toByteString str of + → case parseOnly (finishOff MT.mimeType) $ A.toByteString str of Right d → return $ Just d Left err → throwError $ "malformed Content-Type: " ⧺ A.toString str @@ -229,7 +231,7 @@ partToFormPair pt@(Part {..}) partName ∷ MonadError String m ⇒ Part → m Ascii {-# INLINEABLE partName #-} partName (Part {..}) - = case M.lookup "name" $ dParams ptContDispo of + = case M.lookup "name" params of Just name → case A.fromText name of Just a → return a @@ -238,7 +240,10 @@ partName (Part {..}) Nothing → throwError $ "form-data without name: " ⧺ A.toString (printContDispo ptContDispo) + where + params = case dParams ptContDispo of + MIMEParams m → m partFileName ∷ Part → Maybe Text -partFileName (Part {..}) - = M.lookup "filename" $ dParams ptContDispo +partFileName (dParams ∘ ptContDispo → MIMEParams m) + = M.lookup "filename" m