Streamline.hs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 changing and status
  36. startTls,
  37. isSecure,
  38. isEOF,
  39. transformTarget,
  40. limitInput,
  41. echoTo,
  42. setEcho
  43. ) where
  44. import System.IO (stdout, Handle)
  45. import qualified System.IO.Uniform as S
  46. import qualified System.IO.Uniform.Network as N
  47. import qualified System.IO.Uniform.Null as Null
  48. import System.IO.Uniform (UniformIO, SomeIO(..), TlsSettings)
  49. import System.IO.Uniform.Streamline.Scanner
  50. import Data.Default.Class
  51. import Control.Monad.Trans.Class
  52. import Control.Monad.Trans.Interruptible
  53. import Control.Monad.Trans.Control
  54. import Control.Monad
  55. import Control.Monad.Base
  56. import Control.Monad.IO.Class
  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, targetEOF :: Bool, echo :: Maybe Handle, inLimit :: Int}
  65. instance Default StreamlineState where
  66. def = StreamlineState (SomeIO Null.NullIO) BS.empty False Nothing (-1)
  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. readF :: MonadIO m => Streamline m ()
  72. readF = -- Must try just not to read more than the limit, actual limiting is done by takeBuff
  73. Streamline $ \cl -> if not . BS.null . buff $ cl then return ((), cl)
  74. else do
  75. let lim = inLimit cl
  76. sz = if lim < 0 then blockSize
  77. else if lim <= blockSize then lim
  78. else blockSize
  79. l <- liftIO $ S.uRead (str cl) sz
  80. let cl' = cl{
  81. buff = l,
  82. targetEOF = BS.null l
  83. }
  84. case echo cl of
  85. Just h -> do
  86. liftIO $ BS.hPutStr h "< "
  87. liftIO $ BS.hPutStr h l
  88. Nothing -> return ()
  89. return ((), cl')
  90. -- | Takes the buffer for processing
  91. takeBuff :: MonadIO m => Streamline m ByteString
  92. takeBuff = do
  93. readF
  94. Streamline $ \cl ->
  95. let lim = inLimit cl
  96. --eof = targetEOF cl
  97. b = buff cl
  98. in if lim < 0 then return (b, cl{buff=""})
  99. else let (r, b') = BS.splitAt lim b
  100. in return (r, cl{
  101. buff = b',
  102. inLimit = lim - BS.length r
  103. })
  104. -- | Pushes remaining data back into the buffer
  105. pushBuff :: Monad m => ByteString -> Streamline m ()
  106. pushBuff dt = Streamline $ \cl -> let
  107. lim = inLimit cl
  108. b = buff cl
  109. newb = BS.append dt b
  110. newl = if lim < 0 then lim else lim + BS.length dt
  111. in return ((), cl{buff=newb, inLimit=newl})
  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 [d]
  271. where
  272. continueResult c d dd = case c of
  273. A.Fail i _ msg -> do
  274. pushBuff $ BS.concat (reverse dd) `BS.append` i
  275. return ("", Left msg)
  276. A.Done i r -> do
  277. pushBuff i
  278. return (BS.concat (reverse dd) `BS.append`
  279. BS.take (BS.length d - BS.length i) d,
  280. Right r)
  281. A.Partial c' -> do
  282. dt <- takeBuff
  283. continueResult (c' dt) dt $ dt:dd
  284. -- | Runs an Attoparsec parser over the data read from the
  285. -- streamlined IO target. Returning the parser result.
  286. runAttoparsec :: MonadIO m => A.Parser a -> Streamline m (Either String a)
  287. runAttoparsec p = snd <$> runAttoparsecAndReturn p
  288. -- | Indicates whether transport layer security is being used.
  289. isSecure :: Monad m => Streamline m Bool
  290. isSecure = Streamline $ \cl -> return (S.isSecure $ str cl, cl)
  291. {- |
  292. True if the input is at the end of stream.
  293. If the input is limited, will be true when either the limit is reached,
  294. or the underlining target was at actual end of stream. In case of EOF
  295. due to reaching the limit, changing the limit will immediately make this
  296. return False again.
  297. -}
  298. isEOF :: Monad m => Streamline m Bool
  299. isEOF = Streamline $ \cl -> return (targetEOF cl || inLimit cl == 0, cl)
  300. -- | Sets echo of the streamlines IO target.
  301. -- If echo is set, all the data read an written to the target
  302. -- will be echoed in stdout, with ">" and "<" markers indicating
  303. -- what is read and written.
  304. setEcho :: Monad m => Bool -> Streamline m ()
  305. setEcho e = Streamline $ \cl ->
  306. if e then return ((), cl{echo=Just stdout}) else return ((), cl{echo=Nothing})
  307. {- |
  308. Replaces the enclosed target with the result of the given transformation.
  309. Discards all buffered data in the process.
  310. -}
  311. transformTarget :: (UniformIO a, Monad m) => (SomeIO -> a) -> Streamline m ()
  312. transformTarget w = Streamline $ \cl -> return ((), cl{str = SomeIO . w . str $ cl})
  313. {- |
  314. Limits the input to the given number of bytes, emulating an end of file after them.
  315. If the limit is negative, the input will not be limited.
  316. -}
  317. limitInput :: Monad m => Int -> Streamline m ()
  318. limitInput n = Streamline $ \cl -> return ((), cl{inLimit = n})
  319. {- |
  320. Sets echo of the streamlined IO target.
  321. If echo is set, all the data read an written to the target
  322. will be echoed to the handle, with ">" and "<" markers indicating
  323. what is read and written.
  324. Setting to Nothing will disable echo.
  325. -}
  326. echoTo :: Monad m => Maybe Handle -> Streamline m ()
  327. echoTo h = Streamline $ \cl -> return ((), cl{echo=h})
  328. instance Interruptible Streamline where
  329. type RSt Streamline a = (a, StreamlineState)
  330. resume f (a, st) = withTarget' (f a) st
  331. -- | Creates a Streamline interrutible context
  332. inStreamlineCtx :: UniformIO io => io -> a -> RSt Streamline a
  333. inStreamlineCtx io a = (a, def{str = SomeIO io})
  334. -- | Closes the target of a streamline state, releasing any resource.
  335. closeTarget :: MonadIO m => Streamline m ()
  336. closeTarget = Streamline $ \st -> do
  337. liftIO . S.uClose . str $ st
  338. return ((), st)
  339. -- | Removes a Streamline interruptible context
  340. peelStreamlineCtx :: RSt Streamline a -> (a, SomeIO)
  341. peelStreamlineCtx (a, dt) = (a, str dt)
  342. instance MonadTransControl Streamline where
  343. type StT Streamline a = (a, StreamlineState)
  344. liftWith f = Streamline $ \s ->
  345. liftM (\x -> (x, s))
  346. (f $ \t -> withTarget' t s)
  347. restoreT = Streamline . const
  348. instance MonadBase b m => MonadBase b (Streamline m) where
  349. liftBase = liftBaseDefault
  350. instance MonadBaseControl b m => MonadBaseControl b (Streamline m) where
  351. type StM (Streamline m) a = ComposeSt Streamline m a
  352. liftBaseWith = defaultLiftBaseWith
  353. restoreM = defaultRestoreM