Skip to content

Commit bce16bd

Browse files
mamehsbt
authored andcommitted
lib/net/http/response.rb: support raw deflate correctly
Net::HTTP had used `Zlib::Inflate.new(32 + Zlib::MAX_WBITS)` for all content encoding (deflate, zlib, and gzip). But the argument `32 + Zlib::MAX_WBITS` means zlib and gzip decoding with automatic header detection, so (raw) deflate compression had not been supported. This change makes it support raw deflate correctly by passing an argument `-Zlib::MAX_WBITS` (which means raw deflate) to `Zlib::Inflate.new`. All deflate-mode tests are fixed too. [Bug #11268]
1 parent 9641aae commit bce16bd

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lib/net/http/response.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def inflater # :nodoc:
264264
when 'deflate', 'gzip', 'x-gzip' then
265265
self.delete 'content-encoding'
266266

267-
inflate_body_io = Inflater.new(@socket)
267+
inflate_body_io = Inflater.new(@socket, v.downcase == "deflate")
268268

269269
begin
270270
yield inflate_body_io
@@ -358,10 +358,10 @@ class Inflater # :nodoc:
358358
##
359359
# Creates a new Inflater wrapping +socket+
360360

361-
def initialize socket
361+
def initialize(socket, raw_deflate)
362362
@socket = socket
363363
# zlib with automatic gzip detection
364-
@inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
364+
@inflate = Zlib::Inflate.new(raw_deflate ? -Zlib::MAX_WBITS : 32 + Zlib::MAX_WBITS)
365365
end
366366

367367
##

test/net/http/test_httpresponse.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ def test_read_body_content_encoding_deflate
107107
HTTP/1.1 200 OK
108108
Connection: close
109109
Content-Encoding: deflate
110-
Content-Length: 13
110+
Content-Length: 7
111111
112-
x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
112+
\xCBH\xCD\xC9\xC9\a\x00
113113
EOS
114114

115115
res = Net::HTTPResponse.read_new(io)
@@ -126,7 +126,7 @@ def test_read_body_content_encoding_deflate
126126
assert_equal 'hello', body
127127
else
128128
assert_equal 'deflate', res['content-encoding']
129-
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
129+
assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
130130
end
131131
end
132132

@@ -135,9 +135,9 @@ def test_read_body_content_encoding_deflate_uppercase
135135
HTTP/1.1 200 OK
136136
Connection: close
137137
Content-Encoding: DEFLATE
138-
Content-Length: 13
138+
Content-Length: 7
139139
140-
x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
140+
\xCBH\xCD\xC9\xC9\a\x00
141141
EOS
142142

143143
res = Net::HTTPResponse.read_new(io)
@@ -154,7 +154,7 @@ def test_read_body_content_encoding_deflate_uppercase
154154
assert_equal 'hello', body
155155
else
156156
assert_equal 'DEFLATE', res['content-encoding']
157-
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
157+
assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
158158
end
159159
end
160160

@@ -165,10 +165,10 @@ def test_read_body_content_encoding_deflate_chunked
165165
Content-Encoding: deflate
166166
Transfer-Encoding: chunked
167167
168-
6
169-
x\x9C\xCBH\xCD\xC9
170-
7
171-
\xC9\a\x00\x06,\x02\x15
168+
4
169+
\xCBH\xCD\xC9
170+
3
171+
\xC9\a\x00
172172
0
173173
174174
EOS
@@ -187,7 +187,7 @@ def test_read_body_content_encoding_deflate_chunked
187187
assert_equal 'hello', body
188188
else
189189
assert_equal 'deflate', res['content-encoding']
190-
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
190+
assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body
191191
end
192192
end
193193

@@ -196,9 +196,9 @@ def test_read_body_content_encoding_deflate_disabled
196196
HTTP/1.1 200 OK
197197
Connection: close
198198
Content-Encoding: deflate
199-
Content-Length: 13
199+
Content-Length: 7
200200
201-
x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
201+
\xCBH\xCD\xC9\xC9\a\x00
202202
EOS
203203

204204
res = Net::HTTPResponse.read_new(io)
@@ -211,7 +211,7 @@ def test_read_body_content_encoding_deflate_disabled
211211
end
212212

213213
assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
214-
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
214+
assert_equal "\xCBH\xCD\xC9\xC9\a\x00", body, 'Bug #7381'
215215
end
216216

217217
def test_read_body_content_encoding_deflate_no_length
@@ -220,7 +220,7 @@ def test_read_body_content_encoding_deflate_no_length
220220
Connection: close
221221
Content-Encoding: deflate
222222
223-
x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
223+
\xCBH\xCD\xC9\xC9\a\x00
224224
EOS
225225

226226
res = Net::HTTPResponse.read_new(io)
@@ -237,7 +237,7 @@ def test_read_body_content_encoding_deflate_no_length
237237
assert_equal 'hello', body
238238
else
239239
assert_equal 'deflate', res['content-encoding']
240-
assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
240+
assert_equal "\xCBH\xCD\xC9\xC9\a\x00\r\n", body
241241
end
242242
end
243243

0 commit comments

Comments
 (0)