def webrick
require 'webrick/httpserver'
require 'webrick/https'
require 'camping/webrick'
cert_path = Picnic::Conf.ssl_cert
key_path = Picnic::Conf.ssl_key || Picnic::Conf.ssl_cert
webrick_options = {
:BindAddress => Picnic::Conf.bind_address || "0.0.0.0",
:Port => Picnic::Conf.port
}
unless cert_path.nil? && key_path.nil?
raise "The specified certificate file #{cert_path.inspect} does not exist. " +
" Your 'ssl_cert' configuration setting must be a path to a valid " +
" ssl certificate." unless
File.exists? cert_path
raise "The specified key file #{key_path.inspect} does not exist. " +
" Your 'ssl_key' configuration setting must be a path to a valid " +
" ssl private key." unless
File.exists? key_path
require 'openssl'
cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
key = OpenSSL::PKey::RSA.new(File.read(key_path))
webrick_options[:SSLEnable] = true
webrick_options[:SSLVerifyClient] = ::OpenSSL::SSL::VERIFY_NONE
webrick_options[:SSLCertificate] = cert
webrick_options[:SSLPrivateKey] = key
end
begin
s = WEBrick::HTTPServer.new(webrick_options)
rescue Errno::EACCES
puts "\nThe server could not launch. Are you running on a privileged port? (e.g. port 443) If so, you must run the server as root."
exit 2
end
self.create
s.mount "#{Picnic::Conf.uri_path}", WEBrick::CampingHandler, self
public_dirs = Picnic::Conf.public_dirs || Picnic::Conf.public_dir
if public_dirs
public_dirs = [public_dirs] unless public_dirs.kind_of? Array
public_dirs.each do |d|
dir = d[:dir]
path = "#{Picnic::Conf.uri_path}/#{d[:path]}".gsub(/\/\/+/,'/')
$LOG.debug("Mounting public directory #{dir.inspect} to path #{path.inspect}.")
s.mount(path, WEBrick::HTTPServlet::FileHandler, dir)
end
end
trap(:INT) do
s.shutdown
end
trap(:TERM) do
s.shutdown
end
server_url = "http#{webrick_options[:SSLEnable] ? 's' : ''}://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path}"
if $DAEMONIZE
puts "\n** #{self} will run at #{server_url} and log to #{Picnic::Conf.log[:file].inspect}. "
puts "** Check the log file for further messages!\n\n"
logdev = $LOG.instance_variable_get(:@logdev).instance_variable_get(:@filename)
if logdev == 'STDOUT' || logdev == nil
puts "\n!!! Warning !!!\nLogging to the console (STDOUT) is not possible once the server daemonizes. "+
"You should change the logger configuration to point to a real file."
end
WEBrick::Daemon.start do
begin
write_pid_file if $PID_FILE
$LOG.info "Starting #{self} as a WEBrick daemon with process id #{Process.pid}."
self.prestart if self.respond_to? :prestart
s.start
$LOG.info "Stopping #{self} WEBrick daemon with process id #{Process.pid}."
clear_pid_file
rescue => e
$LOG.error e
raise e
end
end
else
puts "\n** #{self} is running at #{server_url} and logging to #{Picnic::Conf.log[:file].inspect}\n\n"
self.prestart if self.respond_to? :prestart
s.start
end
end