class Async::HTTP::Body::Hijack

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.

Attributes

input[R]

Public Class Methods

new(block, input = nil) click to toggle source
# File lib/async/http/body/hijack.rb, line 39
def initialize(block, input = nil)
        @block = block
        @input = input
        
        @task = nil
        @stream = nil
end
response(request, status, headers, &block) click to toggle source
# 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 = nil, &block) click to toggle source
# File lib/async/http/body/hijack.rb, line 35
def self.wrap(request = nil, &block)
        self.new(block, request&.body)
end

Public Instance Methods

call(stream) click to toggle source
# File lib/async/http/body/hijack.rb, line 52
def call(stream)
        return @block.call(stream)
end
empty?() click to toggle source

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

# File lib/async/http/body/hijack.rb, line 59
def empty?
        if @stream
                @stream.empty?
        else
                false
        end
end
inspect() click to toggle source
# File lib/async/http/body/hijack.rb, line 88
def inspect
        "\#<#{self.class} #{@block.inspect}>"
end
read() click to toggle source

Read the next available chunk.

# File lib/async/http/body/hijack.rb, line 74
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
ready?() click to toggle source
# File lib/async/http/body/hijack.rb, line 67
def ready?
        if @stream
                @stream.output.ready?
        end
end
stream?() click to toggle source

We prefer streaming directly as it's the lowest overhead.

# File lib/async/http/body/hijack.rb, line 48
def stream?
        true
end
to_s() click to toggle source
# File lib/async/http/body/hijack.rb, line 92
def to_s
        "<Hijack #{@block.class}>"
end