]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/httputil.rb
httputil: remove obsolete version_1_2 declaration
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / httputil.rb
index 1b4c35269a1f44c89707f8b47a82330544aa37fe..fb275547169efefce78632b9eb62d3efc0daf39c 100644 (file)
@@ -13,20 +13,9 @@ require 'net/http'
 require 'cgi'
 
 begin
-  require 'net/https'
+  require 'nokogiri'
 rescue LoadError => e
-  error "Couldn't load 'net/https':  #{e}"
-  error "Secured HTTP connections will fail"
-  # give a nicer error than "undefined method `use_ssl='"
-  ::Net::HTTP.class_eval <<-EOC
-    define_method :use_ssl= do |val|
-      # does anybody really set it to false?
-      break if !val
-      raise _("I can't do secure HTTP, sorry (%{msg})") % {
-        :msg => e.message
-      }
-    end
-  EOC
+  error "No nokogiri library found, some features might not be available!"
 end
 
 # To handle Gzipped pages
@@ -44,13 +33,15 @@ module ::Net
       ctype = self['content-type'] || 'text/html'
       return nil unless ctype =~ /^text/i || ctype =~ /x(ht)?ml/i
 
-      charsets = ['latin1'] # should be in config
+      charsets = ['ISO-8859-1'] # should be in config
 
       if ctype.match(/charset=["']?([^\s"']+)["']?/i)
         charsets << $1
         debug "charset #{charsets.last} added from header"
       end
 
+      # str might be invalid utf-8 that will crash on the pattern match:
+      str.encode!('UTF-8', 'UTF-8', :invalid => :replace)
       case str
       when /<\?xml\s[^>]*encoding=['"]([^\s"'>]+)["'][^>]*\?>/i
         charsets << $1
@@ -72,7 +63,7 @@ module ::Net
         begin
           debug "try decoding using #{charset}"
           str.force_encoding(charset)
-          tmp = str.encode(Encoding::UTF_8)
+          tmp = str.encode('UTF-16le', :invalid => :replace, :replace => '').encode('UTF-8')
           if tmp
             str = tmp
             break
@@ -161,11 +152,18 @@ module ::Net
 
       return self.body_to_utf(self.decompress_body(partial))
     end
+
+    def xpath(path)
+      document = Nokogiri::HTML.parse(self.body)
+      document.xpath(path)
+    end
+
+    def to_json
+      JSON::parse(self.body)
+    end
   end
 end
 
-Net::HTTP.version_1_2
-
 module ::Irc
 module Utils
 
@@ -647,7 +645,11 @@ class HttpUtil
       resp = get_response(uri, options, &block)
       raise "http error: #{resp}" unless Net::HTTPOK === resp ||
         Net::HTTPPartialContent === resp
-      return resp.body
+      if options[:resp]
+        return resp
+      else
+        return resp.body
+      end
     rescue Exception => e
       error e
     end