Sha256: 1d67236aa0e6f701b17b45f79f85fcea70365ac61bc04fe08075dac3cf353919

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

require "net_http_hacked"

module Rack

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

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

    def body
      self
    end

    def code
      response.code.to_i
    end
    # #status is deprecated
    alias_method :status, :code

    def headers
      h = Utils::HeaderHash.new

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

      h
    end

    # Can be called only once!
    def each(&block)
      response.read_body(&block)
    ensure
      session.end_request_hacked
      session.finish
    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.verify_mode = self.verify_mode
        http.read_timeout = self.read_timeout
        http.ssl_version = self.ssl_version if self.use_ssl
        http.start
      end
    end

  end

end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rack-proxy-0.7.2 lib/rack/http_streaming_response.rb
rack-proxy-0.7.1 lib/rack/http_streaming_response.rb
rack-proxy-0.7.0 lib/rack/http_streaming_response.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-proxy-0.6.5/lib/rack/http_streaming_response.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-proxy-0.6.5/lib/rack/http_streaming_response.rb
rack-proxy-0.6.5 lib/rack/http_streaming_response.rb
rack-proxy-0.6.4 lib/rack/http_streaming_response.rb
rack-proxy-0.6.3 lib/rack/http_streaming_response.rb
rack-proxy-0.6.2 lib/rack/http_streaming_response.rb
rack-proxy-0.6.1 lib/rack/http_streaming_response.rb