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