Skip to content

Commit

Permalink
Fix error in lib/httpclient/util.rb when frozen strings are enabled.
Browse files Browse the repository at this point in the history
Also fix some of the tests to work with frozen strings. There are still more errors, including sparklemotion/http-cookie#25.

Closes nahi#395
  • Loading branch information
Jason Barnabe authored and tsechingho committed Apr 17, 2024
1 parent d57cc6d commit dfa5cd8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/hexdump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def encode(str)
result = []
while raw = str.slice(offset, 16) and raw.length > 0
# data field
data = ''
data = ''.dup
for v in raw.unpack('N* a*')
if v.kind_of? Integer
data << sprintf("%08x ", v)
Expand Down
2 changes: 1 addition & 1 deletion lib/httpclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ def do_get_block(req, proxy, conn, &block)
conn.push(res)
return res
end
content = block ? nil : ''
content = block ? nil : ''.dup
res = HTTP::Message.new_response(content, req.header)
@debug_dev << "= Request\n\n" if @debug_dev
sess = @session_manager.query(req, proxy)
Expand Down
10 changes: 5 additions & 5 deletions lib/httpclient/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def content_type=(content_type)
if defined?(Encoding::ASCII_8BIT)
def set_body_encoding
if type = self.content_type
OpenURI::Meta.init(o = '')
OpenURI::Meta.init(o = ''.dup)
o.meta_add_field('content-type', type)
@body_encoding = o.encoding
end
Expand Down Expand Up @@ -491,7 +491,7 @@ def init_response(body = nil)
# String.
#
# assert: @size is not nil
def dump(header = '', dev = '')
def dump(header = '', dev = ''.dup)
if @body.is_a?(Parts)
dev << header
@body.parts.each do |part|
Expand Down Expand Up @@ -521,7 +521,7 @@ def dump(header = '', dev = '')
# reason. (header is dumped to dev, too)
# If no dev (the second argument) given, this method returns a dumped
# String.
def dump_chunked(header = '', dev = '')
def dump_chunked(header = '', dev = ''.dup)
dev << header
if @body.is_a?(Parts)
@body.parts.each do |part|
Expand Down Expand Up @@ -574,7 +574,7 @@ def reset_pos(io)
end

def dump_file(io, dev, sz)
buf = ''
buf = ''.dup
rest = sz
while rest > 0
n = io.read([rest, @chunk_size].min, buf)
Expand Down Expand Up @@ -954,7 +954,7 @@ def initialize # :nodoc:

# Dumps message (header and body) to given dev.
# dev needs to respond to <<.
def dump(dev = '')
def dump(dev = ''.dup)
str = @http_header.dump + CRLF
if @http_header.chunked
dev = @http_body.dump_chunked(str, dev)
Expand Down
2 changes: 1 addition & 1 deletion lib/httpclient/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def read_body_rest

