From 5f8f7aa5d283df7ed60145094005ce464660ec2c Mon Sep 17 00:00:00 2001 From: Azul Date: Sun, 10 Apr 2016 17:51:26 +0200 Subject: minor tweaks to hkp response parsing Only parse responses that have status code 200 (OK). Simplify status code handling a bit Also profiled it to see if duplicate calculations matter. They don't (2ms for validating 12 keys) --- lib/nickserver/hkp/parse_key_info.rb | 29 +++++++++++++++++++---------- 1 file 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 -- cgit v1.2.3