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