]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
httputil: work (sub-par) without iconv
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 3 Mar 2012 18:04:34 +0000 (19:04 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 3 Mar 2012 18:04:34 +0000 (19:04 +0100)
lib/rbot/core/utils/httputil.rb

index e69120be8e2cbc533481087b2f9a2936c263fae3..be5c085af7fae312d80105ebf7d738f51c40a73e 100644 (file)
 require 'resolv'
 require 'net/http'
 require 'cgi'
-require 'iconv'
+begin
+  require 'iconv'
+rescue LoadError => e
+  error "Couldn't load 'iconv':  #{e}"
+  error "Non-UTF-8 webpages will not be properly supported"
+end
+
 begin
   require 'net/https'
 rescue LoadError => e
@@ -66,6 +72,7 @@ module ::Net
 
     def body_to_utf(str)
       charsets = self.body_charset(str) or return str
+      return str unless defined? Iconv
 
       charsets.reverse_each do |charset|
         # XXX: this one is really ugly, but i don't know how to make it better
@@ -122,7 +129,7 @@ module ::Net
       when /^(?:iso-8859-\d+|windows-\d+|utf-8|utf8)$/i
         # B0rked servers (Freshmeat being one of them) sometimes return the charset
         # in the content-encoding; in this case we assume that the document has
-        # a standarc content-encoding
+        # a standard content-encoding
         old_hsh = self.to_hash
         self['content-type']= self['content-type']+"; charset="+method.downcase
         warning "Charset vs content-encoding confusion, trying to recover: from\n#{old_hsh.pretty_inspect}to\n#{self.to_hash.pretty_inspect}"