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