Skip to content

Commit

Permalink
support for rack dnode endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Jording committed May 18, 2011
1 parent 1d1d610 commit c7445e1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 199 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ source "http://rubygems.org"
# gem "activesupport", ">= 2.3.5"
gem 'rack-proxy'
gem 'rack'
gem 'dnode'
gem 'eventmachine'
gem 'events'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ GEM
addressable (2.2.5)
crack (0.1.8)
diff-lcs (1.1.2)
dnode (0.0.1)
eventmachine (0.12.10)
events (0.9.5)
git (1.2.5)
jeweler (1.5.2)
bundler (~> 1.0.0)
Expand Down Expand Up @@ -33,6 +36,9 @@ PLATFORMS

DEPENDENCIES
bundler (~> 1.0.0)
dnode
eventmachine
events
jeweler (~> 1.5.2)
rack
rack-proxy
Expand Down
105 changes: 105 additions & 0 deletions lib/whack-a-dnode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require 'dnode'
require 'eventmachine'
require 'events'
class WhackADnode
def initialize(path, host="localhost", port="8820", redirect=false)
@path = path
@host = host
@port = port
@redirect = redirect
@dnode = DNode.new({
:f => proc { |x,cb| cb.call(x) }
}).listen(@port)

end

def proxy_request
uri = self.uri
session = Net::HTTP.new(uri.host, uri.port)
session.start {|http|
req = Net::HTTP::Get.new(uri.request_uri)
body = ''
res = http.request(req) do |res|
res.read_body do |segment|
body << segment
end
end

[res.code, create_response_headers(res), [body]]
}
end

def forward_request
[ 302, {'Location'=> uri.to_s }, [] ]
end

def call(env)
return @redirect ? forward_request : proxy_request
end

private


def get_matcher_and_url path
matches = @paths.select do |matcher, url|
match_path(path, matcher)
end

if matches.length < 1
nil
elsif matches.length > 1
raise AmbiguousProxyMatch.new(path, matches)
else
matches.first.map{|a| a.dup}
end
end

def create_response_headers http_response
response_headers = Rack::Utils::HeaderHash.new(http_response.to_hash)
# handled by Rack
response_headers.delete('status')
# TODO: figure out how to handle chunked responses
response_headers.delete('transfer-encoding')
# TODO: Verify Content Length, and required Rack headers
response_headers
end

def match_path(path, matcher)
if matcher.is_a?(Regexp)
path.match(matcher)
else
path.match(/^#{matcher.to_s}/)
end
end

def get_uri(url, matcher, path)
if url =~/\$\d/
match_path(path, matcher).to_a.each_with_index { |m, i| url.gsub!("$#{i.to_s}", m) }
URI(url)
else
URI.join(url, path)
end
end

def reverse_proxy matcher, url, opts={}
raise GenericProxyURI.new(url) if matcher.is_a?(String) && URI(url).class == URI::Generic
@paths.merge!(matcher => url)
@opts.merge!(opts)
end


def rewrite_env(env)
env["PORT"] = "8000"

env
end

def rewrite_response(triplet)
status, headers, body = triplet

headers["X-Foo"] = "Bar"

triplet
end

end
1 change: 0 additions & 1 deletion lib/whack-a-node.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'rack/streaming_proxy'
class WhackANode

def initialize(path,host="localhost", port="8810",redirect=false)
Expand Down
180 changes: 0 additions & 180 deletions spec/rack/reverse_proxy_spec.rb

This file was deleted.

16 changes: 0 additions & 16 deletions spec/rack/streaming_proxy_spec.rb

This file was deleted.

3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require 'rspec'
require 'rack/test'
require 'whack-a-node'
require 'rack/reverse_proxy'
require 'rack/streaming_proxy'
require 'whack-a-dnode'
require 'webmock/rspec'

# Requires supporting files with custom matchers and macros, etc,
Expand Down
25 changes: 25 additions & 0 deletions spec/whack-a-dnode_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'rack/test'
include Rack::Test::Methods

describe "WhackADnode" do
class DwhackyTest < WhackADnode
def rewrite_env(env)
env['PORT'] = 90220
end
end

def app
WhackADnode.new
end

#before(:each) do
#@app = WhackyTest.new
#end
it "should have a port of 90220" do
get "/"
last_response.should_not be_nil
#@app.should_not be_nil
end
end

0 comments on commit c7445e1

Please sign in to comment.