Index: lib/net/http/generic_request.rb =================================================================== --- lib/net/http/generic_request.rb (revision 35969) +++ lib/net/http/generic_request.rb (working copy) @@ -14,6 +14,18 @@ class Net::HTTPGenericRequest raise ArgumentError, "no HTTP request path given" unless path raise ArgumentError, "HTTP request path is empty" if path.empty? @path = path + + if @response_has_body and Net::HTTP::HAVE_ZLIB then + if !initheader || + !initheader.keys.any? { |k| + %w[accept-encoding range].include? k.downcase + } then + initheader = initheader ? initheader.dup : {} + initheader["accept-encoding"] = + "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" + end + end + initialize_http_header initheader self['Accept'] ||= '*/*' self['User-Agent'] ||= 'Ruby' Index: lib/net/http/request.rb =================================================================== --- lib/net/http/request.rb (revision 35969) +++ lib/net/http/request.rb (working copy) @@ -4,7 +4,12 @@ # subclasses: Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Head. # class Net::HTTPRequest < Net::HTTPGenericRequest - # Creates HTTP request object. + # Creates an HTTP request object for +path+. + # + # +initheader+ are the default headers to use. Net::HTTP adds + # Accept-Encoding to enable compression of the response body unless + # Accept-Encoding or Range are supplied in +initheader+. + def initialize(path, initheader = nil) super self.class::METHOD, self.class::REQUEST_HAS_BODY, Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 35969) +++ lib/net/http.rb (working copy) @@ -283,6 +283,14 @@ module Net #:nodoc: # See Net::HTTP::Proxy for further details and examples such as proxies that # require a username and password. # + # === Compression + # + # Net::HTTP automatically adds Accept-Encoding for compression of response + # bodies and automatically decompresses gzip and deflate responses unless a + # Range header was sent. + # + # Compression can be disabled through the Accept-Encoding: identity header. + # # == HTTP Request Classes # # Here is the HTTP request class hierarchy. @@ -1029,14 +1037,6 @@ module Net #:nodoc: # 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 = initheader.merge({ - "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" - }) - @compression = true - end - end request(Get.new(path, initheader)) {|r| if r.key?("content-encoding") and @compression @compression = nil # Clear it till next set. Index: test/net/http/test_http_request.rb =================================================================== --- test/net/http/test_http_request.rb (revision 0) +++ test/net/http/test_http_request.rb (revision 0) @@ -0,0 +1,57 @@ +require 'net/http' +require 'test/unit' +require 'stringio' + +class HTTPRequestTest < Test::Unit::TestCase + + def test_initialize_GET + req = Net::HTTP::Get.new '/' + + assert_equal 'GET', req.method + refute req.request_body_permitted? + assert req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + } + + expected['accept-encoding'] = %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3] if + Net::HTTP::HAVE_ZLIB + + assert_equal expected, req.to_hash + end + + def test_initialize_GET_range + req = Net::HTTP::Get.new '/', 'Range' => 'bytes=0-9' + + assert_equal 'GET', req.method + refute req.request_body_permitted? + assert req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + 'range' => %w[bytes=0-9], + } + + assert_equal expected, req.to_hash + end + + def test_initialize_HEAD + req = Net::HTTP::Head.new '/' + + assert_equal 'HEAD', req.method + refute req.request_body_permitted? + refute req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + } + + assert_equal expected, req.to_hash + end + +end +