Skip to content

Commit

Permalink
http proxy example, from: https://gist.github.com/794464
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jan 29, 2011
1 parent b89e1bc commit 09672b9
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/http_proxy.rb
@@ -0,0 +1,47 @@
require 'em-proxy'
require 'http/parser' # gem install http_parser.rb
require 'uuid' # gem install uuid

# > ruby em-proxy-http.rb
# > curl --proxy localhost:9889 www.google.com

host = "0.0.0.0"
port = 9889
puts "listening on #{host}:#{port}..."

Proxy.start(:host => host, :port => port) do |conn|

@p = Http::Parser.new
@p.on_headers_complete = proc do |h|
session = UUID.generate
puts "New session: #{session} (#{h.inspect})"

host, port = h['Host'].split(':')
conn.server session, :host => host, :port => (port || 80)
conn.relay_to_servers @buffer

@buffer.clear
end

@buffer = ''

conn.on_connect do |data,b|
puts [:on_connect, data, b].inspect
end

conn.on_data do |data|
@buffer << data
@p << data

data
end

conn.on_response do |backend, resp|
puts [:on_response, backend, resp].inspect
resp
end

conn.on_finish do |backend, name|
puts [:on_finish, name].inspect
end
end

0 comments on commit 09672b9

Please sign in to comment.