class H2::Stream
Constants
- STREAM_EVENTS
Attributes
Public Class Methods
create a new h2 stream
@param [H2::Client] client the Client
bind this Stream
to @param [HTTP2::Stream] stream protocol library HTTP2::Stream
instance @param [Boolean] push true if a push promise stream, false otherwise @param [H2::Stream] parent the parent stream of this, if push promise stream
@return [H2::Stream]
# 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
Public Instance Methods
builds Hash
from associative array, merges into response headers
# 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 a push promise Stream
to this Stream
's list of “child” pushes
# File lib/h2/stream.rb, line 77 def add_push stream @pushes << stream end
binds all stream events to their respective on_ handlers
# 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 this stream until unblocked or timeout
H2::Blockable#block!
# File lib/h2/stream.rb, line 90 def block! timeout = nil @pushes.each {|p| p.block! timeout} super end
@return [String] response headers (blocks)
# 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
call cancel and unblock this Stream
# File lib/h2/stream.rb, line 83 def cancel! @stream.cancel unblock! end
checks for content encoding headers and sets flags
# 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
checks for event source headers and reconfigures +@body+
# 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
@return [Boolean] true if this Stream
is closed
# File lib/h2/stream.rb, line 58 def closed? @closed end
@return [Boolean] true if this Stream
is connected to an EventSource
# File lib/h2/stream.rb, line 70 def eventsource? block! @eventsource end
@return [Hash] response headers (blocks)
# File lib/h2/stream.rb, line 97 def headers block! @headers end
@return [Integer] stream ID
# File lib/h2/stream.rb, line 46 def id @stream.id end
@return [Boolean] true if response status is 200
# File lib/h2/stream.rb, line 52 def ok? headers[STATUS_KEY] == '200' end
@return [Boolean] true if this Stream
is a push promise
# File lib/h2/stream.rb, line 64 def push? @push end
@return [Hash] a simple Hash
with :headers
and :body
keys/values
# File lib/h2/stream.rb, line 185 def to_h { headers: headers, body: body } end