Class: Biryani::Frame::PushPromise

Inherits:
Object
  • Object
show all
Defined in:
lib/biryani/frame/push_promise.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_headers, stream_id, promised_stream_id, fragment, padding) ⇒ PushPromise

Returns a new instance of PushPromise.

Parameters:

  • end_headers (Boolean)
  • stream_id (Integer)
  • promised_stream_id (Integer)
  • fragment (String)
  • padding (String, nil)


11
12
13
14
15
16
17
18
# File 'lib/biryani/frame/push_promise.rb', line 11

def initialize(end_headers, stream_id, promised_stream_id, fragment, padding)
  @f_type = FrameType::PUSH_PROMISE
  @end_headers = end_headers
  @stream_id = stream_id
  @promised_stream_id = promised_stream_id
  @fragment = fragment
  @padding = padding
end

Instance Attribute Details

#f_typeObject (readonly)

Returns the value of attribute f_type.



4
5
6
# File 'lib/biryani/frame/push_promise.rb', line 4

def f_type
  @f_type
end

#fragmentObject (readonly)

Returns the value of attribute fragment.



4
5
6
# File 'lib/biryani/frame/push_promise.rb', line 4

def fragment
  @fragment
end

#paddingObject (readonly)

Returns the value of attribute padding.



4
5
6
# File 'lib/biryani/frame/push_promise.rb', line 4

def padding
  @padding
end

#promised_stream_idObject (readonly)

Returns the value of attribute promised_stream_id.



4
5
6
# File 'lib/biryani/frame/push_promise.rb', line 4

def promised_stream_id
  @promised_stream_id
end

#stream_idObject (readonly)

Returns the value of attribute stream_id.



4
5
6
# File 'lib/biryani/frame/push_promise.rb', line 4

def stream_id
  @stream_id
end

Class Method Details

.read(s, flags, stream_id) ⇒ PushPromise

Parameters:

  • s (String)
  • flags (Integer)
  • stream_id (Integer)

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/biryani/frame/push_promise.rb', line 50

def self.read(s, flags, stream_id)
  padded = Frame.read_padded(flags)
  end_headers = Frame.read_end_headers(flags)

  io = IO::Buffer.for(s)
  if padded
    pad_length, promised_stream_id = io.get_values(%i[U8 U32], 0)
    promised_stream_id %= 2**31 # Promised Stream ID (31)
    fragment_length = s.bytesize - pad_length - 5
    fragment = io.get_string(5, fragment_length)
    padding = io.get_string(5 + fragment_length)
    return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if pad_length >= s.bytesize
    return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if padding.bytesize != pad_length
  else
    promised_stream_id = io.get_value(:U32, 0)
    promised_stream_id %= 2**31 # Promised Stream ID (31)
    fragment = io.get_string(4)
    return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if fragment.bytesize + 4 != s.bytesize
  end

  PushPromise.new(end_headers, stream_id, promised_stream_id, fragment, padding)
end

Instance Method Details

#end_headers?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/biryani/frame/push_promise.rb', line 26

def end_headers?
  @end_headers
end

#lengthInteger

Returns:

  • (Integer)


31
32
33
# File 'lib/biryani/frame/push_promise.rb', line 31

def length
  @fragment.bytesize + 4 + (padded? ? 1 + @padding.bytesize : 0)
end

#padded?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/biryani/frame/push_promise.rb', line 21

def padded?
  !@padding.nil?
end

#to_binary_sString

Returns:

  • (String)


36
37
38
39
40
41
42
43
# File 'lib/biryani/frame/push_promise.rb', line 36

def to_binary_s
  payload_length = length
  flags = Frame.to_flags(padded: padded?, end_headers: end_headers?)
  pad_length = padded? ? @padding.bytesize.chr : ''
  padding = @padding || ''

  Frame.to_binary_s_header(payload_length, @f_type, flags, @stream_id) + pad_length + [@promised_stream_id].pack('N') + @fragment + padding
end