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 'thread'
|
|
|
|
require 'socket'
|
|
|
|
require 'webrick/config'
|
|
|
|
require 'webrick/log'
|
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
|
|
|
|
class ServerError < StandardError; end
|
|
|
|
|
|
|
|
class SimpleServer
|
|
|
|
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
|
|
|
|
exit!(0) if fork
|
|
|
|
Process::setsid
|
|
|
|
exit!(0) if fork
|
|
|
|
Dir::chdir("/")
|
|
|
|
File::umask(0)
|
2005-01-07 06:05:22 -05:00
|
|
|
STDIN.reopen("/dev/null")
|
|
|
|
STDOUT.reopen("/dev/null", "w")
|
|
|
|
STDERR.reopen("/dev/null", "w")
|
2003-07-23 12:51:36 -04:00
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GenericServer
|
|
|
|
attr_reader :status, :config, :logger, :tokens, :listeners
|
|
|
|
|
|
|
|
def initialize(config={}, default=Config::General)
|
|
|
|
@config = default.dup.update(config)
|
|
|
|
@status = :Stop
|
|
|
|
@config[:Logger] ||= Log::new
|
|
|
|
@logger = @config[:Logger]
|
|
|
|
|
|
|
|
@tokens = SizedQueue.new(@config[:MaxClients])
|
|
|
|
@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 = []
|
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
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
@config[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def listen(address, port)
|
2003-08-19 02:00:36 -04:00
|
|
|
@listeners += Utils::create_listeners(address, port, @logger)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start(&block)
|
|
|
|
raise ServerError, "already started." if @status != :Stop
|
|
|
|
server_type = @config[:ServerType] || SimpleServer
|
|
|
|
|
|
|
|
server_type.start{
|
|
|
|
@logger.info \
|
|
|
|
"#{self.class}#start: pid=#{$$} port=#{@config[:Port]}"
|
|
|
|
call_callback(:StartCallback)
|
|
|
|
|
|
|
|
thgroup = ThreadGroup.new
|
|
|
|
@status = :Running
|
|
|
|
while @status == :Running
|
|
|
|
begin
|
|
|
|
if svrs = IO.select(@listeners, nil, nil, 2.0)
|
|
|
|
svrs[0].each{|svr|
|
|
|
|
@tokens.pop # blocks while no token is there.
|
2005-03-07 07:32:07 -05:00
|
|
|
if sock = accept_client(svr)
|
2008-01-08 23:46:55 -05:00
|
|
|
sock.do_not_reverse_lookup = config[:DoNotReverseLookup]
|
2005-03-07 07:32:07 -05:00
|
|
|
th = start_thread(sock, &block)
|
|
|
|
th[:WEBrickThread] = true
|
|
|
|
thgroup.add(th)
|
|
|
|
else
|
|
|
|
@tokens.push(nil)
|
2004-03-11 17:36:11 -05:00
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
}
|
|
|
|
end
|
2003-10-23 07:29:29 -04:00
|
|
|
rescue Errno::EBADF, IOError => ex
|
2003-08-19 02:00:36 -04:00
|
|
|
# if the listening socket was closed in GenericServer#shutdown,
|
|
|
|
# IO::select raise it.
|
2003-12-03 19:12:14 -05:00
|
|
|
rescue Exception => ex
|
2003-07-23 12:51:36 -04:00
|
|
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
|
|
|
@logger.error msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
def stop
|
|
|
|
if @status == :Running
|
|
|
|
@status = :Shutdown
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
stop
|
|
|
|
@listeners.each{|s|
|
|
|
|
if @logger.debug?
|
|
|
|
addr = s.addr
|
|
|
|
@logger.debug("close TCPSocket(#{addr[2]}, #{addr[1]})")
|
|
|
|
end
|
2008-08-22 07:12:06 -04:00
|
|
|
begin
|
|
|
|
s.shutdown
|
|
|
|
rescue Errno::ENOTCONN
|
|
|
|
# when `Errno::ENOTCONN: Socket is not connected' on some platforms,
|
|
|
|
# call #close instead of #shutdown.
|
|
|
|
# (ignore @config[:ShutdownSocketWithoutClose])
|
2007-12-18 09:43:03 -05:00
|
|
|
s.close
|
2008-08-22 07:12:06 -04:00
|
|
|
else
|
|
|
|
unless @config[:ShutdownSocketWithoutClose]
|
|
|
|
s.close
|
|
|
|
end
|
2007-12-18 09:43:03 -05:00
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
}
|
|
|
|
@listeners.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(sock)
|
|
|
|
@logger.fatal "run() must be provided by user."
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2005-03-07 07:32:07 -05:00
|
|
|
def accept_client(svr)
|
|
|
|
sock = nil
|
|
|
|
begin
|
|
|
|
sock = svr.accept
|
|
|
|
sock.sync = true
|
2009-03-05 22:56:38 -05:00
|
|
|
Utils::set_non_blocking(sock)
|
2005-03-07 07:32:07 -05:00
|
|
|
Utils::set_close_on_exec(sock)
|
2007-12-18 09:43:03 -05:00
|
|
|
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
|
|
|
Errno::EPROTO, Errno::EINVAL => ex
|
2005-03-07 07:32:07 -05:00
|
|
|
rescue Exception => ex
|
|
|
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
|
|
|
@logger.error msg
|
|
|
|
end
|
|
|
|
return sock
|
|
|
|
end
|
|
|
|
|
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
|
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
|
2012-04-01 14:36:46 -04:00
|
|
|
sock.close unless sock.closed?
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_callback(callback_name, *args)
|
|
|
|
if cb = @config[callback_name]
|
|
|
|
cb.call(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # end of GenericServer
|
|
|
|
end
|