[前][次][番号順一覧][スレッド一覧]

ruby-changes:2696

From: ko1@a...
Date: 11 Dec 2007 01:54:13 +0900
Subject: [ruby-changes:2696] matz - Ruby:r14187 (trunk): * lib/net/http.rb (Net::HTTP::get): now supports gzip

matz	2007-12-11 01:53:52 +0900 (Tue, 11 Dec 2007)

  New Revision: 14187

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/http.rb

  Log:
    * lib/net/http.rb (Net::HTTP::get): now supports gzip
      content-encoding.  a patch from Hugh Sasse <hgs AT dmu.ac.uk>.
      [ruby-core:13451]

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14187&r2=14186
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/net/http.rb?r1=14187&r2=14186

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14186)
+++ ChangeLog	(revision 14187)
@@ -1,3 +1,9 @@
+Tue Dec 11 01:51:34 2007  Yukihiro Matsumoto  <matz@r...>
+
+	* lib/net/http.rb (Net::HTTP::get): now supports gzip
+	  content-encoding.  a patch from Hugh Sasse <hgs AT dmu.ac.uk>.
+	  [ruby-core:13451]
+
 Tue Dec 11 01:21:21 2007  Yukihiro Matsumoto  <matz@r...>
 
 	* parse.y (shadowing_lvar_gen): no duplicate error for "_".
Index: lib/net/http.rb
===================================================================
--- lib/net/http.rb	(revision 14186)
+++ lib/net/http.rb	(revision 14187)
@@ -287,6 +287,13 @@
     Revision = %q$Revision$.split[1]
     HTTPVersion = '1.1'
     @newimpl = true
+    begin
+      require 'zlib'
+      require 'stringio'  #for our purposes (unpacking gzip) lump these together
+      HAVE_ZLIB=true
+    rescue LoadError
+      HAVE_ZLIB=false
+    end
     # :startdoc:
 
     # Turns on net/http 1.2 (ruby 1.8) features.
@@ -477,6 +484,7 @@
       @use_ssl = false
       @ssl_context = nil
       @enable_post_connection_check = true
+      @compression = nil
     end
 
     def inspect
@@ -740,7 +748,18 @@
     public
 
     # Gets data from +path+ on the connected-to host.
-    # +header+ must be a Hash like { 'Accept' => '*/*', ... }.
+    # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
+    # and it defaults to an empty hash.
+    # If +initheader+ doesn't have the key 'accept-encoding', then
+    # a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
+    # so that gzip compression is used in preference to deflate 
+    # compression, which is used in preference to no compression. 
+    # Ruby doesn't have libraries to support the compress (Lempel-Ziv)
+    # compression, so that is not supported.  The intent of this is
+    # to reduce bandwidth by default.   If this routine sets up
+    # compression, then it does the decompression also, removing
+    # the header as well to prevent confusion.  Otherwise
+    # it leaves the body as it found it. 
     #
     # In version 1.1 (ruby 1.6), this method returns a pair of objects,
     # a Net::HTTPResponse object and the entity body string.
@@ -774,10 +793,33 @@
     #       end
     #     }
     #
-    def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
+    def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+
       res = nil
+      if HAVE_ZLIB
+        unless  initheader.keys.any?{|k| k.downcase == "accept-encoding"}
+          initheader["accept-encoding"] = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
+          @compression = true
+        end
+      end
       request(Get.new(path, initheader)) {|r|
-        r.read_body dest, &block
+        if r.key?("content-encoding") and @compression
+          @compression = nil # Clear it till next set.
+          the_body = r.read_body dest, &block
+          case r["content-encoding"]
+          when "gzip"
+            r.body= Zlib::GzipReader.new(StringIO.new(the_body)).read
+            r.delete("content-encoding")
+          when "deflate"
+            r.body= Zlib::Inflate.inflate(the_body);
+            r.delete("content-encoding")
+          when "identity"
+            ; # nothing needed
+          else 
+            ; # Don't do anything dramatic, unless we need to later
+          end
+        else
+          r.read_body dest, &block
+        end
         res = r
       }
       unless @newimpl
@@ -2261,6 +2303,12 @@
       read_body()
     end
 
+    # Because it may be necessary to modify the body, Eg, decompression
+    # this method facilitates that.
+    def body=(value)
+      @body = value
+    end
+
     alias entity body   #:nodoc: obsolete
 
     private

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]