class SimpleHKP::Server

Public Class Methods

new(options = {}) click to toggle source
# File lib/simpleHKP/server.rb, line 113
    def initialize(options = {})
      #
      # setup the default options
      #
      defaultOptions = {
        'debug'        => false,
        'title'        => "SimpleHKP @ #{Socket.gethostname}",
        'simpleHKPdir' => 'simpleHKP',
        'keyDir'       => 'keys',
        'idDir'        => 'identities',
#        'idIndex'      => 'identities/idIndex.yaml',
        'mediaDir'     => 'media',
        'htmlDir'      => 'html',
        'mimeMap'      => {
          'css'  => 'text/css',
          'html' => 'text/html',
          'js'   => 'text/javascript'
        }
      }
      #
      # merge the options mimeMap into the default options mimeMap
      #
      defaultOptions['mimeMap'].merge!(delete(options['mimeMap'])) if
        options.has_key?('mimeMap')
      #
      # merge in the rest of the options into the default options
      #
      @options  = defaultOptions.merge(options)
      #
      # setup the required variables
      #
      @debug    = @options['debug']
      @title    = @options['title']
      @baseDir  = @options['simpleHKPdir']
      @keyDir   = @baseDir+'/'+@options['keyDir']
      @idDir    = @baseDir+'/'+@options['idDir']
#      @idIndex  = @baseDir+'/'+@options['idIndex']
      @mediaDir = @baseDir+'/'+@options['mediaDir']
      @htmlDir  = @baseDir+'/'+@options['htmlDir']
      @mimeMap  = @options['mimeMap']
      if @debug then
        puts "SimpleHKP options:"
        pp @options
      end
      #
      # ensure the required directories all exist
      #
      FileUtils.mkdir_p(@keyDir)
      FileUtils.chmod(0700, @keyDir)
      FileUtils.mkdir_p(@idDir)
      FileUtils.chmod(0700, @idDir)
      FileUtils.mkdir_p(@mediaDir)
      FileUtils.mkdir_p(@htmlDir)
      #
      # load the existing keys and the html partials
      loadKeys
      loadIdentities
      loadHtml
    end

Public Instance Methods

call(env) click to toggle source
# File lib/simpleHKP/server.rb, line 264
def call(env)
  #
  # initialize the response parts
  #
  @statusCode = 200
  @header     = {"Content-Type" => "text/html; charset=utf-8"}
  @body       = Array.new
  #
  # decode the request
  #
  begin
    puts ppEnv(env) if @debug
    case env['REQUEST_METHOD']
    when 'POST'
      case env['REQUEST_PATH'] 
      when /^\/pks\/add$/i
        storeKey(env)
      when /^\/pis\/add$/i
        storeIdentity(env)
      else 
        replyNotImplemented
      end
    when 'GET'
      case env['REQUEST_PATH'] 
      when /^\/pks\/add$/i
        replyKeyUploadForm
      when /^\/pis\/add$/i
        replyIdentityUploadForm
      when /^\/pks\/lookup$/i
        queryString = decodeQueryString(env)
        if queryString.has_key?('op') then
          case queryString['op']
          when /get/i
            lookUpKey(queryString)
          when /index/i
            indexKeys(queryString)
          else
            replyKeyLookupForm
          end
        else
          replyBadRequest("No op field in queryString")
        end
      when /^\/pis\/lookup$/i
        queryString = decodeQueryString(env)
        if queryString.has_key?('op') then
          case queryString['op']
          when /get/i
            lookUpIdentity(queryString)
          when /index/i
            indexIdentities(queryString)
          else
            replyIdentityLookupForm
          end
        else
          replyBadRequest("No op field in queryString")
        end
      when /media\//i
        replyFile(env)
      when /reload$/i
        loadKeys
        loadIdentities
        loadHtml
        replyDefaultBody
      else
        replyDefaultBody
      end
    else
      replyBadRequest("Unknown request method")
    end
  rescue Exception => exception
    replyInternalError(env, exception)
  end
  #
  # send the response
  #
  if @debug then
    puts "SimpleHKP reply:"
    pp @statusCode
    pp @header
    puts @body.join("\n")
  end
  [ @statusCode, @header, @body.flatten ]
end
decodeQueryString(env) click to toggle source
# File lib/simpleHKP/server.rb, line 256
def decodeQueryString(env)
  queryHash = Hash.new
  URI.decode_www_form(env['QUERY_STRING']).each do | aKeyValue |
    queryHash[aKeyValue[0]] = aKeyValue[1]
  end
  queryHash
