diff options
author | Steve Kemp <steve@steve.org.uk> | 2012-11-14 21:48:31 +0000 |
---|---|---|
committer | Steve Kemp <steve@steve.org.uk> | 2012-11-14 21:48:31 +0000 |
commit | 8d7af91b9409ac5797f02b9a2d2769001d7031cb (patch) | |
tree | 7ee604638c68b99c7af22106f15ae97381cfdf3e /lib/custodian/protocol-tests/http.rb | |
parent | e26d29b15c8012197c3ce72fc5dd75652daa38a6 (diff) |
Timeout correctly.
Diffstat (limited to 'lib/custodian/protocol-tests/http.rb')
-rwxr-xr-x | lib/custodian/protocol-tests/http.rb | 102 |
1 files changed, 52 insertions, 50 deletions
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 |