{-# LANGUAGE OverloadedStrings #-}
module Network.HTTP.Client.MultipartFormData
(
Part
,PartM
,partName
,partFilename
,partContentType
,partHeaders
,partGetBody
,partBS
,partLBS
,partFile
,partFileSource
,partFileSourceChunked
,partFileRequestBody
,partFileRequestBodyM
,addPartHeaders
,formDataBody
,formDataBodyWithBoundary
,webkitBoundary
,webkitBoundaryPure
,renderParts
,renderPart
) where
import Network.HTTP.Client hiding (streamFile)
import Network.Mime
import Network.HTTP.Types (hContentType, methodPost, Header())
import Data.Monoid ((<>))
import Data.Foldable (foldMap)
import Blaze.ByteString.Builder
import Data.Text
import qualified Data.Text.Encoding as TE
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as BS
import qualified Data.CaseInsensitive as CI
import Control.Monad.Trans.State.Strict (state, runState)
import Control.Monad.IO.Class
import System.FilePath
import System.Random
import Data.Array.Base
import System.IO
import Data.Bits
import Data.Word
import Data.Monoid (Monoid(..))
import Control.Monad
import Data.ByteString.Lazy.Internal (defaultChunkSize)
type Part = PartM IO
data PartM m = Part
{ forall (m :: * -> *). PartM m -> Text
partName :: Text
, forall (m :: * -> *). PartM m -> Maybe String
partFilename :: Maybe String
, forall (m :: * -> *). PartM m -> Maybe MimeType
partContentType :: Maybe MimeType
, :: [Header]
, forall (m :: * -> *). PartM m -> m RequestBody
partGetBody :: m RequestBody
}
instance Show (PartM m) where
showsPrec :: Int -> PartM m -> ShowS
showsPrec Int
d (Part Text
n Maybe String
f Maybe MimeType
c [Header]
h m RequestBody
_) =
Bool -> ShowS -> ShowS
showParen (Int
dforall a. Ord a => a -> a -> Bool
>=Int
11) forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"Part "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Text
n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Maybe String
f
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Maybe MimeType
c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 [Header]
h
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"<m (RequestBody m)>"
partBS :: Applicative m
=> Text
-> BS.ByteString
-> PartM m
partBS :: forall (m :: * -> *). Applicative m => Text -> MimeType -> PartM m
partBS Text
n MimeType
b = forall (m :: * -> *).
Text
-> Maybe String
-> Maybe MimeType
-> [Header]
-> m RequestBody
-> PartM m
Part Text
n forall a. Monoid a => a
Data.Monoid.mempty forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ MimeType -> RequestBody
RequestBodyBS MimeType
b
partLBS :: Applicative m
=> Text
-> BL.ByteString
-> PartM m
partLBS :: forall (m :: * -> *).
Applicative m =>
Text -> ByteString -> PartM m
partLBS Text
n ByteString
b = forall (m :: * -> *).
Text
-> Maybe String
-> Maybe MimeType
-> [Header]
-> m RequestBody
-> PartM m
Part Text
n forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteString -> RequestBody
RequestBodyLBS ByteString
b
partFile :: Text
-> FilePath
-> Part
partFile :: Text -> String -> Part
partFile Text
n String
f =
forall (m :: * -> *). Text -> String -> m RequestBody -> PartM m
partFileRequestBodyM Text
n String
f forall a b. (a -> b) -> a -> b
$ do
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM MimeType -> RequestBody
RequestBodyBS forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO MimeType
BS.readFile String
f
partFileSource :: Text
-> FilePath
-> Part
partFileSource :: Text -> String -> Part
partFileSource Text
n String
f =
forall (m :: * -> *). Text -> String -> m RequestBody -> PartM m
partFileRequestBodyM Text
n String
f forall a b. (a -> b) -> a -> b
$ do
Integer
size <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile String
f IOMode
ReadMode Handle -> IO Integer
hFileSize
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Int64 -> GivesPopper () -> RequestBody
RequestBodyStream (forall a. Num a => Integer -> a
fromInteger Integer
size) forall a b. (a -> b) -> a -> b
$ String -> GivesPopper ()
streamFile String
f
streamFile :: FilePath -> GivesPopper ()
streamFile :: String -> GivesPopper ()
streamFile String
fp NeedsPopper ()
np =
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
fp IOMode
ReadMode forall a b. (a -> b) -> a -> b
$ NeedsPopper ()
np forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO MimeType
go
where
go :: Handle -> IO MimeType
go Handle
h = Handle -> Int -> IO MimeType
BS.hGetSome Handle
h Int
defaultChunkSize
partFileSourceChunked :: Text -> FilePath -> Part
partFileSourceChunked :: Text -> String -> Part
partFileSourceChunked Text
n String
f =
forall (m :: * -> *).
Applicative m =>
Text -> String -> RequestBody -> PartM m
partFileRequestBody Text
n String
f forall a b. (a -> b) -> a -> b
$ do
GivesPopper () -> RequestBody
RequestBodyStreamChunked forall a b. (a -> b) -> a -> b
$ String -> GivesPopper ()
streamFile String
f
partFileRequestBody :: Applicative m
=> Text
-> FilePath
-> RequestBody
-> PartM m
partFileRequestBody :: forall (m :: * -> *).
Applicative m =>
Text -> String -> RequestBody -> PartM m
partFileRequestBody Text
n String
f RequestBody
rqb =
forall (m :: * -> *). Text -> String -> m RequestBody -> PartM m
partFileRequestBodyM Text
n String
f forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure RequestBody
rqb
partFileRequestBodyM :: Text
-> FilePath
-> m RequestBody
-> PartM m
partFileRequestBodyM :: forall (m :: * -> *). Text -> String -> m RequestBody -> PartM m
partFileRequestBodyM Text
n String
f m RequestBody
rqb =
forall (m :: * -> *).
Text
-> Maybe String
-> Maybe MimeType
-> [Header]
-> m RequestBody
-> PartM m
Part Text
n (forall a. a -> Maybe a
Just String
f) (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> MimeType
defaultMimeLookup forall a b. (a -> b) -> a -> b
$ String -> Text
pack String
f) forall a. Monoid a => a
mempty m RequestBody
rqb
{-# INLINE cp #-}
cp :: BS.ByteString -> RequestBody
cp :: MimeType -> RequestBody
cp MimeType
bs = Int64 -> Builder -> RequestBody
RequestBodyBuilder (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ MimeType -> Int
BS.length MimeType
bs) forall a b. (a -> b) -> a -> b
$ MimeType -> Builder
copyByteString MimeType
bs
addPartHeaders :: PartM m -> [Header] -> PartM m
PartM m
p [Header]
hs = PartM m
p { partHeaders :: [Header]
partHeaders = forall (m :: * -> *). PartM m -> [Header]
partHeaders PartM m
p forall a. Semigroup a => a -> a -> a
<> [Header]
hs }
renderPart :: Functor m
=> BS.ByteString
-> PartM m -> m RequestBody
renderPart :: forall (m :: * -> *).
Functor m =>
MimeType -> PartM m -> m RequestBody
renderPart MimeType
boundary (Part Text
name Maybe String
mfilename Maybe MimeType
mcontenttype [Header]
hdrs m RequestBody
get) = RequestBody -> RequestBody
render forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m RequestBody
get
where render :: RequestBody -> RequestBody
render RequestBody
renderBody =
MimeType -> RequestBody
cp MimeType
"--" forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
boundary forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"\r\n"
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"Content-Disposition: form-data; name=\""
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
RequestBodyBS (Text -> MimeType
TE.encodeUtf8 Text
name)
forall a. Semigroup a => a -> a -> a
<> (case Maybe String
mfilename of
Just String
f -> MimeType -> RequestBody
cp MimeType
"\"; filename=\""
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
RequestBodyBS (Text -> MimeType
TE.encodeUtf8 forall a b. (a -> b) -> a -> b
$ String -> Text
pack forall a b. (a -> b) -> a -> b
$ ShowS
takeFileName String
f)
Maybe String
_ -> forall a. Monoid a => a
mempty)
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"\""
forall a. Semigroup a => a -> a -> a
<> (case Maybe MimeType
mcontenttype of
Just MimeType
ct -> MimeType -> RequestBody
cp MimeType
"\r\n"
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"Content-Type: "
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
ct
Maybe MimeType
_ -> forall a. Monoid a => a
mempty)
forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Data.Foldable.foldMap (\(CI MimeType
k, MimeType
v) ->
MimeType -> RequestBody
cp MimeType
"\r\n"
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp (forall s. CI s -> s
CI.original CI MimeType
k)
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
": "
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
v) [Header]
hdrs
forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"\r\n\r\n"
forall a. Semigroup a => a -> a -> a
<> RequestBody
renderBody forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"\r\n"
renderParts :: Applicative m
=> BS.ByteString
-> [PartM m] -> m RequestBody
renderParts :: forall (m :: * -> *).
Applicative m =>
MimeType -> [PartM m] -> m RequestBody
renderParts MimeType
boundary [PartM m]
parts = (RequestBody -> RequestBody
fin forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (forall (m :: * -> *).
Functor m =>
MimeType -> PartM m -> m RequestBody
renderPart MimeType
boundary) [PartM m]
parts
where fin :: RequestBody -> RequestBody
fin = (forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"--" forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
boundary forall a. Semigroup a => a -> a -> a
<> MimeType -> RequestBody
cp MimeType
"--\r\n")
webkitBoundary :: IO BS.ByteString
webkitBoundary :: IO MimeType
webkitBoundary = forall (m :: * -> *) a. MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom forall g. RandomGen g => g -> (MimeType, g)
webkitBoundaryPure
webkitBoundaryPure :: RandomGen g => g -> (BS.ByteString, g)
webkitBoundaryPure :: forall g. RandomGen g => g -> (MimeType, g)
webkitBoundaryPure g
g = (forall s a. State s a -> s -> (a, s)
`runState` g
g) forall a b. (a -> b) -> a -> b
$ do
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (MimeType -> MimeType -> MimeType
BS.append MimeType
prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> MimeType
BS.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
Prelude.concat) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
4 forall a b. (a -> b) -> a -> b
$ do
Int
randomness <- forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state forall a b. (a -> b) -> a -> b
$ forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (m :: * -> *) a. Monad m => a -> m a
return [forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt UArray Int Word8
alphaNumericEncodingMap forall a b. (a -> b) -> a -> b
$ Int
randomness forall a. Bits a => a -> Int -> a
`shiftR` Int
24 forall a. Bits a => a -> a -> a
.&. Int
0x3F
,forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt UArray Int Word8
alphaNumericEncodingMap forall a b. (a -> b) -> a -> b
$ Int
randomness forall a. Bits a => a -> Int -> a
`shiftR` Int
16 forall a. Bits a => a -> a -> a
.&. Int
0x3F
,forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt UArray Int Word8
alphaNumericEncodingMap forall a b. (a -> b) -> a -> b
$ Int
randomness forall a. Bits a => a -> Int -> a
`shiftR` Int
8 forall a. Bits a => a -> a -> a
.&. Int
0x3F
,forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt UArray Int Word8
alphaNumericEncodingMap forall a b. (a -> b) -> a -> b
$ Int
randomness forall a. Bits a => a -> a -> a
.&. Int
0x3F]
where
prefix :: MimeType
prefix = MimeType
"----WebKitFormBoundary"
alphaNumericEncodingMap :: UArray Int Word8
alphaNumericEncodingMap :: UArray Int Word8
alphaNumericEncodingMap = forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
0, Int
63)
[Word8
0x41, Word8
0x42, Word8
0x43, Word8
0x44, Word8
0x45, Word8
0x46, Word8
0x47, Word8
0x48,
Word8
0x49, Word8
0x4A, Word8
0x4B, Word8
0x4C, Word8
0x4D, Word8
0x4E, Word8
0x4F, Word8
0x50,
Word8
0x51, Word8
0x52, Word8
0x53, Word8
0x54, Word8
0x55, Word8
0x56, Word8
0x57, Word8
0x58,
Word8
0x59, Word8
0x5A, Word8
0x61, Word8
0x62, Word8
0x63, Word8
0x64, Word8
0x65, Word8
0x66,
Word8
0x67, Word8
0x68, Word8
0x69, Word8
0x6A, Word8
0x6B, Word8
0x6C, Word8
0x6D, Word8
0x6E,
Word8
0x6F, Word8
0x70, Word8
0x71, Word8
0x72, Word8
0x73, Word8
0x74, Word8
0x75, Word8
0x76,
Word8
0x77, Word8
0x78, Word8
0x79, Word8
0x7A, Word8
0x30, Word8
0x31, Word8
0x32, Word8
0x33,
Word8
0x34, Word8
0x35, Word8
0x36, Word8
0x37, Word8
0x38, Word8
0x39, Word8
0x41, Word8
0x42]
formDataBody :: MonadIO m => [Part] -> Request -> m Request
formDataBody :: forall (m :: * -> *). MonadIO m => [Part] -> Request -> m Request
formDataBody [Part]
a Request
b = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
MimeType
boundary <- IO MimeType
webkitBoundary
forall (m :: * -> *).
Applicative m =>
MimeType -> [PartM m] -> Request -> m Request
formDataBodyWithBoundary MimeType
boundary [Part]
a Request
b
formDataBodyWithBoundary :: Applicative m => BS.ByteString -> [PartM m] -> Request -> m Request
formDataBodyWithBoundary :: forall (m :: * -> *).
Applicative m =>
MimeType -> [PartM m] -> Request -> m Request
formDataBodyWithBoundary MimeType
boundary [PartM m]
parts Request
req = do
(\ RequestBody
body -> Request
req
{ method :: MimeType
method = MimeType
methodPost
, requestHeaders :: [Header]
requestHeaders =
(CI MimeType
hContentType, MimeType
"multipart/form-data; boundary=" forall a. Semigroup a => a -> a -> a
<> MimeType
boundary)
forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [a]
Prelude.filter (\(CI MimeType
x, MimeType
_) -> CI MimeType
x forall a. Eq a => a -> a -> Bool
/= CI MimeType
hContentType) (Request -> [Header]
requestHeaders Request
req)
, requestBody :: RequestBody
requestBody = RequestBody
body
}) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
Applicative m =>
MimeType -> [PartM m] -> m RequestBody
renderParts MimeType
boundary [PartM m]
parts