]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/httputil.rb
All lib/rbot files are now upgraded to the new logging feature
[user/henk/code/ruby/rbot.git] / lib / rbot / httputil.rb
index c6f51d8e5a39c9439d99ac289550e747bb8742d1..b254a79165f5c07f6dbf3373b937a838d69c1501 100644 (file)
@@ -3,6 +3,7 @@ module Utils
 
 require 'resolv'
 require 'net/http'
+require 'net/https'
 Net::HTTP.version_1_2
   
 # class for making http requests easier (mainly for plugins to use)
@@ -23,6 +24,7 @@ class HttpUtil
       :default => [],
       :desc => "List of regexps to check against a URI's hostname/ip to see if we should use the proxy to access this URI. All URIs are proxied by default if the proxy is set, so this is only required to re-include URIs that might have been excluded by the exclude list. e.g. exclude /.*\.foo\.com/, include bar\.foo\.com")
     BotConfig.register BotConfigArrayValue.new('http.proxy_exclude',
+      :default => [],
       :desc => "List of regexps to check against a URI's hostname/ip to see if we should use avoid the proxy to access this URI and access it directly")
 
   def initialize(bot)
@@ -44,9 +46,9 @@ class HttpUtil
 
     list = [uri.host]
     begin
-      list.push Resolv.getaddresses(uri.host)
+      list.concat Resolv.getaddresses(uri.host)
     rescue StandardError => err
-      puts "warning: couldn't resolve host uri.host"
+      warning "couldn't resolve host uri.host"
     end
 
     unless @bot.config["http.proxy_exclude"].empty?
@@ -90,13 +92,13 @@ class HttpUtil
 
     if @bot.config["http.use_proxy"]
       if (ENV['http_proxy'])
-        proxy = URI.parse ENV['http_proxy']
+        proxy = URI.parse ENV['http_proxy'] rescue nil
       end
       if (@bot.config["http.proxy_uri"])
-        proxy = URI.parse ENV['http_proxy_uri']
+        proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil
       end
       if proxy
-        debug "proxy is set to #{proxy.uri}"
+        debug "proxy is set to #{proxy.host} port #{proxy.port}"
         if proxy_required(uri)
           proxy_host = proxy.host
           proxy_port = proxy.port
@@ -106,7 +108,9 @@ class HttpUtil
       end
     end
     
-    return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
+    h = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
+    h.use_ssl = true if uri.scheme == "https"
+    return h
   end
 
   # uri::         uri to query (Uri object)
@@ -126,12 +130,12 @@ class HttpUtil
         if resp.code == "200"
           return resp.body
         else
-          puts "HttpUtil.get return code #{resp.code} #{resp.body}"
+          log "HttpUtil.get return code #{resp.code} #{resp.body}"
         end
         return nil
       }
     rescue StandardError, Timeout::Error => e
-      $stderr.puts "HttpUtil.get exception: #{e}, while trying to get #{uri}"
+      error "HttpUtil.get exception: #{e.inspect}, while trying to get #{uri}"
     end
     return nil
   end