]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/httputil.rb
[url] fixes encoding error in encoding detection code, maybe closes #2
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / httputil.rb
1 # encoding: UTF-8
2 #-- vim:sw=2:et
3 #++
4 #
5 # :title: rbot HTTP provider
6 #
7 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
8 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Author:: Dmitry "jsn" Kim <dmitry point kim at gmail point com>
10
11 require 'resolv'
12 require 'net/http'
13 require 'cgi'
14
15 begin
16   require 'net/https'
17 rescue LoadError => e
18   error "Couldn't load 'net/https':  #{e}"
19   error "Secured HTTP connections will fail"
20   # give a nicer error than "undefined method `use_ssl='"
21   ::Net::HTTP.class_eval <<-EOC
22     define_method :use_ssl= do |val|
23       # does anybody really set it to false?
24       break if !val
25       raise _("I can't do secure HTTP, sorry (%{msg})") % {
26         :msg => e.message
27       }
28     end
29   EOC
30 end
31
32 # To handle Gzipped pages
33 require 'stringio'
34 require 'zlib'
35
36 module ::Net
37   class HTTPResponse
38     attr_accessor :no_cache
39     unless method_defined? :raw_body
40       alias :raw_body :body
41     end
42
43     def body_charset(str=self.raw_body)
44       ctype = self['content-type'] || 'text/html'
45       return nil unless ctype =~ /^text/i || ctype =~ /x(ht)?ml/i
46
47       charsets = ['ISO-8859-1'] # should be in config
48
49       if ctype.match(/charset=["']?([^\s"']+)["']?/i)
50         charsets << $1
51         debug "charset #{charsets.last} added from header"
52       end
53
54       # str might be invalid utf-8 that will crash on the pattern match:
55       str.encode!('UTF-8', 'UTF-8', :invalid => :replace)
56       case str
57       when /<\?xml\s[^>]*encoding=['"]([^\s"'>]+)["'][^>]*\?>/i
58         charsets << $1
59         debug "xml charset #{charsets.last} added from xml pi"
60       when /<(meta\s[^>]*http-equiv=["']?Content-Type["']?[^>]*)>/i
61         meta = $1
62         if meta =~ /charset=['"]?([^\s'";]+)['"]?/
63           charsets << $1
64           debug "html charset #{charsets.last} added from meta"
65         end
66       end
67       return charsets.uniq
68     end
69
70     def body_to_utf(str)
71       charsets = self.body_charset(str) or return str
72
73       charsets.reverse_each do |charset|
74         begin
75           debug "try decoding using #{charset}"
76           str.force_encoding(charset)
77           tmp = str.encode('UTF-16le', :invalid => :replace, :replace => '').encode('UTF-8')
78           if tmp
79             str = tmp
80             break
81           end
82         rescue
83           error 'failed to use encoding'
84           error $!
85         end
86       end
87
88       return str
89     end
90
91     def decompress_body(str)
92       method = self['content-encoding']
93       case method
94       when nil
95         return str
96       when /gzip/ # Matches gzip, x-gzip, and the non-rfc-compliant gzip;q=\d sent by some servers
97         debug "gunzipping body"
98         begin
99           return Zlib::GzipReader.new(StringIO.new(str)).read
100         rescue Zlib::Error => e
101           # If we can't unpack the whole stream (e.g. because we're doing a
102           # partial read
103           debug "full gunzipping failed (#{e}), trying to recover as much as possible"
104           ret = ''
105           ret.force_encoding(Encoding::ASCII_8BIT)
106           begin
107             Zlib::GzipReader.new(StringIO.new(str)).each_byte { |byte|
108               ret << byte
109             }
110           rescue
111           end
112           return ret
113         end
114       when 'deflate'
115         debug "inflating body"
116         # From http://www.koders.com/ruby/fid927B4382397E5115AC0ABE21181AB5C1CBDD5C17.aspx?s=thread:
117         # -MAX_WBITS stops zlib from looking for a zlib header
118         inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
119         begin
120           return inflater.inflate(str)
121         rescue Zlib::Error => e
122           raise e
123           # TODO
124           # debug "full inflation failed (#{e}), trying to recover as much as possible"
125         end
126       when /^(?:iso-8859-\d+|windows-\d+|utf-8|utf8)$/i
127         # B0rked servers (Freshmeat being one of them) sometimes return the charset
128         # in the content-encoding; in this case we assume that the document has
129         # a standard content-encoding
130         old_hsh = self.to_hash
131         self['content-type']= self['content-type']+"; charset="+method.downcase
132         warning "Charset vs content-encoding confusion, trying to recover: from\n#{old_hsh.pretty_inspect}to\n#{self.to_hash.pretty_inspect}"
133         return str
134       else
135         debug self.to_hash
136         raise "Unhandled content encoding #{method}"
137       end
138     end
139
140     def cooked_body
141       return self.body_to_utf(self.decompress_body(self.raw_body))
142     end
143
144     # Read chunks from the body until we have at least _size_ bytes, yielding
145     # the partial text at each chunk. Return the partial body.
146     def partial_body(size=0, &block)
147
148       partial = String.new
149
150       if @read
151         debug "using body() as partial"
152         partial = self.body
153         yield self.body_to_utf(self.decompress_body(partial)) if block_given?
154       else
155         debug "disabling cache"
156         self.no_cache = true
157         self.read_body { |chunk|
158           partial << chunk
159           yield self.body_to_utf(self.decompress_body(partial)) if block_given?
160           break if size and size > 0 and partial.length >= size
161         }
162       end
163
164       return self.body_to_utf(self.decompress_body(partial))
165     end
166   end
167 end
168
169 Net::HTTP.version_1_2
170
171 module ::Irc
172 module Utils
173
174 # class for making http requests easier (mainly for plugins to use)
175 # this class can check the bot proxy configuration to determine if a proxy
176 # needs to be used, which includes support for per-url proxy configuration.
177 class HttpUtil
178     Bot::Config.register Bot::Config::IntegerValue.new('http.read_timeout',
179       :default => 10, :desc => "Default read timeout for HTTP connections")
180     Bot::Config.register Bot::Config::IntegerValue.new('http.open_timeout',
181       :default => 20, :desc => "Default open timeout for HTTP connections")
182     Bot::Config.register Bot::Config::BooleanValue.new('http.use_proxy',
183       :default => false, :desc => "should a proxy be used for HTTP requests?")
184     Bot::Config.register Bot::Config::StringValue.new('http.proxy_uri', :default => false,
185       :desc => "Proxy server to use for HTTP requests (URI, e.g http://proxy.host:port)")
186     Bot::Config.register Bot::Config::StringValue.new('http.proxy_user',
187       :default => nil,
188       :desc => "User for authenticating with the http proxy (if required)")
189     Bot::Config.register Bot::Config::StringValue.new('http.proxy_pass',
190       :default => nil,
191       :desc => "Password for authenticating with the http proxy (if required)")
192     Bot::Config.register Bot::Config::ArrayValue.new('http.proxy_include',
193       :default => [],
194       :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")
195     Bot::Config.register Bot::Config::ArrayValue.new('http.proxy_exclude',
196       :default => [],
197       :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")
198     Bot::Config.register Bot::Config::IntegerValue.new('http.max_redir',
199       :default => 5,
200       :desc => "Maximum number of redirections to be used when getting a document")
201     Bot::Config.register Bot::Config::IntegerValue.new('http.expire_time',
202       :default => 60,
203       :desc => "After how many minutes since last use a cached document is considered to be expired")
204     Bot::Config.register Bot::Config::IntegerValue.new('http.max_cache_time',
205       :default => 60*24,
206       :desc => "After how many minutes since first use a cached document is considered to be expired")
207     Bot::Config.register Bot::Config::BooleanValue.new('http.no_expire_cache',
208       :default => false,
209       :desc => "Set this to true if you want the bot to never expire the cached pages")
210     Bot::Config.register Bot::Config::IntegerValue.new('http.info_bytes',
211       :default => 8192,
212       :desc => "How many bytes to download from a web page to find some information. Set to 0 to let the bot download the whole page.")
213
214   class CachedObject
215     attr_accessor :response, :last_used, :first_used, :count, :expires, :date
216
217     def self.maybe_new(resp)
218       debug "maybe new #{resp}"
219       return nil if resp.no_cache
220       return nil unless Net::HTTPOK === resp ||
221       Net::HTTPMovedPermanently === resp ||
222       Net::HTTPFound === resp ||
223       Net::HTTPPartialContent === resp
224
225       cc = resp['cache-control']
226       return nil if cc && (cc =~ /no-cache/i)
227
228       date = Time.now
229       if d = resp['date']
230         date = Time.httpdate(d)
231       end
232
233       return nil if resp['expires'] && (Time.httpdate(resp['expires']) < date)
234
235       debug "creating cache obj"
236
237       self.new(resp)
238     end
239
240     def use
241       now = Time.now
242       @first_used = now if @count == 0
243       @last_used = now
244       @count += 1
245     end
246
247     def expired?
248       debug "checking expired?"
249       if cc = self.response['cache-control'] && cc =~ /must-revalidate/
250         return true
251       end
252       return self.expires < Time.now
253     end
254
255     def setup_headers(hdr)
256       hdr['if-modified-since'] = self.date.rfc2822
257
258       debug "ims == #{hdr['if-modified-since']}"
259
260       if etag = self.response['etag']
261         hdr['if-none-match'] = etag
262         debug "etag: #{etag}"
263       end
264     end
265
266     def revalidate(resp = self.response)
267       @count = 0
268       self.use
269       self.date = resp.key?('date') ? Time.httpdate(resp['date']) : Time.now
270
271       cc = resp['cache-control']
272       if cc && (cc =~ /max-age=(\d+)/)
273         self.expires = self.date + $1.to_i
274       elsif resp.key?('expires')
275         self.expires = Time.httpdate(resp['expires'])
276       elsif lm = resp['last-modified']
277         delta = self.date - Time.httpdate(lm)
278         delta = 10 if delta <= 0
279         delta /= 5
280         self.expires = self.date + delta
281       else
282         self.expires = self.date + 300
283       end
284       # self.expires = Time.now + 10 # DEBUG
285       debug "expires on #{self.expires}"
286
287       return true
288     end
289
290     private
291     def initialize(resp)
292       @response = resp
293       begin
294         self.revalidate
295         self.response.raw_body
296       rescue Exception => e
297         error e
298         raise e
299       end
300     end
301   end
302
303   # Create the HttpUtil instance, associating it with Bot _bot_
304   #
305   def initialize(bot)
306     @bot = bot
307     @cache = Hash.new
308     @headers = {
309       'Accept-Charset' => 'utf-8;q=1.0, *;q=0.8',
310       'Accept-Encoding' => 'gzip;q=1, deflate;q=1, identity;q=0.8, *;q=0.2',
311       'User-Agent' =>
312         "rbot http util #{$version} (#{Irc::Bot::SOURCE_URL})"
313     }
314     debug "starting http cache cleanup timer"
315     @timer = @bot.timer.add(300) {
316       self.remove_stale_cache unless @bot.config['http.no_expire_cache']
317     }
318   end
319
320   # Clean up on HttpUtil unloading, by stopping the cache cleanup timer.
321   def cleanup
322     debug 'stopping http cache cleanup timer'
323     @bot.timer.remove(@timer)
324   end
325
326   # This method checks if a proxy is required to access _uri_, by looking at
327   # the values of config values +http.proxy_include+ and +http.proxy_exclude+.
328   #
329   # Each of these config values, if set, should be a Regexp the server name and
330   # IP address should be checked against.
331   #
332   def proxy_required(uri)
333     use_proxy = true
334     if @bot.config["http.proxy_exclude"].empty? && @bot.config["http.proxy_include"].empty?
335       return use_proxy
336     end
337
338     list = [uri.host]
339     begin
340       list.concat Resolv.getaddresses(uri.host)
341     rescue StandardError => err
342       warning "couldn't resolve host uri.host"
343     end
344
345     unless @bot.config["http.proxy_exclude"].empty?
346       re = @bot.config["http.proxy_exclude"].collect{|r| Regexp.new(r)}
347       re.each do |r|
348         list.each do |item|
349           if r.match(item)
350             use_proxy = false
351             break
352           end
353         end
354       end
355     end
356     unless @bot.config["http.proxy_include"].empty?
357       re = @bot.config["http.proxy_include"].collect{|r| Regexp.new(r)}
358       re.each do |r|
359         list.each do |item|
360           if r.match(item)
361             use_proxy = true
362             break
363           end
364         end
365       end
366     end
367     debug "using proxy for uri #{uri}?: #{use_proxy}"
368     return use_proxy
369   end
370
371   # _uri_:: URI to create a proxy for
372   #
373   # Return a net/http Proxy object, configured for proxying based on the
374   # bot's proxy configuration. See proxy_required for more details on this.
375   #
376   def get_proxy(uri, options = {})
377     opts = {
378       :read_timeout => @bot.config["http.read_timeout"],
379       :open_timeout => @bot.config["http.open_timeout"]
380     }.merge(options)
381
382     proxy = nil
383     proxy_host = nil
384     proxy_port = nil
385     proxy_user = nil
386     proxy_pass = nil
387
388     if @bot.config["http.use_proxy"]
389       if (ENV['http_proxy'])
390         proxy = URI.parse ENV['http_proxy'] rescue nil
391       end
392       if (@bot.config["http.proxy_uri"])
393         proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil
394       end
395       if proxy
396         debug "proxy is set to #{proxy.host} port #{proxy.port}"
397         if proxy_required(uri)
398           proxy_host = proxy.host
399           proxy_port = proxy.port
400           proxy_user = @bot.config["http.proxy_user"]
401           proxy_pass = @bot.config["http.proxy_pass"]
402         end
403       end
404     end
405
406     h = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass)
407     h.use_ssl = true if uri.scheme == "https"
408
409     h.read_timeout = opts[:read_timeout]
410     h.open_timeout = opts[:open_timeout]
411     return h
412   end
413
414   # Internal method used to hanlde response _resp_ received when making a
415   # request for URI _uri_.
416   #
417   # It follows redirects, optionally yielding them if option :yield is :all.
418   #
419   # Also yields and returns the final _resp_.
420   #
421   def handle_response(uri, resp, opts, &block) # :yields: resp
422     if Net::HTTPRedirection === resp && opts[:max_redir] >= 0
423       if resp.key?('location')
424         raise 'Too many redirections' if opts[:max_redir] <= 0
425         yield resp if opts[:yield] == :all && block_given?
426         # some servers actually provide unescaped location, e.g.
427         # http://ulysses.soup.io/post/60734021/Image%20curve%20ball
428         # rediects to something like
429         # http://ulysses.soup.io/post/60734021/Image curve ball?sessid=8457b2a3752085cca3fb1d79b9965446
430         # causing the URI parser to (obviously) complain. We cannot just
431         # escape blindly, as this would make a mess of already-escaped
432         # locations, so we only do it if the URI.parse fails
433         loc = resp['location']
434         escaped = false
435         debug "redirect location: #{loc.inspect}"
436         begin
437           new_loc = URI.join(uri.to_s, loc) rescue URI.parse(loc)
438         rescue
439           if escaped
440             raise $!
441           else
442             loc = URI.escape(loc)
443             escaped = true
444             debug "escaped redirect location: #{loc.inspect}"
445             retry
446           end
447         end
448         new_opts = opts.dup
449         new_opts[:max_redir] -= 1
450         case opts[:method].to_s.downcase.intern
451         when :post, :"net::http::post"
452           new_opts[:method] = :get
453         end
454         if resp['set-cookie']
455           debug "set cookie request for #{resp['set-cookie']}"
456           cookie, cookie_flags = (resp['set-cookie']+'; ').split('; ', 2)
457           domain = uri.host
458           cookie_flags.scan(/(\S+)=(\S+);/) { |key, val|
459             if key.intern == :domain
460               domain = val
461               break
462             end
463           }
464           debug "cookie domain #{domain} / #{new_loc.host}"
465           if new_loc.host.rindex(domain) == new_loc.host.length - domain.length
466             debug "setting cookie"
467             new_opts[:headers] ||= Hash.new
468             new_opts[:headers]['Cookie'] = cookie
469           else
470             debug "cookie is for another domain, ignoring"
471           end
472         end
473         debug "following the redirect to #{new_loc}"
474         return get_response(new_loc, new_opts, &block)
475       else
476         warning ":| redirect w/o location?"
477       end
478     end
479     class << resp
480       undef_method :body
481       alias :body :cooked_body
482     end
483     unless resp['content-type']
484       debug "No content type, guessing"
485       resp['content-type'] =
486         case resp['x-rbot-location']
487         when /.html?$/i
488           'text/html'
489         when /.xml$/i
490           'application/xml'
491         when /.xhtml$/i
492           'application/xml+xhtml'
493         when /.(gif|png|jpe?g|jp2|tiff?)$/i
494           "image/#{$1.sub(/^jpg$/,'jpeg').sub(/^tif$/,'tiff')}"
495         else
496           'application/octetstream'
497         end
498     end
499     if block_given?
500       yield(resp)
501     else
502       # Net::HTTP wants us to read the whole body here
503       resp.raw_body
504     end
505     return resp
506   end
507
508   # _uri_::     uri to query (URI object or String)
509   #
510   # Generic http transaction method. It will return a Net::HTTPResponse
511   # object or raise an exception
512   #
513   # If a block is given, it will yield the response (see :yield option)
514   #
515   # Currently supported _options_:
516   #
517   # method::     request method [:get (default), :post or :head]
518   # open_timeout::     open timeout for the proxy
519   # read_timeout::     read timeout for the proxy
520   # cache::            should we cache results?
521   # yield::      if :final [default], calls the block for the response object;
522   #              if :all, call the block for all intermediate redirects, too
523   # max_redir::  how many redirects to follow before raising the exception
524   #              if -1, don't follow redirects, just return them
525   # range::      make a ranged request (usually GET). accepts a string
526   #              for HTTP/1.1 "Range:" header (i.e. "bytes=0-1000")
527   # body::       request body (usually for POST requests)
528   # headers::    additional headers to be set for the request. Its value must
529   #              be a Hash in the form { 'Header' => 'value' }
530   #
531   def get_response(uri_or_s, options = {}, &block) # :yields: resp
532     uri = uri_or_s.kind_of?(URI) ? uri_or_s : URI.parse(uri_or_s.to_s)
533     unless URI::HTTP === uri
534       if uri.scheme
535         raise "#{uri.scheme.inspect} URI scheme is not supported"
536       else
537         raise "don't know what to do with #{uri.to_s.inspect}"
538       end
539     end
540
541     opts = {
542       :max_redir => @bot.config['http.max_redir'],
543       :yield => :final,
544       :cache => true,
545       :method => :GET
546     }.merge(options)
547
548     req_class = case opts[:method].to_s.downcase.intern
549                 when :head, :"net::http::head"
550                   opts[:max_redir] = -1
551                   Net::HTTP::Head
552                 when :get, :"net::http::get"
553                   Net::HTTP::Get
554                 when :post, :"net::http::post"
555                   opts[:cache] = false
556                   opts[:body] or raise 'post request w/o a body?'
557                   warning "refusing to cache POST request" if options[:cache]
558                   Net::HTTP::Post
559                 else
560                   warning "unsupported method #{opts[:method]}, doing GET"
561                   Net::HTTP::Get
562                 end
563
564     if req_class != Net::HTTP::Get && opts[:range]
565       warning "can't request ranges for #{req_class}"
566       opts.delete(:range)
567     end
568
569     cache_key = "#{opts[:range]}|#{req_class}|#{uri.to_s}"
570
571     if req_class != Net::HTTP::Get && req_class != Net::HTTP::Head
572       if opts[:cache]
573         warning "can't cache #{req_class.inspect} requests, working w/o cache"
574         opts[:cache] = false
575       end
576     end
577
578     debug "get_response(#{uri}, #{opts.inspect})"
579
580     cached = @cache[cache_key]
581
582     if opts[:cache] && cached
583       debug "got cached"
584       if !cached.expired?
585         debug "using cached"
586         cached.use
587         return handle_response(uri, cached.response, opts, &block)
588       end
589     end
590
591     headers = @headers.dup.merge(opts[:headers] || {})
592     headers['Range'] = opts[:range] if opts[:range]
593     headers['Authorization'] = opts[:auth_head] if opts[:auth_head]
594
595     if opts[:cache] && cached && (req_class == Net::HTTP::Get)
596       cached.setup_headers headers
597     end
598
599     req = req_class.new(uri.request_uri, headers)
600     if uri.user && uri.password
601       req.basic_auth(uri.user, uri.password)
602       opts[:auth_head] = req['Authorization']
603     end
604     req.body = opts[:body] if req_class == Net::HTTP::Post
605     debug "prepared request: #{req.to_hash.inspect}"
606
607     begin
608       get_proxy(uri, opts).start do |http|
609         http.request(req) do |resp|
610           resp['x-rbot-location'] = uri.to_s
611           if Net::HTTPNotModified === resp
612             debug "not modified"
613             begin
614               cached.revalidate(resp)
615             rescue Exception => e
616               error e
617             end
618             debug "reusing cached"
619             resp = cached.response
620           elsif Net::HTTPServerError === resp || Net::HTTPClientError === resp
621             debug "http error, deleting cached obj" if cached
622             @cache.delete(cache_key)
623           end
624
625           begin
626             return handle_response(uri, resp, opts, &block)
627           ensure
628             if cached = CachedObject.maybe_new(resp) rescue nil
629               debug "storing to cache"
630               @cache[cache_key] = cached
631             end
632           end
633         end
634       end
635     rescue Exception => e
636       error e
637       raise e.message
638     end
639   end
640
641   # _uri_::     uri to query (URI object or String)
642   #
643   # Simple GET request, returns (if possible) response body following redirs
644   # and caching if requested, yielding the actual response(s) to the optional
645   # block. See get_response for details on the supported _options_
646   #
647   def get(uri, options = {}, &block) # :yields: resp
648     begin
649       resp = get_response(uri, options, &block)
650       raise "http error: #{resp}" unless Net::HTTPOK === resp ||
651         Net::HTTPPartialContent === resp
652       return resp.body
653     rescue Exception => e
654       error e
655     end
656     return nil
657   end
658
659   # _uri_::     uri to query (URI object or String)
660   #
661   # Simple HEAD request, returns (if possible) response head following redirs
662   # and caching if requested, yielding the actual response(s) to the optional
663   # block. See get_response for details on the supported _options_
664   #
665   def head(uri, options = {}, &block) # :yields: resp
666     opts = {:method => :head}.merge(options)
667     begin
668       resp = get_response(uri, opts, &block)
669       # raise "http error #{resp}" if Net::HTTPClientError === resp ||
670       #   Net::HTTPServerError == resp
671       return resp
672     rescue Exception => e
673       error e
674     end
675     return nil
676   end
677
678   # _uri_::     uri to query (URI object or String)
679   # _data_::    body of the POST
680   #
681   # Simple POST request, returns (if possible) response following redirs and
682   # caching if requested, yielding the response(s) to the optional block. See
683   # get_response for details on the supported _options_
684   #
685   def post(uri, data, options = {}, &block) # :yields: resp
686     opts = {:method => :post, :body => data, :cache => false}.merge(options)
687     begin
688       resp = get_response(uri, opts, &block)
689       raise 'http error' unless Net::HTTPOK === resp or Net::HTTPCreated === resp
690       return resp
691     rescue Exception => e
692       error e
693     end
694     return nil
695   end
696
697   # _uri_::     uri to query (URI object or String)
698   # _nbytes_::  number of bytes to get
699   #
700   # Partial GET request, returns (if possible) the first _nbytes_ bytes of the
701   # response body, following redirs and caching if requested, yielding the
702   # actual response(s) to the optional block. See get_response for details on
703   # the supported _options_
704   #
705   def get_partial(uri, nbytes = @bot.config['http.info_bytes'], options = {}, &block) # :yields: resp
706     opts = {:range => "bytes=0-#{nbytes}"}.merge(options)
707     return get(uri, opts, &block)
708   end
709
710   def remove_stale_cache
711     debug "Removing stale cache"
712     now = Time.new
713     max_last = @bot.config['http.expire_time'] * 60
714     max_first = @bot.config['http.max_cache_time'] * 60
715     debug "#{@cache.size} pages before"
716     begin
717       @cache.reject! { |k, val|
718         (now - val.last_used > max_last) || (now - val.first_used > max_first)
719       }
720     rescue => e
721       error "Failed to remove stale cache: #{e.pretty_inspect}"
722     end
723     debug "#{@cache.size} pages after"
724   end
725
726 end
727 end
728 end
729
730 class HttpUtilPlugin < CoreBotModule
731   def initialize(*a)
732     super(*a)
733     debug 'initializing httputil'
734     @bot.httputil = Irc::Utils::HttpUtil.new(@bot)
735   end
736
737   def cleanup
738     debug 'shutting down httputil'
739     @bot.httputil.cleanup
740     @bot.httputil = nil
741     super
742   end
743 end
744
745 HttpUtilPlugin.new