diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/nickserver/hkp/parse_key_info.rb | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/lib/nickserver/hkp/parse_key_info.rb b/lib/nickserver/hkp/parse_key_info.rb index 2bfcb7e..8934829 100644 --- a/lib/nickserver/hkp/parse_key_info.rb +++ b/lib/nickserver/hkp/parse_key_info.rb @@ -1,3 +1,10 @@ +# +# Simple parser for HKP KeyInfo responses. +# +# Focus is on simple here. Trying to avoid state and sideeffects. +# Parsing a response with 12 keys and validating them takes 2ms. +# So no need for memoization and making things more complex. +# module Nickserver; module HKP class ParseKeyInfo @@ -13,10 +20,10 @@ module Nickserver; module HKP end def status(uid) - if header.status == 200 && keys(uid).any? - 200 - else + if hkp_ok? && keys(uid).empty? error_status(uid) + else + header.status end end @@ -38,14 +45,10 @@ module Nickserver; module HKP attr_reader :vindex_result def error_status(uid) - if header.status != 200 - header.status + if errors(uid).any? + 500 else - if errors(uid).any? - 500 - else - 404 - end + 404 end end @@ -67,11 +70,17 @@ module Nickserver; module HKP end def all_key_infos + # only parse hkp responses with status 200 (OK) + return [] unless hkp_ok? @all_key_infos ||= vindex_result.scan(MATCH_PUB_KEY).map do |match| KeyInfo.new(match[0]) end end + def hkp_ok? + header.status == 200 + end + def error_message(uid, key, err) "Ignoring key #{key.keyid} for #{uid}: #{err}" if err end |