Skip to content

Commit

Permalink
chunked: do not chunk on pre-HTTP/1.0 clients
Browse files Browse the repository at this point in the history
Ancient HTTP clients which predate HTTP/1.0 may not set HTTP_VERSION
at all, and those do not support chunking.

RFC 1945 describes HTTP/0.9 as well as HTTP/1.0

Signed-off-by: James Tucker <jftucker@gmail.com>
  • Loading branch information
Eric Wong authored and raggi committed Jul 15, 2014
1 parent b51f303 commit 895beec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rack/chunked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,22 @@ def initialize(app)
@app = app
end

# pre-HTTP/1.0 (informally "HTTP/0.9") HTTP requests did not have
# a version (nor response headers)
def chunkable_version?(ver)
case ver
when "HTTP/1.0", nil, "HTTP/0.9"
false
else
true
end
end

def call(env)
status, headers, body = @app.call(env)
headers = HeaderHash.new(headers)

if env['HTTP_VERSION'] == 'HTTP/1.0' ||
if ! chunkable_version?(env['HTTP_VERSION']) ||
STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
headers['Content-Length'] ||
headers['Transfer-Encoding']
Expand Down
16 changes: 16 additions & 0 deletions test/spec_chunked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def chunked(app)
body.join.should.equal 'Hello World!'
end

should 'not modify response when client is ancient, pre-HTTP/1.0' do
app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
check = lambda do
status, headers, body = chunked(app).call(@env.dup)
status.should.equal 200
headers.should.not.include 'Transfer-Encoding'
body.join.should.equal 'Hello World!'
end

@env.delete('HTTP_VERSION') # unicorn will do this on pre-HTTP/1.0 requests
check.call

@env['HTTP_VERSION'] = 'HTTP/0.9' # not sure if this happens in practice
check.call
end

should 'not modify response when Transfer-Encoding header already present' do
app = lambda { |env|
[200, {"Content-Type" => "text/plain", 'Transfer-Encoding' => 'identity'}, ['Hello', ' ', 'World!']]
Expand Down

0 comments on commit 895beec

Please sign in to comment.