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