Sha256: aa7f8e1c3f1d3c0e3db55d83920372abf95244a401fd53deb3480b540a3e1b32

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require "net_http_hacked"

module Rack

  # Wraps the hacked net/http in a Rack way.
  class HttpStreamingResponse
    attr_accessor :use_ssl

    def initialize(request, host, port = nil)
      @request, @host, @port = request, host, port
    end

    def status
      response.code.to_i
    end

    def headers
      h = Utils::HeaderHash.new

      response.to_hash.each do |k, v|
        h[k] = v
      end

      h
    end

    def body
      self
    end

    # Can be called only once!
    def each(&block)
      response.read_body(&block)
    ensure
      session.end_request_hacked
    end

    def to_s
      @body ||= begin
        lines = []

        each do |line|
          lines << line
        end

        lines.join
      end
    end

    protected

    # Net::HTTPResponse
    def response
      @response ||= session.begin_request_hacked(@request)
    end

    # Net::HTTP
    def session
      @session ||= begin
        http = Net::HTTP.new @host, @port
        http.use_ssl = self.use_ssl
        http.start
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-proxy-0.5.1 lib/rack/http_streaming_response.rb
rack-proxy-0.5.0 lib/rack/http_streaming_response.rb
rack-proxy-0.4.0 lib/rack/http_streaming_response.rb