Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTTP CONNECT by example
require 'socket'
listen_socket = TCPServer.new('127.0.0.1', 9292)
client_conn = listen_socket.accept
request_line = client_conn.gets
while(line = client_conn.gets) do
break unless line.include?(':')
end
host, port = *request_line.split[1].split(':')
dest_conn = TCPSocket.new(host, port)
client_conn.write "HTTP/1.1 200 OK\r\n"
def transfer(src_conn, dest_conn)
IO.copy_stream(src_conn, dest_conn)
rescue => e
puts e.message
end
[
Thread.new { transfer(client_conn, dest_conn) },
Thread.new { transfer(dest_conn, client_conn) }
].each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment