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
35 if @bot.config["http_proxy_exclude"]
38 if @bot.config["http_proxy_include"]
46 if @bot.config["http_proxy_user"]
47 proxy_user = @bot.config["http_proxy_user"]
48 if @bot.config["http_proxy_pass"]
49 proxy_pass = @bot.config["http_proxy_pass"]
53 proxy_host = proxy.host
54 proxy_port = proxy.port
57 return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
60 # uri:: uri to query (Uri object)
61 # readtimeout:: timeout for reading the response
62 # opentimeout:: timeout for opening the connection
64 # simple get request, returns response body if the status code is 200 and
65 # the request doesn't timeout.
66 def get(uri, readtimeout=10, opentimeout=5)
67 proxy = get_proxy(uri)
68 proxy.open_timeout = opentimeout
69 proxy.read_timeout = readtimeout
73 resp = http.get(uri.request_uri(), @headers)
77 puts "HttpUtil.get return code #{resp.code} #{resp.body}"
81 rescue StandardError, Timeout::Error => e
82 $stderr.puts "HttpUtil.get exception: #{e}, while trying to get #{uri}"