end
loadHtml() click to toggle source
# File lib/simpleHKP/server.rb, line 31
def loadHtml
  puts "SimpleHKP: loading html partials" if @debug
  Dir.chdir(@htmlDir) do
    @headerHtml =  "<html><head>"
    @headerHtml << "  <title>#{@title}</title>\n"
    @headerHtml << "</head><body class=\"simpleHKP-body\">\n"
    @headerHtml << "<p><a href=\"/\">#{@title}</a></p>"
    @headerHtml = File.open('header.html','r').read if
      File.exists?('header.html')
    puts @headerHtml if @debug

    @defaultBody = ""
    @defaultBody << "<h1 class=\"simpleHKP-welcome\">Welcome to SimpleHKP</h1>\n"
    @defaultBody << "<ul class=\"simpleHKP-tasksList\">\n"
    @defaultBody << "  <li class=\"simpleHKP-taskItem\"><a href=\"/pks/lookup?op=form\">Search keys</a></li>\n"
    @defaultBody << "  <li class=\"simpleHKP-taskItem\"><a href=\"/pks/add\">Upload new key</a></li>\n"
    @defaultBody << "  <li class=\"simpleHKP-taskItem\"><a href=\"/pis/lookup?op=form\">Search identities</a></li>\n"
    @defaultBody << "  <li class=\"simpleHKP-taskItem\"><a href=\"/pis/add\">Upload new identity</a></li>\n"
    @defaultBody << "  <li class=\"simpleHKP-taskItem\"><a href=\"reload\">Reload existing keys and identities</a></li>\n"
    @defaultBody << "</ul>\n"
    @defaultBody = File.open('defaultBody.html','r').read if
      File.exists?('defaultBody.html')
    puts @defaultBody if @debug

    @lookupKeysForm = ""
    @lookupKeysForm << "<div class=\"simpleHKP-lookupKeysFormDiv\">\n"
    @lookupKeysForm << "  <h1 class=\"simpleHKP-lookupKeysFormTitle\">Search GnuPG keys</h1>\n"
    @lookupKeysForm << "  <form action=\"/pks/lookup\" method=\"get\" id=\"simpleHKP-lookupKeysForm\">\n"
    @lookupKeysForm << "    <input type=\"text\" name=\"search\" size=\"80\" />\n"
    @lookupKeysForm << "    <input type=\"hidden\" name=\"op\" value=\"index\" />\n"
    @lookupKeysForm << "    <input type=\"submit\" value=\"Search\" />\n"
    @lookupKeysForm << "  </form>\n"
    @lookupKeysForm << "</div>\n"
    @lookupKeysForm = File.open('lookupKeysForm.html','r').read if
      File.exists?('lookupKeysForm.html')
    puts @lookupKeysForm if @debug

    @lookupIdentitiesForm = ""
    @lookupIdentitiesForm << "<div class=\"simpleHKP-lookupIdentitiesFormDiv\">\n"
    @lookupIdentitiesForm << "  <h1 class=\"simpleHKP-lookupIdentitiesFormTitle\">Search GnuPG keys</h1>\n"
    @lookupIdentitiesForm << "  <form action=\"/pis/lookup\" method=\"get\" id=\"simpleHKP-lookupIdentitiesForm\">\n"
    @lookupIdentitiesForm << "    <input type=\"text\" name=\"search\" size=\"80\" />\n"
    @lookupIdentitiesForm << "    <input type=\"hidden\" name=\"op\" value=\"index\" />\n"
    @lookupIdentitiesForm << "    <input type=\"submit\" value=\"Search\" />\n"
    @lookupIdentitiesForm << "  </form>\n"
    @lookupIdentitiesForm << "</div>\n"
    @lookupIdentitiesForm = File.open('lookupIdentitiesForm.html','r').read if
      File.exists?('lookupIdentitiesForm.html')
    puts @lookupIdentitiesForm if @debug

    @uploadKeyForm = ""
    @uploadKeyForm << "<div class=\"simpleHKP-uploadKeyFormDiv\">\n"
    @uploadKeyForm << "  <h1 class=\"simpleHKP-uploadKeyFormTitle\">Paste GnuPG key to be uploaded below</h1>\n"
    @uploadKeyForm << "  <form action=\"/pks/add\" method=\"post\" id=\"simpleHKP-uploadKeyForm\">\n"
    @uploadKeyForm << "    <textarea name=\"keytext\" form=\"simpleHKP-uploadKeyForm\" rows = \"30\" cols=\"70\" ></textarea>\n"
    @uploadKeyForm << "    <input type=\"submit\" value=\"Upload\" />\n"
    @uploadKeyForm << "  </form>\n"
    @uploadKeyForm << "</div>\n"
    @uploadKeyForm = File.open('uploadKeyForm.html','r').read if
      File.exists?('uploadKeyForm.html')
    puts @uploadKeyForm if @debug

    @uploadIdentityForm = ""
    @uploadIdentityForm << "<div class=\"simpleHKP-uploadIdentityFormDiv\">\n"
    @uploadIdentityForm << "  <h1 class=\"simpleHKP-uploadIdentityFormTitle\">Paste GnuPG encrypted identity to be uploaded below</h1>\n"
    @uploadIdentityForm << "  <form action=\"/pis/add\" method=\"post\" id=\"simpleHKP-uploadIdentityForm\">\n"
    @uploadIdentityForm << "    <textarea name=\"keytext\" form=\"simpleHKP-uploadIdentityForm\" rows = \"30\" cols=\"70\" ></textarea>\n"
    @uploadIdentityForm << "    <input type=\"submit\" value=\"Upload\" />\n"
    @uploadIdentityForm << "  </form>\n"
    @uploadIdentityForm << "</div>\n"
    @uploadIdentityForm = File.open('uploadIdentityForm.html','r').read if
      File.exists?('uploadIdentityForm.html')
    puts @uploadIdentityForm if @debug

    @footer = "<p style=\"color:grey\">SimpleHKP version: #{SimpleHKP::VERSION}</p></body></html>\n"
    @footer = File.open('footer.html','r').read if
     File.exists?('footer.html')
    puts @footer if @debug
  end
  puts "SimpleHKP: finished loading html partials" if @debug
