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.
41 42 43 44 45 46 47 48 |
# File 'lib/async/http/body/hijack.rb', line 41 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.
59 60 61 |
# File 'lib/async/http/body/hijack.rb', line 59 def input @input end |
Class Method Details
.response(request, status, headers, &block) ⇒ Object
33 34 35 |
# File 'lib/async/http/body/hijack.rb', line 33 def self.response(request, status, headers, &block) ::Protocol::HTTP::Response[status, headers, self.wrap(request, &block)] end |
.wrap(request = nil, &block) ⇒ Object
37 38 39 |
# File 'lib/async/http/body/hijack.rb', line 37 def self.wrap(request = nil, &block) self.new(block, request&.body) end |
Instance Method Details
#call(stream) ⇒ Object
55 56 57 |
# File 'lib/async/http/body/hijack.rb', line 55 def call(stream) return @block.call(stream) end |
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?
62 63 64 |
# File 'lib/async/http/body/hijack.rb', line 62 def empty? @output&.empty? end |
#inspect ⇒ Object
86 87 88 |
# File 'lib/async/http/body/hijack.rb', line 86 def inspect "\#<#{self.class} #{@block.inspect}>" end |
#read ⇒ Object
Read the next available chunk.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/async/http/body/hijack.rb', line 71 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
66 67 68 |
# File 'lib/async/http/body/hijack.rb', line 66 def ready? @output&.ready? end |
#stream? ⇒ Boolean
We prefer streaming directly as it’s the lowest overhead.
51 52 53 |
# File 'lib/async/http/body/hijack.rb', line 51 def stream? true end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/async/http/body/hijack.rb', line 90 def to_s "<Hijack #{@block.class}>" end |