# File lib/picnic/postambles.rb, line 28
    def webrick
      require 'webrick/httpserver'
      require 'webrick/https'
      require 'camping/webrick'
      
      # TODO: verify the certificate's validity
      # example of how to do this is here: http://pablotron.org/download/ruri-20050331.rb
      
      cert_path = Picnic::Conf.ssl_cert
      key_path = Picnic::Conf.ssl_key || Picnic::Conf.ssl_cert
        # look for the key in the ssl_cert if no ssl_key is specified
            
      begin
        s = WEBrick::HTTPServer.new(
          :BindAddress => Picnic::Conf.bind_address || "0.0.0.0",
          :Port => Picnic::Conf.port
        )
      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
    
      # This lets Ctrl+C shut down your server
      trap(:INT) do
        s.shutdown
      end
      trap(:TERM) do
        s.shutdown
      end
            
      if $DAEMONIZE
        puts "\n** #{self} will run at http://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path} 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 http://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path} and logging to #{Picnic::Conf.log[:file].inspect}\n\n"
        self.prestart if self.respond_to? :prestart
        s.start
      end
    end