File: helper.rb

package info (click to toggle)
ruby-http-2 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,360 kB
  • sloc: ruby: 6,031; makefile: 4
file content (171 lines) | stat: -rw-r--r-- 3,402 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

GC.auto_compact = true if GC.respond_to?(:auto_compact=)

if ENV.key?("CI")
  require "simplecov"
  SimpleCov.command_name "#{RUBY_ENGINE}-#{RUBY_VERSION}"
  SimpleCov.coverage_dir "coverage/#{RUBY_ENGINE}-#{RUBY_VERSION}"
end

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

require "json"

# 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

HTTP2::Connection.__send__ :public, :send_buffer
HTTP2::Stream.__send__ :public, :send_buffer

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,
      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(REQUEST_HEADERS)
    }
  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,
      stream: 1,
      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 origin_frame
    {
      type: :origin,
      payload: %w[https://www.example.com https://www.example.org]
    }
  end

  DATA_FRAMES = %w[headers continuation push_promise data].freeze

  def control_frames
    methods.select { |meth| meth.to_s.end_with?("_frame") }
           .reject { |meth| DATA_FRAMES.include?(meth.to_s.gsub(/_frame$/, "")) }
           .map { |meth| __send__(meth) }
  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"
  head = bytes.slice!(0, 9).unpack(scheme)
  head[4] = id

  head.pack(scheme) + bytes
end