Class: Raptor::Request
- Inherits:
-
Object
- Object
- Raptor::Request
- Defined in:
- lib/raptor/request.rb
Overview
Handles HTTP request processing and Rack application integration.
Request manages the HTTP parsing pipeline using Ractors and coordinates with the reactor for connection state management. It bridges between the low-level HTTP parsing and high-level Rack application interface, handling both incomplete requests (that need more data) and complete requests (ready for application processing).
Defined Under Namespace
Classes: Error, WriteError
Constant Summary collapse
- BODY_BUFFER_THRESHOLD =
256 * 1024
- FILE_CHUNK_SIZE =
64 * 1024
- READ_BUFFER_SIZE =
64 * 1024
- WRITE_TIMEOUT =
5- KEEPALIVE_READ_TIMEOUT =
0.001- MAX_KEEPALIVE_REQUESTS =
100- HTTP_SCHEME =
"http"- HTTP_10 =
"HTTP/1.0"- HTTP_11 =
"HTTP/1.1"- STATUS_LINE_CACHE_10 =
Hash.new do |h, status| reason = Rack::Utils::HTTP_STATUS_CODES[status] h[status] = "HTTP/1.0 #{status}#{reason ? " #{reason}" : ""}\r\n".freeze end
- STATUS_LINE_CACHE_11 =
Hash.new do |h, status| reason = Rack::Utils::HTTP_STATUS_CODES[status] h[status] = "HTTP/1.1 #{status}#{reason ? " #{reason}" : ""}\r\n".freeze end
- STATUS_WITH_NO_ENTITY_BODY =
Set.new([204, 304, *100..199]).freeze
- ERROR_RESPONSE_500 =
"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"- CONNECTION_CLOSE =
"close"- CONNECTION_KEEPALIVE =
"keep-alive"- TRANSFER_ENCODING_CHUNKED =
"chunked"- HTTP_CONNECTION =
"HTTP_CONNECTION"- HTTP_TRANSFER_ENCODING =
"HTTP_TRANSFER_ENCODING"- RACK_HEADER_PREFIX =
"rack."- RACK_HIJACKED =
"rack.hijacked"- RACK_HIJACK_IO =
"rack.hijack_io"- ILLEGAL_HEADER_KEY_REGEX =
/[\x00-\x20\(\)<>@,;:\\"\/\[\]\?=\{\}\x7F]/- ILLEGAL_HEADER_VALUE_REGEX =
/[\x00-\x08\x0A-\x1F]/
Class Method Summary collapse
-
.decode_chunked(buffer) ⇒ Array(String, Boolean)
Decodes a chunked transfer-encoded body buffer.
Instance Method Summary collapse
-
#eager_accept(socket, id, reactor, thread_pool, remote_addr, url_scheme) ⇒ void
Eagerly reads and parses the first request on a freshly accepted connection on the server thread, dispatching directly to the thread pool when complete.
-
#handle_parsed_request(parsed_request, reactor, thread_pool) ⇒ void
Handles a parsed HTTP request by either continuing parsing or dispatching to the Rack app.
-
#http_parser_worker ⇒ Proc
Returns a Proc for HTTP parsing work in Ractor context.
-
#initialize(app, server_port) ⇒ void
constructor
Creates a new Request handler.
Constructor Details
#initialize(app, server_port) ⇒ void
Creates a new Request handler.
101 102 103 104 |
# File 'lib/raptor/request.rb', line 101 def initialize(app, server_port) @app = app @server_port = server_port end |
Class Method Details
.decode_chunked(buffer) ⇒ Array(String, Boolean)
Decodes a chunked transfer-encoded body buffer.
Returns the decoded bytes and a flag indicating whether the terminating zero-length chunk was found. The decoder stops at the first unparseable boundary (incomplete CRLF) or zero-length chunk.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/raptor/request.rb', line 72 def self.decode_chunked(buffer) decoded = String.new offset = 0 while offset < buffer.bytesize crlf = buffer.index("\r\n", offset) return [decoded, false] unless crlf chunk_size = buffer.byteslice(offset, crlf - offset).to_i(16) return [decoded, true] if chunk_size == 0 offset = crlf + 2 decoded << buffer.byteslice(offset, chunk_size) offset += chunk_size + 2 end [decoded, false] end |
Instance Method Details
#eager_accept(socket, id, reactor, thread_pool, remote_addr, url_scheme) ⇒ void
This method returns an undefined value.
Eagerly reads and parses the first request on a freshly accepted connection on the server thread, dispatching directly to the thread pool when complete. Falls back to the reactor when more data is needed.
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/request.rb', line 119 def eager_accept(socket, id, reactor, thread_pool, remote_addr, url_scheme) data = begin socket.read_nonblock(READ_BUFFER_SIZE) rescue IO::WaitReadable reactor.add( id: id, socket: socket, remote_addr: remote_addr, url_scheme: url_scheme ) return rescue EOFError, IOError socket.close rescue nil return end buffer = String.new buffer << data while socket.respond_to?(:pending) && socket.pending > 0 buffer << socket.read_nonblock(socket.pending) end parser = HttpParser.new env = {} nread = parser.execute(env, buffer, 0) parse_data = { parse_count: 1, content_length: parser.content_length } body = nil if !parser.finished? fallback_to_reactor(socket, id, buffer, env, parse_data, reactor, 0, remote_addr, url_scheme, persisted: false) return elsif parser.has_body? body = buffer.byteslice(nread..-1) || "" if env[HTTP_TRANSFER_ENCODING]&.include?(TRANSFER_ENCODING_CHUNKED) body, chunked_complete = Request.decode_chunked(body) if chunked_complete env.delete(HTTP_TRANSFER_ENCODING) else fallback_to_reactor(socket, id, buffer, env, parse_data, reactor, 0, remote_addr, url_scheme, persisted: false) return end elsif parser.content_length > body.bytesize fallback_to_reactor(socket, id, buffer, env, parse_data, reactor, 0, remote_addr, url_scheme, persisted: false) return end end thread_pool << proc do process_client(socket, id, env, parse_data, body, reactor, thread_pool, 1, remote_addr, url_scheme) end end |
#handle_parsed_request(parsed_request, reactor, thread_pool) ⇒ void
This method returns an undefined value.
Handles a parsed HTTP request by either continuing parsing or dispatching to the Rack app.
For incomplete requests, updates reactor state and re-registers for more I/O. For complete requests, removes from reactor, builds Rack env, and dispatches to thread pool.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/raptor/request.rb', line 235 def handle_parsed_request(parsed_request, reactor, thread_pool) unless parsed_request[:complete] reactor.update_state(parsed_request) else socket = reactor.remove(parsed_request[:id]) request_count = (parsed_request[:request_count] || 0) + 1 remote_addr = parsed_request[:remote_addr] || "127.0.0.1" url_scheme = parsed_request[:url_scheme] || HTTP_SCHEME thread_pool << proc do process_client( socket, parsed_request[:id], parsed_request[:env].dup, parsed_request[:parse_data], parsed_request[:body], reactor, thread_pool, request_count, remote_addr, url_scheme ) end end end |
#http_parser_worker ⇒ Proc
Returns a Proc for HTTP parsing work in Ractor context.
The returned Proc processes raw socket data through the appropriate HTTP parser and returns either a complete request state (ready for app processing) or incomplete request state (needs more data).
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 |
# File 'lib/raptor/request.rb', line 182 def http_parser_worker proc do |data| next Raptor::Http2.process_frames(data) if data[:protocol] == :http2 parser = Raptor::HttpParser.new env = {} nread = parser.execute(env, data[:buffer], 0) parse_data = if data[:parse_data] data[:parse_data].dup else { parse_count: 0, content_length: parser.content_length } end parse_data[:parse_count] += 1 = if parser.finished? if parser.has_body? body_buffer = data[:buffer].byteslice(nread..-1) || "" if env[HTTP_TRANSFER_ENCODING]&.include?(TRANSFER_ENCODING_CHUNKED) decoded_body, chunked_complete = Raptor::Request.decode_chunked(body_buffer) if chunked_complete env.delete(HTTP_TRANSFER_ENCODING) data.merge(env: env, body: decoded_body, parse_data: parse_data, complete: true) else data.merge(env: env, parse_data: parse_data) end elsif parser.content_length > body_buffer.bytesize data.merge(env: env, parse_data: parse_data) else data.merge(env: env, body: body_buffer, parse_data: parse_data, complete: true) end else data.merge(env: env, body: nil, parse_data: parse_data, complete: true) end else data.merge(env: env, parse_data: parse_data) end Ractor.make_shareable() end end |