1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add #no_content_type attribute to AD::Response

Setting this attribute to `true` will remove the content type header
from the request. This is use in `render :body` feature.
This commit is contained in:
Prem Sichanugrist 2014-02-14 15:50:08 -05:00
parent 9fe506e394
commit 3047376870
5 changed files with 25 additions and 11 deletions

View file

@ -5,8 +5,8 @@ module ActionController
module RackDelegation module RackDelegation
extend ActiveSupport::Concern extend ActiveSupport::Concern
delegate :headers, :status=, :location=, :content_type=, delegate :headers, :status=, :location=, :content_type=, :no_content_type=,
:status, :location, :content_type, :to => "@_response" :status, :location, :content_type, :no_content_type, :to => "@_response"
def dispatch(action, request) def dispatch(action, request)
set_response!(request) set_response!(request)

View file

@ -44,15 +44,13 @@ module ActionController
def _process_format(format, options = {}) def _process_format(format, options = {})
super super
self.content_type ||= format.to_s
if options[:body].present? if options[:body]
self.content_type = "none"
self.headers.delete "Content-Type" self.headers.delete "Content-Type"
end elsif options[:plain]
if options[:plain].present?
self.content_type = Mime::TEXT self.content_type = Mime::TEXT
else
self.content_type ||= format.to_s
end end
end end

View file

@ -63,6 +63,8 @@ module ActionDispatch # :nodoc:
# content you're giving them, so we need to send that along. # content you're giving them, so we need to send that along.
attr_accessor :charset attr_accessor :charset
attr_accessor :no_content_type # :nodoc:
CONTENT_TYPE = "Content-Type".freeze CONTENT_TYPE = "Content-Type".freeze
SET_COOKIE = "Set-Cookie".freeze SET_COOKIE = "Set-Cookie".freeze
LOCATION = "Location".freeze LOCATION = "Location".freeze
@ -288,7 +290,7 @@ module ActionDispatch # :nodoc:
end end
def assign_default_content_type_and_charset!(headers) def assign_default_content_type_and_charset!(headers)
return if headers[CONTENT_TYPE].present? || @content_type == "none" return if headers[CONTENT_TYPE].present?
@content_type ||= Mime::HTML @content_type ||= Mime::HTML
@charset ||= self.class.default_charset unless @charset == false @charset ||= self.class.default_charset unless @charset == false
@ -303,8 +305,17 @@ module ActionDispatch # :nodoc:
!@sending_file && @charset != false !@sending_file && @charset != false
end end
def remove_content_type!
headers.delete CONTENT_TYPE
end
def rack_response(status, header) def rack_response(status, header)
assign_default_content_type_and_charset!(header) if no_content_type
remove_content_type!
else
assign_default_content_type_and_charset!(header)
end
handle_conditional_get! handle_conditional_get!
header[SET_COOKIE] = header[SET_COOKIE].join("\n") if header[SET_COOKIE].respond_to?(:join) header[SET_COOKIE] = header[SET_COOKIE].join("\n") if header[SET_COOKIE].respond_to?(:join)

View file

@ -238,7 +238,7 @@ class ResponseTest < ActiveSupport::TestCase
test "does not add default content-type if Content-Type is none" do test "does not add default content-type if Content-Type is none" do
resp = ActionDispatch::Response.new.tap { |response| resp = ActionDispatch::Response.new.tap { |response|
response.content_type = 'none' response.no_content_type = true
} }
assert_not resp.headers.has_key?('Content-Type') assert_not resp.headers.has_key?('Content-Type')

View file

@ -102,6 +102,11 @@ module ActionView
# Assign the rendered format to lookup context. # Assign the rendered format to lookup context.
def _process_format(format, options = {}) #:nodoc: def _process_format(format, options = {}) #:nodoc:
super super
if options[:body]
self.no_content_type = true
end
lookup_context.formats = [format.to_sym] lookup_context.formats = [format.to_sym]
lookup_context.rendered_format = lookup_context.formats.first lookup_context.rendered_format = lookup_context.formats.first
end end