Project

General

Profile

Feature #6494 » net.http.accept_gzip_deflate_by_default.patch

drbrain (Eric Hodel), 05/25/2012 08:22 AM

View differences:

lib/net/http/generic_request.rb (working copy)
include Net::HTTPHeader
def initialize(m, reqbody, resbody, path, initheader = nil)
def initialize(m, reqbody, resbody, path, initheader = {})
@method = m
@request_has_body = reqbody
@response_has_body = resbody
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
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"
})
end
end
initialize_http_header initheader
self['Accept'] ||= '*/*'
self['User-Agent'] ||= 'Ruby'
lib/net/http/request.rb (working copy)
#
class Net::HTTPRequest < Net::HTTPGenericRequest
# Creates HTTP request object.
def initialize(path, initheader = nil)
def initialize(path, initheader = {})
super self.class::METHOD,
self.class::REQUEST_HAS_BODY,
self.class::RESPONSE_HAS_BODY,
lib/net/http.rb (working copy)
#
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.
test/net/http/test_http_request.rb (revision 0)
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_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
(1-1/3)