Simple http proxy, TCPServer<->TCPSocket->

this thing kind of works… i have connected my webbrowser to 8181 for
testing… im looking for help, everything i try produce the same
resaults… i dont really want to bother with event machines

require ‘socket’
server = TCPServer.open(8181)
loop {
Thread.start(server.accept) do |client|
data=client.recv(5000)
host=data.scan(/Host.*.$/).to_s.gsub(“Host: “,””).chomp
#now lets connect to the webserver
connect_to_site=TCPSocket.new(host,“80”)
puts"connection established?\n"
connect_to_site.print(data)

  while(response=connect_to_site.gets)
  client.print response
  end

client.close
connect_to_site.close
puts"connections closed"
#close connection

end #DO
}

Bigmac T. wrote in post #1052529:

this thing kind of works… i have connected my webbrowser to 8181 for
testing… im looking for help, everything i try produce the same
resaults… i dont really want to bother with event machines

And what exactly do you need help with?

Cheers

robert

Robert K. wrote in post #1052606:

Bigmac T. wrote in post #1052529:

this thing kind of works… i have connected my webbrowser to 8181 for
testing… im looking for help, everything i try produce the same
resaults… i dont really want to bother with event machines

And what exactly do you need help with?

Cheers

robert

so, my code has a few problems, the web browser will only load a few
pages and the connection will always stay open as if the browser is
waiting for more data…

i was thinking maybe i need 2 or 3 threads to handle both sides…

Thread.start{ receive_get from webbrowser }#step one
Thread.start{ send_get to webserver } #step two
Thread.start{ send_response to webbrowser }#step three

if i understand, the first step i need to gather a host: field
then establish a connection to the host web server
and pass the response back to the web client…

i think my loops are wrong… maybe i can use IO to handle sockets?

Bigmac T. wrote in post #1052885:

Robert K. wrote in post #1052606:

Bigmac T. wrote in post #1052529:

this thing kind of works… i have connected my webbrowser to 8181 for
testing… im looking for help, everything i try produce the same
resaults… i dont really want to bother with event machines

so, my code has a few problems, the web browser will only load a few
pages and the connection will always stay open as if the browser is
waiting for more data…

A few things:

  • Better use #read and #write for your IO
  • I think you are not properly implementing the HTTP proxy protocol.
  • You can make your life easier by using #gets("\r\n") to read HTTP
    request from client

i was thinking maybe i need 2 or 3 threads to handle both sides…

Thread.start{ receive_get from webbrowser }#step one
Thread.start{ send_get to webserver } #step two
Thread.start{ send_response to webbrowser }#step three

I don’t think you’ll necessarily need threads for HTTP 1.0.

if i understand, the first step i need to gather a host: field
then establish a connection to the host web server
and pass the response back to the web client…

Right.

i think my loops are wrong… maybe i can use IO to handle sockets?

No idea what you mean by that.

Cheers

robert