Skip to content

Commit

Permalink
Added little sinatra server to test the whole thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Lütke committed May 24, 2009
1 parent 4e4ad9e commit 661617b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 46 deletions.
63 changes: 41 additions & 22 deletions api_proxy.rb
@@ -1,43 +1,62 @@

require 'logger'
require 'lib/crc32'
require 'lib/em-proxy'
require 'lib/request'
require 'lib/proxy_endpoint'
require 'memcached'
require 'httparty'
require 'zlib'

$cache = Memcached.new('localhost:11211')


Proxy.start(:host => "0.0.0.0", :port => 3005) do |conn|
conn.server :shopify, :host => "127.0.0.1", :port => 80
$cache = Memcached.new('localhost:11211')
$logger = Logger.new(STDOUT)

conn.on_data do |data|

request = Request.new(data)
class Resolver
Interesting = /^\/proxy/

if request.path =~ /^\/proxy/

def self.dispatch(request)
if Interesting.match(request.path)

proxy = ProxyEndpoint.new(request.request_uri)

if proxy.available?

$logger.info " * Contacting endpoint at #{proxy.endpoint_location}"

proxy.forward(request)

cache_key = "proxy-content/#{proxy.content.crc32}"

$cache.set cache_key, proxy.content

cache_key = "proxy-content/#{crc32(proxy.content)}"

$cache.set cache_key, proxy.content

$logger.info " * Endpoint returned Status:#{proxy.status}, #{proxy.content.length}b content"

request.add_header('X-Proxy-Content', cache_key)
request.add_header('X-Proxy-Status', proxy.status)
request.data
end

request.data
else
request.data
else
$logger.info " * No endpoint found for #{proxy.endpoint_location}"
end
end
end

end


Proxy.start(:host => "0.0.0.0", :port => 3005) do |conn|
conn.server :shopify, :host => "127.0.0.1", :port => 2222

conn.on_data do |data|

request = Request.new(data)

$logger.info "*** Request for #{request.request_uri}"

Resolver.dispatch(request)

$logger.info "*** Forwarding to backend"

p request.data
request.data

end

conn.on_response do |backend, resp|
resp
Expand Down
10 changes: 2 additions & 8 deletions lib/crc32.rb
@@ -1,9 +1,3 @@
def crc32
Zlib.crc32(@body, 0)
def crc32(content)
Zlib.crc32(content, 0)
end

def String
def crc32
Kernel.crc32(self)
end
end
3 changes: 0 additions & 3 deletions lib/em-proxy/backend.rb
Expand Up @@ -9,12 +9,10 @@ def initialize
end

def connection_completed
p [@name, :conn_complete]
@connected.succeed
end

def receive_data(data)
p [@name, data]
@data.push data
@plexer.relay_from_backend(@name, data)
end
Expand All @@ -28,7 +26,6 @@ def send(data)
# Notify upstream plexer that the backend server is done
# processing the request
def unbind
p [@name, :unbind]
@plexer.unbind_backend(@name)
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/em-proxy/connection.rb
Expand Up @@ -13,7 +13,6 @@ def initialize
end

def receive_data(data)
p [:connection, data]
data = @on_data.call(data)

@servers.values.compact.each do |s|
Expand All @@ -37,8 +36,6 @@ def server(name, opts)
# relay data from backend server to client
#
def relay_from_backend(name, data)
p [:relay_from_backend, name, data]

data = @on_response.call(name, data)
send_data data unless data.nil?
end
Expand Down
8 changes: 1 addition & 7 deletions lib/proxy_endpoint.rb
Expand Up @@ -10,7 +10,7 @@ class ConnectionError < Error; end
class TimeoutError < Error; end
class MethodNotAllowed < Error; end

ProxyRoot = /https?\:\/\/.*?\/[\w_-]+\/[\w_-]+/
ProxyRoot = /https?\:\/\/.*?\/[\w\:_-]+?\/[\w_-]+/

def initialize(uri)
@uri = uri.to_s
Expand Down Expand Up @@ -54,12 +54,6 @@ def forward(request)
else
raise MethodNotAllowed.new(api_client)
end

if (200..299).include?(response.code.to_i)
response.body
else
raise RequestError.new(api_client, response)
end

self.content = response.body
self.status = response.code
Expand Down
2 changes: 1 addition & 1 deletion lib/request.rb
Expand Up @@ -17,7 +17,7 @@ def content_type
end

def request_uri
headers['HTTP_REQUEST_URI']
'http://' + headers['Host'] + @path
end

def headers
Expand Down
5 changes: 3 additions & 2 deletions test/proxy_endpoint_test.rb
Expand Up @@ -26,6 +26,7 @@ def test_proxy_root
assert_equal 'http://127.0.0.1/a/b', ProxyEndpoint.new('http://127.0.0.1/a/b').proxy_root
assert_equal 'http://127.0.0.1/a-b/b_c', ProxyEndpoint.new('http://127.0.0.1/a-b/b_c').proxy_root
assert_equal 'http://www.proxyserver.com:81/path/to', ProxyEndpoint.new('http://www.proxyserver.com:81/path/to/endpoint?with=param').proxy_root
assert_equal 'http://localhost:3005/a/b', ProxyEndpoint.new('http://localhost:3005/a/b').proxy_root
end

def test_invalid_proxy_root
Expand Down Expand Up @@ -59,8 +60,8 @@ def test_get_forward
assert_equal 'Third party content', proxy.content
assert_equal '200', proxy.status

end

end
def test_post_forward
proxy = ProxyEndpoint.new('http://127.0.0.1/a/b')

Expand Down
25 changes: 25 additions & 0 deletions testserver/server.rb
@@ -0,0 +1,25 @@
require 'rubygems'
require 'sinatra'
require 'memcached'

$cache = Memcached.new('localhost:11211')


# Set endpoint location for localhost address to be google.com
$cache.set 'proxy/http://localhost:3005/proxy/test', 'http://www.example.com'


get '/' do
"<h1>Proxy Test</h1><p>Go to <a href='/proxy/test'>Test page</a>"
end


get '/proxy/test' do

content_key = request.env['HTTP_X_PROXY_CONTENT'] ||'nothing'
status = request.env['HTTP_X_PROXY_STATUS'] ||'nothing'

content = $cache.get(content_key)

"<h1>Proxy Test</h1><p>Proxy Status: #{status}</p><p>Proxy Key: #{content_key}</p><p>Proxy Content: #{content.length} bytes</p><blockquote style='padding:10px; border: 1px solid #ccc;'>#{content}</blockquote>"
end

0 comments on commit 661617b

Please sign in to comment.