on master help sign in
Removed enumerator from wai-extra
commit63fac2b6ca715a1a41c0cdb2eb9320887103c895 authorMichael Snoyman <michael@snoyman.com> dateTue 27 Dec 2011, 11:37 (15 years ago) parent846f365
4 files changed+50−79
wai-extra/Network/Wai/Middleware/RequestLogger.hs+1−2 wai-extra/Network/Wai/Parse.hs+38−67 wai-extra/test/WaiExtraTest.hs+11−6 wai-extra/wai-extra.cabal−4
wai-extra/Network/Wai/Middleware/RequestLogger.hs+1−2
@@ -20,7 +20,6 @@ import qualified Data.Text.Encoding as TE
2020
2121import Network.Wai.Parse (parseRequestBody, lbsSink, fileName, Param, File)
2222import qualified Data.ByteString.Lazy as LBS
23−import Data.Enumerator (run_, ($$), enumList)
2423import System.IO (hPutStrLn, stderr)
2524
2625import qualified Data.Conduit as C
@@ -95,7 +94,7 @@ logHandleDev cb app req = do
9594 if null params then ""
9695 else BS.concat ["\n", prefix, pack (show params)]
9796
98− allPostParams req' body = run_ $ enumList 1 body $$ parseRequestBody lbsSink req'
97+ allPostParams req' body = C.runResourceT $ CL.sourceList body C.$$ parseRequestBody lbsSink req'
9998
10099 emptyGetParam :: (BS.ByteString, Maybe BS.ByteString) -> (BS.ByteString, BS.ByteString)
101100 emptyGetParam (k, Just v) = (k,v)
wai-extra/Network/Wai/Parse.hs+38−67
@@ -32,9 +32,8 @@ import Data.Function (on)
3232import System.Directory (removeFile, getTemporaryDirectory)
3333import System.IO (hClose, openBinaryTempFile, Handle)
3434import Network.Wai
35−import Data.Enumerator (Iteratee, yield)
36−import qualified Data.Enumerator as E
37−import qualified Data.Enumerator.List as EL
35+import qualified Data.Conduit as C
36+import qualified Data.Conduit.List as CL
3837import Control.Monad.IO.Class (liftIO)
3938import qualified Network.HTTP.Types as H
4039
@@ -129,7 +128,7 @@ type File y = (S.ByteString, FileInfo y)
129128
130129parseRequestBody :: Sink x y
131130 -> Request
132− -> Iteratee S.ByteString IO ([Param], [File y])
131+ -> C.Sink S.ByteString IO ([Param], [File y])
133132parseRequestBody sink req = do
134133 case ctype of
135134 Nothing -> return ([], [])
@@ -137,7 +136,7 @@ parseRequestBody sink req = do
137136 -- NOTE: in general, url-encoded data will be in a single chunk.
138137 -- Therefore, I'm optimizing for the usual case by sticking with
139138 -- strict byte strings here.
140− bs <- EL.consume
139+ bs <- CL.consume
141140 return (H.parseSimpleQuery $ S.concat bs, [])
142141 Just (Just bound) -> -- multi-part
143142 let bound'' = S8.pack "--" `S.append` bound
@@ -162,24 +161,20 @@ parseRequestBody sink req = do
162161 Just x -> Just $ Just x
163162 Nothing -> Nothing
164163
165−takeLine :: Iteratee S.ByteString IO (Maybe S.ByteString)
166−takeLine = do
167− mbs <- EL.head
168− case mbs of
169− Nothing -> return Nothing
170− Just bs ->
171− let (x, y) = S.break (== 10) bs -- LF
172− in if S.null y
173− then do
174− x' <- takeLine
175− case x' of
176− Nothing -> return $ Just $ killCR x
177− Just x'' -> return $ Just $ killCR $ S.append x x''
178− else do
179− E.yield () $ E.Chunks [S.drop 1 y]
180− return $ Just $ killCR x
164+takeLine :: C.Sink S.ByteString IO (Maybe S.ByteString)
165+takeLine =
166+ C.sinkState id push close
167+ where
168+ close _ = return Nothing
169+ push front bs = do
170+ let (x, y) = S.break (== 10) $ front bs -- LF
171+ in if S.null y
172+ then return (S.append x, C.Processing)
173+ else do
174+ let lo = if S.length y > 1 then Just (S.drop 1 y) else Nothing
175+ return (error "takeLine", C.Done lo $ Just $ killCR x)
181176
182−takeLines :: Iteratee S.ByteString IO [S.ByteString]
177+takeLines :: C.Sink S.ByteString IO [S.ByteString]
183178takeLines = do
184179 res <- takeLine
185180 case res of
@@ -191,7 +186,7 @@ takeLines = do
191186 return $ l : ls
192187
193188parsePieces :: Sink x y -> S.ByteString
194− -> Iteratee S.ByteString IO ([Param], [File y])
189+ -> C.Sink S.ByteString IO ([Param], [File y])
195190parsePieces sink bound = do
196191 _boundLine <- takeLine
197192 res' <- takeLines
@@ -271,56 +266,32 @@ findBound b bs = go [0..S.length bs - 1]
271266sinkTillBound :: S.ByteString
272267 -> (x -> S.ByteString -> IO x)
273268 -> x
274− -> Iteratee S.ByteString IO (x, Bool)
275−sinkTillBound bound iter seed = do
276− mbs <- EL.head
277− case mbs of
278− Nothing -> return (seed, False)
279− Just bs -> go bs
269+ -> C.Sink S.ByteString IO (x, Bool)
270+sinkTillBound bound iter seed0 = C.sinkState
271+ (id, seed0)
272+ push
273+ close
280274 where
281− go bs =
275+ close (front, seed) = do
276+ seed' <- liftIO $ iter seed $ front S.empty
277+ return (seed', False)
278+ push (front, seed) bs' = do
279+ let bs = front bs'
282280 case findBound bound bs of
283281 FoundBound before after -> do
284282 let before' = killCRLF before
285283 seed' <- liftIO $ iter seed before'
286− yield () $ E.Chunks [after]
287− return (seed', True)
288− PartialBound -> do
289− mbs <- EL.head
290− case mbs of
291− Nothing -> do
292− seed' <- liftIO $ iter seed bs
293− return (seed', False)
294− Just bs2 -> do
295− let bs' = bs `S.append` bs2
296− yield () $ E.Chunks [bs']
297− sinkTillBound bound iter seed
284+ return (undefined, C.Done (Just after) (seed', True))
298285 NoBound -> do
299− mbs <- EL.head
300− case mbs of
301− Nothing -> do
302− seed' <- liftIO $ iter seed bs
303− sinkTillBound bound iter seed'
304− Just bs' -> do
305− -- this funny bit is to catch when there's a
306− -- newline at the end of the previous chunk
307− (seed', bs'') <-
308− if not (S8.null bs) && S8.last bs `elem` "\n\r"
309− then do
310− let (front, back) =
311− S.splitAt (S.length bs - 2) bs
312− seed' <- liftIO $ iter seed front
313− return (seed', back `S.append` bs')
314− else do
315− seed' <- liftIO $ iter seed bs
316− return (seed', bs')
317− yield () $ E.Chunks [bs'']
318− sinkTillBound bound iter seed'
319− {-
320− NoBound -> do
321− case msrc of
322− Just (bs', src') -> do
323− -}
286+ -- don't emit newlines, in case it's part of a bound
287+ let (toEmit, front') =
288+ if not (S8.null bs) && S8.last bs `elem` "\r\n"
289+ then let (x, y) = S.splitAt (S.length bs - 2) bs
290+ in (x, S.append y)
291+ else (bs, id)
292+ seed' <- liftIO $ iter seed toEmit
293+ return ((front', seed'), C.Processing)
294+ PartialBound -> return ((S.append bs, seed), C.Processing)
324295
325296parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
326297parseAttrs = map go . S.split 59 -- semicolon
wai-extra/test/WaiExtraTest.hs+11−6
@@ -24,11 +24,13 @@ import Network.Wai.Middleware.AcceptOverride
2424import Network.Wai.Middleware.Debug (debugHandle)
2525import Codec.Compression.GZip (decompress)
2626
27−import Data.Enumerator (run_, enumList, ($$), Iteratee)
28−import Data.Enumerator.Binary (enumFile)
27+import qualified Data.Conduit as C
28+import qualified Data.Conduit.List as CL
29+import Data.Conduit.Binary (sourceFile)
2930import Control.Monad.IO.Class (liftIO)
3031import Data.Maybe (fromMaybe)
3132import Network.HTTP.Types (parseSimpleQuery, status200)
33+import Data.Monoid (mappend)
3234
3335specs :: Specs
3436specs = do
@@ -95,12 +97,14 @@ caseParseHttpAccept = do
9597
9698parseRequestBody' :: Sink ([S8.ByteString] -> [S8.ByteString]) L.ByteString
9799 -> SRequest
98− -> Iteratee S.ByteString IO ([(S.ByteString, S.ByteString)], [(S.ByteString, FileInfo L.ByteString)])
100+ -> C.ResourceT IO ([(S.ByteString, S.ByteString)], [(S.ByteString, FileInfo L.ByteString)])
99101parseRequestBody' sink (SRequest req bod) =
100− enumList 1 (L.toChunks bod) $$ parseRequestBody sink req
102+ CL.sourceList (L.toChunks bod) C.$$ parseRequestBody sink req
101103
102104caseParseRequestBody :: Assertion
103−caseParseRequestBody = run_ t where
105+caseParseRequestBody =
106+ C.runResourceT t
107+ where
104108 content2 = S8.pack $
105109 "--AaB03x\n" ++
106110 "Content-Disposition: form-data; name=\"document\"; filename=\"b.txt\"\n" ++
@@ -394,7 +398,8 @@ caseDalvikMultipart = do
394398 let request' = defaultRequest
395399 { requestHeaders = headers
396400 }
397− (params, files) <- run_ $ enumFile "test/requests/dalvik-request" $$ parseRequestBody lbsSink request'
401+ (params, files) <- C.runResourceT $ sourceFile "test/requests/dalvik-request"
402+ C.$$ parseRequestBody lbsSink request'
398403 lookup "scannedTime" params @?= Just "1.298590056748E9"
399404 lookup "geoLong" params @?= Just "0"
400405 lookup "geoLat" params @?= Just "0"
wai-extra/wai-extra.cabal−4
@@ -38,8 +38,6 @@ Library
3838 , conduit
3939 , zlib-conduit
4040 , blaze-builder-conduit
41− -- FIXME this must be removed
42− , enumerator
4341
4442 Exposed-modules: Network.Wai.Handler.CGI
4543 Network.Wai.Middleware.AcceptOverride
@@ -78,8 +76,6 @@ test-suite tests
7876 , blaze-builder >= 0.2.1.4 && < 0.4
7977 , data-default
8078 , conduit
81− -- FIXME this must be removed
82− , enumerator
8379
8480source-repository head
8581 type: git