lib/libravatar.rb in libravatar-1.1.0 vs lib/libravatar.rb in libravatar-1.2.0
- old
+ new
@@ -55,32 +55,44 @@
:port => 443 }
]
# Grab the DNS SRV records associated with the target domain,
# and choose one according to RFC2782.
- def get_base_url
+ def srv_lookup
profile = @@profiles[ @https ? 1 : 0 ]
Resolv::DNS::open do |dns|
rrs = dns.getresources(profile[:srv] + get_target_domain(),
Resolv::DNS::Resource::IN::SRV).to_a
- return profile[:scheme] + profile[:host] unless rrs.any?
+ return [nil, nil] unless rrs.any?
+
min_priority = rrs.map{ |r| r.priority }.min
rrs.delete_if{ |r| r.priority != min_priority }
rrs = rrs.select{ |r| r.weight == 0 } +
rrs.select{ |r| r.weight > 0 }.shuffle
weight_sum = rrs.inject(0) { |a,r| a+r.weight }
value = rand( weight_sum + 1 )
rrs.each do |r|
- port_fragment = r.port != profile[:port] ? ':' + r.port : ''
- return profile[:scheme] + r.target.to_s + port_fragment if r.weight <= value
+ return [r.target, r.port] if r.weight <= value
value -= r.weight
end
end
end
+ def get_base_url
+ profile = @@profiles[ @https ? 1 : 0 ]
+ target, port = srv_lookup
+
+ if (target && port)
+ port_fragment = port != profile[:port] ? ':' + port : ''
+ return profile[:scheme] + target.to_s + port_fragment
+ else
+ return profile[:scheme] + profile[:host]
+ end
+ end
+
# Generate the libravatar URL
def to_s
if @email
@email.downcase!
id = Digest::MD5.hexdigest(@email)
@@ -95,9 +107,17 @@
baseurl = get_base_url() + '/avatar/'
return baseurl + id + query
end
private
+
+ def sanitize_srv_lookup(hostname, port)
+ unless hostname.match(/^[0-9a-zA-Z\-.]+$/) && 1 <= port && port <= 65535
+ return [nil, nil]
+ end
+
+ return [hostname, port]
+ end
# Normalize an openid URL following the description on libravatar.org
def normalize_openid(s)
x = URI.parse(s)
x.host.downcase!