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_content_encoding(h) ⇒ Object
checks for content encoding headers and sets flags.
-
#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 41 42 |
# 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 @gzip = false @deflate = 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
152 153 154 155 156 157 158 |
# File 'lib/h2/stream.rb', line 152 def add_headers h check_event_source h check_content_encoding 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
77 78 79 |
# File 'lib/h2/stream.rb', line 77 def add_push stream @pushes << stream end |
#bind_events ⇒ Object
binds all stream events to their respective on_ handlers
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/h2/stream.rb', line 123 def bind_events @stream.on(:close) do @parent.add_push self if @parent && push? @client.last_stream = self @closed = true if @eventsource @body << :close else @body = Zlib.gunzip @body if @gzip @body = Zlib.inflate @body if @deflate end 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 @body << d end end |
#block!(timeout = nil) ⇒ Object
block this stream until unblocked or timeout
90 91 92 93 |
# File 'lib/h2/stream.rb', line 90 def block! timeout = nil @pushes.each {|p| p.block! timeout} super end |
#body ⇒ String
Returns response headers (blocks).
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/h2/stream.rb', line 104 def body if @eventsource loop do event = @body.pop break if event == :close event = ::Zlib.gunzip event if @gzip event = ::Zlib.inflate event if @deflate yield event end else block! @body end end |
#cancel! ⇒ Object
call cancel and unblock this Stream
83 84 85 86 |
# File 'lib/h2/stream.rb', line 83 def cancel! @stream.cancel unblock! end |
#check_content_encoding(h) ⇒ Object
checks for content encoding headers and sets flags
173 174 175 176 177 178 179 180 181 |
# File 'lib/h2/stream.rb', line 173 def check_content_encoding h return if @gzip or @deflate h.each do |e| if e[0] == CONTENT_ENCODING_KEY @gzip = true if e[1] == GZIP_ENCODING @deflate = true if e[1] == DEFLATE_ENCODING end end end |
#check_event_source(h) ⇒ Object
checks for event source headers and reconfigures @body
162 163 164 165 166 167 168 169 |
# File 'lib/h2/stream.rb', line 162 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 unblock! end end |
#closed? ⇒ Boolean
Returns true if this Stream
is closed.
58 59 60 |
# File 'lib/h2/stream.rb', line 58 def closed? @closed end |
#eventsource? ⇒ Boolean
Returns true if this Stream
is connected to an EventSource
.
70 71 72 73 |
# File 'lib/h2/stream.rb', line 70 def eventsource? block! @eventsource end |
#headers ⇒ Hash
Returns response headers (blocks).
97 98 99 100 |
# File 'lib/h2/stream.rb', line 97 def headers block! @headers end |
#id ⇒ Integer
Returns stream ID.
46 47 48 |
# File 'lib/h2/stream.rb', line 46 def id @stream.id end |
#ok? ⇒ Boolean
Returns true if response status is 200.
52 53 54 |
# File 'lib/h2/stream.rb', line 52 def ok? headers[STATUS_KEY] == '200' end |
#push? ⇒ Boolean
Returns true if this Stream
is a push promise.
64 65 66 |
# File 'lib/h2/stream.rb', line 64 def push? @push end |
#to_h ⇒ Hash
Returns a simple Hash
with :headers
and :body
keys/values.
185 186 187 |
# File 'lib/h2/stream.rb', line 185 def to_h { headers: headers, body: body } end |