Sha256: aa7472f75299232f8f816d718431ab56e8e28c54b564c20e066f5df866832f04

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

require 'net/http'
require 'rack'

module ProxES
  # A lot of code in this comes from Rack::Proxy
  class Forwarder
    attr_reader :backend, :streaming

    def initialize(opts= {})
      @backend = URI(opts[:backend]) if opts[:backend]
    end

    def call(env)
      source_request = Rack::Request.new(env)
      full_path = source_request.fullpath == "" ? URI.parse(env['REQUEST_URI']).request_uri : source_request.fullpath
      target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(full_path)

      http = Net::HTTP.new(backend.host, backend.port)
      target_response = http.request(target_request)

      headers = (target_response.respond_to?(:headers) && target_response.headers) || self.class.normalize_headers(target_response.to_hash)
      body    = target_response.body || [""]
      body    = [body] unless body.respond_to?(:each)

      # Not sure where this is coming from, but it causes timeouts on the client
      headers.delete('transfer-encoding')

      [target_response.code, headers, body]
    end

    class << self
      def normalize_headers(headers)
        mapped = headers.map do |k, v|
          [k, if v.is_a? Array then v.join("\n") else v end]
        end
        Rack::Utils::HeaderHash.new Hash[mapped]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
proxes-0.4.0 lib/proxes/forwarder.rb
proxes-0.3.6 lib/proxes/forwarder.rb
proxes-0.3.5 lib/proxes/forwarder.rb
proxes-0.3.3 lib/proxes/forwarder.rb
proxes-0.3.2 lib/proxes/forwarder.rb