Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure correct encoding on chunked responses

  • Loading branch information...
commit a81e9e166244a957fcdf0f20df88d245f929f703 1 parent f6ee254
@dim dim authored
Showing with 12 additions and 0 deletions.
  1. +2 −0  lib/rack/chunked.rb
  2. +10 −0 test/spec_chunked.rb
View
2  lib/rack/chunked.rb
@@ -23,6 +23,8 @@ def each
@body.each do |chunk|
size = bytesize(chunk)
next if size == 0
+
+ chunk = chunk.dup.force_encoding(Encoding::BINARY) if chunk.respond_to?(:force_encoding)
yield [size.to_s(16), term, chunk, term].join
end
yield TAIL
View
10 test/spec_chunked.rb
@@ -23,6 +23,16 @@
response.body.should.equal "0\r\n\r\n"
end
+ should 'chunks encoded bodies properly' do
+ body = ["\uFFFEHello", " ", "World"].map {|t| t.encode("UTF-16LE") }
+ app = lambda { |env| [200, {}, body] }
+ response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
+ response.headers.should.not.include 'Content-Length'
+ response.headers['Transfer-Encoding'].should.equal 'chunked'
+ response.body.encoding.to_s.should == "ASCII-8BIT"
+ response.body.should.equal "c\r\n\xFE\xFFH\x00e\x00l\x00l\x00o\x00\r\n2\r\n \x00\r\na\r\nW\x00o\x00r\x00l\x00d\x00\r\n0\r\n\r\n"
+ end if RUBY_VERSION >= "1.9"
+
should 'not modify response when Content-Length header present' do
app = lambda { |env| [200, {'Content-Length'=>'12'}, ['Hello', ' ', 'World!']] }
status, headers, body = Rack::Chunked.new(app).call(@env)
Please sign in to comment.
Something went wrong with that request. Please try again.