mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/webrick/accesslog.rb: Improved WEBrick documentation.
* lib/webrick/cgi.rb: ditto. * lib/webrick/config.rb: ditto. * lib/webrick/cookie.rb: ditto. * lib/webrick/httpauth/authenticator.rb: ditto. * lib/webrick/httpauth/basicauth.rb: ditto. * lib/webrick/httpauth/digestauth.rb: ditto. * lib/webrick/httpproxy.rb: ditto. * lib/webrick/httprequest.rb: ditto. * lib/webrick/httpresponse.rb: ditto. * lib/webrick/https.rb: ditto. * lib/webrick/httpserver.rb: ditto. * lib/webrick/httpservlet/cgihandler.rb: ditto. * lib/webrick/httpservlet/filehandler.rb: ditto. * lib/webrick/httpservlet/prochandler.rb: ditto. * lib/webrick/httputils.rb: ditto. * lib/webrick/httpversion.rb: ditto. * lib/webrick/log.rb: ditto. * lib/webrick/server.rb: ditto. * lib/webrick/ssl.rb: ditto. * lib/webrick/utils.rb: ditto. * lib/webrick/version.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38945 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
89232d1dd9
commit
28afe277a8
23 changed files with 839 additions and 85 deletions
|
@ -15,9 +15,19 @@ require 'webrick/log'
|
|||
|
||||
module WEBrick
|
||||
|
||||
##
|
||||
# Server error exception
|
||||
|
||||
class ServerError < StandardError; end
|
||||
|
||||
##
|
||||
# Base server class
|
||||
|
||||
class SimpleServer
|
||||
|
||||
##
|
||||
# A SimpleServer only yields when you start it
|
||||
|
||||
def SimpleServer.start
|
||||
yield
|
||||
end
|
||||
|
@ -45,8 +55,41 @@ module WEBrick
|
|||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Base TCP server class. You must subclass GenericServer and provide a #run
|
||||
# method.
|
||||
|
||||
class GenericServer
|
||||
attr_reader :status, :config, :logger, :tokens, :listeners
|
||||
|
||||
##
|
||||
# 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+.
|
||||
|
||||
def initialize(config={}, default=Config::General)
|
||||
@config = default.dup.update(config)
|
||||
|
@ -74,10 +117,17 @@ module WEBrick
|
|||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Retrieves +key+ from the configuration
|
||||
|
||||
def [](key)
|
||||
@config[key]
|
||||
end
|
||||
|
||||
##
|
||||
# Adds listeners from +address+ and +port+ to the server. See
|
||||
# WEBrick::Utils::create_listeners for details.
|
||||
|
||||
def listen(address, port)
|
||||
@listeners += Utils::create_listeners(address, port, @logger)
|
||||
end
|
||||
|
@ -189,12 +239,22 @@ module WEBrick
|
|||
@listeners.clear
|
||||
end
|
||||
|
||||
##
|
||||
# You must subclass GenericServer and implement \#run which accepts a TCP
|
||||
# client socket
|
||||
|
||||
def run(sock)
|
||||
@logger.fatal "run() must be provided by user."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# :stopdoc:
|
||||
|
||||
##
|
||||
# Accepts a TCP client socket from the TCP server socket +svr+ and returns
|
||||
# the client socket.
|
||||
|
||||
def accept_client(svr)
|
||||
sock = nil
|
||||
begin
|
||||
|
@ -211,6 +271,15 @@ module WEBrick
|
|||
return sock
|
||||
end
|
||||
|
||||
##
|
||||
# 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.
|
||||
|
||||
def start_thread(sock, &block)
|
||||
Thread.start{
|
||||
begin
|
||||
|
@ -244,6 +313,9 @@ module WEBrick
|
|||
}
|
||||
end
|
||||
|
||||
##
|
||||
# Calls the callback +callback_name+ from the configuration with +args+
|
||||
|
||||
def call_callback(callback_name, *args)
|
||||
if cb = @config[callback_name]
|
||||
cb.call(*args)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue