9 # class for making http requests easier (mainly for plugins to use)
10 # this class can check the bot proxy configuration to determine if a proxy
11 # needs to be used, which includes support for per-url proxy configuration.
13 BotConfig.register BotConfigBooleanValue.new('http.use_proxy',
14 :default => false, :desc => "should a proxy be used for HTTP requests?")
15 BotConfig.register BotConfigStringValue.new('http.proxy_uri', :default => false,
16 :desc => "Proxy server to use for HTTP requests (URI, e.g http://proxy.host:port)")
17 BotConfig.register BotConfigStringValue.new('http.proxy_user',
19 :desc => "User for authenticating with the http proxy (if required)")
20 BotConfig.register BotConfigStringValue.new('http.proxy_pass',
22 :desc => "Password for authenticating with the http proxy (if required)")
23 BotConfig.register BotConfigArrayValue.new('http.proxy_include',
25 :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")
26 BotConfig.register BotConfigArrayValue.new('http.proxy_exclude',
28 :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")
29 BotConfig.register BotConfigIntegerValue.new('http.max_redir',
31 :desc => "Maximum number of redirections to be used when getting a document")
32 BotConfig.register BotConfigIntegerValue.new('http.expire_time',
34 :desc => "After how many minutes since last use a cached document is considered to be expired")
35 BotConfig.register BotConfigIntegerValue.new('http.max_cache_time',
37 :desc => "After how many minutes since first use a cached document is considered to be expired")
38 BotConfig.register BotConfigIntegerValue.new('http.no_expire_cache',
40 :desc => "Set this to true if you want the bot to never expire the cached pages")
46 'User-Agent' => "rbot http util #{$version} (http://linuxbrit.co.uk/rbot/)",
50 attr_reader :last_response
53 # if http_proxy_include or http_proxy_exclude are set, then examine the
54 # uri to see if this is a proxied uri
55 # the in/excludes are a list of regexps, and each regexp is checked against
56 # the server name, and its IP addresses
57 def proxy_required(uri)
59 if @bot.config["http.proxy_exclude"].empty? && @bot.config["http.proxy_include"].empty?
65 list.concat Resolv.getaddresses(uri.host)
66 rescue StandardError => err
67 warning "couldn't resolve host uri.host"
70 unless @bot.config["http.proxy_exclude"].empty?
71 re = @bot.config["http.proxy_exclude"].collect{|r| Regexp.new(r)}
81 unless @bot.config["http.proxy_include"].empty?
82 re = @bot.config["http.proxy_include"].collect{|r| Regexp.new(r)}
92 debug "using proxy for uri #{uri}?: #{use_proxy}"
96 # uri:: Uri to create a proxy for
98 # return a net/http Proxy object, which is configured correctly for
99 # proxying based on the bot's proxy configuration.
100 # This will include per-url proxy configuration based on the bot config
101 # +http_proxy_include/exclude+ options.
109 if @bot.config["http.use_proxy"]
110 if (ENV['http_proxy'])
111 proxy = URI.parse ENV['http_proxy'] rescue nil
113 if (@bot.config["http.proxy_uri"])
114 proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil
117 debug "proxy is set to #{proxy.host} port #{proxy.port}"
118 if proxy_required(uri)
119 proxy_host = proxy.host
120 proxy_port = proxy.port
121 proxy_user = @bot.config["http.proxy_user"]
122 proxy_pass = @bot.config["http.proxy_pass"]
127 h = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
128 h.use_ssl = true if uri.scheme == "https"
132 # uri:: uri to query (Uri object)
133 # readtimeout:: timeout for reading the response
134 # opentimeout:: timeout for opening the connection
136 # simple get request, returns (if possible) response body following redirs
137 # and caching if requested
138 # if a block is given, it yields the urls it gets redirected to
139 # TODO we really need something to implement proper caching
140 def get(uri_or_str, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"], cache=false)
141 if uri_or_str.kind_of?(URI)
144 uri = URI.parse(uri_or_str.to_s)
146 debug "Getting #{uri}"
148 proxy = get_proxy(uri)
149 proxy.open_timeout = opentimeout
150 proxy.read_timeout = readtimeout
153 proxy.start() {|http|
154 yield uri.request_uri() if block_given?
155 req = Net::HTTP::Get.new(uri.request_uri(), @headers)
156 if uri.user and uri.password
157 req.basic_auth(uri.user, uri.password)
159 resp = http.request(req)
161 when Net::HTTPSuccess
163 debug "Caching #{uri.to_s}"
164 cache_response(uri.to_s, resp)
167 when Net::HTTPRedirection
168 if resp.key?('location')
169 new_loc = URI.join(uri, resp['location'])
170 debug "Redirecting #{uri} to #{new_loc}"
171 yield new_loc if block_given?
173 # If cache is an Array, we assume get was called by get_cached
174 # because of a cache miss and that the first value of the Array
175 # was the noexpire value. Since the cache miss might have been
176 # caused by a redirection, we want to try get_cached again
177 # TODO FIXME look at Python's httplib2 for a most likely
178 # better way to handle all this mess
179 if cache.kind_of?(Array)
180 return get_cached(new_loc, readtimeout, opentimeout, max_redir-1, cache[0])
182 return get(new_loc, readtimeout, opentimeout, max_redir-1, cache)
185 warning "Max redirection reached, not going to #{new_loc}"
188 warning "Unknown HTTP redirection #{resp.inspect}"
191 debug "HttpUtil.get return code #{resp.code} #{resp.body}"
193 @last_response = resp
196 rescue StandardError, Timeout::Error => e
197 error "HttpUtil.get exception: #{e.inspect}, while trying to get #{uri}"
198 debug e.backtrace.join("\n")
204 # just like the above, but only gets the head
205 def head(uri_or_str, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"])
206 if uri_or_str.kind_of?(URI)
209 uri = URI.parse(uri_or_str.to_s)
212 proxy = get_proxy(uri)
213 proxy.open_timeout = opentimeout
214 proxy.read_timeout = readtimeout
217 proxy.start() {|http|
218 yield uri.request_uri() if block_given?
219 req = Net::HTTP::Head.new(uri.request_uri(), @headers)
220 if uri.user and uri.password
221 req.basic_auth(uri.user, uri.password)
223 resp = http.request(req)
225 when Net::HTTPSuccess
227 when Net::HTTPRedirection
228 debug "Redirecting #{uri} to #{resp['location']}"
229 yield resp['location'] if block_given?
231 return head( URI.parse(resp['location']), readtimeout, opentimeout, max_redir-1)
233 warning "Max redirection reached, not going to #{resp['location']}"
236 debug "HttpUtil.head return code #{resp.code}"
238 @last_response = resp
241 rescue StandardError, Timeout::Error => e
242 error "HttpUtil.head exception: #{e.inspect}, while trying to get #{uri}"
243 debug e.backtrace.join("\n")
249 def cache_response(k, resp)
251 if resp.key?('pragma') and resp['pragma'] == 'no-cache'
252 debug "Not caching #{k}, it has Pragma: no-cache"
255 # TODO should we skip caching if neither last-modified nor etag are present?
260 u[:last_modified] = nil
261 u[:last_modified] = Time.httpdate(resp['date']) if resp.key?('date')
262 u[:last_modified] = Time.httpdate(resp['last-modified']) if resp.key?('last-modified')
264 u[:expires] = Time.httpdate(resp['expires']) if resp.key?('expires')
265 u[:revalidate] = false
266 if resp.key?('cache-control')
268 case resp['cache-control']
269 when /no-cache|must-revalidate/
270 u[:revalidate] = true
274 u[:etag] = resp['etag'] if resp.key?('etag')
279 error "Failed to cache #{k}/#{resp.to_hash.inspect}: #{e.inspect}"
283 debug "Cached #{k}/#{resp.to_hash.inspect}: #{u.inspect_no_body}"
284 debug "#{@cache.size} pages (#{@cache.keys.join(', ')}) cached up to now"
287 # For debugging purposes
296 def expired?(uri, readtimeout, opentimeout)
298 debug "Checking cache validity for #{k}"
300 return true unless @cache.key?(k)
303 # TODO we always revalidate for the time being
305 if u[:etag].empty? and u[:last_modified].nil?
310 proxy = get_proxy(uri)
311 proxy.open_timeout = opentimeout
312 proxy.read_timeout = readtimeout
314 proxy.start() {|http|
315 yield uri.request_uri() if block_given?
316 headers = @headers.dup
317 headers['If-None-Match'] = u[:etag] unless u[:etag].empty?
318 headers['If-Modified-Since'] = u[:last_modified].rfc2822 if u[:last_modified]
319 debug "Cache HEAD request headers: #{headers.inspect}"
320 # FIXME TODO We might want to use a Get here
321 # because if a 200 OK is returned we would get the new body
322 # with one connection less ...
323 req = Net::HTTP::Head.new(uri.request_uri(), headers)
324 if uri.user and uri.password
325 req.basic_auth(uri.user, uri.password)
327 resp = http.request(req)
328 debug "Checking cache validity of #{u.inspect_no_body} against #{resp.inspect}/#{resp.to_hash.inspect}"
330 when Net::HTTPNotModified
337 error "Failed to check cache validity for #{uri}: #{e.inspect}"
342 # gets a page from the cache if it's still (assumed to be) valid
343 # TODO remove stale cached pages, except when called with noexpire=true
344 def get_cached(uri_or_str, readtimeout=10, opentimeout=5,
345 max_redir=@bot.config['http.max_redir'],
346 noexpire=@bot.config['http.no_expire_cache'])
347 if uri_or_str.kind_of?(URI)
350 uri = URI.parse(uri_or_str.to_s)
352 debug "Getting cached #{uri}"
354 if expired?(uri, readtimeout, opentimeout)
355 debug "Cache expired"
356 bod = get(uri, readtimeout, opentimeout, max_redir, [noexpire])
357 def bod.cached?; false; end
361 @cache[k][:count] += 1
362 @cache[k][:last_use] = Time.now
363 bod = @cache[k][:body]
364 def bod.cached?; true; end
372 # We consider a page to be manually expired if it has no
373 # etag and no last-modified and if any of the expiration
374 # conditions are met (expire_time, max_cache_time, Expires)
375 def manually_expired?(hash, time)
376 auto = hash[:etag].empty? and hash[:last_modified].nil?
378 manual = (time - hash[:last_use] > @bot.config['http.expire_time']*60) or
379 (time - hash[:first_use] > @bot.config['http.max_cache_time']*60) or
380 (hash[:expires] < time)
381 return (auto and manual)
384 def remove_stale_cache
385 debug "Removing stale cache"
386 debug "#{@cache.size} pages before"
389 @cache.reject! { |k, val|
390 manually_expired?(val, now)
393 error "Failed to remove stale cache: #{e.inspect}"
395 debug "#{@cache.size} pages after"