Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use String#dup in Excon::Utils#binary_encode for frozen string #711

Merged
merged 2 commits into from Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -12,7 +12,12 @@ rvm:
- 2.4
- 2.5
- 2.6
- ruby-head
script:
- "bundle exec shindont"
- "bundle exec rake spec[progress]"
sudo: false

matrix:
allow_failures:
- rvm: ruby-head
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
16 changes: 11 additions & 5 deletions lib/excon/utils.rb
Expand Up @@ -12,7 +12,13 @@ module Utils

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

Expand Down Expand Up @@ -89,29 +95,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