Created
August 5, 2010 21:08
-
-
Save goozbach/510376 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/ruby | |
require 'net/http' | |
require 'net/https' | |
require 'rubygems' | |
def fetch(uri_str, limit = 10) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
req_url = URI.parse(uri_str) | |
http = Net::HTTP.new(req_url.host, req_url.port) | |
http.use_ssl = (req_url.port == 443) | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http.open_timeout = 3 # in seconds | |
http.read_timeout = 3 # in seconds | |
response = Net::HTTP.get_response(req_url) | |
case response | |
when Net::HTTPSuccess then response.body | |
when Net::HTTPRedirection then fetch(response['location'], limit - 1) | |
else | |
response.error! | |
end | |
end | |
puts fetch('http://kessel.friocorte.com/') | |
puts fetch('https://kessel.friocorte.com/') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
testing