Class: Net::HTTPResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/em-net-http.rb

Instance Method Summary collapse

Instance Method Details

#orig_net_http_read_bodyObject



74
# File 'lib/em-net-http.rb', line 74

alias_method :orig_net_http_read_body, :read_body

#read_body(dest = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/em-net-http.rb', line 76

def read_body(dest=nil, &block)
  return @body if @already_buffered
  return orig_net_http_read_body(dest, &block) unless ::EM.reactor_running?
  if block_given?
    f = Fiber.current
    @httpreq.callback { |res| f.resume }
    @httpreq.stream &block
    Fiber.yield
  else
    unless @body || @already_buffered
      if self.class.body_permitted?
        f = Fiber.current
        io = StringIO.new '', 'wb'
        io.set_encoding 'ASCII-8BIT'
        @httpreq.callback { |res| f.resume io.string }
        @httpreq.errback { |err| f.resume err }
        @httpreq.stream { |chunk| io.write chunk }
        @body = Fiber.yield
      end
      @already_buffered = true
    end
    @body
  end
end