end
ppEnv(env) click to toggle source
# File lib/simpleHKP/server.rb, line 173
    def ppEnv(env)
      Dir.pwd+' '+
#      '['+URI.decode_www_form(env["rack.input"].rewind.read).pretty_inspect+']'+
      env.pretty_inspect
    end
replyBadRequest(message) click to toggle source
# File lib/simpleHKP/server.rb, line 201
def replyBadRequest(message)
  @statusCode = 400
  @body << @headerHtml
  @body << '<p>Bad request!</p>'
  @body << "<p>#{message}</p>"
  @body << @footer
end
replyDefaultBody() click to toggle source
# File lib/simpleHKP/server.rb, line 216
def replyDefaultBody
  @body << @headerHtml
  @body << @defaultBody
  @body << @footer
end
replyFile(env) click to toggle source
# File lib/simpleHKP/server.rb, line 246
def replyFile(env)
  fileName = env['REQUEST_PATH'].sub(/^.*\/media\//,'')
  if File.exists?(fileName) then
    fileExt = File.extname(fileName).sub(/^\./,'')
    @header['Content-Type'] = @mimeMap[fileExt] if
      @mimeMap.has_key?(fileExt)
    @body << File.open(fileName,'r').read
  end
end
replyIdentityLookupForm() click to toggle source
# File lib/simpleHKP/server.rb, line 228
def replyIdentityLookupForm
  @body << @headerHtml
  @body << @lookupIdentitiesForm
  @body << @footer
end
replyIdentityUploadForm() click to toggle source
# File lib/simpleHKP/server.rb, line 240
def replyIdentityUploadForm
  @body << @headerHtml
  @body << @uploadIdentityForm
  @body << @footer
end
replyInternalError(env, exception) click to toggle source
# File lib/simpleHKP/server.rb, line 179
def replyInternalError(env, exception)
  @statusCode = 500
  @body << @headerHtml
  @body << "<p>Internal server error!</p>\n"
  @body << "<pre>#{exception}</pre>\n" if @debug
  @body << @footer
  if @debug then
    puts "\n\n-----------------------------------------------------------"
    puts exception
    puts "-----------------------------------------------------------"
    puts ppEnv(env)
    puts "-----------------------------------------------------------\n\n"
  end
end
replyKeyLookupForm() click to toggle source
# File lib/simpleHKP/server.rb, line 222
def replyKeyLookupForm
  @body << @headerHtml
  @body << @lookupKeysForm
  @body << @footer
end
replyKeyUploadForm() click to toggle source
# File lib/simpleHKP/server.rb, line 234
def replyKeyUploadForm
  @body << @headerHtml
  @body << @uploadKeyForm
  @body << @footer
end
replyNotFound() click to toggle source
# File lib/simpleHKP/server.rb, line 209
def replyNotFound
  @statusCode = 404
  @body << @headerHtml
  @body << '<p>Not found!</p>'
  @body << @footer
end
replyNotImplemented() click to toggle source
# File lib/simpleHKP/server.rb, line 194
def replyNotImplemented
  @statusCode = 501
  @body << @headerHtml
  @body << '<p>Not implemented!<p>'
  @body << @footer
end