]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/httputil.rb
HttpUtil: inform servers that we accept gzipped data
[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 'iconv'
17 begin
18   require 'net/https'
19 rescue LoadError => e
20   error "Couldn't load 'net/https':  #{e.inspect}"
21   error "Secured HTTP connections will fail"
22 end
23
24 # To handle Gzipped pages
25 require 'stringio'
26 require 'zlib'
27
28 module ::Net 
29   class HTTPResponse 
30     attr_accessor :no_cache 
31     if !instance_methods.include?('raw_body')
32       alias :raw_body :body
33     end
34
35     def body_charset(str=self.raw_body)
36       ctype = self['content-type'] || 'text/html'
37       return nil unless ctype =~ /^text/i || ctype =~ /x(ht)?ml/i
38
39       charsets = ['latin1'] # should be in config
40
41       if self['content-type'].match(/charset=["']?([^\s"']+)["']?/i)
42         charsets << $1
43         debug "charset #{charsets.last} added from header"
44       end
45
46       case str
47       when /<\?xml\s[^>]*encoding=['"]([^\s"'>]+)["'][^>]*\?>/i
48         charsets << $1
49         debug "xml charset #{charsets.last} added from xml pi"
50       when /<(meta\s[^>]*http-equiv=["']?Content-Type["']?[^>]*)>/i
51         meta = $1
52         if meta =~ /charset=['"]?([^\s'";]+)['"]?/
53           charsets << $1
54           debug "html charset #{charsets.last} added from meta"
55         end
56       end
57       return charsets.uniq
58     end
59
60     def body_to_utf(str)
61       charsets = self.body_charset(str) or return str
62
63       charsets.reverse_each { |charset|
64         begin
65           return Iconv.iconv('utf-8//ignore', charset, str).first
66         rescue
67           debug "conversion failed for #{charset}"
68         end
69       }
70       return str
71     end
72
73     def decompress_body(str)
74       method = self['content-encoding']
75       case method
76       when nil
77         return str
78       when 'gzip', 'x-gzip'
79         debug "gunzipping body"
80         return Zlib::GzipReader.new(StringIO.new(str)).read
81       else
82         raise "Unhandled content encoding #{method}"
83       end
84     end
85
86     def body
87       return self.body_to_utf(self.decompress_body(self.raw_body))
88     end
89
90     # Read chunks from the body until we have at least _size_ bytes, yielding 
91     # the partial text at each chunk. Return the partial body. 
92     def partial_body(size=0, &block) 
93
94       self.no_cache = true
95       partial = String.new 
96
97       self.read_body { |chunk| 
98         partial << chunk 
99         yield self.body_to_utf(partial) if block_given? 
100         break if size and size > 0 and partial.length >= size 
101       } 
102
103       return self.body_to_utf(partial)
104     end 
105   end 
106 end
107
108 Net::HTTP.version_1_2
109
110 module ::Irc
111 module Utils
112
113 # class for making http requests easier (mainly for plugins to use)
114 # this class can check the bot proxy configuration to determine if a proxy
115 # needs to be used, which includes support for per-url proxy configuration.
116 class HttpUtil
117     BotConfig.register BotConfigBooleanValue.new('http.use_proxy',
118       :default => false, :desc => "should a proxy be used for HTTP requests?")
119     BotConfig.register BotConfigStringValue.new('http.proxy_uri', :default => false,
120       :desc => "Proxy server to use for HTTP requests (URI, e.g http://proxy.host:port)")
121     BotConfig.register BotConfigStringValue.new('http.proxy_user',
122       :default => nil,
123       :desc => "User for authenticating with the http proxy (if required)")
124     BotConfig.register BotConfigStringValue.new('http.proxy_pass',
125       :default => nil,
126       :desc => "Password for authenticating with the http proxy (if required)")
127     BotConfig.register BotConfigArrayValue.new('http.proxy_include',
128       :default => [],
129       :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")
130     BotConfig.register BotConfigArrayValue.new('http.proxy_exclude',
131       :default => [],
132       :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")
133     BotConfig.register BotConfigIntegerValue.new('http.max_redir',
134       :default => 5,
135       :desc => "Maximum number of redirections to be used when getting a document")
136     BotConfig.register BotConfigIntegerValue.new('http.expire_time',
137       :default => 60,
138       :desc => "After how many minutes since last use a cached document is considered to be expired")
139     BotConfig.register BotConfigIntegerValue.new('http.max_cache_time',
140       :default => 60*24,
141       :desc => "After how many minutes since first use a cached document is considered to be expired")
142     BotConfig.register BotConfigIntegerValue.new('http.no_expire_cache',
143       :default => false,
144       :desc => "Set this to true if you want the bot to never expire the cached pages")
145     BotConfig.register BotConfigIntegerValue.new('http.info_bytes',
146       :default => 8192,
147       :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.")
148
149   class CachedObject
150     attr_accessor :response, :last_used, :first_used, :count, :expires, :date
151
152     def self.maybe_new(resp)
153       debug "maybe new #{resp}"
154       return nil if resp.no_cache
155       return nil unless Net::HTTPOK === resp ||
156       Net::HTTPMovedPermanently === resp ||
157       Net::HTTPFound === resp ||
158       Net::HTTPPartialContent === resp
159
160       cc = resp['cache-control']
161       return nil if cc && (cc =~ /no-cache/i)
162
163       date = Time.now
164       if d = resp['date']
165         date = Time.httpdate(d)
166       end
167
168       return nil if resp['expires'] && (Time.httpdate(resp['expires']) < date)
169
170       debug "creating cache obj"
171
172       self.new(resp)
173     end
174
175     def use
176       now = Time.now
177       @first_used = now if @count == 0
178       @last_used = now
179       @count += 1
180     end
181
182     def expired?
183       debug "checking expired?"
184       if cc = self.response['cache-control'] && cc =~ /must-revalidate/
185         return true
186       end
187       return self.expires < Time.now
188     end
189
190     def setup_headers(hdr)
191       hdr['if-modified-since'] = self.date.rfc2822
192
193       debug "ims == #{hdr['if-modified-since']}"
194
195       if etag = self.response['etag']
196         hdr['if-none-match'] = etag
197         debug "etag: #{etag}"
198       end
199     end
200
201     def revalidate(resp = self.response)
202       @count = 0
203       self.use
204       self.date = resp.key?('date') ? Time.httpdate(resp['date']) : Time.now
205
206       cc = resp['cache-control']
207       if cc && (cc =~ /max-age=(\d+)/)
208         self.expires = self.date + $1.to_i
209       elsif resp.key?('expires')
210         self.expires = Time.httpdate(resp['expires'])
211       elsif lm = resp['last-modified']
212         delta = self.date - Time.httpdate(lm)
213         delta = 10 if delta <= 0
214         delta /= 5
215         self.expires = self.date + delta
216       else
217         self.expires = self.date + 300
218       end
219       # self.expires = Time.now + 10 # DEBUG
220       debug "expires on #{self.expires}"
221
222       return true
223     end
224
225     private
226     def initialize(resp)
227       @response = resp
228       begin
229         self.revalidate
230         self.response.raw_body
231       rescue Exception => e
232         error e.message
233         error e.backtrace.join("\n")
234         raise e
235       end
236     end
237   end
238
239   def initialize(bot)
240     @bot = bot
241     @cache = Hash.new
242     @headers = {
243       'Accept-Charset' => 'utf-8;q=1.0, *;q=0.8',
244       'Accept-Encoding' => 'gzip;q=1, identity;q=0.8, *;q=0.2',
245       'User-Agent' =>
246         "rbot http util #{$version} (http://linuxbrit.co.uk/rbot/)"
247     } 
248     debug "starting http cache cleanup timer"
249     @timer = @bot.timer.add(300) {
250       self.remove_stale_cache unless @bot.config['http.no_expire_cache']
251     }
252   end 
253
254   def cleanup
255     debug 'stopping http cache cleanup timer'
256     @bot.timer.remove(@timer)
257   end
258
259   # if http_proxy_include or http_proxy_exclude are set, then examine the
260   # uri to see if this is a proxied uri
261   # the in/excludes are a list of regexps, and each regexp is checked against
262   # the server name, and its IP addresses
263   def proxy_required(uri)
264     use_proxy = true
265     if @bot.config["http.proxy_exclude"].empty? && @bot.config["http.proxy_include"].empty?
266       return use_proxy
267     end
268
269     list = [uri.host]
270     begin
271       list.concat Resolv.getaddresses(uri.host)
272     rescue StandardError => err
273       warning "couldn't resolve host uri.host"
274     end
275
276     unless @bot.config["http.proxy_exclude"].empty?
277       re = @bot.config["http.proxy_exclude"].collect{|r| Regexp.new(r)}
278       re.each do |r|
279         list.each do |item|
280           if r.match(item)
281             use_proxy = false
282             break
283           end
284         end
285       end
286     end
287     unless @bot.config["http.proxy_include"].empty?
288       re = @bot.config["http.proxy_include"].collect{|r| Regexp.new(r)}
289       re.each do |r|
290         list.each do |item|
291           if r.match(item)
292             use_proxy = true
293             break
294           end
295         end
296       end
297     end
298     debug "using proxy for uri #{uri}?: #{use_proxy}"
299     return use_proxy
300   end
301
302   # uri:: Uri to create a proxy for
303   #
304   # return a net/http Proxy object, which is configured correctly for
305   # proxying based on the bot's proxy configuration.
306   # This will include per-url proxy configuration based on the bot config
307   # +http_proxy_include/exclude+ options.
308   
309   def get_proxy(uri, options = {})
310     opts = {
311       :read_timeout => 10,
312       :open_timeout => 5
313     }.merge(options)
314
315     proxy = nil
316     proxy_host = nil
317     proxy_port = nil
318     proxy_user = nil
319     proxy_pass = nil
320
321     if @bot.config["http.use_proxy"]
322       if (ENV['http_proxy'])
323         proxy = URI.parse ENV['http_proxy'] rescue nil
324       end
325       if (@bot.config["http.proxy_uri"])
326         proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil
327       end
328       if proxy
329         debug "proxy is set to #{proxy.host} port #{proxy.port}"
330         if proxy_required(uri)
331           proxy_host = proxy.host
332           proxy_port = proxy.port
333           proxy_user = @bot.config["http.proxy_user"]
334           proxy_pass = @bot.config["http.proxy_pass"]
335         end
336       end
337     end
338
339     h = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
340     h.use_ssl = true if uri.scheme == "https"
341
342     h.read_timeout = opts[:read_timeout]
343     h.open_timeout = opts[:open_timeout]
344     return h
345   end
346
347   def handle_response(uri, resp, opts, &block)
348     if Net::HTTPRedirection === resp && opts[:max_redir] >= 0
349       if resp.key?('location')
350         raise 'Too many redirections' if opts[:max_redir] <= 0
351         yield resp if opts[:yield] == :all && block_given?
352         loc = resp['location']
353         new_loc = URI.join(uri.to_s, loc) rescue URI.parse(loc)
354         new_opts = opts.dup
355         new_opts[:max_redir] -= 1
356         case opts[:method].to_s.downcase.intern
357         when :post, :"net::http::post"
358           new_opts[:method] = :get
359         end
360         debug "following the redirect to #{new_loc}"
361         return get_response(new_loc, new_opts, &block)
362       else
363         warning ":| redirect w/o location?"
364       end
365     end
366     if block_given?
367       yield(resp)
368     else
369       # Net::HTTP wants us to read the whole body here
370       resp.raw_body
371     end
372     return resp
373   end
374
375   # uri::         uri to query (Uri object or String)
376   # opts::        options. Currently used:
377   # :method::     request method [:get (default), :post or :head]
378   # :open_timeout::     open timeout for the proxy
379   # :read_timeout::     read timeout for the proxy
380   # :cache::            should we cache results?
381   # :yield::      if :final [default], call &block for the response object
382   #               if :all, call &block for all intermediate redirects, too
383   # :max_redir::  how many redirects to follow before raising the exception
384   #               if -1, don't follow redirects, just return them
385   # :range::      make a ranged request (usually GET). accepts a string
386   #               for HTTP/1.1 "Range:" header (i.e. "bytes=0-1000")
387   # :body::       request body (usually for POST requests)
388   #
389   # Generic http transaction method
390   #
391   # It will return a HTTP::Response object or raise an exception
392   #
393   # If a block is given, it will yield the response (see :yield option)
394
395   def get_response(uri_or_s, options = {}, &block)
396     uri = uri_or_s.kind_of?(URI) ? uri_or_s : URI.parse(uri_or_s.to_s)
397     opts = {
398       :max_redir => @bot.config['http.max_redir'],
399       :yield => :final,
400       :cache => true,
401       :method => :GET
402     }.merge(options)
403
404     resp = nil
405     cached = nil
406
407     req_class = case opts[:method].to_s.downcase.intern
408                 when :head, :"net::http::head"
409                   opts[:max_redir] = -1
410                   Net::HTTP::Head
411                 when :get, :"net::http::get"
412                   Net::HTTP::Get
413                 when :post, :"net::http::post"
414                   opts[:cache] = false
415                   opts[:body] or raise 'post request w/o a body?'
416                   warning "refusing to cache POST request" if options[:cache]
417                   Net::HTTP::Post
418                 else
419                   warning "unsupported method #{opts[:method]}, doing GET"
420                   Net::HTTP::Get
421                 end
422
423     if req_class != Net::HTTP::Get && opts[:range]
424       warning "can't request ranges for #{req_class}"
425       opts.delete(:range)
426     end
427
428     cache_key = "#{opts[:range]}|#{req_class}|#{uri.to_s}"
429
430     if req_class != Net::HTTP::Get && req_class != Net::HTTP::Head
431       if opts[:cache]
432         warning "can't cache #{req_class.inspect} requests, working w/o cache"
433         opts[:cache] = false
434       end
435     end
436
437     debug "get_response(#{uri}, #{opts.inspect})"
438
439     if opts[:cache] && cached = @cache[cache_key]
440       debug "got cached"
441       if !cached.expired?
442         debug "using cached"
443         cached.use
444         return handle_response(uri, cached.response, opts, &block)
445       end
446     end
447     
448     headers = @headers.dup.merge(opts[:headers] || {})
449     headers['Range'] = opts[:range] if opts[:range]
450
451     cached.setup_headers(headers) if cached && (req_class == Net::HTTP::Get)
452     req = req_class.new(uri.request_uri, headers)
453     req.basic_auth(uri.user, uri.password) if uri.user && uri.password
454     req.body = opts[:body] if req_class == Net::HTTP::Post
455     debug "prepared request: #{req.to_hash.inspect}"
456
457     get_proxy(uri, opts).start do |http|
458       http.request(req) do |resp|
459         resp['x-rbot-location'] = uri.to_s
460         if Net::HTTPNotModified === resp
461           debug "not modified"
462           begin
463             cached.revalidate(resp)
464           rescue Exception => e
465             error e.message
466             error e.backtrace.join("\n")
467           end
468           debug "reusing cached"
469           resp = cached.response
470         elsif Net::HTTPServerError === resp || Net::HTTPClientError === resp
471           debug "http error, deleting cached obj" if cached
472           @cache.delete(cache_key)
473         elsif opts[:cache]
474           begin
475             return handle_response(uri, resp, opts, &block)
476           ensure
477             if cached = CachedObject.maybe_new(resp) rescue nil
478               debug "storing to cache"
479               @cache[cache_key] = cached
480             end
481           end
482           return ret
483         end
484         return handle_response(uri, resp, opts, &block)
485       end
486     end
487   end
488
489   # uri::         uri to query (Uri object)
490   #
491   # simple get request, returns (if possible) response body following redirs
492   # and caching if requested
493   def get(uri, opts = {}, &block)
494     begin
495       resp = get_response(uri, opts, &block)
496       raise "http error: #{resp}" unless Net::HTTPOK === resp ||
497         Net::HTTPPartialContent === resp
498       return resp.body
499     rescue Exception => e
500       error e.message
501       error e.backtrace.join("\n")
502     end
503     return nil
504   end
505
506   def head(uri, options = {}, &block)
507     opts = {:method => :head}.merge(options)
508     begin
509       resp = get_response(uri, opts, &block)
510       raise "http error #{resp}" if Net::HTTPClientError === resp ||
511         Net::HTTPServerError == resp
512       return resp
513     rescue Exception => e
514       error e.message
515       error e.backtrace.join("\n")
516     end
517     return nil
518   end
519
520   def post(uri, data, options = {}, &block)
521     opts = {:method => :post, :body => data, :cache => false}.merge(options)
522     begin
523       resp = get_response(uri, opts, &block)
524       raise 'http error' unless Net::HTTPOK === resp
525       return resp
526     rescue Exception => e
527       error e.message
528       error e.backtrace.join("\n")
529     end
530     return nil
531   end
532
533   def get_partial(uri, nbytes = @bot.config['http.info_bytes'], options = {}, &block)
534     opts = {:range => "bytes=0-#{nbytes}"}.merge(options)
535     return get(uri, opts, &block)
536   end
537
538   def remove_stale_cache
539     debug "Removing stale cache"
540     now = Time.new
541     max_last = @bot.config['http.expire_time'] * 60
542     max_first = @bot.config['http.max_cache_time'] * 60
543     debug "#{@cache.size} pages before"
544     begin
545       @cache.reject! { |k, val|
546         (now - val.last_used > max_last) || (now - val.first_used > max_first)
547       }
548     rescue => e
549       error "Failed to remove stale cache: #{e.inspect}"
550     end
551     debug "#{@cache.size} pages after"
552   end
553
554 end
555 end
556 end
557
558 class HttpUtilPlugin < CoreBotModule
559   def initialize(*a)
560     super(*a)
561     debug 'initializing httputil'
562     @bot.httputil = Irc::Utils::HttpUtil.new(@bot)
563   end
564
565   def cleanup
566     debug 'shutting down httputil'
567     @bot.httputil.cleanup
568     @bot.httputil = nil
569   end
570 end
571
572 HttpUtilPlugin.new