Skip to content

Commit ac85f68

Browse files
authored
Merge pull request #711 from unasuke/frozen_string_literal
Use String#dup in Excon::Utils#binary_encode for frozen string
2 parents 1149d44 + 6609703 commit ac85f68

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ rvm:
1212
- 2.4
1313
- 2.5
1414
- 2.6
15+
- ruby-head
1516
script:
1617
- "bundle exec shindont"
1718
- "bundle exec rake spec[progress]"
1819
sudo: false
20+
21+
matrix:
22+
allow_failures:
23+
- rvm: ruby-head

lib/excon/connection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def request_call(datum)
161161
socket.write(request) # write out request + headers
162162
while true # write out body with chunked encoding
163163
chunk = datum[:request_block].call
164-
binary_encode(chunk)
164+
chunk = binary_encode(chunk)
165165
if chunk.length > 0
166166
socket.write(chunk.length.to_s(16) << CR_NL << chunk << CR_NL)
167167
else
@@ -180,10 +180,10 @@ def request_call(datum)
180180
end
181181

182182
# if request + headers is less than chunk size, fill with body
183-
binary_encode(request)
183+
request = binary_encode(request)
184184
chunk = body.read([datum[:chunk_size] - request.length, 0].max)
185185
if chunk
186-
binary_encode(chunk)
186+
chunk = binary_encode(chunk)
187187
socket.write(request << chunk)
188188
else
189189
socket.write(request) # write out request + headers

lib/excon/socket.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def read_block(max_length)
237237
end
238238

239239
def write_nonblock(data)
240-
binary_encode(data)
240+
data = binary_encode(data)
241241
loop do
242242
written = nil
243243
begin

lib/excon/utils.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ module Utils
1212

1313
def binary_encode(string)
1414
if FORCE_ENC && string.encoding != Encoding::ASCII_8BIT
15-
string.force_encoding('BINARY')
15+
if string.frozen?
16+
string.dup.force_encoding('BINARY')
17+
else
18+
string.force_encoding('BINARY')
19+
end
20+
else
21+
string
1622
end
1723
end
1824

@@ -89,29 +95,29 @@ def query_string(datum)
8995
def split_header_value(str)
9096
return [] if str.nil?
9197
str = str.dup.strip
92-
binary_encode(str)
98+
str = binary_encode(str)
9399
str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
94100
(?:,\s*|\Z)'xn).flatten
95101
end
96102

97103
# Escapes HTTP reserved and unwise characters in +str+
98104
def escape_uri(str)
99105
str = str.dup
100-
binary_encode(str)
106+
str = binary_encode(str)
101107
str.gsub(UNESCAPED) { "%%%02X" % $1[0].ord }
102108
end
103109

104110
# Unescapes HTTP reserved and unwise characters in +str+
105111
def unescape_uri(str)
106112
str = str.dup
107-
binary_encode(str)
113+
str = binary_encode(str)
108114
str.gsub(ESCAPED) { $1.hex.chr }
109115
end
110116

111117
# Unescape form encoded values in +str+
112118
def unescape_form(str)
113119
str = str.dup
114-
binary_encode(str)
120+
str = binary_encode(str)
115121
str.gsub!(/\+/, ' ')
116122
str.gsub(ESCAPED) { $1.hex.chr }
117123
end

0 commit comments

Comments
 (0)