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 server responses - a response which uses a block to read and write the request and response bodies respectively.

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.



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

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.



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

def input
  @input
end

Class Method Details

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



31
32
33
# File 'lib/async/http/body/hijack.rb', line 31

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

.wrap(request, &block) ⇒ Object



35
36
37
# File 'lib/async/http/body/hijack.rb', line 35

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

Instance Method Details

#call(stream) ⇒ Object



47
48
49
# File 'lib/async/http/body/hijack.rb', line 47

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)


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

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

#inspectObject



77
78
79
# File 'lib/async/http/body/hijack.rb', line 77

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

#readObject

Read the next available chunk.



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

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



81
82
83
# File 'lib/async/http/body/hijack.rb', line 81

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