class PhusionPassenger::RequestHandler::ThreadHandler

This class encapsulates the logic of a single RequestHandler thread.

Constants

CONTENT_LENGTH
GC_SUPPORTS_CLEAR_STATS
GC_SUPPORTS_TIME
GET
MAX_HEADER_SIZE
OBJECT_SPACE_SUPPORTS_ALLOCATED_OBJECTS
OBJECT_SPACE_SUPPORTS_COUNT_OBJECTS
OBJECT_SPACE_SUPPORTS_LIVE_OBJECTS
OOBW
PASSENGER_CONNECT_PASSWORD
PING
REQUEST_METHOD
TRANSFER_ENCODING

Attributes

interruptable[R]
iteration[R]
stats_mutex[R]
thread[R]

Public Class Methods

new(request_handler, options = {}) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 63
def initialize(request_handler, options = {})
  @request_handler   = request_handler
  @server_socket     = Utils.require_option(options, :server_socket)
  @socket_name       = Utils.require_option(options, :socket_name)
  @protocol          = Utils.require_option(options, :protocol)
  @app_group_name    = Utils.require_option(options, :app_group_name)
  Utils.install_options_as_ivars(self, options,
    :app,
    :union_station_core,
    :connect_password,
    :keepalive_enabled
  )

  @stats_mutex   = Mutex.new
  @interruptable = false
  @iteration     = 0

  if @protocol == :session
    metaclass = class << self; self; end
    metaclass.class_eval do
      alias parse_request parse_session_request
    end
  elsif @protocol == :http
    metaclass = class << self; self; end
    metaclass.class_eval do
      alias parse_request parse_http_request
    end
  else
    raise ArgumentError, "Unknown protocol specified"
  end
end

Public Instance Methods

install() click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 95
def install
  @thread = Thread.current
  Thread.current[:passenger_thread_handler] = self
  PhusionPassenger.call_event(:starting_request_handler_thread)
end
main_loop(finish_callback) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 101
def main_loop(finish_callback)
  socket_wrapper = Utils::UnseekableSocket.new
  channel        = MessageChannel.new
  buffer         = ''
  buffer.force_encoding('binary') if buffer.respond_to?(:force_encoding)

  @union_station_hooks_defined = defined?(UnionStationHooks)

  begin
    finish_callback.call
    while true
      hijacked = accept_and_process_next_request(socket_wrapper, channel, buffer)
      socket_wrapper = Utils::UnseekableSocket.new if hijacked
    end
  rescue Interrupted
    # Do nothing.
  end
  debug("Thread handler main loop exited normally")
ensure
  @stats_mutex.synchronize { @interruptable = true }
end

Private Instance Methods

accept_and_process_next_request(socket_wrapper, channel, buffer) click to toggle source

Returns true if the socket has been hijacked, false otherwise.

# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 125
def accept_and_process_next_request(socket_wrapper, channel, buffer)
  @stats_mutex.synchronize do
    @interruptable = true
  end
  if @last_connection
    connection = @last_connection
    channel.io = connection
    @last_connection = nil
    headers = parse_request(connection, channel, buffer)
  else
    connection = socket_wrapper.wrap(@server_socket.accept)
  end
  @stats_mutex.synchronize do
    @interruptable = false
    @iteration    += 1
  end
  trace(3, "Accepted new request on socket #{@socket_name}")
  if !headers
    # New socket accepted, instead of keeping-alive an old one
    channel.io = connection
    headers = parse_request(connection, channel, buffer)
  end
  if headers
    prepare_request(connection, headers)
    begin
      if headers[REQUEST_METHOD] == GET
        process_request(headers, connection, socket_wrapper, @protocol == :http)
      elsif headers[REQUEST_METHOD] == PING
        process_ping(headers, connection)
        false
      elsif headers[REQUEST_METHOD] == OOBW
        process_oobw(headers, connection)
        false
      else
        process_request(headers, connection, socket_wrapper, @protocol == :http)
      end
    rescue Exception
      has_error = true
      raise
    ensure
      if headers[RACK_HIJACK_IO]
        socket_wrapper = nil
        connection = nil
        channel = nil
      end
      finalize_request(connection, headers, has_error)
      trace(3, "Request done.")
    end
  else
    trace(2, "No headers parsed; disconnecting client.")
    false
  end
rescue Interrupted
  raise
rescue => e
  if socket_wrapper && socket_wrapper.source_of_exception?(e)
    # EPIPE is harmless, it just means that the client closed the connection.
    if !e.is_a?(Errno::EPIPE)
      print_exception("Passenger RequestHandler's client socket", e)
    end
  else
    if headers
      log_exception_to_union_station(headers, e)
    end
    # should_reraise_error? returns true except in unit tests,
    # so we normally stop the request handler upon encountering
    # non-EPIPE errors. We do this because process_request is already
    # supposed to catch application-level exceptions. If an
    # exception happened outside of process_request, then it's
    # probably serious enough to warrant stopping the request handler.
    raise e if should_reraise_error?(e)
  end
  # Here we do not know whether the connection was hijacked, but
  # to be on the safe side (and have a new socket_wrapper created)
  # let's say that it is.
  true
