summaryrefslogtreecommitdiff
path: root/lib/webrick/server.rb
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-26 01:12:54 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-26 01:12:54 +0000
commit28afe277a8e543da0e6353bdacbcad0b69739e06 (patch)
tree1591c370f08ab4db6c888eea99f2936262e137ca /lib/webrick/server.rb
parent89232d1dd97251b6fc626d4338c49e9e8c4f6535 (diff)
* 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
Diffstat (limited to 'lib/webrick/server.rb')
-rw-r--r--lib/webrick/server.rb74
1 files changed, 73 insertions, 1 deletions
diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb
index 2eabf5d..3f5371b 100644
--- a/lib/webrick/server.rb
+++ b/lib/webrick/server.rb
@@ -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)