summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-04-10 17:51:26 +0200
committerAzul <azul@riseup.net>2016-04-10 18:22:38 +0200
commit5f8f7aa5d283df7ed60145094005ce464660ec2c (patch)
tree958e838458695f43543639a33f3639a1dfc885eb
parentcc94af9d89e7ed076854782a30f7cd9de9e0ced6 (diff)
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)
-rw-r--r--lib/nickserver/hkp/parse_key_info.rb29
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