Created
February 23, 2011 11:52
-
-
Save yawn/840330 to your computer and use it in GitHub Desktop.
To proxy or not to proxy, that's the question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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