On 8/17/06, William Crawford <wccrawford / gmail.com> wrote:
> Hi all,
>
> This is my first post here, and if I have chosen the wrong spot, my
> appologies.
>
> I wish to make a simple proxy server.  It will accept incoming
> connections and then connect to a pre-defined foreign address/port.
>
> It needs to transmit the information as it comes in, even if that is
> character by character (or byte by byte) and does not necessarily
> include line feeds, but if there is a large amount of data received, it
> should transmit it again all at once, after the modifications.
>
> I would like to modifying some of the information before passing it on.
>
> I am new to ruby programming, but I have dealt with PHP, C++, and C#
> pretty extensively.  Unfortunately, network programming has never been
> my thing.
>
> I have come up with a few thoughts...  Having the main thread accept the
> incoming connection and create a new thread for the connection.  that
> thread would create a thread to handle user-to-server data and the
> original thread would handle server-to-user data.  If either sees a lost
> connection, it would have to signal the other thread that it has
> happened.
>
> My concern on this approach is whether TCPSocket will freak out when
> being read from 1 connection and written to by another, simultaneously.
>
> I didn't see a way to use TCPSocket to check if data was present or not.
> There were methods named 'nonblock' but the examples they showed looked
> like they blocked anyhow.  So I couldn't see a way to use just the
> single thread to swap the data back and forth.  (I can't imagine it's
> not possible...  Ruby is just too flexible to not have that.)
>
> Or maybe there's some way that I haven't thought of?
>
> I appreciate any input.
>
> Thanks,
>
> William

For a simple(?) pure-ruby HTTP proxy, you may have a look at MouseHole
(http://code.whytheluckystiff.net/mouseHole/)