Skip to content

Commit

Permalink
Use String#dup in Excon::Utils#binary_encode for frozen string
Browse files Browse the repository at this point in the history
Excon::Utils#binary_encode may receive frosen string.
  • Loading branch information
unasuke committed Dec 18, 2019
1 parent 2e260f7 commit 5b4b44e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/excon/connection.rb
Expand Up @@ -161,7 +161,7 @@ def request_call(datum)
socket.write(request) # write out request + headers
while true # write out body with chunked encoding
chunk = datum[:request_block].call
binary_encode(chunk)
chunk = binary_encode(chunk)
if chunk.length > 0
socket.write(chunk.length.to_s(16) << CR_NL << chunk << CR_NL)
else
Expand All @@ -180,10 +180,10 @@ def request_call(datum)
end

# if request + headers is less than chunk size, fill with body
binary_encode(request)
request = binary_encode(request)
chunk = body.read([datum[:chunk_size] - request.length, 0].max)
if chunk
binary_encode(chunk)
chunk = binary_encode(chunk)
socket.write(request << chunk)
else
socket.write(request) # write out request + headers
Expand Down
2 changes: 1 addition & 1 deletion lib/excon/socket.rb
Expand Up @@ -237,7 +237,7 @@ def read_block(max_length)
end

def write_nonblock(data)
binary_encode(data)
data = binary_encode(data)
loop do
written = nil
begin
Expand Down
12 changes: 7 additions & 5 deletions lib/excon/utils.rb
Expand Up @@ -12,7 +12,9 @@ module Utils

def binary_encode(string)
if FORCE_ENC && string.encoding != Encoding::ASCII_8BIT
string.force_encoding('BINARY')
string.dup.force_encoding('BINARY')
else
string
end
end

Expand Down Expand Up @@ -89,29 +91,29 @@ def query_string(datum)
def split_header_value(str)
return [] if str.nil?
str = str.dup.strip
binary_encode(str)
str = binary_encode(str)
str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
(?:,\s*|\Z)'xn).flatten
end

# Escapes HTTP reserved and unwise characters in +str+
def escape_uri(str)
str = str.dup
binary_encode(str)
str = binary_encode(str)
str.gsub(UNESCAPED) { "%%%02X" % $1[0].ord }
end

# Unescapes HTTP reserved and unwise characters in +str+
def unescape_uri(str)
str = str.dup
binary_encode(str)
str = binary_encode(str)
str.gsub(ESCAPED) { $1.hex.chr }
end

# Unescape form encoded values in +str+
def unescape_form(str)
str = str.dup
binary_encode(str)
str = binary_encode(str)
str.gsub!(/\+/, ' ')
str.gsub(ESCAPED) { $1.hex.chr }
end
Expand Down

0 comments on commit 5b4b44e

Please sign in to comment.