lib/eat.rb in eat-0.1.2 vs lib/eat.rb in eat-0.1.3

- old
+ new

@@ -1,7 +1,6 @@ require 'uri' -require 'singleton' # http://weblog.jamisbuck.org/2007/2/7/infinity unless defined?(::Infinity) ::Infinity = 1.0/0 end @@ -18,13 +17,11 @@ # Example: # eat('http://brighterplanet.com') #=> '...' # eat('http://brighterplanet.com', :timeout => 10) #=> '...' # eat('http://brighterplanet.com', :limit => 1) #=> '.' def eat(url, options = {}) - timeout = options[:timeout] || options['timeout'] || 2 - limit = options[:limit] || options['limit'] || ::Infinity - openssl_verify_mode = options[:openssl_verify_mode] || options['openssl_verify_mode'] + limit = options.fetch(:limit, ::Infinity) uri = ::URI.parse url.to_s body = [] read_so_far = 0 @@ -40,29 +37,28 @@ body << chunk end end when 'http', 'https' - require 'net/http' - require 'net/https' if uri.scheme == 'https' - (defined?(::SystemTimer) ? ::SystemTimer : ::Timeout).timeout(timeout) do - http = ::Net::HTTP.new uri.host, uri.port - if uri.scheme == 'https' - http.use_ssl = true - http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if openssl_verify_mode.to_s == 'none' + require 'httpclient' + timeout = options.fetch(:timeout, 2) + openssl_verify_mode = options.fetch(:openssl_verify_mode, ::OpenSSL::SSL::VERIFY_PEER) + if openssl_verify_mode == 'none' + openssl_verify_mode = ::OpenSSL::SSL::VERIFY_NONE + end + http = ::HTTPClient.new + http.redirect_uri_callback = ::Proc.new { |uri, res| ::URI.parse(res.header['location'][0]) } + http.receive_timeout = timeout + if uri.scheme == 'https' + http.ssl_config.verify_mode = openssl_verify_mode + end + catch :stop do + http.get_content(uri.to_s) do |chunk| + throw :stop if read_so_far > limit + read_so_far += chunk.length + body << chunk end - http.start do |session| - catch :stop do - session.get(uri.request_uri, 'Accept-Encoding' => '') do |chunk| - throw :stop if read_so_far > limit - read_so_far += chunk.length - body << chunk - end - session.finish - end - end end - end limit == ::Infinity ? body.join : body.join[0...limit] end end