do not call super in Response#finish to avoid infinite loop or body proxy

This commit is contained in:
Konstantin Haase 2013-01-26 10:27:01 +01:00
parent a099dffc35
commit a43f5709d8
1 changed files with 29 additions and 9 deletions

View File

@ -78,19 +78,39 @@ module Sinatra
end
def finish
if status.to_i / 100 == 1
result = body
if drop_content_info?
headers.delete "Content-Length"
headers.delete "Content-Type"
elsif Array === body and not [204, 205, 304].include?(status.to_i)
# if some other code has already set Content-Length, don't muck with it
# currently, this would be the static file-handler
headers["Content-Length"] ||= body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
end
# Rack::Response#finish sometimes returns self as response body. We don't want that.
status, headers, result = super
result = body if result == self || Rack::BodyProxy === result
[status, headers, result]
if drop_body?
close
result = []
end
if calculate_content_length?
# if some other code has already set Content-Length, don't muck with it
# currently, this would be the static file-handler
headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
end
[status.to_i, header, result]
end
private
def calculate_content_length?
headers["Content-Type"] and not headers["Content-Length"] and Array === body
end
def drop_content_info?
status.to_i / 100 == 1 or drop_body?
end
def drop_body?
[204, 205, 304].include?(status.to_i)
end
end