def empty_bin_str
str = ''
str.force_encoding('BINARY') if str.respond_to?(:force_encoding)
str = str.dup.force_encoding('BINARY') if str.respond_to?(:force_encoding)
str
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/httpclient/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AddressableURI < Addressable::URI
# Overwrites the original definition just for one line...
def authority
self.host && @authority ||= (begin
authority = ""
authority = "".dup
if self.userinfo != nil
authority << "#{self.userinfo}@"
end
Expand Down
28 changes: 14 additions & 14 deletions test/test_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_basic_auth_reuses_credentials
c.set_auth("http://localhost:#{serverport}/", 'admin', 'admin')
assert_equal('basic_auth OK', c.get_content("http://localhost:#{serverport}/basic_auth/"))
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content("http://localhost:#{serverport}/basic_auth/sub/dir/")
assert_match(/Authorization: Basic YWRtaW46YWRtaW4=/, str)
end
Expand All @@ -265,7 +265,7 @@ def test_digest_auth_reuses_credentials
c.set_auth("http://localhost:#{serverport}/", 'admin', 'admin')
assert_equal('digest_auth OK', c.get_content("http://localhost:#{serverport}/digest_auth/"))
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content("http://localhost:#{serverport}/digest_auth/sub/dir/")
assert_match(/Authorization: Digest/, str)
end
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_perfer_digest
c.set_auth('http://example.com/', 'admin', 'admin')
c.test_loopback_http_response << "HTTP/1.0 401 Unauthorized\nWWW-Authenticate: Basic realm=\"foo\"\nWWW-Authenticate: Digest realm=\"foo\", nonce=\"nonce\", stale=false\nContent-Length: 2\n\nNG"
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://example.com/')
assert_match(/^Authorization: Digest/, str)
end
Expand All @@ -327,7 +327,7 @@ def test_proxy_auth
c.set_proxy_auth('admin', 'admin')
c.test_loopback_http_response << "HTTP/1.0 407 Unauthorized\nProxy-Authenticate: Basic realm=\"foo\"\nContent-Length: 2\n\nNG"
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://example.com/')
assert_match(/Proxy-Authorization: Basic YWRtaW46YWRtaW4=/, str)
end
Expand All @@ -337,7 +337,7 @@ def test_proxy_auth_force
c.set_proxy_auth('admin', 'admin')
c.force_basic_auth = true
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://example.com/')
assert_match(/Proxy-Authorization: Basic YWRtaW46YWRtaW4=/, str)
end
Expand All @@ -349,7 +349,7 @@ def test_proxy_auth_reuses_credentials
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.get_content('http://www1.example.com/')
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://www2.example.com/')
assert_match(/Proxy-Authorization: Basic YWRtaW46YWRtaW4=/, str)
end
Expand All @@ -363,7 +363,7 @@ def test_digest_proxy_auth_loop
ha1 = md5.hexdigest("admin:foo:admin")
ha2 = md5.hexdigest("GET:/")
response = md5.hexdigest("#{ha1}:nonce:#{ha2}")
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://example.com/')
assert_match(/Proxy-Authorization: Digest/, str)
assert_match(%r"response=\"#{response}\"", str)
Expand Down Expand Up @@ -394,7 +394,7 @@ def test_prefer_digest_to_basic_proxy_auth
ha1 = md5.hexdigest("admin:foo:admin")
ha2 = md5.hexdigest("GET:/")
response = md5.hexdigest("#{ha1}:nonce:#{ha2}")
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://example.com/')
assert_match(/Proxy-Authorization: Digest/, str)
assert_match(%r"response=\"#{response}\"", str)
Expand All @@ -411,7 +411,7 @@ def test_digest_proxy_auth_reuses_credentials
ha2 = md5.hexdigest("GET:/")
response = md5.hexdigest("#{ha1}:nonce:#{ha2}")
c.get_content('http://www1.example.com/')
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://www2.example.com/')
assert_match(/Proxy-Authorization: Digest/, str)
assert_match(%r"response=\"#{response}\"", str)
Expand All @@ -433,19 +433,19 @@ def test_oauth
c.www_auth.oauth.set_config('http://photos.example.net/', config)
c.www_auth.oauth.challenge('http://photos.example.net/')
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://photos.example.net/photos', [[:file, 'vacation.jpg'], [:size, 'original']])
assert(str.index(%q(GET /photos?file=vacation.jpg&size=original)))
assert(str.index(%q(Authorization: OAuth realm="http://photos.example.net/", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_nonce="kllo9940pd9333jh", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1191242096", oauth_token="nnch734d00sl2jdk", oauth_version="1.0")))
#
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get_content('http://photos.example.net/photos?file=vacation.jpg&size=original')
assert(str.index(%q(GET /photos?file=vacation.jpg&size=original)))
assert(str.index(%q(Authorization: OAuth realm="http://photos.example.net/", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_nonce="kllo9940pd9333jh", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1191242096", oauth_token="nnch734d00sl2jdk", oauth_version="1.0")))
#
c.test_loopback_http_response << "HTTP/1.0 200 OK\nContent-Length: 2\n\nOK"
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.post_content('http://photos.example.net/photos', [[:file, 'vacation.jpg'], [:size, 'original']])
assert(str.index(%q(POST /photos)))
assert(str.index(%q(Authorization: OAuth realm="http://photos.example.net/", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_nonce="kllo9940pd9333jh", oauth_signature="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1191242096", oauth_token="nnch734d00sl2jdk", oauth_version="1.0")))
Expand Down Expand Up @@ -475,7 +475,7 @@ def test_negotiate_and_basic
c.test_loopback_http_response << %Q(HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABAAAAAAAAAAAAAAA=\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\n\r\n)
c.test_loopback_http_response << %Q(HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: 1\r\n\r\na)
c.test_loopback_http_response << %Q(HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: 1\r\n\r\nb)
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.set_auth('http://www.example.org/', 'admin', 'admin')
# Do NTLM negotiation
c.get('http://www.example.org/foo')
Expand All @@ -484,7 +484,7 @@ def test_negotiate_and_basic
assert_match(%r(Authorization: NTLM), str)
assert_not_match(%r(Authorization: Basic), str)
# ditto for other resource that is protected with NTLM
c.debug_dev = str = ''
c.debug_dev = str = ''.dup
c.get('http://www.example.org/foo/subdir')
assert_not_match(%r(Authorization: NTLM), str)
assert_not_match(%r(Authorization: Basic), str)
Expand Down
2 changes: 1 addition & 1 deletion test/test_hexdump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestHexDump < Test::Unit::TestCase
def test_encode
str = "\032l\277\370\2429\216\236\351[{\{\262\350\274\376"
str = "\032l\277\370\2429\216\236\351[{\{\262\350\274\376".dup
str.force_encoding('BINARY') if str.respond_to?(:force_encoding)
assert_equal(["00000000 1a6cbff8 a2398e9e e95b7b7b b2e8bcfe .l...9...[{{...."], HexDump.encode(str))
end
Expand Down
20 changes: 10 additions & 10 deletions test/test_http-access2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_initialize

def test_agent_name
@client = HTTPAccess2::Client.new(nil, "agent_name_foo")
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
Expand All @@ -44,7 +44,7 @@ def test_agent_name

def test_from
@client = HTTPAccess2::Client.new(nil, nil, "from_bar")
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
Expand All @@ -53,7 +53,7 @@ def test_from
end

def test_debug_dev
str = ""
str = "".dup
@client.debug_dev = str
assert(str.empty?)
@client.get(serverurl)
Expand All @@ -62,7 +62,7 @@ def test_debug_dev

def _test_protocol_version_http09
@client.protocol_version = 'HTTP/0.9'
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl + 'hello')
lines = str.split(/(?:\r?\n)+/)
Expand All @@ -76,7 +76,7 @@ def _test_protocol_version_http09

def test_protocol_version_http10
@client.protocol_version = 'HTTP/1.0'
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl + 'hello')
lines = str.split(/(?:\r?\n)+/)
Expand All @@ -88,7 +88,7 @@ def test_protocol_version_http10
end

def test_protocol_version_http11
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
Expand All @@ -97,15 +97,15 @@ def test_protocol_version_http11
assert_equal("GET / HTTP/1.1", lines[3])
assert_equal("Host: localhost:#{serverport}", lines[7])
@client.protocol_version = 'HTTP/1.1'
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
@client.protocol_version = 'HTTP/1.0'
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
Expand Down Expand Up @@ -314,14 +314,14 @@ def test_post_body
end

def test_extra_headers
str = ""
str = "".dup
@client.debug_dev = str
@client.head(serverurl, nil, {"ABC" => "DEF"})
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
assert_match("ABC: DEF", lines[4])
#
str = ""
str = "".dup
@client.debug_dev = str
@client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
lines = str.split(/(?:\r?\n)+/)
Expand Down
Loading

0 comments on commit dfa5cd8

Please sign in to comment.