Class: Rack::HttpStreamingResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/http_streaming_response.rb

Overview

Wraps the hacked net/http in a Rack way.

Constant Summary collapse

STATUSES_WITH_NO_ENTITY_BODY =
{
  204 => true,
  205 => true,
  304 => true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, host, port = nil) ⇒ HttpStreamingResponse

Returns a new instance of HttpStreamingResponse.



15
16
17
# File 'lib/rack/http_streaming_response.rb', line 15

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

Instance Attribute Details

#read_timeoutObject

Returns the value of attribute read_timeout.



13
14
15
# File 'lib/rack/http_streaming_response.rb', line 13

def read_timeout
  @read_timeout
end

#ssl_versionObject

Returns the value of attribute ssl_version.



13
14
15
# File 'lib/rack/http_streaming_response.rb', line 13

def ssl_version
  @ssl_version
end

#use_sslObject

Returns the value of attribute use_ssl.



13
14
15
# File 'lib/rack/http_streaming_response.rb', line 13

def use_ssl
  @use_ssl
end

#verify_modeObject

Returns the value of attribute verify_mode.



13
14
15
# File 'lib/rack/http_streaming_response.rb', line 13

def verify_mode
  @verify_mode
end

Instance Method Details

#bodyObject



19
20
21
# File 'lib/rack/http_streaming_response.rb', line 19

def body
  self
end

#codeObject Also known as: status



23
24
25
26
27
# File 'lib/rack/http_streaming_response.rb', line 23

def code
  response.code.to_i.tap do |response_code|
    STATUSES_WITH_NO_ENTITY_BODY[response_code] && close_connection
  end
end

#each(&block) ⇒ Object

Can be called only once!



38
39
40
41
42
43
44
# File 'lib/rack/http_streaming_response.rb', line 38

def each(&block)
  return if connection_closed

  response.read_body(&block)
ensure
  close_connection
end

#headersObject



31
32
33
34
35
# File 'lib/rack/http_streaming_response.rb', line 31

def headers
  Utils::HeaderHash.new.tap do |h|
    response.to_hash.each { |k, v| h[k] = v }
  end
end

#to_sObject



46
47
48
# File 'lib/rack/http_streaming_response.rb', line 46

def to_s
  @to_s ||= StringIO.new.tap { |io| each { |line| io << line } }.string
end