Created
August 10, 2009 00:43
-
-
Save dbatwa/164963 to your computer and use it in GitHub Desktop.
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 "rubygems" | |
require 'net/http' | |
require 'uri' | |
module WEBrick | |
class HTTPRequest | |
attr_writer :host, :port, :request_uri, :unparsed_uri, :request_line, :path | |
end | |
end | |
class MultiplexingProxy < WEBrick::HTTPServer | |
def initialize(options) | |
super(options) | |
end | |
def service(req, res) | |
puts "*" * 50 | |
dest = destination(req) | |
puts "Destination: #{dest}" | |
url = URI.parse(dest) | |
post = Net::HTTP::Post.new(url.path) | |
post.body = req.body | |
post.set_content_type(req.content_type) | |
response = Net::HTTP.new(url.host, url.port).start {|http| http.request(post) } | |
case response | |
when Net::HTTPSuccess, Net::HTTPRedirection | |
# OK | |
else | |
response.error! | |
end | |
end | |
def destination(req) | |
req.body.match(/<PartyName>(.*?)<\/PartyName>/)[1] | |
end | |
end | |
s = MultiplexingProxy.new(:Port => 8099) | |
trap("INT") {s.shutdown} | |
s.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment