[Haskell-cafe] Trying to write a TCP proxy

John Ky newhoggy at gmail.com
Wed Sep 20 10:24:04 EDT 2006


Hi Bulat,

Thanks.  Yes it helps with an earlier implementation I wrote (below).
But surely there must be a better way to write this.  My code is way
to verbose.

-John

---

> doProxyServer handleToClient handleToServer = do
>   eof <- hIsEOF handleToServer
>   if not eof
>     then do
>       ready <- hReady handleToServer
>       if ready
>         then do
>           text <- hGetChar handleToServer
>           putChar text
>           hPutChar handleToClient text
>           doProxyServer handleToClient handleToServer
>         else do
>           hPutStr handleToClient "\n"
>           doProxyClient handleToClient handleToServer
>     else do
>       hClose handleToServer
>       hClose handleToClient

> doProxyClient handleToClient handleToServer = do
>   ready <- hReady handleToClient
>   if ready
>     then do
>       text <- hGetChar handleToClient
>       putChar text
>       hPutChar handleToServer text
>       doProxyClient handleToClient handleToServer
>     else do
>       hPutStr handleToServer "\n"
>       doProxyServer handleToClient handleToServer

> listen = withSocketsDo $ do
>   putStrLn "Listening..."
>   socket <- listenOn $ PortNumber 8082
>   (handleToClient, hostName, portNumber) <- accept socket
>   putStrLn "Connected to client"
>   handleToServer <- connectTo "localhost" (PortNumber 8080)
>   hSetBuffering handleToServer LineBuffering
>   putStrLn "Connected to server"
>   doProxyClient handleToClient handleToServer
>   hClose handleToServer
>   putStrLn "Closed server"
>   hClose handleToClient
>   putStrLn "Closed client"
>   sClose socket

> main = listen


On 9/20/06, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> Hello John,
>
> Wednesday, September 20, 2006, 3:59:36 PM, you wrote:
>
> > I'm trying to use haskell to put together a TCP proxy I can put
> > between my browser and my webserver.
>
> > This is as far as I got.  The webserver isn't returning my request:
>
> hSetBuffering handleToServer LineBuffering
>
> may help
>
>
> --
> Best regards,
>  Bulat                            mailto:Bulat.Ziganshin at gmail.com
>
>


More information about the Haskell-Cafe mailing list