From 8508346dd06399aecd413b95da92b2d1b52f7d3c Mon Sep 17 00:00:00 2001 From: Kevin Casey Date: Sat, 15 Feb 2014 13:05:00 -0800 Subject: Correct prestreaming controller response status. if the controller action has not yet streamed any data, actions should process as normal, and errors should trigger the appropriate behavior (500, or in the case of ActionController::BadRequest, a 400 Bad Request) --- actionpack/lib/action_controller/metal/live.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/live.rb b/actionpack/lib/action_controller/metal/live.rb index 33014b97ca..fdf4ef293d 100644 --- a/actionpack/lib/action_controller/metal/live.rb +++ b/actionpack/lib/action_controller/metal/live.rb @@ -205,6 +205,8 @@ module ActionController begin super(name) rescue => e + @_response.status = 500 unless @_response.committed? + @_response.status = 400 if e.class == ActionController::BadRequest begin @_response.stream.write(ActionView::Base.streaming_completion_on_exception) if request.format == :html @_response.stream.call_on_error -- cgit v1.2.3 From 103e18c87d50a53cd0a33b4e03f2c8a8eff19ece Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 31 Jan 2014 15:37:58 -0500 Subject: Introduce `render :body` for render raw content This is an option for sending a raw content back to browser. Note that this rendering option will unset the default content type and does not include "Content-Type" header back in the response. You should only use this option if you are expecting the "Content-Type" header to not be set. More information on "Content-Type" header can be found on RFC 2616, section 7.2.1. Please see #12374 for more detail. --- actionpack/lib/action_controller/metal/rendering.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index 5c48b4ab98..c9ae1ab388 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -27,14 +27,19 @@ module ActionController end def render_to_body(options = {}) - super || options[:text].presence || ' ' + super || options[:body].presence || options[:text].presence || ' ' end private - def _process_format(format) + def _process_format(format, options = {}) super self.content_type ||= format.to_s + + if options[:body].present? + self.content_type = "none" + self.headers.delete "Content-Type" + end end # Normalize arguments by catching blocks and setting them on :update. @@ -46,12 +51,16 @@ module ActionController # Normalize both text and status options. def _normalize_options(options) #:nodoc: + if options.key?(:body) && options[:body].respond_to?(:to_text) + options[:body] = options[:body].to_text + end + if options.key?(:text) && options[:text].respond_to?(:to_text) options[:text] = options[:text].to_text end - if options.delete(:nothing) || (options.key?(:text) && options[:text].nil?) - options[:text] = " " + if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) + options[:body] = " " end if options[:status] -- cgit v1.2.3 From 8cd9f6d205e5db5331dd5b01be35b537da17cdee Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 7 Feb 2014 13:45:57 -0500 Subject: Introduce `render :plain` for render plain text This is as an option to render content with a content type of `text/plain`. This is the preferred option if you are planning to render a plain text content. Please see #12374 for more detail. --- actionpack/lib/action_controller/metal/rendering.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index c9ae1ab388..3133334369 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -27,7 +27,7 @@ module ActionController end def render_to_body(options = {}) - super || options[:body].presence || options[:text].presence || ' ' + super || options[:body].presence || options[:text].presence || options[:plain].presence || ' ' end private @@ -40,6 +40,10 @@ module ActionController self.content_type = "none" self.headers.delete "Content-Type" end + + if options[:plain].present? + self.content_type = Mime::TEXT + end end # Normalize arguments by catching blocks and setting them on :update. @@ -59,7 +63,11 @@ module ActionController options[:text] = options[:text].to_text end - if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) + if options.key?(:plain) && options[:plain].respond_to?(:to_text) + options[:plain] = options[:plain].to_text + end + + if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) || (options.key?(:plain) && options[:plain].nil?) options[:body] = " " end -- cgit v1.2.3 From 920f3ba2668e0622335f16f2f1318d9e6b5e6b28 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 14 Feb 2014 09:57:47 -0500 Subject: Introduce `render :html` for render HTML string This is an option for to HTML content with a content type of `text/html`. This rendering option calls `ERB::Util.html_escape` internally to escape unsafe HTML string, so you will have to mark your string as html safe if you have any HTML tag in it. Please see #12374 for more detail. --- actionpack/lib/action_controller/metal/rendering.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index 3133334369..cff0405351 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -27,7 +27,7 @@ module ActionController end def render_to_body(options = {}) - super || options[:body].presence || options[:text].presence || options[:plain].presence || ' ' + super || options[:body].presence || options[:text].presence || options[:plain].presence || ERB::Util.h(options[:html]).presence || ' ' end private @@ -67,7 +67,7 @@ module ActionController options[:plain] = options[:plain].to_text end - if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) || (options.key?(:plain) && options[:plain].nil?) + if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) || (options.key?(:plain) && options[:plain].nil?) || (options.key?(:html) && options[:html].nil?) options[:body] = " " end -- cgit v1.2.3 From 79c4983f893da985806d3d59ac23836bccb906f8 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 14 Feb 2014 10:45:46 -0500 Subject: Cleanup `ActionController::Rendering` --- .../lib/action_controller/metal/rendering.rb | 38 +++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index cff0405351..ce37eaefd8 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -2,6 +2,8 @@ module ActionController module Rendering extend ActiveSupport::Concern + RENDER_FORMATS_IN_PRIORITY = [:body, :text, :plain, :html] + # Before processing, set the request formats in current controller formats. def process_action(*) #:nodoc: self.formats = request.formats.map(&:ref).compact @@ -27,11 +29,19 @@ module ActionController end def render_to_body(options = {}) - super || options[:body].presence || options[:text].presence || options[:plain].presence || ERB::Util.h(options[:html]).presence || ' ' + super || _render_in_priorities(options) || ' ' end private + def _render_in_priorities(options) + RENDER_FORMATS_IN_PRIORITY.each do |format| + return options[format] if options.key?(format) + end + + nil + end + def _process_format(format, options = {}) super self.content_type ||= format.to_s @@ -55,19 +65,13 @@ module ActionController # Normalize both text and status options. def _normalize_options(options) #:nodoc: - if options.key?(:body) && options[:body].respond_to?(:to_text) - options[:body] = options[:body].to_text - end + _normalize_text(options) - if options.key?(:text) && options[:text].respond_to?(:to_text) - options[:text] = options[:text].to_text + if options[:html] + options[:html] = ERB::Util.html_escape(options[:html]) end - if options.key?(:plain) && options[:plain].respond_to?(:to_text) - options[:plain] = options[:plain].to_text - end - - if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) || (options.key?(:plain) && options[:plain].nil?) || (options.key?(:html) && options[:html].nil?) + if options.delete(:nothing) || _any_render_format_is_nil?(options) options[:body] = " " end @@ -78,6 +82,18 @@ module ActionController super end + def _normalize_text(options) + RENDER_FORMATS_IN_PRIORITY.each do |format| + if options.key?(format) && options[format].respond_to?(:to_text) + options[format] = options[format].to_text + end + end + end + + def _any_render_format_is_nil?(options) + RENDER_FORMATS_IN_PRIORITY.any? { |format| options.key?(format) && options[format].nil? } + end + # Process controller specific options, as status, content-type and location. def _process_options(options) #:nodoc: status, content_type, location = options.values_at(:status, :content_type, :location) -- cgit v1.2.3 From 3047376870d4a7adc7ff15c3cb4852e073c8f1da Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 14 Feb 2014 15:50:08 -0500 Subject: 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. --- actionpack/lib/action_controller/metal/rack_delegation.rb | 4 ++-- actionpack/lib/action_controller/metal/rendering.rb | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/rack_delegation.rb b/actionpack/lib/action_controller/metal/rack_delegation.rb index bdf6e88699..e1bee9e60c 100644 --- a/actionpack/lib/action_controller/metal/rack_delegation.rb +++ b/actionpack/lib/action_controller/metal/rack_delegation.rb @@ -5,8 +5,8 @@ module ActionController module RackDelegation extend ActiveSupport::Concern - delegate :headers, :status=, :location=, :content_type=, - :status, :location, :content_type, :to => "@_response" + delegate :headers, :status=, :location=, :content_type=, :no_content_type=, + :status, :location, :content_type, :no_content_type, :to => "@_response" def dispatch(action, request) set_response!(request) diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index ce37eaefd8..3c4ef596c7 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -44,15 +44,13 @@ module ActionController def _process_format(format, options = {}) super - self.content_type ||= format.to_s - if options[:body].present? - self.content_type = "none" + if options[:body] self.headers.delete "Content-Type" - end - - if options[:plain].present? + elsif options[:plain] self.content_type = Mime::TEXT + else + self.content_type ||= format.to_s end end -- cgit v1.2.3