Sha256: 376029d642bcbc6c9e493e451e9a16db3a4cc731e0f115a34abbb89424a19659

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'net/http'
require 'rubygems'
require 'rack'
require 'delegate'
require 'util'

class RatHole

  VERSION = '0.1.6'

  def initialize(host)
    @host = host
  end

  def process_user_request(rack_request)
    rack_request
  end

  def process_server_response(rack_response, rack_request)
    rack_response
  end

  def call(env)
    Net::HTTP.start(@host) do |http|
      http.instance_eval{@socket = MethodSpy.new(@socket){|method| method =~ /#{ENV['RH_METHOD_SPY_FILTER']}/}} if $DEBUG

      env.delete('HTTP_ACCEPT_ENCODING')
      source_request = process_user_request(Rack::Request.new(env))
      source_headers = request_headers(source_request.env)
      
      if source_request.query_string.nil? || source_request.query_string == ''
        request_uri = source_request.path_info
      else
        request_uri = "#{source_request.path_info}?#{source_request.query_string}"
      end

      if source_request.get?
        response = http.get(request_uri, source_headers)
      elsif source_request.post?
        post = Net::HTTP::Post.new(request_uri, source_headers)
        class << post
          include HTTPHeaderPatch
        end
        post.form_data = source_request.POST
        response = http.request(post)
      end

      code = response.code.to_i
      headers = response.to_hash
      body = response.body || ''
      headers.delete('transfer-encoding')

      process_server_response(Rack::Response.new(body, code, headers), source_request).finish
    end
  end

  def request_headers(env)
    env.select{|k,v| k =~ /^HTTP/}.inject({}) do |h, e|
      k,v = e
      h.merge(k.split('_')[1..-1].join('-').to_camel_case => v)
    end
  end
end

# This class simply extends RatHole and does nothing.
# It's only useful for making sure that you have everything hooked up correctly.
class EmptyRatHole < RatHole
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mikehale-rat-hole-0.1.6 lib/rat_hole.rb