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 = {})
finger = EMWebfinger.new(account)
finger.on_person do |response|
Rails.logger.info("event=controller_webfinger status=callback response=#{response.inspect}")
if response.class == Person
response.socket_to_uid(current_user.id, opts)
else
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)
end
Rails.logger.info("event=controller_webfinger status=callback_complete response=#{response.inspect}")
end
end
end

View file

@ -3,6 +3,8 @@ require File.join(Rails.root, 'lib/webfinger_profile')
class EMWebfinger
TIMEOUT = 5
REDIRECTS = 3
OPTS = {:timeout => TIMEOUT, :redirects => REDIRECTS}
def initialize(account)
@account = account.strip.gsub('acct:','').to_s
@callbacks = []
@ -36,7 +38,7 @@ class EMWebfinger
private
def get_xrd
http = EventMachine::HttpRequest.new(xrd_url).get :timeout => TIMEOUT
http = EventMachine::HttpRequest.new(xrd_url).get OPTS
http.callback {
profile_url = webfinger_profile_url(http.response)
if profile_url
@ -60,7 +62,7 @@ class EMWebfinger
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.errback{ process_callbacks "failed to fetch webfinger profile for #{profile_url}"}
end
@ -68,13 +70,21 @@ class EMWebfinger
def make_person_from_webfinger(webfinger_profile)
unless webfinger_profile.strip == ""
wf_profile = WebfingerProfile.new(@account, webfinger_profile)
begin
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{
hcard = HCard.build http.response
p = Person.build_from_webfinger(wf_profile, hcard)
process_callbacks(p)
begin
hcard = HCard.build http.response
p = Person.build_from_webfinger(wf_profile, hcard)
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}"}
end
@ -82,7 +92,7 @@ class EMWebfinger
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|
begin
c.call(person)