Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added little sinatra server to test the whole thing

  • Loading branch information...
commit 661617ba70ecf0414f9f6de79318a1fdfcff234d 1 parent 4e4ad9e
@tobi authored
View
63 api_proxy.rb
@@ -1,4 +1,5 @@
-
+require 'logger'
+require 'lib/crc32'
require 'lib/em-proxy'
require 'lib/request'
require 'lib/proxy_endpoint'
@@ -6,38 +7,56 @@
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
View
10 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
View
3  lib/em-proxy/backend.rb
@@ -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
@@ -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
View
3  lib/em-proxy/connection.rb
@@ -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|
@@ -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
View
8 lib/proxy_endpoint.rb
@@ -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
@@ -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
View
2  lib/request.rb
@@ -17,7 +17,7 @@ def content_type
end
def request_uri
- headers['HTTP_REQUEST_URI']
+ 'http://' + headers['Host'] + @path
end
def headers
View
5 test/proxy_endpoint_test.rb
@@ -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
@@ -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')
View
25 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
Please sign in to comment.
Something went wrong with that request. Please try again.