Streamline.hs 13 KB

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