diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-10 16:53:52 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-10 16:53:52 +0000 |
commit | 1bf63d9e78751859766331358080084632429cdf (patch) | |
tree | ee95505e6ce644968abc99e5cb6dbb1606a36ab8 | |
parent | b40c614b29994fa84d6d1a6bf890a67eec061aae (diff) | |
download | ruby-1bf63d9e78751859766331358080084632429cdf.tar.gz ruby-1bf63d9e78751859766331358080084632429cdf.tar.xz ruby-1bf63d9e78751859766331358080084632429cdf.zip |
* 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]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@14187 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | lib/net/http.rb | 54 |
2 files changed, 57 insertions, 3 deletions
@@ -1,3 +1,9 @@ +Tue Dec 11 01:51:34 2007 Yukihiro Matsumoto <matz@ruby-lang.org> + + * 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@ruby-lang.org> * parse.y (shadowing_lvar_gen): no duplicate error for "_". diff --git a/lib/net/http.rb b/lib/net/http.rb index 19f0983a3..4a16e118d 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -287,6 +287,13 @@ module Net #:nodoc: 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 @@ module Net #:nodoc: @use_ssl = false @ssl_context = nil @enable_post_connection_check = true + @compression = nil end def inspect @@ -740,7 +748,18 @@ module Net #:nodoc: 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 @@ module Net #:nodoc: # 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 @@ module Net #:nodoc: 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 |