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.
13 'User-Agent' => "rbot http util #{$version} (http://linuxbrit.co.uk/rbot/)",
17 # uri:: Uri to create a proxy for
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.
25 if (ENV['http_proxy'])
26 proxy = URI.parse ENV['http_proxy']
28 if (@bot.config["http.proxy"])
29 proxy = URI.parse ENV['http_proxy']
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
37 if @bot.config["http.proxy_exclude"]
40 if @bot.config["http.proxy_include"]
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"]
55 proxy_host = proxy.host
56 proxy_port = proxy.port
59 return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
62 # uri:: uri to query (Uri object)
63 # readtimeout:: timeout for reading the response
64 # opentimeout:: timeout for opening the connection
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
75 resp = http.get(uri.request_uri(), @headers)
79 puts "HttpUtil.get return code #{resp.code} #{resp.body}"
83 rescue StandardError, Timeout::Error => e
84 $stderr.puts "HttpUtil.get exception: #{e}, while trying to get #{uri}"