Created
March 28, 2012 18:32
-
-
Save anonymous/2229180 to your computer and use it in GitHub Desktop.
Demonstration of Pipes for Web Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude hiding (take) | |
import Control.Pipe | |
import Control.Pipe.Combinators hiding (take) | |
import Control.Pipe.Binary | |
import Data.ByteString hiding (take,cons) | |
import Data.ByteString.Char8 hiding (take,cons) | |
type Header = Int | |
requestSize :: Header -> Int | |
requestSize = id | |
main = runPipe (fileReader "input.txt" >+> webserver echoApp >+> fileWriter "output.txt") | |
echoApp :: Pipe ByteString ByteString IO () | |
echoApp = idP | |
webserver :: Pipe ByteString ByteString IO a -> Pipe ByteString ByteString IO () | |
webserver app = do | |
(header,rest) <- getHeader | |
cons rest >+> (take (requestSize header) >> return ()) >+> (app >> discard) | |
cons :: (Monad m) => a -> Pipe a a m r | |
cons x = yield x >> idP | |
getHeader :: Pipe ByteString ByteString IO (Header,ByteString) | |
getHeader = await >>= return . maybe (error "couldn't parse header") id . readInt | |
{- input.txt | |
13 hello this | |
is a | |
test | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment