@@ -51,13 +51,9 @@ import qualified Network.HTTP.Types as H
51import Data.Either (partitionEithers)
51import Data.Either (partitionEithers)
52import Control.Monad (when, unless)
52import Control.Monad (when, unless)
53import Control.Monad.Trans.Class (lift)
53import Control.Monad.Trans.Class (lift)
54−import Control.Monad.Trans.Resource (allocate, release, register)
54+import Control.Monad.Trans.Resource (allocate, release, register, InternalState, runInternalState)
55−#if MIN_VERSION_conduit(1, 0, 0)
56import Data.Conduit.Internal (Pipe (NeedInput, HaveOutput), (>+>), withUpstream, injectLeftovers, ConduitM (..))
55import Data.Conduit.Internal (Pipe (NeedInput, HaveOutput), (>+>), withUpstream, injectLeftovers, ConduitM (..))
57import Data.Void (Void)
56import Data.Void (Void)
59−import Data.Conduit.Internal (sinkToPipe)
62breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
58breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
63breakDiscard w s =
59breakDiscard w s =
@@ -130,7 +126,7 @@ type File y = (S.ByteString, FileInfo y)
130-- type, and returns a `Sink` for storing the contents.
126-- type, and returns a `Sink` for storing the contents.
131type BackEnd a = S.ByteString -- ^ parameter name
127type BackEnd a = S.ByteString -- ^ parameter name
132 -> FileInfo ()
128 -> FileInfo ()
133− -> Sink S.ByteString IO a
129+ -> Sink S.ByteString (ResourceT IO) a
135data RequestBodyType = UrlEncoded | Multipart S.ByteString
131data RequestBodyType = UrlEncoded | Multipart S.ByteString
@@ -173,30 +169,28 @@ parseRequestBody :: BackEnd y
173parseRequestBody s r =
169parseRequestBody s r =
174 case getRequestBodyType r of
170 case getRequestBodyType r of
175 Nothing -> return ([], [])
171 Nothing -> return ([], [])
176− Just rbt -> fmap partitionEithers $ requestBody r $$ conduitRequestBody s rbt =$ CL.consume
172+ Just rbt -> fmap partitionEithers $ requestBody r $$ conduitRequestBody (resourceInternalState r) s rbt =$ CL.consume
178−sinkRequestBody :: BackEnd y
174+sinkRequestBody :: InternalState
179 -> RequestBodyType
176 -> RequestBodyType
180 -> Sink S.ByteString IO ([Param], [File y])
177 -> Sink S.ByteString IO ([Param], [File y])
181−sinkRequestBody s r = fmap partitionEithers $ conduitRequestBody s r =$ CL.consume
178+sinkRequestBody internalState s r = fmap partitionEithers $ conduitRequestBody internalState s r =$ CL.consume
183−conduitRequestBody :: BackEnd y
180+conduitRequestBody :: InternalState
184 -> RequestBodyType
182 -> RequestBodyType
185 -> Conduit S.ByteString IO (Either Param (File y))
183 -> Conduit S.ByteString IO (Either Param (File y))
186−conduitRequestBody _ UrlEncoded = do
184+conduitRequestBody _ _ UrlEncoded = do
187 -- NOTE: in general, url-encoded data will be in a single chunk.
185 -- NOTE: in general, url-encoded data will be in a single chunk.
188 -- Therefore, I'm optimizing for the usual case by sticking with
186 -- Therefore, I'm optimizing for the usual case by sticking with
189 -- strict byte strings here.
187 -- strict byte strings here.
190 bs <- CL.consume
188 bs <- CL.consume
191 mapM_ yield $ map Left $ H.parseSimpleQuery $ S.concat bs
189 mapM_ yield $ map Left $ H.parseSimpleQuery $ S.concat bs
192−conduitRequestBody backend (Multipart bound) =
190+conduitRequestBody internalState backend (Multipart bound) =
193− parsePieces backend $ S8.pack "--" `S.append` bound
191+ parsePieces internalState backend $ S8.pack "--" `S.append` bound
195−#if MIN_VERSION_conduit(1, 0, 0)
196takeLine :: Monad m => Consumer S.ByteString m (Maybe S.ByteString)
193takeLine :: Monad m => Consumer S.ByteString m (Maybe S.ByteString)
198−takeLine :: Monad m => Pipe S.ByteString S.ByteString o u m (Maybe S.ByteString)
200takeLine =
194takeLine =
@@ -211,11 +205,7 @@ takeLine =
211 when (S.length y > 1) $ leftover $ S.drop 1 y
205 when (S.length y > 1) $ leftover $ S.drop 1 y
212 return $ Just $ killCR x
206 return $ Just $ killCR x
214−#if MIN_VERSION_conduit(1, 0, 0)
215takeLines :: Consumer S.ByteString IO [S.ByteString]
208takeLines :: Consumer S.ByteString IO [S.ByteString]
217−takeLines :: Pipe S.ByteString S.ByteString o u IO [S.ByteString]
219takeLines = do
209takeLines = do
220 res <- takeLine
210 res <- takeLine
221 case res of
211 case res of
@@ -226,14 +216,11 @@ takeLines = do
226 ls <- takeLines
216 ls <- takeLines
227 return $ l : ls
217 return $ l : ls
219+parsePieces :: InternalState
229−parsePieces :: BackEnd y
220+ -> BackEnd y
230 -> S.ByteString
221 -> S.ByteString
231−#if MIN_VERSION_conduit(1, 0, 0)
232 -> ConduitM S.ByteString (Either Param (File y)) IO ()
222 -> ConduitM S.ByteString (Either Param (File y)) IO ()
234− -> Pipe S.ByteString S.ByteString (Either Param (File y)) u IO ()
236−parsePieces sink bound =
223+parsePieces internalState sink bound =
239 loop = do
226 loop = do
@@ -251,7 +238,7 @@ parsePieces sink bound =
251 Just (mct, name, Just filename) -> do
238 Just (mct, name, Just filename) -> do
252 let ct = fromMaybe "application/octet-stream" mct
239 let ct = fromMaybe "application/octet-stream" mct
253 fi0 = FileInfo filename ct ()
240 fi0 = FileInfo filename ct ()
254− (wasFound, y) <- sinkTillBound' bound name fi0 sink
241+ (wasFound, y) <- sinkTillBound' internalState bound name fi0 sink
255 yield $ Right (name, fi0 { fileContent = y })
242 yield $ Right (name, fi0 { fileContent = y })
256 when wasFound loop
243 when wasFound loop
257 Just (_ct, name, Nothing) -> do
244 Just (_ct, name, Nothing) -> do
@@ -303,45 +290,30 @@ findBound b bs = handleBreak $ Search.breakOn b bs
303 | S.index b x == S.index bs y = mismatch xs ys
290 | S.index b x == S.index bs y = mismatch xs ys
304 | otherwise = True
291 | otherwise = True
293+sinkTillBound' :: InternalState
306−sinkTillBound' :: S.ByteString
294+ -> S.ByteString
307 -> S.ByteString
295 -> S.ByteString
308 -> FileInfo ()
296 -> FileInfo ()
309 -> BackEnd y
297 -> BackEnd y
310−#if MIN_VERSION_conduit(1, 0, 0)
311 -> ConduitM S.ByteString o IO (Bool, y)
298 -> ConduitM S.ByteString o IO (Bool, y)
313− -> Pipe S.ByteString S.ByteString o u IO (Bool, y)
315−sinkTillBound' bound name fi sink =
299+sinkTillBound' internalState bound name fi sink =
316−#if MIN_VERSION_conduit(1, 0, 0)
317 ConduitM $ anyOutput $
300 ConduitM $ anyOutput $
319 conduitTillBound bound >+> withUpstream (fix $ sink name fi)
301 conduitTillBound bound >+> withUpstream (fix $ sink name fi)
321−#if MIN_VERSION_conduit(1, 0, 0)
322− fix :: Sink S8.ByteString IO y -> Pipe Void S8.ByteString Void Bool IO y
303+ fix :: Sink S8.ByteString (ResourceT IO) y -> Pipe Void S8.ByteString Void Bool IO y
323− fix (ConduitM p) = ignoreTerm >+> injectLeftovers p
304+ fix p = ignoreTerm >+> injectLeftovers (unConduitM $ transPipe (flip runInternalState internalState) p)
324 ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
305 ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
325 await' = NeedInput (return . Just) (const $ return Nothing)
306 await' = NeedInput (return . Just) (const $ return Nothing)
326 yield' = HaveOutput (return ()) (return ())
307 yield' = HaveOutput (return ()) (return ())
328 anyOutput p = p >+> dropInput
309 anyOutput p = p >+> dropInput
329 dropInput = NeedInput (const dropInput) return
310 dropInput = NeedInput (const dropInput) return
334conduitTillBound :: Monad m
312conduitTillBound :: Monad m
335 => S.ByteString -- bound
313 => S.ByteString -- bound
336−#if MIN_VERSION_conduit(1, 0, 0)
337 -> Pipe S.ByteString S.ByteString S.ByteString () m Bool
314 -> Pipe S.ByteString S.ByteString S.ByteString () m Bool
339− -> Pipe S.ByteString S.ByteString S.ByteString u m Bool
341conduitTillBound bound =
315conduitTillBound bound =
342−#if MIN_VERSION_conduit(1, 0, 0)
343 unConduitM $
316 unConduitM $
347 go front = await >>= maybe (close front) (push front)
319 go front = await >>= maybe (close front) (push front)
@@ -371,26 +343,16 @@ conduitTillBound bound =
371sinkTillBound :: S.ByteString
343sinkTillBound :: S.ByteString
372 -> (x -> S.ByteString -> IO x)
344 -> (x -> S.ByteString -> IO x)
374−#if MIN_VERSION_conduit(1, 0, 0)
375 -> Consumer S.ByteString IO (Bool, x)
346 -> Consumer S.ByteString IO (Bool, x)
377− -> Pipe S.ByteString S.ByteString o u IO (Bool, x)
379sinkTillBound bound iter seed0 =
347sinkTillBound bound iter seed0 =
380−#if MIN_VERSION_conduit(1, 0, 0)
381 ConduitM $
348 ConduitM $
383 (conduitTillBound bound >+> (withUpstream $ ij $ CL.foldM iter' seed0))
349 (conduitTillBound bound >+> (withUpstream $ ij $ CL.foldM iter' seed0))
385 iter' a b = liftIO $ iter a b
351 iter' a b = liftIO $ iter a b
386−#if MIN_VERSION_conduit(1, 0, 0)
387 ij (ConduitM p) = ignoreTerm >+> injectLeftovers p
352 ij (ConduitM p) = ignoreTerm >+> injectLeftovers p
388 ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
353 ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
389 await' = NeedInput (return . Just) (const $ return Nothing)
354 await' = NeedInput (return . Just) (const $ return Nothing)
390 yield' = HaveOutput (return ()) (return ())
355 yield' = HaveOutput (return ()) (return ())
395parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
357parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
396parseAttrs = map go . S.split 59 -- semicolon
358parseAttrs = map go . S.split 59 -- semicolon