2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# server.rb -- GenericServer Class
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: server.rb,v 1.62 2003/07/22 19:20:43 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'webrick/config'
|
|
|
|
require 'webrick/log'
|
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Server error exception
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class ServerError < StandardError; end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Base server class
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class SimpleServer
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# A SimpleServer only yields when you start it
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def SimpleServer.start
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# A generic module for daemonizing a process
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class Daemon
|
2011-05-09 20:13:58 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Performs the standard operations for daemonizing a process. Runs a
|
|
|
|
# block, if given.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def Daemon.start
|
2016-11-21 19:16:23 -05:00
|
|
|
Process.daemon
|
|
|
|
File.umask(0)
|
2003-07-23 12:51:36 -04:00
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Base TCP server class. You must subclass GenericServer and provide a #run
|
|
|
|
# method.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class GenericServer
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# The server status. One of :Stop, :Running or :Shutdown
|
|
|
|
|
|
|
|
attr_reader :status
|
|
|
|
|
|
|
|
##
|
|
|
|
# The server configuration
|
|
|
|
|
|
|
|
attr_reader :config
|
|
|
|
|
|
|
|
##
|
|
|
|
# The server logger. This is independent from the HTTP access log.
|
|
|
|
|
|
|
|
attr_reader :logger
|
|
|
|
|
|
|
|
##
|
|
|
|
# Tokens control the number of outstanding clients. The
|
|
|
|
# <code>:MaxClients</code> configuration sets this.
|
|
|
|
|
|
|
|
attr_reader :tokens
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sockets listening for connections.
|
|
|
|
|
|
|
|
attr_reader :listeners
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new generic server from +config+. The default configuration
|
|
|
|
# comes from +default+.
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
def initialize(config={}, default=Config::General)
|
|
|
|
@config = default.dup.update(config)
|
|
|
|
@status = :Stop
|
|
|
|
@config[:Logger] ||= Log::new
|
|
|
|
@logger = @config[:Logger]
|
|
|
|
|
2016-08-30 02:22:30 -04:00
|
|
|
@tokens = Thread::SizedQueue.new(@config[:MaxClients])
|
2003-07-23 12:51:36 -04:00
|
|
|
@config[:MaxClients].times{ @tokens.push(nil) }
|
|
|
|
|
|
|
|
webrickv = WEBrick::VERSION
|
|
|
|
rubyv = "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
|
|
|
@logger.info("WEBrick #{webrickv}")
|
|
|
|
@logger.info("ruby #{rubyv}")
|
|
|
|
|
2003-08-19 02:00:36 -04:00
|
|
|
@listeners = []
|
2014-11-09 22:46:22 -05:00
|
|
|
@shutdown_pipe = nil
|
2003-09-08 05:52:34 -04:00
|
|
|
unless @config[:DoNotListen]
|
|
|
|
if @config[:Listen]
|
|
|
|
warn(":Listen option is deprecated; use GenericServer#listen")
|
|
|
|
end
|
2003-08-19 02:00:36 -04:00
|
|
|
listen(@config[:BindAddress], @config[:Port])
|
2005-03-23 05:09:07 -05:00
|
|
|
if @config[:Port] == 0
|
|
|
|
@config[:Port] = @listeners[0].addr[1]
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Retrieves +key+ from the configuration
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def [](key)
|
|
|
|
@config[key]
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Adds listeners from +address+ and +port+ to the server. See
|
|
|
|
# WEBrick::Utils::create_listeners for details.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def listen(address, port)
|
2015-01-02 01:53:12 -05:00
|
|
|
@listeners += Utils::create_listeners(address, port)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2012-04-11 16:28:11 -04:00
|
|
|
##
|
|
|
|
# Starts the server and runs the +block+ for each connection. This method
|
|
|
|
# does not return until the server is stopped from a signal handler or
|
|
|
|
# another thread using #stop or #shutdown.
|
|
|
|
#
|
|
|
|
# If the block raises a subclass of StandardError the exception is logged
|
|
|
|
# and ignored. If an IOError or Errno::EBADF exception is raised the
|
|
|
|
# exception is ignored. If an Exception subclass is raised the exception
|
|
|
|
# is logged and re-raised which stops the server.
|
|
|
|
#
|
|
|
|
# To completely shut down a server call #shutdown from ensure:
|
|
|
|
#
|
|
|
|
# server = WEBrick::GenericServer.new
|
|
|
|
# # or WEBrick::HTTPServer.new
|
|
|
|
#
|
|
|
|
# begin
|
|
|
|
# server.start
|
|
|
|
# ensure
|
|
|
|
# server.shutdown
|
|
|
|
# end
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def start(&block)
|
|
|
|
raise ServerError, "already started." if @status != :Stop
|
|
|
|
server_type = @config[:ServerType] || SimpleServer
|
|
|
|
|
2015-03-10 07:05:21 -04:00
|
|
|
setup_shutdown_pipe
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
server_type.start{
|
|
|
|
@logger.info \
|
|
|
|
"#{self.class}#start: pid=#{$$} port=#{@config[:Port]}"
|
|
|
|
call_callback(:StartCallback)
|
|
|
|
|
2014-11-09 22:46:22 -05:00
|
|
|
shutdown_pipe = @shutdown_pipe
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
thgroup = ThreadGroup.new
|
|
|
|
@status = :Running
|
2012-04-11 16:28:11 -04:00
|
|
|
begin
|
|
|
|
while @status == :Running
|
|
|
|
begin
|
2015-07-13 22:20:16 -04:00
|
|
|
sp = shutdown_pipe[0]
|
2017-10-09 15:00:22 -04:00
|
|
|
if svrs = IO.select([sp, *@listeners])
|
2015-07-13 22:20:16 -04:00
|
|
|
if svrs[0].include? sp
|
|
|
|
# swallow shutdown pipe
|
|
|
|
buf = String.new
|
|
|
|
nil while String ===
|
|
|
|
sp.read_nonblock([sp.nread, 8].max, buf, exception: false)
|
2014-11-05 05:24:53 -05:00
|
|
|
break
|
2014-05-30 08:32:48 -04:00
|
|
|
end
|
2012-04-11 16:28:11 -04:00
|
|
|
svrs[0].each{|svr|
|
|
|
|
@tokens.pop # blocks while no token is there.
|
|
|
|
if sock = accept_client(svr)
|
2015-03-16 04:43:28 -04:00
|
|
|
unless config[:DoNotReverseLookup].nil?
|
|
|
|
sock.do_not_reverse_lookup = !!config[:DoNotReverseLookup]
|
|
|
|
end
|
2012-04-11 16:28:11 -04:00
|
|
|
th = start_thread(sock, &block)
|
|
|
|
th[:WEBrickThread] = true
|
|
|
|
thgroup.add(th)
|
|
|
|
else
|
|
|
|
@tokens.push(nil)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2014-05-07 21:17:07 -04:00
|
|
|
rescue Errno::EBADF, Errno::ENOTSOCK, IOError => ex
|
2012-04-11 16:28:11 -04:00
|
|
|
# if the listening socket was closed in GenericServer#shutdown,
|
|
|
|
# IO::select raise it.
|
2012-04-13 22:30:43 -04:00
|
|
|
rescue StandardError => ex
|
2012-04-13 02:21:50 -04:00
|
|
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
|
|
|
@logger.error msg
|
2012-04-13 22:30:43 -04:00
|
|
|
rescue Exception => ex
|
|
|
|
@logger.fatal ex
|
|
|
|
raise
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-11 16:28:11 -04:00
|
|
|
ensure
|
2014-11-09 22:46:22 -05:00
|
|
|
cleanup_shutdown_pipe(shutdown_pipe)
|
2014-11-09 18:03:40 -05:00
|
|
|
cleanup_listener
|
2012-04-13 02:21:50 -04:00
|
|
|
@status = :Shutdown
|
2012-04-11 16:28:11 -04:00
|
|
|
@logger.info "going to shutdown ..."
|
|
|
|
thgroup.list.each{|th| th.join if th[:WEBrickThread] }
|
|
|
|
call_callback(:StopCallback)
|
|
|
|
@logger.info "#{self.class}#start done."
|
|
|
|
@status = :Stop
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-11 16:28:11 -04:00
|
|
|
##
|
|
|
|
# Stops the server from accepting new connections.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def stop
|
|
|
|
if @status == :Running
|
2012-04-13 02:21:50 -04:00
|
|
|
@status = :Shutdown
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
2015-07-13 22:20:16 -04:00
|
|
|
|
|
|
|
alarm_shutdown_pipe {|f| f.write_nonblock("\0")}
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2012-04-11 16:28:11 -04:00
|
|
|
##
|
|
|
|
# Shuts down the server and all listening sockets. New listeners must be
|
|
|
|
# provided to restart the server.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def shutdown
|
|
|
|
stop
|
2014-05-30 08:32:48 -04:00
|
|
|
|
2016-11-23 18:14:15 -05:00
|
|
|
alarm_shutdown_pipe(&:close)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# You must subclass GenericServer and implement \#run which accepts a TCP
|
|
|
|
# client socket
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def run(sock)
|
|
|
|
@logger.fatal "run() must be provided by user."
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Accepts a TCP client socket from the TCP server socket +svr+ and returns
|
|
|
|
# the client socket.
|
|
|
|
|
2005-03-07 07:32:07 -05:00
|
|
|
def accept_client(svr)
|
2017-10-12 14:50:07 -04:00
|
|
|
case sock = svr.to_io.accept_nonblock(exception: false)
|
|
|
|
when :wait_readable
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
if svr.respond_to?(:start_immediately)
|
|
|
|
sock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
|
|
|
|
sock.sync_close = true
|
|
|
|
# we cannot do OpenSSL::SSL::SSLSocket#accept here because
|
|
|
|
# a slow client can prevent us from accepting connections
|
|
|
|
# from other clients
|
|
|
|
end
|
|
|
|
sock
|
2005-03-07 07:32:07 -05:00
|
|
|
end
|
2017-10-12 14:50:07 -04:00
|
|
|
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
|
|
|
Errno::EPROTO, Errno::EINVAL
|
|
|
|
nil
|
|
|
|
rescue StandardError => ex
|
|
|
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
|
|
|
@logger.error msg
|
|
|
|
nil
|
2005-03-07 07:32:07 -05:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Starts a server thread for the client socket +sock+ that runs the given
|
|
|
|
# +block+.
|
|
|
|
#
|
|
|
|
# Sets the socket to the <code>:WEBrickSocket</code> thread local variable
|
|
|
|
# in the thread.
|
|
|
|
#
|
|
|
|
# If any errors occur in the block they are logged and handled.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def start_thread(sock, &block)
|
|
|
|
Thread.start{
|
|
|
|
begin
|
|
|
|
Thread.current[:WEBrickSocket] = sock
|
2004-12-15 03:47:49 -05:00
|
|
|
begin
|
|
|
|
addr = sock.peeraddr
|
|
|
|
@logger.debug "accept: #{addr[3]}:#{addr[1]}"
|
|
|
|
rescue SocketError
|
|
|
|
@logger.debug "accept: <address unknown>"
|
|
|
|
raise
|
|
|
|
end
|
2017-10-12 14:50:07 -04:00
|
|
|
if sock.respond_to?(:sync_close=) && @config[:SSLStartImmediately]
|
2017-10-18 19:34:02 -04:00
|
|
|
WEBrick::Utils.timeout(@config[:RequestTimeout]) do
|
|
|
|
begin
|
2017-10-18 17:45:34 -04:00
|
|
|
sock.accept # OpenSSL::SSL::SSLSocket#accept
|
2017-10-18 19:34:02 -04:00
|
|
|
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
|
|
|
Errno::EPROTO, Errno::EINVAL
|
|
|
|
Thread.exit
|
2017-10-18 17:45:34 -04:00
|
|
|
end
|
2017-10-12 14:50:07 -04:00
|
|
|
end
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
call_callback(:AcceptCallback, sock)
|
|
|
|
block ? block.call(sock) : run(sock)
|
2003-12-03 19:12:14 -05:00
|
|
|
rescue Errno::ENOTCONN
|
|
|
|
@logger.debug "Errno::ENOTCONN raised"
|
|
|
|
rescue ServerError => ex
|
2003-07-23 12:51:36 -04:00
|
|
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
|
|
|
@logger.error msg
|
|
|
|
rescue Exception => ex
|
|
|
|
@logger.error ex
|
|
|
|
ensure
|
2005-03-07 07:32:07 -05:00
|
|
|
@tokens.push(nil)
|
2003-07-23 12:51:36 -04:00
|
|
|
Thread.current[:WEBrickSocket] = nil
|
2003-12-03 19:12:14 -05:00
|
|
|
if addr
|
|
|
|
@logger.debug "close: #{addr[3]}:#{addr[1]}"
|
|
|
|
else
|
|
|
|
@logger.debug "close: <address unknown>"
|
|
|
|
end
|
2016-11-21 18:05:41 -05:00
|
|
|
sock.close
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Calls the callback +callback_name+ from the configuration with +args+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def call_callback(callback_name, *args)
|
2016-11-22 18:11:41 -05:00
|
|
|
@config[callback_name]&.call(*args)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
2014-11-09 18:03:40 -05:00
|
|
|
|
2014-11-10 06:05:00 -05:00
|
|
|
def setup_shutdown_pipe
|
2016-11-22 18:11:41 -05:00
|
|
|
return @shutdown_pipe ||= IO.pipe
|
2014-11-10 06:05:00 -05:00
|
|
|
end
|
|
|
|
|
2014-11-09 22:46:22 -05:00
|
|
|
def cleanup_shutdown_pipe(shutdown_pipe)
|
|
|
|
@shutdown_pipe = nil
|
2016-11-22 18:11:41 -05:00
|
|
|
shutdown_pipe&.each(&:close)
|
2014-11-09 18:03:40 -05:00
|
|
|
end
|
|
|
|
|
2015-07-13 22:20:16 -04:00
|
|
|
def alarm_shutdown_pipe
|
|
|
|
_, pipe = @shutdown_pipe # another thread may modify @shutdown_pipe.
|
|
|
|
if pipe
|
|
|
|
if !pipe.closed?
|
|
|
|
begin
|
|
|
|
yield pipe
|
|
|
|
rescue IOError # closed by another thread.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-09 18:03:40 -05:00
|
|
|
def cleanup_listener
|
|
|
|
@listeners.each{|s|
|
|
|
|
if @logger.debug?
|
|
|
|
addr = s.addr
|
|
|
|
@logger.debug("close TCPSocket(#{addr[2]}, #{addr[1]})")
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
s.shutdown
|
|
|
|
rescue Errno::ENOTCONN
|
|
|
|
# when `Errno::ENOTCONN: Socket is not connected' on some platforms,
|
|
|
|
# call #close instead of #shutdown.
|
|
|
|
# (ignore @config[:ShutdownSocketWithoutClose])
|
|
|
|
s.close
|
|
|
|
else
|
|
|
|
unless @config[:ShutdownSocketWithoutClose]
|
|
|
|
s.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@listeners.clear
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
end # end of GenericServer
|
|
|
|
end
|