From 79e1fc8854053690c7fa908e7935e9ff090b4d66 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 23 Nov 2012 23:02:45 +0000 Subject: Use the curb rubygem --- lib/custodian/protocoltest/http.rb | 136 ++++++++++--------------------------- 1 file changed, 36 insertions(+), 100 deletions(-) (limited to 'lib') diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index 44f76a6..f9766b1 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -1,8 +1,4 @@ -require 'openssl' -require 'socket' require 'timeout' -require 'uri' - # @@ -39,7 +35,7 @@ module Custodian # - # The actual status & content + # The actual status & content received. # attr_reader :status, :content @@ -91,9 +87,6 @@ module Custodian @expected_content = nil end - @status = nil - @content = nil - end @@ -114,46 +107,52 @@ module Custodian def run_test # Reset state, in case we've previously run. - @error = nil + @error = nil + @status = nil + @content = nil - # Parse the URL - uri = URI.parse(@url) + begin + require 'rubygems' + require 'curb' + rescue LoadError + @error = "The required rubygem 'curb' was not found." + return false + end - # - # Ensure we have a path to request - to cover people who write: - # - # http://example.com must run http .. - # - if ( uri.path.empty? ) - uri.path = "/" + begin + timeout( 20 ) do + begin + c = Curl::Easy.new(@url) + c.follow_location = true + c.max_redirects = 3 + c.timeout = 20 + c.perform + @status = c.response_code + @content = c.body_str + rescue Curl::Err::TimeoutError + @error = "Timed out fetching page." + return nil + rescue => x + @error = "Exception: #{x}" + return false + end + end + rescue Timeout::Error => e + @error = "Timed out during fetch." + return false end # - # Connect a socket to the host. + # A this point we've either had an exception, or we've + # got a result # - socket = connect( uri.host, uri.port, uri.scheme == "https" ) - - path = uri.path - if ( uri.query ) - path = "#{url.path}?#{url.query}" - end - - req =< err - @error = err - ensure - socket.close if socket.is_a?(Socket) and not socket.closed? - end - - end - - - - # - # If the test fails then report the error. + # If the test fails then report the error../ # def error @error -- cgit v1.2.1