Class: H2::Stream

Inherits:
Object
  • Object
show all
Includes:
Blockable, On
Defined in:
lib/h2/stream.rb

Constant Summary collapse

STREAM_EVENTS =
[
  :close,
  :headers,
  :data
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from On

#on

Methods included from Blockable

#init_blocking, #unblock!

Constructor Details

#initialize(client:, stream:, push: false, parent: nil) {|_self| ... } ⇒ H2::Stream

create a new h2 stream

Parameters:

  • client (H2::Client)

    the Client bind this Stream to

  • stream (HTTP2::Stream)

    protocol library HTTP2::Stream instance

  • push (Boolean) (defaults to: false)

    true if a push promise stream, false otherwise

  • parent (H2::Stream) (defaults to: nil)

    the parent stream of this, if push promise stream

Yields:

  • (_self)

Yield Parameters:

  • _self (H2::Stream)

    the object that the method was called on



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

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/h2/stream.rb', line 14

def client
  @client
end

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/h2/stream.rb', line 14

def parent
  @parent
end

#pushesObject (readonly)

Returns the value of attribute pushes.



14
15
16
# File 'lib/h2/stream.rb', line 14

def pushes
  @pushes
end

#streamObject (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_eventsObject

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

#bodyString

Returns response headers (blocks).

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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

#headersHash

Returns response headers (blocks).

Returns:

  • (Hash)

    response headers (blocks)



97
98
99
100
# File 'lib/h2/stream.rb', line 97

def headers
  block!
  @headers
end

#idInteger

Returns stream ID.

Returns:

  • (Integer)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    true if this Stream is a push promise



64
65
66
# File 'lib/h2/stream.rb', line 64

def push?
  @push
end

#to_hHash

Returns a simple Hash with :headers and :body keys/values.

Returns:

  • (Hash)

    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