Methods
- #
- A
- C
- L
- N
- R
- S
Attributes
[R] | config | |
[R] | listeners | |
[R] | logger | |
[R] | status | |
[R] | tokens |
Class Public methods
new(config={}, default=Config::General)
Link
# File ../ruby/lib/webrick/server.rb, line 51 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}") @listeners = [] unless @config[:DoNotListen] if @config[:Listen] warn(":Listen option is deprecated; use GenericServer#listen") end listen(@config[:BindAddress], @config[:Port]) if @config[:Port] == 0 @config[:Port] = @listeners[0].addr[1] end end end
Instance Public methods
[](key)
Link
listen(address, port)
Link
run(sock)
Link
setup_ssl_context(config)
Link
# File ../ruby/lib/webrick/ssl.rb, line 103 def setup_ssl_context(config) unless config[:SSLCertificate] cn = config[:SSLCertName] comment = config[:SSLCertComment] cert, key = Utils::create_self_signed_cert(1024, cn, comment) config[:SSLCertificate] = cert config[:SSLPrivateKey] = key end ctx = OpenSSL::SSL::SSLContext.new ctx.key = config[:SSLPrivateKey] ctx.cert = config[:SSLCertificate] ctx.client_ca = config[:SSLClientCA] ctx.extra_chain_cert = config[:SSLExtraChainCert] ctx.ca_file = config[:SSLCACertificateFile] ctx.ca_path = config[:SSLCACertificatePath] ctx.cert_store = config[:SSLCertificateStore] ctx.verify_mode = config[:SSLVerifyClient] ctx.verify_depth = config[:SSLVerifyDepth] ctx.verify_callback = config[:SSLVerifyCallback] ctx.timeout = config[:SSLTimeout] ctx.options = config[:SSLOptions] ctx end
shutdown()
Link
# File ../ruby/lib/webrick/server.rb, line 134 def shutdown stop @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
ssl_context()
Link
start(&block)
Link
# File ../ruby/lib/webrick/server.rb, line 85 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. if sock = accept_client(svr) sock.do_not_reverse_lookup = config[:DoNotReverseLookup] th = start_thread(sock, &block) th[:WEBrickThread] = true thgroup.add(th) else @tokens.push(nil) end } end rescue Errno::EBADF, IOError => ex # if the listening socket was closed in GenericServer#shutdown, # IO::select raise it. rescue Exception => ex 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
stop()
Link
Instance Private methods
accept_client(svr)
Link
# File ../ruby/lib/webrick/server.rb, line 163 def accept_client(svr) sock = nil begin sock = svr.accept sock.sync = true Utils::set_non_blocking(sock) Utils::set_close_on_exec(sock) rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINVAL => ex rescue Exception => ex msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}" @logger.error msg end return sock end
call_callback(callback_name, *args)
Link
start_thread(sock, &block)
Link
# File ../ruby/lib/webrick/server.rb, line 179 def start_thread(sock, &block) Thread.start{ begin Thread.current[:WEBrickSocket] = sock begin addr = sock.peeraddr @logger.debug "accept: #{addr[3]}:#{addr[1]}" rescue SocketError @logger.debug "accept: <address unknown>" raise end call_callback(:AcceptCallback, sock) block ? block.call(sock) : run(sock) rescue Errno::ENOTCONN @logger.debug "Errno::ENOTCONN raised" rescue ServerError => ex msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}" @logger.error msg rescue Exception => ex @logger.error ex ensure @tokens.push(nil) Thread.current[:WEBrickSocket] = nil if addr @logger.debug "close: #{addr[3]}:#{addr[1]}" else @logger.debug "close: <address unknown>" end sock.close end } end