Skip to content

Instantly share code, notes, and snippets.

@yawn
Created February 23, 2011 11:52
Show Gist options
  • Save yawn/840330 to your computer and use it in GitHub Desktop.
Save yawn/840330 to your computer and use it in GitHub Desktop.
To proxy or not to proxy, that's the question
class Connection
require 'net/http'
TEST_HOST = 'www.example.com'
TEST_PORT = 80
TEST_PATH = '/index.html'
# yields either one of the supplied proxy or :unrestricted (no proxy) or :restricted (nothing worked)
def self.quality? *proxies
if request
:unrestricted
else
proxies.each do |proxy|
host, port = proxy.split ':'
return proxy if request host, port
end
:restricted
end
end
protected
def self.request host = nil, port = nil
opt = { :open_timeout => 1, :read_timeout => 1 }
!(Net::HTTP.start(TEST_HOST, TEST_PORT, host, port, nil, nil, opt) do |http|
http.head(TEST_PATH)
end rescue nil).nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment