diff --git a/Gemfile b/Gemfile index d3fe4f902..f780724f5 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,10 @@ gem 'devise_invitable', '0.5.0' gem 'omniauth', '0.2.6' gem 'twitter', '1.5.0' +#Web +gem 'faraday' +gem 'faraday-stack' + #Views gem 'haml', '3.0.25' gem 'will_paginate', '3.0.pre2' diff --git a/Gemfile.lock b/Gemfile.lock index 175aa0d7c..a8ff7a3a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -154,6 +154,8 @@ GEM addressable (~> 2.2.4) multipart-post (~> 1.1.0) rack (>= 1.1.0, < 2) + faraday-stack (0.1.2) + faraday (~> 0.6) faraday_middleware (0.6.3) faraday (~> 0.6.0) fastercsv (1.5.4) @@ -415,6 +417,8 @@ DEPENDENCIES em-websocket! excon (= 0.2.4) factory_girl_rails + faraday + faraday-stack fastercsv (= 1.5.4) fixture_builder (~> 0.2.0) fog (= 0.3.25) diff --git a/config/initializers/faraday.rb b/config/initializers/faraday.rb index 574066c8a..00bc3e6e9 100644 --- a/config/initializers/faraday.rb +++ b/config/initializers/faraday.rb @@ -2,4 +2,7 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -Faraday.default_connection = Faraday::Connection.new( :ssl => {:ca_file => AppConfig[:ca_file]} ) +Faraday.default_connection = Faraday::Connection.new( :ssl => {:ca_file => AppConfig[:ca_file]}, :timeout => 5 ) do |b| + b.use FaradayStack::FollowRedirects + b.adapter Faraday.default_adapter +end diff --git a/lib/webfinger.rb b/lib/webfinger.rb index 2e8659fd8..7c36f4e23 100644 --- a/lib/webfinger.rb +++ b/lib/webfinger.rb @@ -3,9 +3,6 @@ require File.join(Rails.root, 'lib/webfinger_profile') class Webfinger class WebfingerFailedError < RuntimeError; end - TIMEOUT = 5 - REDIRECTS = 3 - OPTS = {:timeout => TIMEOUT, :redirects => REDIRECTS} def initialize(account) @account = account.strip.gsub('acct:','').to_s @ssl = true @@ -47,7 +44,7 @@ class Webfinger private def get_xrd begin - http = Faraday.get xrd_url, OPTS + http = Faraday.get xrd_url profile_url = webfinger_profile_url(http.body) if profile_url @@ -69,7 +66,7 @@ class Webfinger def get_webfinger_profile(profile_url) begin - http = Faraday.get(profile_url, OPTS) + http = Faraday.get(profile_url) rescue raise I18n.t('webfinger.fetch_failed', :profile_url => profile_url) @@ -83,7 +80,7 @@ class Webfinger @wf_profile = WebfingerProfile.new(@account, webfinger_profile) begin - hcard = Faraday.get(@wf_profile.hcard, OPTS) + hcard = Faraday.get(@wf_profile.hcard) rescue return I18n.t('webfinger.hcard_fetch_failed', :account => @account) end diff --git a/spec/lib/webfinger_spec.rb b/spec/lib/webfinger_spec.rb index 80e71aa7a..912a7d9e2 100644 --- a/spec/lib/webfinger_spec.rb +++ b/spec/lib/webfinger_spec.rb @@ -67,6 +67,21 @@ describe Webfinger do end end + describe '#get_xrd' do + it 'follows redirects' do + puts "Now in spec." + redirect_url = "http://whereami.whatisthis/host-meta" + stub_request(:get, "https://tom.joindiaspora.com/.well-known/host-meta"). + to_return(:status => 302, :headers => { 'Location' => redirect_url }) + stub_request(:get, redirect_url). + to_return(:status => 200, :body => diaspora_xrd) + begin + finger.send :get_xrd + rescue; end + a_request(:get, redirect_url).should have_been_made + end + end + context 'webfingering local people' do it 'should return a person from the database if it matches its handle' do