Class: Raptor::Http2

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/http2.rb

Overview

Handles HTTP/2 request processing and Rack application integration.

Http2 manages the HTTP/2 protocol lifecycle including frame processing, HPACK header compression, stream management, and response writing. It integrates with the same reactor, ractor pool, and thread pool pipeline used by HTTP/1.1 connections.

Defined Under Namespace

Classes: Writer

Constant Summary collapse

FLAG_END_STREAM =
0x1
FLAG_END_HEADERS =
0x4
FLAG_ACK =
0x1
FLAG_PRIORITY =
0x20
SERVER_PROTOCOL =
"HTTP/2"
RACK_HEADER_PREFIX =
"rack."
HOP_BY_HOP_HEADERS =
Set.new(%w[connection transfer-encoding keep-alive upgrade proxy-connection]).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, server_port) ⇒ void

Creates a new Http2 handler.

Parameters:

  • app (#call)

    the Rack application to dispatch requests to

  • server_port (Integer)

    port number used to populate SERVER_PORT in the Rack env



96
97
98
99
# File 'lib/raptor/http2.rb', line 96

def initialize(app, server_port)
  @app = app
  @server_port = server_port
end

Class Method Details

.build_server_settings_frameString

Builds the initial server SETTINGS frame to send on connection establishment.

Returns:

  • (String)

    the encoded SETTINGS frame



106
107
108
109
110
111
112
113
# File 'lib/raptor/http2.rb', line 106

def self.build_server_settings_frame
  parser = Http2Parser.new
  settings_payload = parser.build_settings(
    max_concurrent_streams: 100,
    initial_window_size: 65_535
  )
  parser.build_frame(:settings, 0, 0, settings_payload)
end

.process_frames(data) ⇒ Hash

Processes HTTP/2 frames from the connection buffer.

Parses frames, handles HPACK decoding, tracks stream state, and returns updated connection state along with any outgoing protocol frames and completed stream requests. Ractor-safe.

Parameters:

  • data (Hash)

    the connection state including buffer and HPACK table

Returns:

  • (Hash)

    updated state with outgoing_frames and completed_requests



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/raptor/http2.rb', line 125

def self.process_frames(data)
  parser = Http2Parser.new
  buffer = data[:buffer]
  hpack_table = data[:hpack_table] || []
  streams = data[:http2_streams] ? data[:http2_streams].dup : {}
  outgoing_frames = []
  completed_requests = []
  connection_window = data[:http2_window] || 65_535
  preface_received = data[:http2_preface_received] || false

  unless preface_received
    if buffer.bytesize >= 24 && buffer.byteslice(0, 24) == Http2Parser.connection_preface
      buffer = buffer.byteslice(24..-1) || ""
      preface_received = true
    else
      return build_result(data, buffer, hpack_table, streams, outgoing_frames, completed_requests, connection_window, preface_received)
    end
  end

  loop do
    parsed = parser.parse_frame(buffer)
    break unless parsed

    frame, consumed = parsed
    buffer = buffer.byteslice(consumed..-1) || ""

    case frame[:type]
    when :settings
      if (frame[:flags] & FLAG_ACK).zero?
        outgoing_frames << parser.build_frame(:settings, FLAG_ACK, 0, nil)
      end

    when :headers
      stream_id = frame[:stream_id]
      header_payload = frame[:payload]

      if (frame[:flags] & FLAG_PRIORITY) != 0
        header_payload = header_payload.byteslice(5..-1) || ""
      end

      decoded_headers, hpack_table = parser.parse_headers(header_payload, hpack_table)
      stream = streams[stream_id] || {}
      stream = stream.merge(headers: decoded_headers)

      if (frame[:flags] & FLAG_END_STREAM) != 0
        stream = stream.merge(end_stream: true)
        completed_requests << {
          stream_id: stream_id,
          headers: decoded_headers,
          body: stream[:body] || ""
        }

        streams.delete(stream_id)
      else
        streams[stream_id] = stream
      end

    when :data
      stream_id = frame[:stream_id]
      stream = streams[stream_id] || {}
      existing_body = stream[:body] || ""
      stream = stream.merge(body: existing_body + frame[:payload])

      if frame[:payload].bytesize.positive?
        connection_window -= frame[:payload].bytesize
        if connection_window < 32_768
          increment = 65_535 - connection_window
          wu_payload = [increment].pack("N")
          outgoing_frames << parser.build_frame(:window_update, 0, 0, wu_payload)
          outgoing_frames << parser.build_frame(:window_update, 0, stream_id, wu_payload)
          connection_window += increment
        end
      end

      if (frame[:flags] & FLAG_END_STREAM) != 0
        stream_headers = stream[:headers] || []
        completed_requests << {
          stream_id: stream_id,
          headers: stream_headers,
          body: stream[:body]
        }

        streams.delete(stream_id)
      else
        streams[stream_id] = stream
      end

    when :window_update
      parser.parse_window_update(frame[:payload])

    when :ping
      if (frame[:flags] & FLAG_ACK).zero?
        outgoing_frames << parser.build_frame(:ping, FLAG_ACK, 0, frame[:payload])
      end

    when :goaway
      break

    when :rst_stream
      streams.delete(frame[:stream_id])
    end
  end

  build_result(data, buffer, hpack_table, streams, outgoing_frames, completed_requests, connection_window, preface_received)
end

Instance Method Details

#handle_parsed_request(result, reactor, thread_pool) ⇒ void

This method returns an undefined value.

Handles a parsed HTTP/2 request from the ractor pool.

Writes outgoing protocol frames to the socket, updates reactor state, and dispatches completed stream requests to the thread pool.

Parameters:

  • result (Hash)

    the parsed result from the ractor pool

  • reactor (Reactor)

    the reactor managing the connection

  • thread_pool (AtomicThreadPool)

    thread pool for Rack app dispatch



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/raptor/http2.rb', line 272

def handle_parsed_request(result, reactor, thread_pool)
  socket = reactor.socket_for(result[:id])
  return unless socket

  writer = reactor.writer_for(result[:id])

  writer.write_frames(socket, result[:outgoing_frames])

  reactor.update_http2_state(result)

  result[:completed_requests]&.each do |request|
    stream_id = request[:stream_id]
    remote_addr = result[:remote_addr] || "127.0.0.1"

    thread_pool << proc do
      dispatch_stream_request(
        socket, writer, stream_id,
        request[:headers], request[:body],
        remote_addr: remote_addr
      )
    end
  end
end