Added rescues to many of the webfinger methods and made EM-HTTP-Request follow 3 redirects

This commit is contained in:
Raphael 2010-11-17 18:48:27 -08:00
parent cb23ed1136
commit 37a694ed1f
2 changed files with 18 additions and 10 deletions

View file

@ -104,14 +104,12 @@ class PeopleController < ApplicationController
def webfinger(account, opts = {}) def webfinger(account, opts = {})
finger = EMWebfinger.new(account) finger = EMWebfinger.new(account)
finger.on_person do |response| finger.on_person do |response|
Rails.logger.info("event=controller_webfinger status=callback response=#{response.inspect}")
if response.class == Person if response.class == Person
response.socket_to_uid(current_user.id, opts) response.socket_to_uid(current_user.id, opts)
else else
require File.join(Rails.root,'lib/diaspora/websocket') require File.join(Rails.root,'lib/diaspora/websocket')
Diaspora::WebSocket.queue_to_user(current_user.id, {:class => 'people', :status => 'fail', :query => account, :response => response}.to_json) Diaspora::WebSocket.queue_to_user(current_user.id, {:class => 'people', :status => 'fail', :query => account, :response => response}.to_json)
end end
Rails.logger.info("event=controller_webfinger status=callback_complete response=#{response.inspect}")
end end
end end
end end

View file

@ -3,6 +3,8 @@ require File.join(Rails.root, 'lib/webfinger_profile')
class EMWebfinger class EMWebfinger
TIMEOUT = 5 TIMEOUT = 5
REDIRECTS = 3
OPTS = {:timeout => TIMEOUT, :redirects => REDIRECTS}
def initialize(account) def initialize(account)
@account = account.strip.gsub('acct:','').to_s @account = account.strip.gsub('acct:','').to_s
@callbacks = [] @callbacks = []
@ -36,7 +38,7 @@ class EMWebfinger
private private
def get_xrd def get_xrd
http = EventMachine::HttpRequest.new(xrd_url).get :timeout => TIMEOUT http = EventMachine::HttpRequest.new(xrd_url).get OPTS
http.callback { http.callback {
profile_url = webfinger_profile_url(http.response) profile_url = webfinger_profile_url(http.response)
if profile_url if profile_url
@ -60,7 +62,7 @@ class EMWebfinger
def get_webfinger_profile(profile_url) def get_webfinger_profile(profile_url)
http = EventMachine::HttpRequest.new(profile_url).get :timeout => TIMEOUT http = EventMachine::HttpRequest.new(profile_url).get OPTS
http.callback{ make_person_from_webfinger(http.response) } http.callback{ make_person_from_webfinger(http.response) }
http.errback{ process_callbacks "failed to fetch webfinger profile for #{profile_url}"} http.errback{ process_callbacks "failed to fetch webfinger profile for #{profile_url}"}
end end
@ -68,13 +70,21 @@ class EMWebfinger
def make_person_from_webfinger(webfinger_profile) def make_person_from_webfinger(webfinger_profile)
unless webfinger_profile.strip == "" unless webfinger_profile.strip == ""
begin
wf_profile = WebfingerProfile.new(@account, webfinger_profile) wf_profile = WebfingerProfile.new(@account, webfinger_profile)
rescue
return process_callbacks "No person could be constructed from this webfinger profile."
end
http = EventMachine::HttpRequest.new(wf_profile.hcard).get :timeout => TIMEOUT http = EventMachine::HttpRequest.new(wf_profile.hcard).get OPTS
http.callback{ http.callback{
begin
hcard = HCard.build http.response hcard = HCard.build http.response
p = Person.build_from_webfinger(wf_profile, hcard) p = Person.build_from_webfinger(wf_profile, hcard)
process_callbacks(p) process_callbacks(p)
rescue
process_callbacks "No person could be constructed from this hcard."
end
} }
http.errback{process_callbacks "there was a problem fetching the hcard for #{@account}"} http.errback{process_callbacks "there was a problem fetching the hcard for #{@account}"}
end end
@ -82,7 +92,7 @@ class EMWebfinger
def process_callbacks(person) def process_callbacks(person)
Rails.logger.info("event=EMWebfinger status=callbacks_started response=#{person.inspect}") Rails.logger.info("event=EMWebfinger status=callbacks_started target=#{@account} response=#{person.inspect}")
@callbacks.each { |c| @callbacks.each { |c|
begin begin
c.call(person) c.call(person)