Class: Raptor::Http2
- Inherits:
-
Object
- Object
- Raptor::Http2
- 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.
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
-
#handle_parsed_request(result, reactor, thread_pool) ⇒ void
Handles a parsed HTTP/2 request from the ractor pool.
-
#initialize(app, server_port) ⇒ void
constructor
Creates a new Http2 handler.
Constructor Details
#initialize(app, server_port) ⇒ void
Creates a new Http2 handler.
38 39 40 41 |
# File 'lib/raptor/http2.rb', line 38 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.
48 49 50 51 52 53 54 55 |
# File 'lib/raptor/http2.rb', line 48 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.
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 |
# File 'lib/raptor/http2.rb', line 67 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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/raptor/http2.rb', line 214 def handle_parsed_request(result, reactor, thread_pool) socket = reactor.socket_for(result[:id]) return unless socket mutex = reactor.mutex_for(result[:id]) if result[:outgoing_frames]&.any? mutex.synchronize do result[:outgoing_frames].each { |frame| socket.write(frame) rescue nil } end end 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, mutex, stream_id, request[:headers], request[:body], remote_addr: remote_addr ) end end end |