Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Chunked response, encoding issues #278

Merged
merged 1 commit into from

2 participants

@dim

Chunking responses with e.g. UTF-16 bodies fails, since the can't simply be joined with binary encoded content. Please check my solution, not sure if this is the best approach though.

@raggi raggi merged commit 5e43924 into rack:master
@raggi
Owner

Thank you. Also congrats, this may be the first correct encoding patch I can remember rack ever receiving from a community member!

@raggi
Owner

(don't quote me on that, my memory for patch correctness may not be that accurate - either way, thanks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 30, 2011
  1. @dim
This page is out of date. Refresh to see the latest.
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)
Something went wrong with that request. Please try again.