]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/httputil.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / lib / rbot / httputil.rb
1 module Irc
2
3 require 'net/http'
4 Net::HTTP.version_1_2
5   
6 # class for making http requests easier (mainly for plugins to use)
7 # this class can check the bot proxy configuration to determine if a proxy
8 # needs to be used, which includes support for per-url proxy configuration.
9 class HttpUtil
10   def initialize(bot)
11     @bot = bot
12     @headers = {
13       'User-Agent' => "rbot http util #{$version} (http://linuxbrit.co.uk/rbot/)",
14     }
15   end
16
17   # uri:: Uri to create a proxy for
18   #
19   # return a net/http Proxy object, which is configured correctly for
20   # proxying based on the bot's proxy configuration. 
21   # This will include per-url proxy configuration based on the bot config
22   # +http_proxy_include/exclude+ options.
23   def get_proxy(uri)
24     proxy = nil
25     if (ENV['http_proxy'])
26       proxy = URI.parse ENV['http_proxy']
27     end
28     if (@bot.config["http.proxy"])
29       proxy = URI.parse ENV['http_proxy']
30     end
31
32     # if http_proxy_include or http_proxy_exclude are set, then examine the
33     # uri to see if this is a proxied uri
34     # the excludes are a list of regexps, and each regexp is checked against
35     # the server name, and its IP addresses
36     if uri
37       if @bot.config["http.proxy_exclude"]
38         # TODO
39       end
40       if @bot.config["http.proxy_include"]
41       end
42     end
43     
44     proxy_host = nil
45     proxy_port = nil
46     proxy_user = nil
47     proxy_pass = nil
48     if @bot.config["http.proxy_user"]
49       proxy_user = @bot.config["http.proxy_user"]
50       if @bot.config["http.proxy_pass"]
51         proxy_pass = @bot.config["http.proxy_pass"]
52       end
53     end
54     if proxy
55       proxy_host = proxy.host
56       proxy_port = proxy.port
57     end
58     
59     return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
60   end
61
62   # uri::         uri to query (Uri object)
63   # readtimeout:: timeout for reading the response
64   # opentimeout:: timeout for opening the connection
65   #
66   # simple get request, returns response body if the status code is 200 and
67   # the request doesn't timeout.
68   def get(uri, readtimeout=10, opentimeout=5)
69     proxy = get_proxy(uri)
70     proxy.open_timeout = opentimeout
71     proxy.read_timeout = readtimeout
72    
73     begin
74       proxy.start() {|http|
75         resp = http.get(uri.request_uri(), @headers)
76         if resp.code == "200"
77           return resp.body
78         else
79           puts "HttpUtil.get return code #{resp.code} #{resp.body}"
80         end
81         return nil
82       }
83     rescue StandardError, Timeout::Error => e
84       $stderr.puts "HttpUtil.get exception: #{e}, while trying to get #{uri}"
85     end
86     return nil
87   end
88 end
89
90 end