File: helper.rb

package info (click to toggle)
ruby-http-2 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,384 kB
  • sloc: ruby: 5,399; makefile: 4
file content (147 lines) | stat: -rw-r--r-- 2,731 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
require './spec/support/deep_dup'

RSpec.configure(&:disable_monkey_patching!)
RSpec::Expectations.configuration.warn_about_potential_false_positives = false

require 'json'
require 'coveralls'

Coveralls.wear! if ENV['CI']

# rubocop: disable Style/MixinUsage
require 'http/2'
include HTTP2
include HTTP2::Header
include HTTP2::Error
# rubocop: enable Style/MixinUsage

REQUEST_HEADERS = [%w(:scheme https),
                   %w(:path /),
                   %w(:authority example.com),
                   %w(:method GET),
                   %w(a b)].freeze
RESPONSE_HEADERS = [%w(:status 200)].freeze

module FrameHelpers
  def data_frame
    {
      type: :data,
      flags: [:end_stream],
      stream: 1,
      payload: 'text',
    }
  end

  def headers_frame
    {
      type: :headers,
      flags: [:end_headers].freeze,
      stream: 1,
      payload: Compressor.new.encode(REQUEST_HEADERS),
    }
  end

  def priority_frame
    {
      type: :priority,
      stream: 1,
      exclusive: false,
      stream_dependency: 0,
      weight: 20,
    }
  end

  def rst_stream_frame
    {
      type: :rst_stream,
      stream: 1,
      error: :stream_closed,
    }
  end

  def settings_frame
    {
      type: :settings,
      stream: 0,
      payload: [
        [:settings_max_concurrent_streams, 10],
        [:settings_initial_window_size, 0x7fffffff],
      ],
    }
  end

  def push_promise_frame
    {
      type: :push_promise,
      flags: [:end_headers],
      stream: 1,
      promise_stream: 2,
      payload: Compressor.new.encode([%w(a b)]),
    }
  end

  def ping_frame
    {
      stream: 0,
      type: :ping,
      payload: '12345678',
    }
  end

  def pong_frame
    {
      stream: 0,
      type: :ping,
      flags: [:ack],
      payload: '12345678',
    }
  end

  def goaway_frame
    {
      type: :goaway,
      last_stream: 2,
      error: :no_error,
      payload: 'debug',
    }
  end

  def window_update_frame
    {
      type: :window_update,
      increment: 10,
    }
  end

  def continuation_frame
    {
      type: :continuation,
      flags: [:end_headers],
      payload: '-second-block',
    }
  end

  def altsvc_frame
    {
      type: :altsvc,
      max_age: 1_402_290_402,           # 4
      port: 8080,                       # 2    reserved 1
      proto: 'h2-12',                   # 1 + 5
      host: 'www.example.com',          # 1 + 15
      origin: 'www.example.com'         # 15
    }
  end

  def frame_types
    methods.select { |meth| meth.to_s.end_with?('_frame') }
           .map { |meth| __send__(meth) }
  end
end

def set_stream_id(bytes, id)
  scheme = 'CnCCN'.freeze
  head = bytes.slice!(0, 9).unpack(scheme)
  head[4] = id

  head.pack(scheme) + bytes
end