Skip to content

Instantly share code, notes, and snippets.

@imarzi
Created June 30, 2011 20:01
Show Gist options
  • Save imarzi/1057082 to your computer and use it in GitHub Desktop.
Save imarzi/1057082 to your computer and use it in GitHub Desktop.
octopus gist
require 'rubygems'
require 'goliath'
require 'em-synchrony'
require 'em-synchrony/em-http'
class Octopus < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::Heartbeat
use Goliath::Rack::Validation::RequestMethod, %w(GET POST HEAD)
def on_headers(env, headers)
env['client-headers'] = headers
end
def response(env)
host_requested = env['HTTP_HOST']
path_requested = env[Goliath::Request::REQUEST_PATH]
#url = formare url in base agli ip presenti nel set di redis
url = 'http://localhost' + path_requested
params = {:head => env['client-headers'], :keepalive => true, :query => env.params}
req = EM::HttpRequest.new(url)
resp = case(env[Goliath::Request::REQUEST_METHOD])
when 'GET' then req.get(params)
when 'POST' then req.post(params.merge(:body => env[Goliath::Request::RACK_INPUT].read))
when 'HEAD' then req.head(params)
end
response_headers = {}
resp.response_header.each_pair do |k, v|
response_headers[to_http_header(k)] = v
end
response_headers.delete('Connection') if (response_headers['Content-Type'] == 'text/html; charset=UTF-8' )
response_headers.delete('Connection') if (response_headers['Content-Type'] == 'text/html' )
[resp.response_header.status, response_headers, resp.response]
end
def to_http_header(k)
k.downcase.split('_').collect { |e| e.capitalize }.join('-')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment