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.

Instance Attribute Summary collapse

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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



49
50
51
# File 'lib/async/http/body/hijack.rb', line 49

def input
  @input
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)


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

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

#inspectObject



75
76
77
# File 'lib/async/http/body/hijack.rb', line 75

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

#readObject

Read the next available chunk.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/async/http/body/hijack.rb', line 61

def read
  unless @task
    @stream = Stream.new(@input)
    
    @task = Task.current.async do |task|
      task.annotate "Streaming hijacked body."
      
      @block.call(@stream)
    end
  end
  
  return @stream.output.read
end

#to_sObject



79
80
81
# File 'lib/async/http/body/hijack.rb', line 79

def to_s
  "<Hijack #{@block.class}>"
end