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