Class: Raptor::Http2
- Inherits:
-
Object
- Object
- Raptor::Http2
- Defined in:
- lib/raptor/http2.rb,
sig/generated/raptor/http2.rbs
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
-
.build_server_settings_frame ⇒ String
Builds the initial server SETTINGS frame to send on connection establishment.
-
.process_frames(data) ⇒ Hash
Processes HTTP/2 frames from the connection buffer.
Instance Method Summary collapse
-
#build_rack_env(headers, body, remote_addr:) ⇒ Hash
Builds a Rack environment hash from HTTP/2 headers and body.
-
#dispatch_stream_request(socket, writer, stream_id, headers, body, remote_addr:) ⇒ void
Dispatches a completed stream request to the Rack app and writes the response back as HTTP/2 frames.
-
#handle_parsed_request(result, reactor, thread_pool) ⇒ void
Handles a parsed HTTP/2 request from the ractor pool.
-
#initialize(app, server_port) ⇒ Http2
constructor
Creates a new Http2 handler.
-
#populate_server_name_and_port(env) ⇒ void
Populates SERVER_NAME and SERVER_PORT from the HTTP_HOST header.
-
#write_http2_error_response(socket, writer, stream_id) ⇒ void
Writes a 500 error response as HTTP/2 frames.
-
#write_http2_response(socket, writer, stream_id, status, headers, body) ⇒ void
Writes a Rack response as HTTP/2 frames to the socket.
Constructor Details
#initialize(app, server_port) ⇒ Http2
Creates a new Http2 handler.
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_frame ⇒ String
Builds the initial server SETTINGS frame to send on connection establishment.
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.
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
#build_rack_env(headers, body, remote_addr:) ⇒ Hash
Builds a Rack environment hash from HTTP/2 headers and body.
Translates HTTP/2 pseudo-headers into Rack-compatible environment keys and populates all required Rack env entries.
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/raptor/http2.rb', line 398 def build_rack_env(headers, body, remote_addr:) env = {} headers.each do |name, value| if name.start_with?(":") case name when ":method" then env[Rack::REQUEST_METHOD] = value when ":path" path, query = value.split("?", 2) env[Rack::PATH_INFO] = path env[Rack::QUERY_STRING] = query || "" when ":scheme" then env[Rack::RACK_URL_SCHEME] = value when ":authority" then env[Rack::HTTP_HOST] = value end elsif name == "content-type" env["CONTENT_TYPE"] = value elsif name == "content-length" env["CONTENT_LENGTH"] = value else rack_key = "HTTP_#{name.upcase.tr("-", "_")}" env[rack_key] = value end end env[Rack::SERVER_PROTOCOL] = SERVER_PROTOCOL env[Rack::RACK_VERSION] = Rack::VERSION env[Rack::RACK_INPUT] = StringIO.new(body).set_encoding(Encoding::ASCII_8BIT) env[Rack::RACK_ERRORS] = $stderr env[Rack::RACK_RESPONSE_FINISHED] = [] env[Rack::RACK_IS_HIJACK] = false env[Rack::SCRIPT_NAME] = "" unless env.key?(Rack::SCRIPT_NAME) env[Rack::PATH_INFO] = "" unless env.key?(Rack::PATH_INFO) env[Rack::QUERY_STRING] = "" unless env.key?(Rack::QUERY_STRING) if body.bytesize.positive? && !env.key?("CONTENT_LENGTH") env["CONTENT_LENGTH"] = body.bytesize.to_s end env["REMOTE_ADDR"] = remote_addr populate_server_name_and_port(env) env end |
#dispatch_stream_request(socket, writer, stream_id, headers, body, remote_addr:) ⇒ void
This method returns an undefined value.
Dispatches a completed stream request to the Rack app and writes the response back as HTTP/2 frames.
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/raptor/http2.rb', line 310 def dispatch_stream_request(socket, writer, stream_id, headers, body, remote_addr:) env = build_rack_env(headers, body, remote_addr: remote_addr) status, response_headers, response_body = @app.call(env) write_http2_response(socket, writer, stream_id, status, response_headers, response_body) rescue write_http2_error_response(socket, writer, stream_id) raise ensure response_body.close if response_body.respond_to?(:close) end |
#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.
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 |
#populate_server_name_and_port(env) ⇒ void
This method returns an undefined value.
Populates SERVER_NAME and SERVER_PORT from the HTTP_HOST header.
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/raptor/http2.rb', line 450 def populate_server_name_and_port(env) http_host = env[Rack::HTTP_HOST] if http_host if http_host.start_with?("[") host = http_host[/\A\[([^\]]+)\]/, 1] port = http_host[/\]:(\d+)\z/, 1] else host, port = http_host.split(":", 2) end env[Rack::SERVER_NAME] ||= host env[Rack::SERVER_PORT] ||= port || @server_port.to_s else env[Rack::SERVER_NAME] ||= "localhost" env[Rack::SERVER_PORT] ||= @server_port.to_s end end |
#write_http2_error_response(socket, writer, stream_id) ⇒ void
This method returns an undefined value.
Writes a 500 error response as HTTP/2 frames.
377 378 379 380 381 382 383 384 385 |
# File 'lib/raptor/http2.rb', line 377 def write_http2_error_response(socket, writer, stream_id) parser = Http2Parser.new encoded = parser.encode_headers([[":status", "500"]]) writer.write_frames( socket, [parser.build_frame(:headers, FLAG_END_STREAM | FLAG_END_HEADERS, stream_id, encoded)] ) end |
#write_http2_response(socket, writer, stream_id, status, headers, body) ⇒ void
This method returns an undefined value.
Writes a Rack response as HTTP/2 frames to the socket.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/raptor/http2.rb', line 333 def write_http2_response(socket, writer, stream_id, status, headers, body) parser = Http2Parser.new header_pairs = [[":status", status.to_s]] headers.each do |name, value| lowered = name.downcase next if lowered.start_with?(RACK_HEADER_PREFIX) next if HOP_BY_HOP_HEADERS.include?(lowered) if value.is_a?(Array) value.each { |val| header_pairs << [lowered, val.to_s] } else header_pairs << [lowered, value.to_s] end end encoded_headers = parser.encode_headers(header_pairs) body_chunks = [] body.each { |chunk| body_chunks << chunk unless chunk.empty? } frames = [] if body_chunks.empty? frames << parser.build_frame(:headers, FLAG_END_STREAM | FLAG_END_HEADERS, stream_id, encoded_headers) else frames << parser.build_frame(:headers, FLAG_END_HEADERS, stream_id, encoded_headers) last_index = body_chunks.size - 1 body_chunks.each_with_index do |chunk, index| flags = index == last_index ? FLAG_END_STREAM : 0 frames << parser.build_frame(:data, flags, stream_id, chunk) end end writer.write_frames(socket, frames) end |