public
Last active

A quick HTTP proxy server in Ruby.

  • Download Gist
proxy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/usr/bin/env ruby
# A quick and dirty implementation of an HTTP proxy server in Ruby
# because I did not want to install anything.
#
# Copyright (C) 2009 Torsten Becker <torsten.becker@gmail.com>
 
require 'socket'
require 'uri'
 
 
class Proxy
def run port
begin
# Start our server to handle connections (will raise things on errors)
@socket = TCPServer.new port
# Handle every request in another thread
loop do
s = @socket.accept
Thread.new s, &method(:handle_request)
end
# CTRL-C
rescue Interrupt
puts 'Got Interrupt..'
# Ensure that we release the socket on errors
ensure
if @socket
@socket.close
puts 'Socked closed..'
end
puts 'Quitting.'
end
end
def handle_request to_client
request_line = to_client.readline
verb = request_line[/^\w+/]
url = request_line[/^\w+\s+(\S+)/, 1]
version = request_line[/HTTP\/(1\.\d)\s*$/, 1]
uri = URI::parse url
# Show what got requested
puts((" %4s "%verb) + url)
to_server = TCPSocket.new(uri.host, (uri.port.nil? ? 80 : uri.port))
to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/#{version}\r\n")
content_len = 0
loop do
line = to_client.readline
if line =~ /^Content-Length:\s+(\d+)\s*$/
content_len = $1.to_i
end
# Strip proxy headers
if line =~ /^proxy/i
next
elsif line.strip.empty?
to_server.write("Connection: close\r\n\r\n")
if content_len >= 0
to_server.write(to_client.read(content_len))
end
break
else
to_server.write(line)
end
end
buff = ""
loop do
to_server.read(4048, buff)
to_client.write(buff)
break if buff.size < 4048
end
# Close the sockets
to_client.close
to_server.close
end
end
 
 
# Get parameters and start the server
if ARGV.empty?
port = 8008
elsif ARGV.size == 1
port = ARGV[0].to_i
else
puts 'Usage: proxy.rb [port]'
exit 1
end
 
Proxy.new.run port

I don't see a license: is it okay if I use this code? I want to adapt it into a custom no-dependencies proxy for the Chrome Postman app.

Would love to see a license as well. :-)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.