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