Class: Async::HTTP::Body::Hijack

Inherits:
Protocol::HTTP::Body::Readable
  • Object
show all
Defined in:
lib/async/http/body/hijack.rb

Overview

A body which is designed for hijacked connections.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, &block) ⇒ Hijack

Returns a new instance of Hijack.



37
38
39
40
41
42
43
# File 'lib/async/http/body/hijack.rb', line 37

def initialize(input = nil, &block)
  @input = input
  @block = block
  
  @task = nil
  @stream = nil
end

Class Method Details

.response(request, status, headers, &block) ⇒ Object



29
30
31
# File 'lib/async/http/body/hijack.rb', line 29

def self.response(request, status, headers, &block)
  ::Protocol::HTTP::Response[status, headers, self.wrap(request, &block)]
end

.wrap(request, &block) ⇒ Object



33
34
35
# File 'lib/async/http/body/hijack.rb', line 33

def self.wrap(request, &block)
  self.new(request.body, &block)
end

Instance Method Details

#call(stream) ⇒ Object



45
46
47
# File 'lib/async/http/body/hijack.rb', line 45

def call(stream)
  return @block.call(stream)
end

#empty?Boolean

Has the producer called #finish and has the reader consumed the nil token?

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/async/http/body/hijack.rb', line 50

def empty?
  if @stream
    @stream.empty?
  else
    false
  end
end

#inspectObject



71
72
73
# File 'lib/async/http/body/hijack.rb', line 71

def inspect
  "\#<#{self.class} #{@block.inspect}>"
end

#readObject

Read the next available chunk.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/http/body/hijack.rb', line 59

def read
  unless @task
    @stream = Stream.new(@input)
    
    @task = Task.current.async do
      @block.call(@stream)
    end
  end
  
  return @stream.output.read
end