Class: H2::Stream
Constant Summary collapse
- STREAM_EVENTS =
[ :close, :headers, :data ]
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#pushes ⇒ Object
readonly
Returns the value of attribute pushes.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#add_headers(h) ⇒ Object
builds
Hash
from associative array, merges into response headers. -
#add_push(stream) ⇒ Object
add a push promise
Stream
to thisStream
‘s list of “child” pushes. -
#bind_events ⇒ Object
binds all stream events to their respective on_ handlers.
-
#block!(timeout = nil) ⇒ Object
block this stream until unblocked or timeout.
-
#body ⇒ String
Response headers (blocks).
-
#cancel! ⇒ Object
call cancel and unblock this
Stream
. -
#check_event_source(h) ⇒ Object
checks for event source headers and reconfigures @body.
-
#closed? ⇒ Boolean
True if this
Stream
is closed. -
#eventsource? ⇒ Boolean
True if this
Stream
is connected to anEventSource
. -
#headers ⇒ Hash
Response headers (blocks).
-
#id ⇒ Integer
Stream ID.
-
#initialize(client:, stream:, push: false, parent: nil) {|_self| ... } ⇒ H2::Stream
constructor
create a new h2 stream.
-
#ok? ⇒ Boolean
True if response status is 200.
-
#push? ⇒ Boolean
True if this
Stream
is a push promise. -
#to_h ⇒ Hash
A simple
Hash
with:headers
and:body
keys/values.
Methods included from On
Methods included from Blockable
Constructor Details
#initialize(client:, stream:, push: false, parent: nil) {|_self| ... } ⇒ H2::Stream
create a new h2 stream
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/h2/stream.rb', line 25 def initialize client:, stream:, push: false, parent: nil @body = '' @client = client @closed = false @headers = {} @parent = parent @push = push @pushes = Set.new @stream = stream @eventsource = false init_blocking yield self if block_given? bind_events end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
14 15 16 |
# File 'lib/h2/stream.rb', line 14 def client @client end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
14 15 16 |
# File 'lib/h2/stream.rb', line 14 def parent @parent end |
#pushes ⇒ Object (readonly)
Returns the value of attribute pushes.
14 15 16 |
# File 'lib/h2/stream.rb', line 14 def pushes @pushes end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
14 15 16 |
# File 'lib/h2/stream.rb', line 14 def stream @stream end |
Instance Method Details
#add_headers(h) ⇒ Object
builds Hash
from associative array, merges into response headers
140 141 142 143 144 145 |
# File 'lib/h2/stream.rb', line 140 def add_headers h check_event_source h h = Hash[h] on :headers, h @headers.merge! h end |
#add_push(stream) ⇒ Object
add a push promise Stream
to this Stream
‘s list of “child” pushes
75 76 77 |
# File 'lib/h2/stream.rb', line 75 def add_push stream @pushes << stream end |
#bind_events ⇒ Object
binds all stream events to their respective on_ handlers
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/h2/stream.rb', line 117 def bind_events @stream.on(:close) do @parent.add_push self if @parent && push? @client.last_stream = self @closed = true @body << :close if @eventsource unblock! on :close end ah = method :add_headers @stream.on :promise_headers, &ah @stream.on :headers, &ah @stream.on(:data) do |d| on :data, d unblock! if @eventsource @body << d end end |
#block!(timeout = nil) ⇒ Object
block this stream until unblocked or timeout
88 89 90 91 |
# File 'lib/h2/stream.rb', line 88 def block! timeout = nil @pushes.each {|p| p.block! timeout} super end |
#body ⇒ String
Returns response headers (blocks).
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/h2/stream.rb', line 102 def body if @eventsource loop do event = @body.pop break if event == :close yield event end else block! @body end end |
#cancel! ⇒ Object
call cancel and unblock this Stream
81 82 83 84 |
# File 'lib/h2/stream.rb', line 81 def cancel! @stream.cancel unblock! end |
#check_event_source(h) ⇒ Object
checks for event source headers and reconfigures @body
149 150 151 152 153 154 155 |
# File 'lib/h2/stream.rb', line 149 def check_event_source h return if @eventsource if h.any? {|e| e[0] == CONTENT_TYPE_KEY && e[1] == EVENT_SOURCE_CONTENT_TYPE } @eventsource = true @body = Queue.new end end |
#closed? ⇒ Boolean
Returns true if this Stream
is closed.
56 57 58 |
# File 'lib/h2/stream.rb', line 56 def closed? @closed end |
#eventsource? ⇒ Boolean
Returns true if this Stream
is connected to an EventSource
.
68 69 70 71 |
# File 'lib/h2/stream.rb', line 68 def eventsource? block! @eventsource end |
#headers ⇒ Hash
Returns response headers (blocks).
95 96 97 98 |
# File 'lib/h2/stream.rb', line 95 def headers block! @headers end |
#id ⇒ Integer
Returns stream ID.
44 45 46 |
# File 'lib/h2/stream.rb', line 44 def id @stream.id end |
#ok? ⇒ Boolean
Returns true if response status is 200.
50 51 52 |
# File 'lib/h2/stream.rb', line 50 def ok? headers[STATUS_KEY] == '200' end |
#push? ⇒ Boolean
Returns true if this Stream
is a push promise.
62 63 64 |
# File 'lib/h2/stream.rb', line 62 def push? @push end |
#to_h ⇒ Hash
Returns a simple Hash
with :headers
and :body
keys/values.
159 160 161 |
# File 'lib/h2/stream.rb', line 159 def to_h { headers: headers, body: body } end |