From 82eb5415abd3bfcab4c796e0a15e852ceea61712 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 19 Jun 2015 01:52:12 +0200 Subject: [PATCH] cache generated host-meta xml --- .../diaspora_federation/webfinger_controller.rb | 10 ++++++++-- lib/diaspora_federation.rb | 2 +- .../{webfinger.rb => web_finger.rb} | 0 .../diaspora_federation/webfinger_controller_spec.rb | 7 +++++++ 4 files changed, 16 insertions(+), 3 deletions(-) rename lib/diaspora_federation/{webfinger.rb => web_finger.rb} (100%) diff --git a/app/controllers/diaspora_federation/webfinger_controller.rb b/app/controllers/diaspora_federation/webfinger_controller.rb index f5f5585..ec0e76e 100644 --- a/app/controllers/diaspora_federation/webfinger_controller.rb +++ b/app/controllers/diaspora_federation/webfinger_controller.rb @@ -15,8 +15,7 @@ module DiasporaFederation # # GET /.well-known/host-meta def host_meta - doc = WebFinger::HostMeta.from_base_url(DiasporaFederation.server_uri.to_s) - render body: doc.to_xml, content_type: "application/xrd+xml" + render body: WebfingerController.host_meta_xml, content_type: "application/xrd+xml" end ## @@ -34,6 +33,13 @@ module DiasporaFederation private + ## + # creates the host-meta xml with the configured server_uri and caches it + # @return [String] XML string + def self.host_meta_xml + @host_meta_xml ||= WebFinger::HostMeta.from_base_url(DiasporaFederation.server_uri.to_s).to_xml + end + def find_person(query) DiasporaFederation.person_class.find_local_by_diaspora_handle(query.strip.downcase.gsub("acct:", "")) end diff --git a/lib/diaspora_federation.rb b/lib/diaspora_federation.rb index b0a819a..70ab8a8 100644 --- a/lib/diaspora_federation.rb +++ b/lib/diaspora_federation.rb @@ -1,7 +1,7 @@ require "diaspora_federation/engine" require "diaspora_federation/logging" -require "diaspora_federation/webfinger" +require "diaspora_federation/web_finger" ## # diaspora* federation rails engine diff --git a/lib/diaspora_federation/webfinger.rb b/lib/diaspora_federation/web_finger.rb similarity index 100% rename from lib/diaspora_federation/webfinger.rb rename to lib/diaspora_federation/web_finger.rb diff --git a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb index 5a565e4..582a00e 100644 --- a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb +++ b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb @@ -5,6 +5,7 @@ module DiasporaFederation describe "#host_meta" do before do DiasporaFederation.server_uri = URI("http://localhost:3000/") + WebfingerController.instance_variable_set(:@host_meta_xml, nil) # clear cache end it "succeeds" do @@ -26,6 +27,12 @@ module DiasporaFederation expect(WebFinger::HostMeta).to receive(:from_base_url).with("http://localhost:3000/").and_call_original get :host_meta end + + it "caches the xml" do + expect(WebFinger::HostMeta).to receive(:from_base_url).exactly(1).times.and_call_original + get :host_meta + get :host_meta + end end describe "#legacy_webfinger" do