Streamline.hs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. {-# LANGUAGE OverloadedStrings, TypeFamilies, MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}
  2. {- |
  3. Streamline exports a monad that, given an uniform IO target, emulates
  4. character stream IO using high performance block IO.
  5. -}
  6. module System.IO.Uniform.Streamline (
  7. -- * Basic Type
  8. Streamline,
  9. -- * Running streamline targets
  10. -- ** Single pass runners
  11. withClient,
  12. withServer,
  13. withTarget,
  14. -- ** Interruptible support
  15. inStreamlineCtx,
  16. peelStreamlineCtx,
  17. closeTarget,
  18. -- * Sending and recieving data
  19. send,
  20. send',
  21. recieveLine,
  22. recieveLine',
  23. recieveN,
  24. recieveN',
  25. -- ** Running a parser
  26. runAttoparsec,
  27. runAttoparsecAndReturn,
  28. -- ** Scanning the input
  29. runScanner,
  30. runScanner',
  31. scan,
  32. scan',
  33. recieveTill,
  34. recieveTill',
  35. -- * Behavior settings
  36. startTls,
  37. isSecure,
  38. transformTarget,
  39. limitInput,
  40. echoTo,
  41. setEcho
  42. ) where
  43. import System.IO (stdout, Handle)
  44. import qualified System.IO.Uniform as S
  45. import qualified System.IO.Uniform.Network as N
  46. import qualified System.IO.Uniform.Std as Std
  47. import System.IO.Uniform (UniformIO, SomeIO(..), TlsSettings)
  48. import System.IO.Uniform.Streamline.Scanner
  49. import Data.Default.Class
  50. import Control.Monad.Trans.Class
  51. import Control.Monad.Trans.Interruptible
  52. import Control.Monad.Trans.Control
  53. import Control.Monad
  54. import Control.Monad.Base
  55. import Control.Monad.IO.Class
  56. import System.IO.Error
  57. import Data.ByteString (ByteString)
  58. import qualified Data.ByteString as BS
  59. import qualified Data.ByteString.Lazy as LBS
  60. import Data.Word8 (Word8)
  61. import Data.IP (IP)
  62. import qualified Data.Attoparsec.ByteString as A
  63. -- | Internal state for a Streamline monad
  64. data StreamlineState = StreamlineState {str :: SomeIO, buff :: ByteString, isEOF :: Bool, echo :: Maybe Handle, inLimit :: Int, sentEmpty :: Bool}
  65. instance Default StreamlineState where
  66. -- | Will open StdIO
  67. def = StreamlineState (SomeIO Std.StdIO) BS.empty False Nothing (-1) False
  68. -- | Monad that emulates character stream IO over block IO.
  69. newtype Streamline m a = Streamline {withTarget' :: StreamlineState -> m (a, StreamlineState)}
  70. blockSize :: Int
  71. blockSize = 4096
  72. readF :: MonadIO m => Streamline m ()
  73. readF = -- Must try just not to read more than the limit, actual limiting is done by takeBuff
  74. Streamline $ \cl -> if not . BS.null . buff $ cl then return ((), cl)
  75. else do
  76. let lim = inLimit cl
  77. sz = if lim < 0 then blockSize
  78. else if lim <= blockSize then lim
  79. else blockSize
  80. l <- liftIO $ S.uRead (str cl) sz
  81. let cl' = cl{buff= l}
  82. case echo cl of
  83. Just h -> do
  84. liftIO $ BS.hPutStr h "< "
  85. liftIO $ BS.hPutStr h l
  86. Nothing -> return ()
  87. return ((), cl')
  88. -- | Takes the buffer for processing
  89. takeBuff :: MonadIO m => Streamline m ByteString
  90. takeBuff = do
  91. readF
  92. Streamline $ \cl ->
  93. let lim = inLimit cl
  94. eof = isEOF cl
  95. b = buff cl
  96. in if eof then eofError "System.IO.Uniform.Streamline"
  97. else if lim < 0 then return (b, cl{buff="", isEOF=BS.null b})
  98. else let (r, b') = BS.splitAt lim b
  99. in return (r, cl{
  100. isEOF = BS.null b || sentEmpty cl,
  101. sentEmpty = BS.null r,
  102. buff = b',
  103. inLimit = lim - BS.length r
  104. })
  105. -- | Pushes remaining data back into the buffer
  106. pushBuff :: Monad m => ByteString -> Streamline m ()
  107. pushBuff dt = Streamline $ \cl -> let
  108. lim = inLimit cl
  109. b = buff cl
  110. in if lim <= 0 then return ((), cl{buff = BS.append dt b})
  111. else return ((), cl{buff = BS.append dt b, inLimit = lim - BS.length dt})
  112. writeF :: MonadIO m => ByteString -> Streamline m ()
  113. writeF l = Streamline $ \cl -> case echo cl of
  114. Just h -> do
  115. liftIO $ BS.hPutStr h "> "
  116. liftIO $ BS.hPutStr h l
  117. liftIO $ S.uPut (str cl) l
  118. return ((), cl)
  119. Nothing -> liftIO $ S.uPut (str cl) l >> return ((), cl)
  120. -- | > withServer f serverIP port
  121. --
  122. -- Connects to the given server port, runs f, and closes the connection.
  123. withServer :: MonadIO m => IP -> Int -> Streamline m a -> m a
  124. withServer host port f = do
  125. ds <- liftIO $ N.connectTo host port
  126. (ret, _) <- withTarget' f def{str=SomeIO ds}
  127. liftIO $ S.uClose ds
  128. return ret
  129. -- | > withClient f boundPort
  130. --
  131. -- Accepts a connection at the bound port, runs f and closes the connection.
  132. withClient :: MonadIO m => N.BoundedPort -> (IP -> Int -> Streamline m a) -> m a
  133. withClient port f = do
  134. ds <- liftIO $ N.accept port
  135. (peerIp, peerPort) <- liftIO $ N.getPeer ds
  136. (ret, _) <- withTarget' (f peerIp peerPort) def{str=SomeIO ds}
  137. liftIO $ S.uClose ds
  138. return ret
  139. {- |
  140. > withTarget f someIO
  141. Runs f wrapped on a Streamline monad that does IO on someIO.
  142. -}
  143. withTarget :: (Monad m, UniformIO a) => a -> Streamline m b -> m b
  144. withTarget s f = do
  145. (r, _) <- withTarget' f def{str=SomeIO s}
  146. return r
  147. instance Monad m => Monad (Streamline m) where
  148. --return :: (Monad m) => a -> Streamline m a
  149. return x = Streamline $ \cl -> return (x, cl)
  150. --(>>=) :: Monad m => Streamline m a -> (a -> Streamline m b) -> Streamline m b
  151. a >>= b = Streamline $ \cl -> do
  152. (x, cl') <- withTarget' a cl
  153. withTarget' (b x) cl'
  154. instance Monad m => Functor (Streamline m) where
  155. --fmap :: (a -> b) -> Streamline m a -> Streamline m b
  156. fmap f m = Streamline $ \cl -> do
  157. (x, cl') <- withTarget' m cl
  158. return (f x, cl')
  159. instance Monad m => Applicative (Streamline m) where
  160. pure = return
  161. (<*>) = ap
  162. instance MonadTrans Streamline where
  163. --lift :: Monad m => m a -> Streamline m a
  164. lift x = Streamline $ \cl -> do
  165. a <- x
  166. return (a, cl)
  167. instance MonadIO m => MonadIO (Streamline m) where
  168. liftIO = lift . liftIO
  169. -- | Sends data over the IO target.
  170. send :: MonadIO m => ByteString -> Streamline m ()
  171. send r = writeF r
  172. -- | Sends data from a lazy byte string
  173. send' :: MonadIO m => LBS.ByteString -> Streamline m ()
  174. send' r = do
  175. let dd = LBS.toChunks r
  176. mapM_ writeF dd
  177. {- |
  178. Very much like Attoparsec's runScanner:
  179. > runScanner scanner initial_state
  180. Recieves data, running the scanner on each byte,
  181. using the scanner result as initial state for the
  182. next byte, and stopping when the scanner returns
  183. Nothing.
  184. Returns the scanned ByteString.
  185. -}
  186. runScanner :: MonadIO m => s -> IOScanner s -> Streamline m (ByteString, s)
  187. runScanner state scanner = do
  188. (rt, st) <- runScanner' state scanner
  189. return (LBS.toStrict rt, st)
  190. -- | Equivalent to runScanner, but returns a lazy ByteString
  191. runScanner' :: MonadIO m => s -> IOScanner s -> Streamline m (LBS.ByteString, s)
  192. runScanner' state scanner = do
  193. (tx, st) <- in_scan state
  194. return (LBS.fromChunks tx, st)
  195. where
  196. --in_scan :: MonadIO m => s -> Streamline m ([ByteString], s)
  197. in_scan st = do
  198. d <- takeBuff
  199. if BS.null d then return ([], st)
  200. else case sscan scanner st 0 $ BS.unpack d of
  201. AllInput st' -> do
  202. (tx', st'') <- in_scan st'
  203. return (d:tx', st'')
  204. SplitAt n st' -> do
  205. let (r, i) = BS.splitAt n d
  206. pushBuff i
  207. return ([r], st')
  208. -- I'll avoid rebuilding a list on high level code. The ByteString functions are way better.
  209. sscan :: (s -> Word8 -> IOScannerState s) -> s -> Int -> [Word8] -> ScanResult s
  210. sscan _ s0 _ [] = AllInput s0
  211. sscan s s0 i (w:ww) = case s s0 w of
  212. Finished -> SplitAt i s0
  213. LastPass s1 -> SplitAt (i+1) s1
  214. Running s1 -> sscan s s1 (i+1) ww
  215. data ScanResult s = SplitAt Int s | AllInput s
  216. -- | Equivalent to runScanner, but discards the final state
  217. scan :: MonadIO m => s -> IOScanner s -> Streamline m ByteString
  218. scan state scanner = fst <$> runScanner state scanner
  219. -- | Equivalent to runScanner', but discards the final state
  220. scan' :: MonadIO m => s -> IOScanner s -> Streamline m LBS.ByteString
  221. scan' state scanner = fst <$> runScanner' state scanner
  222. -- | Recieves data untill the next end of line (\n or \r\n)
  223. recieveLine :: MonadIO m => Streamline m ByteString
  224. recieveLine = recieveTill "\n"
  225. -- | Lazy version of recieveLine
  226. recieveLine' :: MonadIO m => Streamline m LBS.ByteString
  227. recieveLine' = recieveTill' "\n"
  228. {- |
  229. Recieves the given number of bytes, or less in case of end of file.
  230. -}
  231. recieveN :: MonadIO m => Int -> Streamline m ByteString
  232. recieveN n = LBS.toStrict <$> recieveN' n
  233. -- | Lazy version of recieveN
  234. recieveN' :: MonadIO m => Int -> Streamline m LBS.ByteString
  235. recieveN' n = LBS.fromChunks <$> recieve n
  236. where
  237. recieve sz
  238. | sz <= 0 = return []
  239. | otherwise = do
  240. d <- takeBuff
  241. if BS.null d then return []
  242. else do
  243. let (h, t) = BS.splitAt sz d
  244. sz' = sz - BS.length h
  245. unless (BS.null t) $ pushBuff t
  246. r <- recieve sz'
  247. return $ h : r
  248. -- | Recieves data until it matches the argument.
  249. -- Returns all of it, including the matching data.
  250. recieveTill :: MonadIO m => ByteString -> Streamline m ByteString
  251. recieveTill t = LBS.toStrict <$> recieveTill' t
  252. -- | Lazy version of recieveTill
  253. recieveTill' :: MonadIO m => ByteString -> Streamline m LBS.ByteString
  254. recieveTill' = recieve . BS.unpack
  255. where
  256. recieve t' = scan' [] (textScanner t')
  257. -- | Wraps the streamlined IO target on TLS, streamlining
  258. -- the new wrapper afterwads.
  259. startTls :: MonadIO m => TlsSettings -> Streamline m ()
  260. startTls st = Streamline $ \cl -> do
  261. ds' <- liftIO $ S.startTls st $ str cl
  262. return ((), cl{str=SomeIO ds'}{buff=""})
  263. -- | Runs an Attoparsec parser over the data read from the
  264. -- streamlined IO target. Returns both the parser
  265. -- result and the string consumed by it.
  266. runAttoparsecAndReturn :: MonadIO m => A.Parser a -> Streamline m (ByteString, Either String a)
  267. runAttoparsecAndReturn p = do
  268. d <- takeBuff
  269. let c = A.parse p d
  270. continueResult c d
  271. where
  272. continueResult c d = case c of
  273. A.Fail i _ msg -> do
  274. pushBuff i
  275. return (BS.take (BS.length d - BS.length i) d, Left msg)
  276. A.Done i r -> do
  277. pushBuff i
  278. return (BS.take (BS.length d - BS.length i) d, Right r)
  279. A.Partial c' -> do
  280. dt <- takeBuff
  281. continueResult (c' dt) dt
  282. -- | Runs an Attoparsec parser over the data read from the
  283. -- streamlined IO target. Returning the parser result.
  284. runAttoparsec :: MonadIO m => A.Parser a -> Streamline m (Either String a)
  285. runAttoparsec p = snd <$> runAttoparsecAndReturn p
  286. -- | Indicates whether transport layer security is being used.
  287. isSecure :: Monad m => Streamline m Bool
  288. isSecure = Streamline $ \cl -> return (S.isSecure $ str cl, cl)
  289. -- | Sets echo of the streamlines IO target.
  290. -- If echo is set, all the data read an written to the target
  291. -- will be echoed in stdout, with ">" and "<" markers indicating
  292. -- what is read and written.
  293. setEcho :: Monad m => Bool -> Streamline m ()
  294. setEcho e = Streamline $ \cl ->
  295. if e then return ((), cl{echo=Just stdout}) else return ((), cl{echo=Nothing})
  296. {- |
  297. Replaces the enclosed target with the result of the given transformation.
  298. Discards all buffered data in the process.
  299. -}
  300. transformTarget :: (UniformIO a, Monad m) => (SomeIO -> a) -> Streamline m ()
  301. transformTarget w = Streamline $ \cl -> return ((), cl{str = SomeIO . w . str $ cl})
  302. {- |
  303. Limits the input to the given number of bytes, emulating an end of file after them.
  304. If the limit is negative, the input will not be limited.
  305. -}
  306. limitInput :: Monad m => Int -> Streamline m ()
  307. limitInput n = Streamline $ \cl -> return ((), cl{inLimit = n})
  308. {- |
  309. Sets echo of the streamlined IO target.
  310. If echo is set, all the data read an written to the target
  311. will be echoed to the handle, with ">" and "<" markers indicating
  312. what is read and written.
  313. Setting to Nothing will disable echo.
  314. -}
  315. echoTo :: Monad m => Maybe Handle -> Streamline m ()
  316. echoTo h = Streamline $ \cl -> return ((), cl{echo=h})
  317. eofError :: MonadIO m => String -> m a
  318. eofError msg = liftIO . ioError $ mkIOError eofErrorType msg Nothing Nothing
  319. instance Interruptible Streamline where
  320. type RSt Streamline a = (a, StreamlineState)
  321. resume f (a, st) = withTarget' (f a) st
  322. -- | Creates a Streamline interrutible context
  323. inStreamlineCtx :: UniformIO io => io -> a -> RSt Streamline a
  324. inStreamlineCtx io a = (a, def{str = SomeIO io})
  325. -- | Closes the target of a streamline state, releasing any resource.
  326. closeTarget :: MonadIO m => Streamline m ()
  327. closeTarget = Streamline $ \st -> do
  328. liftIO . S.uClose . str $ st
  329. return ((), st)
  330. -- | Removes a Streamline interruptible context
  331. peelStreamlineCtx :: RSt Streamline a -> (a, SomeIO)
  332. peelStreamlineCtx (a, dt) = (a, str dt)
  333. instance MonadTransControl Streamline where
  334. type StT Streamline a = (a, StreamlineState)
  335. liftWith f = Streamline $ \s ->
  336. liftM (\x -> (x, s))
  337. (f $ \t -> withTarget' t s)
  338. restoreT = Streamline . const
  339. instance MonadBase b m => MonadBase b (Streamline m) where
  340. liftBase = liftBaseDefault
  341. instance MonadBaseControl b m => MonadBaseControl b (Streamline m) where
  342. type StM (Streamline m) a = ComposeSt Streamline m a
  343. liftBaseWith = defaultLiftBaseWith
  344. restoreM = defaultRestoreM