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
# 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

  init_blocking
  yield self if block_given?
  bind_events
end

Instance Attribute Details

#bodyString (readonly)

Returns response headers (blocks).

Returns:

  • (String)

    response headers (blocks)



93
94
95
# File 'lib/h2/stream.rb', line 93

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#headersHash (readonly)

Returns response headers (blocks).

Returns:

  • (Hash)

    response headers (blocks)



86
87
88
# File 'lib/h2/stream.rb', line 86

def headers
  @headers
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



121
122
123
124
125
126
# File 'lib/h2/stream.rb', line 121

def add_headers h
  h = Hash[h]
  on :headers, h
  @headers.merge! h
  @headers
end

#add_push(stream) ⇒ Object

add a push promise Stream to this Stream‘s list of “child” pushes



66
67
68
# File 'lib/h2/stream.rb', line 66

def add_push stream
  @pushes << stream
end

#bind_eventsObject

binds all stream events to their respective on_ handlers



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/h2/stream.rb', line 100

def bind_events
  @stream.on(:close) do
    @parent.add_push self if @parent && push?
    @client.last_stream = self
    @closed = true
    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



79
80
81
82
# File 'lib/h2/stream.rb', line 79

def block! timeout = nil
  @pushes.each {|p| p.block! timeout}
  super
end

#cancel!Object

call cancel and unblock this Stream



72
73
74
75
# File 'lib/h2/stream.rb', line 72

def cancel!
  @stream.cancel
  unblock!
end

#closed?Boolean

Returns true if this Stream is closed.

Returns:

  • (Boolean)

    true if this Stream is closed



54
55
56
# File 'lib/h2/stream.rb', line 54

def closed?
  @closed
end

#idInteger

Returns stream ID.

Returns:

  • (Integer)

    stream ID



42
43
44
# File 'lib/h2/stream.rb', line 42

def id
  @stream.id
end

#ok?Boolean

Returns true if response status is 200.

Returns:

  • (Boolean)

    true if response status is 200



48
49
50
# File 'lib/h2/stream.rb', line 48

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



60
61
62
# File 'lib/h2/stream.rb', line 60

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



130
131
132
# File 'lib/h2/stream.rb', line 130

def to_h
  { headers: headers, body: body }
end