Created
February 1, 2012 17:47
-
-
Save artemave/1718293 to your computer and use it in GitHub Desktop.
liquid_proxy prototype
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 'em-proxy' | |
require 'http/parser' # gem install http_parser.rb | |
require 'http_tools' | |
require 'awesome_print' | |
# > 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, :debug => true) do |conn| | |
@body = '' | |
@p = Http::Parser.new | |
@p.on_message_complete = proc do | |
host_header = @p.headers['Host'] | |
ap host_header | |
new_request = HTTPTools::Builder.request( | |
@p.http_method, | |
host_header, | |
@p.request_url, | |
@p.headers | |
) | |
new_request+= @body if @body | |
@body.clear | |
if not host_header =~ /localhost:#{port}/ | |
puts new_request: new_request | |
h, p = host_header.split(':') | |
conn.server :test, :host => h, :port => (p || 80) | |
conn.relay_to_servers new_request | |
else | |
puts "CONTROL REQUEST RECEIVED" | |
conn.send_data HTTPTools::Builder.response(:ok) | |
conn.close_connection_after_writing | |
end | |
end | |
@p.on_body = proc do |chunk| | |
@body << chunk | |
end | |
conn.on_data do |data| | |
@p << data | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment