require 'rubygems' require 'rack' require File.join(File.dirname(__FILE__), 'endpoint_initializer') module Jangle # Effectively a passthrough endpoint which proxies a request # through to another HTTP server class HttpEndpoint include EndpointInitializer def serve(rack_request) original_rack_request = rack_request.clone # Filter the request @request_filters.each do |rf| rf.filter(rack_request) end LOGGER.debug "Request to be forwarded (after filtering):" LOGGER.debug rack_request.inspect # Forward the request options = {:port => @port} options[:path] = @path unless @path.nil? http_response = rack_request.forward_to(@host, options) LOGGER.debug "Response before filtering (from proxied host):" LOGGER.debug http_response.to_s # Filter the HTTP response @response_filters.each do |rf| rf.filter(http_response) end # Filter the HTTP response through filters which need the original request @request_sensitive_response_filters.each do |rf| rf.filter(original_rack_request, rack_request, http_response) end # Construct the response to send back from the endpoint response_headers = http_response.to_hash body = http_response.body || '' rack_response = Rack::Response.new(body, http_response.code, response_headers) # Fix for Rack failing to respect the Content-Type header when creating a new # Rack::Response instance rack_response['Content-Type'] = response_headers['content-type'] rack_response end end end