Class: Async::HTTP::Body::Hijack
- Inherits:
-
Protocol::HTTP::Body::Readable
- Object
- Protocol::HTTP::Body::Readable
- Async::HTTP::Body::Hijack
- 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
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Class Method Summary collapse
Instance Method Summary collapse
- #call(stream) ⇒ Object
-
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?.
-
#initialize(block, input = nil) ⇒ Hijack
constructor
A new instance of Hijack.
- #inspect ⇒ Object
-
#read ⇒ Object
Read the next available chunk.
- #ready? ⇒ Boolean
-
#stream? ⇒ Boolean
We prefer streaming directly as it’s the lowest overhead.
- #to_s ⇒ Object
Constructor Details
#initialize(block, input = nil) ⇒ Hijack
Returns a new instance of Hijack.
24 25 26 27 28 29 30 31 |
# File 'lib/async/http/body/hijack.rb', line 24 def initialize(block, input = nil) @block = block @input = input @task = nil @stream = nil @output = nil end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
42 43 44 |
# File 'lib/async/http/body/hijack.rb', line 42 def input @input end |
Class Method Details
.response(request, status, headers, &block) ⇒ Object
16 17 18 |
# File 'lib/async/http/body/hijack.rb', line 16 def self.response(request, status, headers, &block) ::Protocol::HTTP::Response[status, headers, self.wrap(request, &block)] end |
.wrap(request = nil, &block) ⇒ Object
20 21 22 |
# File 'lib/async/http/body/hijack.rb', line 20 def self.wrap(request = nil, &block) self.new(block, request&.body) end |
Instance Method Details
#call(stream) ⇒ Object
38 39 40 |
# File 'lib/async/http/body/hijack.rb', line 38 def call(stream) @block.call(stream) end |
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?
45 46 47 |
# File 'lib/async/http/body/hijack.rb', line 45 def empty? @output&.empty? end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/async/http/body/hijack.rb', line 69 def inspect "\#<#{self.class} #{@block.inspect}>" end |
#read ⇒ Object
Read the next available chunk.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/async/http/body/hijack.rb', line 54 def read unless @output @output = Writable.new @stream = ::Protocol::HTTP::Body::Stream.new(@input, @output) @task = Task.current.async do |task| task.annotate "Streaming hijacked body." @block.call(@stream) end end return @output.read end |
#ready? ⇒ Boolean
49 50 51 |
# File 'lib/async/http/body/hijack.rb', line 49 def ready? @output&.ready? end |
#stream? ⇒ Boolean
We prefer streaming directly as it’s the lowest overhead.
34 35 36 |
# File 'lib/async/http/body/hijack.rb', line 34 def stream? true end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/async/http/body/hijack.rb', line 73 def to_s "<Hijack #{@block.class}>" end |