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