Last active
September 26, 2021 00:25
-
-
Save jamesmoriarty/a6100395d2efb17dcd06173300f988bb to your computer and use it in GitHub Desktop.
HTTP CONNECT by example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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