ensure
  # Close connection if keep-alive not possible
  if connection && !connection.closed? && !@last_connection
    # The 'close_write' here prevents forked child
    # processes from unintentionally keeping the
    # connection open.
    begin
      connection.close_write
    rescue SystemCallError, IOError
    end
    begin
      connection.close
    rescue SystemCallError
    end
  end
end
finalize_request(connection, headers, has_error) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 330
def finalize_request(connection, headers, has_error)
  if connection
    connection.stop_simulating_eof!
  end

  if @union_station_hooks_defined
    UnionStationHooks.end_rack_request(headers, has_error)
    @ush_reporter = nil
  end

  if !has_error && @keepalive_performed && connection
    trace(3, "Keep-aliving connection.")
    @last_connection = connection
  end

  #################
end
log_exception_to_union_station(env, exception) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 348
def log_exception_to_union_station(env, exception)
  reporter = env['union_station_hooks']
  if reporter
    reporter.log_exception(exception)
  end
end
parse_http_request(connection, channel, buffer) click to toggle source

Like #parse_session_request, but parses an HTTP request. This is a very minimalistic HTTP parser and is not intended to be complete, fast or secure, since the HTTP server socket is intended to be used for debugging purposes only.

# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 240
def parse_http_request(connection, channel, buffer)
  headers = {}

  data = ""
  while data !~ /\r\n\r\n/ && data.size < MAX_HEADER_SIZE
    data << connection.readpartial(16 * 1024)
  end
  if data.size >= MAX_HEADER_SIZE
    warn("*** Passenger RequestHandler warning: " <<
      "HTTP header size exceeded maximum.")
    return
  end

  data.gsub!(/\r\n\r\n.*/, '')
  data.split("\r\n").each_with_index do |line, i|
    if i == 0
      # GET / HTTP/1.1
      line =~ /^([A-Za-z]+) (.+?) (HTTP\/\d\.\d)$/
      request_method = $1
      request_uri    = $2
      protocol       = $3
      if request_method.nil?
        warn("*** Passenger RequestHandler warning: " <<
          "Invalid HTTP request.")
        return
      end
      path_info, query_string    = request_uri.split("?", 2)
      headers[REQUEST_METHOD]    = request_method
      headers["REQUEST_URI"]     = request_uri
      headers["QUERY_STRING"]    = query_string || ""
      headers["SCRIPT_NAME"]     = ""
      headers["PATH_INFO"]       = path_info
      headers["SERVER_NAME"]     = "127.0.0.1"
      headers["SERVER_PORT"]     = connection.addr[1].to_s
      headers["SERVER_PROTOCOL"] = protocol
    else
      header, value = line.split(/\s*:\s*/, 2)
      header.upcase!            # "Foo-Bar" => "FOO-BAR"
      header.gsub!("-", "_")    #           => "FOO_BAR"
      if header == CONTENT_LENGTH || header == "CONTENT_TYPE"
        headers[header] = value
      else
        headers["HTTP_#{header}"] = value
      end
    end
  end

  if @connect_password && headers["HTTP_X_PASSENGER_CONNECT_PASSWORD"] != @connect_password
    warn "*** Passenger RequestHandler warning: " <<
      "someone tried to connect with an invalid connect password."
    return
  else
    return headers
  end
rescue EOFError
  return
end
parse_session_request(connection, channel, buffer) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 218
def parse_session_request(connection, channel, buffer)
  headers_data = channel.read_scalar(buffer, MAX_HEADER_SIZE)
  if headers_data.nil?
    return
  end
  headers = Utils::NativeSupportUtils.split_by_null_into_hash(headers_data)
  if @connect_password && headers[PASSENGER_CONNECT_PASSWORD] != @connect_password
    warn "*** Passenger RequestHandler warning: " <<
      "someone tried to connect with an invalid connect password."
    return
  else
    return headers
  end
rescue SecurityError => e
  warn("*** Passenger RequestHandler warning: " <<
    "HTTP header size exceeded maximum.")
  return
end
prepare_request(connection, headers) click to toggle source

def process_request(env, connection, socket_wrapper, full_http_response)

raise NotImplementedError, "Override with your own implementation!"

end

# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 311
def prepare_request(connection, headers)
  transfer_encoding = headers[TRANSFER_ENCODING]
  content_length = headers[CONTENT_LENGTH]
  @can_keepalive = @keepalive_enabled &&
    !transfer_encoding &&
    !content_length
  @keepalive_performed = false

  if !transfer_encoding && !content_length
    connection.simulate_eof!
  end

  if @union_station_hooks_defined
    @ush_reporter = UnionStationHooks.begin_rack_request(headers)
  end

  #################
end
process_oobw(env, connection) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 302
def process_oobw(env, connection)
  PhusionPassenger.call_event(:oob_work)
  connection.write("oobw done")
end
process_ping(env, connection) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 298
def process_ping(env, connection)
  connection.write("pong")
end
should_reraise_error?(e) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 355
def should_reraise_error?(e)
  # Stubable by unit tests.
  return true
end
should_swallow_app_error?(e, socket_wrapper) click to toggle source
# File src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb, line 360
def should_swallow_app_error?(e, socket_wrapper)
  return socket_wrapper && socket_wrapper.source_of_exception?(e) && e.is_a?(Errno::EPIPE)
end