hmm I wanted to use it because I have many different server sockets. They are created in a fork. I did it now with your loop and it seems to work now except the fact the browsing through my webproxy is now pretty slow and I'm getting really confusing errors:
./httpsocket.rb:23:in `readline': end of file reached (EOFError)
        from ./httpsocket.rb:23
./httpsocket.rb:23:in `readline': end of file reached (EOFError)
        from ./httpsocket.rb:23
./httpsocket.rb:23:in `readline': end of file reached (EOFError)
        from ./httpsocket.rb:23
./httpsocket.rb:23:in `readline': end of file reached (EOFError)
        from ./httpsocket.rb:23

which seems to me like a very strange behavior because this loop means read a line from client[0] as long as possible: while client[0].readline
why is an eof error there possible? shoudln't make an eof ending the loop?


#!/usr/bin/env ruby

$Verbose=true

require 'socket'
require 'uri'

$bind_port = '23322'
$bind_address='localhost'

# opens a socket on the local machine and binds the proxy to it
proxy = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
proxy.bind(Socket.pack_sockaddr_in($bind_port, $bind_address ))
proxy.listen(1)

# waits for Browser-client connections
while  client = proxy.accept
        fork()
        # reads the browsers request
        request=''
        while client[0].readline
                   request += $_
                   break if $_ =~  /^\s*$/m
                   if $_ =~ /^GET .+/
                           host = URI.parse(URI.extract($_)[0]).host
                           port = URI.parse(URI.extract($_)[0]).port
                   end
        end
        # connects to  the webserver and sends the request  
        server = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
        server.connect(Socket.pack_sockaddr_in(port, host.chomp.sub('/','') ))
        server.write(request)
        # reads webservers response
        response=''
        while server.readline
                   response += $_
                   break if $_ =~  /^\s*$/m
         end
        # sends the http-header to browser
        client[0].write(response)
       # listens for further responses of the server and sends it to the browser
        while ( response = server.read(2**16) )
              client[0].write(response)
        end
 end

-- 
kazaam <kazaam / oleco.net>