aboutsummaryrefslogtreecommitdiffstats
path: root/lib/quiet_opener.rb
blob: c6e259b935d3fa3555e6fd932c3c0d73b962fc53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'open-uri'
require 'net-purge'
if RUBY_VERSION.to_f < 2.0
    require 'net/http/local'
end

def quietly_try_to_open(url)
    begin
        result = open(url).read.strip
    rescue OpenURI::HTTPError,
           SocketError,
           Errno::ETIMEDOUT,
           Errno::ECONNREFUSED,
           Errno::EHOSTUNREACH,
           Errno::ECONNRESET,
           Timeout::Error => exception
        e = Exception.new("Unable to open third-party URL #{url}: #{exception.message}")
        e.set_backtrace(exception.backtrace)
        if !AlaveteliConfiguration.exception_notifications_from.blank? && !AlaveteliConfiguration.exception_notifications_to.blank?
            ExceptionNotifier::Notifier.exception_notification(request.env, e).deliver
        end
        Rails.logger.warn(e.message)
        result = ""
    end
    return result
end

# On Ruby versions before 2.0, we need to use the net-http-local gem
# to force the use of 127.0.0.1 as the local interface for the
# connection.  However, at the time of writing this gem doesn't work
# on Ruby 2.0 and it's not necessary with that Ruby version - one can
# supply a :local_host option to Net::HTTP:start.  So, this helper
# function is to abstract away that difference, and can be used as you
# would Net::HTTP.start(host) when passed a block.
def http_from_localhost(host)
    if RUBY_VERSION.to_f >= 2.0
        Net::HTTP.start(host, :local_host => '127.0.0.1') do |http|
            yield http
        end
    else
        Net::HTTP.bind '127.0.0.1' do
            Net::HTTP.start(host) do |http|
                yield http
            end
        end
    end
end

def quietly_try_to_purge(host, url)
    begin
        result = ""
        result_body = ""
        http_from_localhost(host) do |http|
            request = Net::HTTP::Purge.new(url)
            response = http.request(request)
            result = response.code
            result_body = response.body
        end
    rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, Errno::ENETUNREACH
        Rails.logger.warn("PURGE: Unable to reach host #{host}")
    end
    if result == "200"
        Rails.logger.debug("PURGE: Purged URL #{url} at #{host}: #{result}")
    else
        Rails.logger.warn("PURGE: Unable to purge URL #{url} at #{host}: status #{result}")
    end
    return result
end