Fix previous commit by allowing a proc to be given as response_body. This is deprecated and is going to be removed in future releases.
This commit is contained in:
parent
4d5ce4738b
commit
3aa8f348ef
@ -42,20 +42,6 @@ module Response
|
|||||||
attr_reader :cache_control, :etag
|
attr_reader :cache_control, :etag
|
||||||
alias :etag? :etag
|
alias :etag? :etag
|
||||||
|
|
||||||
def initialize(*)
|
|
||||||
super
|
|
||||||
|
|
||||||
@cache_control = {}
|
|
||||||
@etag = self["ETag"]
|
|
||||||
|
|
||||||
if cache_control = self["Cache-Control"]
|
|
||||||
cache_control.split(/,\s*/).each do |segment|
|
|
||||||
first, last = segment.split("=")
|
|
||||||
@cache_control[first.to_sym] = last || true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def last_modified
|
def last_modified
|
||||||
if last = headers['Last-Modified']
|
if last = headers['Last-Modified']
|
||||||
Time.httpdate(last)
|
Time.httpdate(last)
|
||||||
@ -77,6 +63,18 @@ def etag=(etag)
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def prepare_cache_control!
|
||||||
|
@cache_control = {}
|
||||||
|
@etag = self["ETag"]
|
||||||
|
|
||||||
|
if cache_control = self["Cache-Control"]
|
||||||
|
cache_control.split(/,\s*/).each do |segment|
|
||||||
|
first, last = segment.split("=")
|
||||||
|
@cache_control[first.to_sym] = last || true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def handle_conditional_get!
|
def handle_conditional_get!
|
||||||
if etag? || last_modified? || !@cache_control.empty?
|
if etag? || last_modified? || !@cache_control.empty?
|
||||||
set_conditional_cache_control!
|
set_conditional_cache_control!
|
||||||
|
@ -56,27 +56,26 @@ class Response
|
|||||||
|
|
||||||
cattr_accessor(:default_charset) { "utf-8" }
|
cattr_accessor(:default_charset) { "utf-8" }
|
||||||
|
|
||||||
module Setup
|
|
||||||
def initialize(status = 200, header = {}, body = [])
|
|
||||||
self.body, self.header, self.status = body, header, status
|
|
||||||
|
|
||||||
@sending_file = false
|
|
||||||
@blank = false
|
|
||||||
|
|
||||||
if content_type = self["Content-Type"]
|
|
||||||
type, charset = content_type.split(/;\s*charset=/)
|
|
||||||
@content_type = Mime::Type.lookup(type)
|
|
||||||
@charset = charset || "UTF-8"
|
|
||||||
end
|
|
||||||
|
|
||||||
yield self if block_given?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
include Rack::Response::Helpers
|
include Rack::Response::Helpers
|
||||||
include Setup
|
|
||||||
include ActionDispatch::Http::Cache::Response
|
include ActionDispatch::Http::Cache::Response
|
||||||
|
|
||||||
|
def initialize(status = 200, header = {}, body = [])
|
||||||
|
self.body, self.header, self.status = body, header, status
|
||||||
|
|
||||||
|
@sending_file = false
|
||||||
|
@blank = false
|
||||||
|
|
||||||
|
if content_type = self["Content-Type"]
|
||||||
|
type, charset = content_type.split(/;\s*charset=/)
|
||||||
|
@content_type = Mime::Type.lookup(type)
|
||||||
|
@charset = charset || "UTF-8"
|
||||||
|
end
|
||||||
|
|
||||||
|
prepare_cache_control!
|
||||||
|
|
||||||
|
yield self if block_given?
|
||||||
|
end
|
||||||
|
|
||||||
def status=(status)
|
def status=(status)
|
||||||
@status = Rack::Utils.status_code(status)
|
@status = Rack::Utils.status_code(status)
|
||||||
end
|
end
|
||||||
@ -116,9 +115,32 @@ def body
|
|||||||
|
|
||||||
EMPTY = " "
|
EMPTY = " "
|
||||||
|
|
||||||
|
class BodyBuster #:nodoc:
|
||||||
|
def initialize(response)
|
||||||
|
@response = response
|
||||||
|
@body = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
def bust(body)
|
||||||
|
body.call(@response, self)
|
||||||
|
body.close if body.respond_to?(:close)
|
||||||
|
@body
|
||||||
|
end
|
||||||
|
|
||||||
|
def write(string)
|
||||||
|
@body << string.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def body=(body)
|
def body=(body)
|
||||||
@blank = true if body == EMPTY
|
@blank = true if body == EMPTY
|
||||||
|
|
||||||
|
if body.respond_to?(:call)
|
||||||
|
ActiveSupport::Deprecation.warn "Setting a Proc or an object that responds to call " \
|
||||||
|
"in response_body is no longer supported", caller
|
||||||
|
body = BodyBuster.new(self).bust(body)
|
||||||
|
end
|
||||||
|
|
||||||
# Explicitly check for strings. This is *wrong* theoretically
|
# Explicitly check for strings. This is *wrong* theoretically
|
||||||
# but if we don't check this, the performance on string bodies
|
# but if we don't check this, the performance on string bodies
|
||||||
# is bad on Ruby 1.8 (because strings responds to each then).
|
# is bad on Ruby 1.8 (because strings responds to each then).
|
||||||
@ -150,6 +172,10 @@ def location=(url)
|
|||||||
headers['Location'] = url
|
headers['Location'] = url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@body.close if @body.respond_to?(:close)
|
||||||
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
assign_default_content_type_and_charset!
|
assign_default_content_type_and_charset!
|
||||||
handle_conditional_get!
|
handle_conditional_get!
|
||||||
|
@ -3,25 +3,24 @@
|
|||||||
class ResponseBodyIsProcTest < ActionDispatch::IntegrationTest
|
class ResponseBodyIsProcTest < ActionDispatch::IntegrationTest
|
||||||
class TestController < ActionController::Base
|
class TestController < ActionController::Base
|
||||||
def test
|
def test
|
||||||
request.session_options[:renew] = true
|
|
||||||
self.response_body = proc { |response, output|
|
self.response_body = proc { |response, output|
|
||||||
puts caller
|
|
||||||
output.write 'Hello'
|
output.write 'Hello'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def rescue_action(e) raise end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_simple_get
|
def test_simple_get
|
||||||
with_test_route_set do
|
with_test_route_set do
|
||||||
get '/test'
|
assert_deprecated do
|
||||||
|
get '/test'
|
||||||
|
end
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_equal 'Hello', response.body
|
assert_equal 'Hello', response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def with_test_route_set(options = {})
|
def with_test_route_set(options = {})
|
||||||
with_routing do |set|
|
with_routing do |set|
|
||||||
set.draw do
|
set.draw do
|
||||||
|
Loading…
Reference in New Issue
Block a user