From 8d7af91b9409ac5797f02b9a2d2769001d7031cb Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 14 Nov 2012 21:48:31 +0000 Subject: Timeout correctly. --- lib/custodian/protocol-tests/http.rb | 102 ++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 50 deletions(-) (limited to 'lib/custodian/protocol-tests/http.rb') diff --git a/lib/custodian/protocol-tests/http.rb b/lib/custodian/protocol-tests/http.rb index c11e27a..34fc472 100755 --- a/lib/custodian/protocol-tests/http.rb +++ b/lib/custodian/protocol-tests/http.rb @@ -113,64 +113,66 @@ class HTTPTest # # NOTE: This came from sentinel. def getURL (uri_str, timeout) - begin - uri_str = 'http://' + uri_str unless uri_str.match(/^http/) - url = URI.parse(uri_str) - http = Net::HTTP.new(url.host, url.port) - http.open_timeout = timeout - http.read_timeout = timeout - - if (url.scheme == "https") - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = nil + timeout( timeout ) do + begin + uri_str = 'http://' + uri_str unless uri_str.match(/^http/) + url = URI.parse(uri_str) + http = Net::HTTP.new(url.host, url.port) + http.open_timeout = timeout + http.read_timeout = timeout + + if (url.scheme == "https") + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end - # - # Ensure we have a trailing "/" - # - if ( url.path.empty? ) - url.path = "/" - end + response = nil - if nil == url.query - response = http.start { http.get(url.path) } - else - response = http.start { http.get("#{url.path}?#{url.query}") } - end + # + # Ensure we have a trailing "/" + # + if ( url.path.empty? ) + url.path = "/" + end - case response - when Net::HTTPRedirection - then - newURL = response['location'].match(/^http/)? - response['Location']:uri_str+response['Location'] - return( getURL(newURL, timeout) ) - else - @status = response.code.to_i - @body = response.body - end + if nil == url.query + response = http.start { http.get(url.path) } + else + response = http.start { http.get("#{url.path}?#{url.query}") } + end - return true - rescue Errno::EHOSTUNREACH => ex - @error = "no route to host" - return false - rescue Timeout::Error => ex - @error = "time out reached" - return false - rescue Errno::ECONNREFUSED => ex - @error = "Connection refused" - return false - rescue => ex - @error = ex - return false + case response + when Net::HTTPRedirection + then + newURL = response['location'].match(/^http/)? + response['Location']:uri_str+response['Location'] + return( getURL(newURL, timeout) ) + else + @status = response.code.to_i + @body = response.body + end + + return true + rescue Errno::EHOSTUNREACH => ex + @error = "no route to host" + return false + rescue Timeout::Error => ex + @error = "time out reached" + return false + rescue Errno::ECONNREFUSED => ex + @error = "Connection refused" + return false + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false + rescue => ex + @error = ex + return false + end end - @error = "Misc failure" - return false end - - end -- cgit v1.2.1