The request handler is the layer which connects Apache with the underlying application‘s request dispatcher (i.e. either Rails‘s Dispatcher class or Rack). The request handler‘s job is to process incoming HTTP requests using the currently loaded Ruby on Rails application. HTTP requests are forwarded to the request handler by the web server. HTTP responses generated by the RoR application are forwarded to the web server, which, in turn, sends the response back to the HTTP client.
AbstractRequestHandler is an abstract base class for easing the implementation of request handlers for Rails and Rack.
Design decisions
Some design decisions are made because we want to decrease system administrator maintenance overhead. These decisions are documented in this section.
Abstract namespace Unix sockets
AbstractRequestHandler listens on a Unix socket for incoming requests. If possible, AbstractRequestHandler will try to create a Unix socket on the _abstract namespace_, instead of on the filesystem. If the RoR application crashes (segfault), or if it gets killed by SIGKILL, or if the system loses power, then there will be no stale socket files left on the filesystem. Unfortunately, abstract namespace Unix sockets are only supported by Linux. On systems that do not support abstract namespace Unix sockets, AbstractRequestHandler will automatically fallback to using regular Unix socket files.
It is possible to force AbstractRequestHandler to use regular Unix socket files by setting the environment variable PASSENGER_NO_ABSTRACT_NAMESPACE_SOCKETS to 1.
Owner pipes
Because only the web server communicates directly with a request handler, we want the request handler to exit if the web server has also exited. This is implemented by using a so-called _owner pipe_. The writable part of the pipe will be owned by the web server. AbstractRequestHandler will continuously check whether the other side of the pipe has been closed. If so, then it knows that the web server has exited, and so the request handler will exit as well. This works even if the web server gets killed by SIGKILL.
Request format
Incoming "HTTP requests" are not true HTTP requests, i.e. their binary representation do not conform to RFC 2616. Instead, the request format is based on CGI, and is similar to that of SCGI.
The format consists of 3 parts:
- A 32-bit big-endian integer, containing the size of the transformed headers.
- The transformed HTTP headers.
- The verbatim (untransformed) HTTP request body.
HTTP headers are transformed to a format that satisfies the following grammar:
headers ::= header* header ::= name NUL value NUL name ::= notnull+ value ::= notnull+ notnull ::= "\x01" | "\x02" | "\x02" | ... | "\xFF" NUL = "\x00"
The web server transforms the HTTP request to the aforementioned format, and sends it to the request handler.
HARD_TERMINATION_SIGNAL | = | "SIGTERM" |
Signal which will cause the Rails application to exit immediately. | ||
SOFT_TERMINATION_SIGNAL | = | "SIGUSR1" |
Signal which will cause the Rails application to exit as soon as it‘s done processing a request. | ||
BACKLOG_SIZE | = | 50 |
MAX_HEADER_SIZE | = | 128 * 1024 |
PASSENGER_VERSION | = | determine_passenger_version |
PASSENGER_HEADER | = | determine_passenger_header |
[R] | socket_name |
The name of the socket on which the request handler accepts new connections. This is
either a Unix socket filename, or the name for an abstract namespace Unix
socket.
If socket_name refers to an abstract namespace Unix socket, then the name does not contain a leading null byte. See also using_abstract_namespace? |
Create a new RequestHandler with the given owner pipe. owner_pipe must be the readable part of a pipe IO object.
[ show source ]
# File lib/passenger/abstract_request_handler.rb, line 119 119: def initialize(owner_pipe) 120: if abstract_namespace_sockets_allowed? 121: @using_abstract_namespace = create_unix_socket_on_abstract_namespace 122: else 123: @using_abstract_namespace = false 124: end 125: if !@using_abstract_namespace 126: create_unix_socket_on_filesystem 127: end 128: @owner_pipe = owner_pipe 129: @previous_signal_handlers = {} 130: end
Clean up temporary stuff created by the request handler. This method should be called after the main loop has exited.
[ show source ]
# File lib/passenger/abstract_request_handler.rb, line 134 134: def cleanup 135: @socket.close rescue nil 136: @owner_pipe.close rescue nil 137: if !using_abstract_namespace? 138: File.unlink(@socket_name) rescue nil 139: end 140: end
Enter the request handler‘s main loop.
[ show source ]
# File lib/passenger/abstract_request_handler.rb, line 148 148: def main_loop 149: reset_signal_handlers 150: begin 151: done = false 152: while !done 153: client = accept_connection 154: if client.nil? 155: break 156: end 157: trap SOFT_TERMINATION_SIGNAL do 158: done = true 159: end 160: begin 161: headers, input = parse_request(client) 162: if headers 163: process_request(headers, input, client) 164: end 165: rescue IOError, SocketError, SystemCallError => e 166: print_exception("Passenger RequestHandler", e) 167: ensure 168: client.close rescue nil 169: end 170: trap SOFT_TERMINATION_SIGNAL, DEFAULT 171: end 172: rescue EOFError 173: # Exit main loop. 174: rescue Interrupt 175: # Exit main loop. 176: rescue SignalException => signal 177: if signal.message != HARD_TERMINATION_SIGNAL && 178: signal.message != SOFT_TERMINATION_SIGNAL 179: raise 180: end 181: ensure 182: revert_signal_handlers 183: end 184: end
Returns whether socket_name refers to an abstract namespace Unix socket.
[ show source ]
# File lib/passenger/abstract_request_handler.rb, line 143 143: def using_abstract_namespace? 144: return @using_abstract_namespace 145: end