Skip to content

Commit

Permalink
Merge pull request #711 from unasuke/frozen_string_literal
Browse files Browse the repository at this point in the history
Use String#dup in Excon::Utils#binary_encode for frozen string
  • Loading branch information
geemus committed Dec 18, 2019
2 parents 1149d44 + 6609703 commit ac85f68
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
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

0 comments on commit ac85f68

Please sign in to comment.