diff options
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/base/base.rb (renamed from actionpack/lib/action_controller/base.rb) | 527 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/chained/benchmarking.rb (renamed from actionpack/lib/action_controller/benchmarking.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/chained/filters.rb (renamed from actionpack/lib/action_controller/filters.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/chained/flash.rb (renamed from actionpack/lib/action_controller/flash.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/cookies.rb (renamed from actionpack/lib/action_controller/cookies.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/helpers.rb (renamed from actionpack/lib/action_controller/helpers.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/http_authentication.rb (renamed from actionpack/lib/action_controller/http_authentication.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/layout.rb (renamed from actionpack/lib/action_controller/layout.rb) | 144 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/redirect.rb | 91 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/render.rb | 378 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/request_forgery_protection.rb (renamed from actionpack/lib/action_controller/request_forgery_protection.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/responder.rb | 41 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/streaming.rb (renamed from actionpack/lib/action_controller/streaming.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/verification.rb (renamed from actionpack/lib/action_controller/verification.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/cgi/ext.rb (renamed from actionpack/lib/action_controller/cgi_ext.rb) | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/cgi/ext/cookie.rb (renamed from actionpack/lib/action_controller/cgi_ext/cookie.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/cgi/ext/query_extension.rb (renamed from actionpack/lib/action_controller/cgi_ext/query_extension.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/cgi/ext/stdinput.rb (renamed from actionpack/lib/action_controller/cgi_ext/stdinput.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/cgi/process.rb (renamed from actionpack/lib/action_controller/cgi_process.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/dispatcher.rb (renamed from actionpack/lib/action_controller/dispatcher.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/middlewares.rb (renamed from actionpack/lib/action_controller/middlewares.rb) | 10 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/rescue.rb (renamed from actionpack/lib/action_controller/rescue.rb) | 14 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/_request_and_response.erb (renamed from actionpack/lib/action_controller/templates/rescues/_request_and_response.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/_trace.erb (renamed from actionpack/lib/action_controller/templates/rescues/_trace.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/diagnostics.erb (renamed from actionpack/lib/action_controller/templates/rescues/diagnostics.erb) | 5 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/layout.erb (renamed from actionpack/lib/action_controller/templates/rescues/layout.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/missing_template.erb (renamed from actionpack/lib/action_controller/templates/rescues/missing_template.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/routing_error.erb (renamed from actionpack/lib/action_controller/templates/rescues/routing_error.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/template_error.erb (renamed from actionpack/lib/action_controller/templates/rescues/template_error.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/dispatch/templates/rescues/unknown_action.erb (renamed from actionpack/lib/action_controller/templates/rescues/unknown_action.erb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/failsafe.rb | 52 | ||||
-rw-r--r-- | actionpack/lib/action_controller/headers.rb | 33 | ||||
-rw-r--r-- | actionpack/lib/action_controller/middleware_stack.rb | 109 | ||||
-rw-r--r-- | actionpack/lib/action_controller/mime/responds.rb (renamed from actionpack/lib/action_controller/mime_responds.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/mime_type.rb | 212 | ||||
-rw-r--r-- | actionpack/lib/action_controller/mime_types.rb | 21 | ||||
-rw-r--r-- | actionpack/lib/action_controller/params_parser.rb | 71 | ||||
-rw-r--r-- | actionpack/lib/action_controller/rack_ext.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_controller/rack_ext/lock.rb | 21 | ||||
-rw-r--r-- | actionpack/lib/action_controller/rack_ext/multipart.rb | 22 | ||||
-rw-r--r-- | actionpack/lib/action_controller/rack_ext/parse_query.rb | 18 | ||||
-rw-r--r-- | actionpack/lib/action_controller/record_identifier.rb | 2 | ||||
-rwxr-xr-x | actionpack/lib/action_controller/request.rb | 474 | ||||
-rw-r--r-- | actionpack/lib/action_controller/response.rb | 255 | ||||
-rw-r--r-- | actionpack/lib/action_controller/rewindable_input.rb | 28 | ||||
-rw-r--r-- | actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb (renamed from actionpack/lib/action_controller/polymorphic_routes.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/routing/generation/url_rewriter.rb (renamed from actionpack/lib/action_controller/url_rewriter.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/routing/resources.rb (renamed from actionpack/lib/action_controller/resources.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/routing/route_set.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/session/abstract_store.rb | 168 | ||||
-rw-r--r-- | actionpack/lib/action_controller/session/cookie_store.rb | 224 | ||||
-rw-r--r-- | actionpack/lib/action_controller/session/management.rb (renamed from actionpack/lib/action_controller/session_management.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/session/mem_cache_store.rb | 51 | ||||
-rw-r--r-- | actionpack/lib/action_controller/status_codes.rb | 88 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/dom.rb (renamed from actionpack/lib/action_controller/assertions/dom_assertions.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/model.rb (renamed from actionpack/lib/action_controller/assertions/model_assertions.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/response.rb (renamed from actionpack/lib/action_controller/assertions/response_assertions.rb) | 4 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/routing.rb (renamed from actionpack/lib/action_controller/assertions/routing_assertions.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/selector.rb (renamed from actionpack/lib/action_controller/assertions/selector_assertions.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/assertions/tag.rb (renamed from actionpack/lib/action_controller/assertions/tag_assertions.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/integration.rb (renamed from actionpack/lib/action_controller/integration.rb) | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/performance.rb (renamed from actionpack/lib/action_controller/performance_test.rb) | 0 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/process.rb (renamed from actionpack/lib/action_controller/test_process.rb) | 4 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/test_case.rb (renamed from actionpack/lib/action_controller/test_case.rb) | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/uploaded_file.rb | 44 | ||||
-rw-r--r-- | actionpack/lib/action_controller/url_encoded_pair_parser.rb | 155 |
66 files changed, 635 insertions, 2660 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base/base.rb index 36b80d5780..a01d8f25cc 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base/base.rb @@ -58,22 +58,6 @@ module ActionController #:nodoc: end end - class DoubleRenderError < ActionControllerError #:nodoc: - DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"." - - def initialize(message = nil) - super(message || DEFAULT_MESSAGE) - end - end - - class RedirectBackError < ActionControllerError #:nodoc: - DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].' - - def initialize(message = nil) - super(message || DEFAULT_MESSAGE) - end - end - class UnknownHttpMethod < ActionControllerError #:nodoc: end @@ -247,9 +231,8 @@ module ActionController #:nodoc: # end # class Base - DEFAULT_RENDER_STATUS_CODE = "200 OK" - include StatusCodes + include ActionDispatch::StatusCodes cattr_reader :protected_instance_variables # Controller specific instance variables which will not be accessible inside views. @@ -301,7 +284,10 @@ module ActionController #:nodoc: # A YAML parser is also available and can be turned on with: # # ActionController::Base.param_parsers[Mime::YAML] = :yaml - @@param_parsers = {} + @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form, + Mime::URL_ENCODED_FORM => :url_encoded_form, + Mime::XML => :xml_simple, + Mime::JSON => :json } cattr_accessor :param_parsers # Controls the default charset for all renders. @@ -381,8 +367,8 @@ module ActionController #:nodoc: class << self def call(env) # HACK: For global rescue to have access to the original request and response - request = env["action_controller.rescue.request"] ||= Request.new(env) - response = env["action_controller.rescue.response"] ||= Response.new + request = env["action_controller.rescue.request"] ||= ActionDispatch::Request.new(env) + response = env["action_controller.rescue.response"] ||= ActionDispatch::Response.new process(request, response) end @@ -514,8 +500,8 @@ module ActionController #:nodoc: def process(request, response, method = :perform_action, *arguments) #:nodoc: response.request = request - initialize_template_class(response) assign_shortcuts(request, response) + initialize_template_class(response) initialize_current_url assign_names @@ -678,339 +664,6 @@ module ActionController #:nodoc: @template.view_paths.push(*path) end - protected - # Renders the content that will be returned to the browser as the response body. - # - # === Rendering an action - # - # Action rendering is the most common form and the type used automatically by Action Controller when nothing else is - # specified. By default, actions are rendered within the current layout (if one exists). - # - # # Renders the template for the action "goal" within the current controller - # render :action => "goal" - # - # # Renders the template for the action "short_goal" within the current controller, - # # but without the current active layout - # render :action => "short_goal", :layout => false - # - # # Renders the template for the action "long_goal" within the current controller, - # # but with a custom layout - # render :action => "long_goal", :layout => "spectacular" - # - # === Rendering partials - # - # Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page - # without reloading. Rendering of partials from the controller makes it possible to use the same partial template in - # both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the - # controller action responding to Ajax calls). By default, the current layout is not used. - # - # # Renders the same partial with a local variable. - # render :partial => "person", :locals => { :name => "david" } - # - # # Renders the partial, making @new_person available through - # # the local variable 'person' - # render :partial => "person", :object => @new_person - # - # # Renders a collection of the same partial by making each element - # # of @winners available through the local variable "person" as it - # # builds the complete response. - # render :partial => "person", :collection => @winners - # - # # Renders a collection of partials but with a custom local variable name - # render :partial => "admin_person", :collection => @winners, :as => :person - # - # # Renders the same collection of partials, but also renders the - # # person_divider partial between each person partial. - # render :partial => "person", :collection => @winners, :spacer_template => "person_divider" - # - # # Renders a collection of partials located in a view subfolder - # # outside of our current controller. In this example we will be - # # rendering app/views/shared/_note.r(html|xml) Inside the partial - # # each element of @new_notes is available as the local var "note". - # render :partial => "shared/note", :collection => @new_notes - # - # # Renders the partial with a status code of 500 (internal error). - # render :partial => "broken", :status => 500 - # - # Note that the partial filename must also be a valid Ruby variable name, - # so e.g. 2005 and register-user are invalid. - # - # - # == Automatic etagging - # - # Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the - # response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified - # and the response body will be set to an empty string. No etag header will be inserted if it's already set. - # - # === Rendering a template - # - # Template rendering works just like action rendering except that it takes a path relative to the template root. - # The current layout is automatically applied. - # - # # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb) - # render :template => "weblog/show" - # - # # Renders the template with a local variable - # render :template => "weblog/show", :locals => {:customer => Customer.new} - # - # === Rendering a file - # - # File rendering works just like action rendering except that it takes a filesystem path. By default, the path - # is assumed to be absolute, and the current layout is not applied. - # - # # Renders the template located at the absolute filesystem path - # render :file => "/path/to/some/template.erb" - # render :file => "c:/path/to/some/template.erb" - # - # # Renders a template within the current layout, and with a 404 status code - # render :file => "/path/to/some/template.erb", :layout => true, :status => 404 - # render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404 - # - # === Rendering text - # - # Rendering of text is usually used for tests or for rendering prepared content, such as a cache. By default, text - # rendering is not done within the active layout. - # - # # Renders the clear text "hello world" with status code 200 - # render :text => "hello world!" - # - # # Renders the clear text "Explosion!" with status code 500 - # render :text => "Explosion!", :status => 500 - # - # # Renders the clear text "Hi there!" within the current active layout (if one exists) - # render :text => "Hi there!", :layout => true - # - # # Renders the clear text "Hi there!" within the layout - # # placed in "app/views/layouts/special.r(html|xml)" - # render :text => "Hi there!", :layout => "special" - # - # The <tt>:text</tt> option can also accept a Proc object, which can be used to manually control the page generation. This should - # generally be avoided, as it violates the separation between code and content, and because almost everything that can be - # done with this method can also be done more cleanly using one of the other rendering methods, most notably templates. - # - # # Renders "Hello from code!" - # render :text => proc { |response, output| output.write("Hello from code!") } - # - # === Rendering XML - # - # Rendering XML sets the content type to application/xml. - # - # # Renders '<name>David</name>' - # render :xml => {:name => "David"}.to_xml - # - # It's not necessary to call <tt>to_xml</tt> on the object you want to render, since <tt>render</tt> will - # automatically do that for you: - # - # # Also renders '<name>David</name>' - # render :xml => {:name => "David"} - # - # === Rendering JSON - # - # Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected - # that the response will be parsed (or eval'd) for use as a data structure. - # - # # Renders '{"name": "David"}' - # render :json => {:name => "David"}.to_json - # - # It's not necessary to call <tt>to_json</tt> on the object you want to render, since <tt>render</tt> will - # automatically do that for you: - # - # # Also renders '{"name": "David"}' - # render :json => {:name => "David"} - # - # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), - # so the <tt>:callback</tt> option is provided for these cases. - # - # # Renders 'show({"name": "David"})' - # render :json => {:name => "David"}.to_json, :callback => 'show' - # - # === Rendering an inline template - # - # Rendering of an inline template works as a cross between text and action rendering where the source for the template - # is supplied inline, like text, but its interpreted with ERb or Builder, like action. By default, ERb is used for rendering - # and the current layout is not used. - # - # # Renders "hello, hello, hello, again" - # render :inline => "<%= 'hello, ' * 3 + 'again' %>" - # - # # Renders "<p>Good seeing you!</p>" using Builder - # render :inline => "xml.p { 'Good seeing you!' }", :type => :builder - # - # # Renders "hello david" - # render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" } - # - # === Rendering inline JavaScriptGenerator page updates - # - # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details), - # you can also pass the <tt>:update</tt> parameter to +render+, along with a block, to render page updates inline. - # - # render :update do |page| - # page.replace_html 'user_list', :partial => 'user', :collection => @users - # page.visual_effect :highlight, 'user_list' - # end - # - # === Rendering vanilla JavaScript - # - # In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js. - # - # # Renders "alert('hello')" and sets the mime type to text/javascript - # render :js => "alert('hello')" - # - # === Rendering with status and location headers - # All renders take the <tt>:status</tt> and <tt>:location</tt> options and turn them into headers. They can even be used together: - # - # render :xml => post.to_xml, :status => :created, :location => post_url(post) - def render(options = nil, extra_options = {}, &block) #:doc: - raise DoubleRenderError, "Can only render or redirect once per action" if performed? - - validate_render_arguments(options, extra_options, block_given?) - - if options.nil? - options = { :template => default_template, :layout => true } - elsif options == :update - options = extra_options.merge({ :update => true }) - elsif options.is_a?(String) || options.is_a?(Symbol) - case options.to_s.index('/') - when 0 - extra_options[:file] = options - when nil - extra_options[:action] = options - else - extra_options[:template] = options - end - - options = extra_options - end - - layout = pick_layout(options) - response.layout = layout.path_without_format_and_extension if layout - logger.info("Rendering template within #{layout.path_without_format_and_extension}") if logger && layout - - if content_type = options[:content_type] - response.content_type = content_type.to_s - end - - if location = options[:location] - response.headers["Location"] = url_for(location) - end - - if options.has_key?(:text) - text = layout ? @template.render(options.merge(:text => options[:text], :layout => layout)) : options[:text] - render_for_text(text, options[:status]) - - else - if file = options[:file] - render_for_file(file, options[:status], layout, options[:locals] || {}) - - elsif template = options[:template] - render_for_file(template, options[:status], layout, options[:locals] || {}) - - elsif inline = options[:inline] - render_for_text(@template.render(options.merge(:layout => layout)), options[:status]) - - elsif action_name = options[:action] - render_for_file(default_template(action_name.to_s), options[:status], layout) - - elsif xml = options[:xml] - response.content_type ||= Mime::XML - render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status]) - - elsif js = options[:js] - response.content_type ||= Mime::JS - render_for_text(js, options[:status]) - - elsif json = options[:json] - json = json.to_json unless json.is_a?(String) - json = "#{options[:callback]}(#{json})" unless options[:callback].blank? - response.content_type ||= Mime::JSON - render_for_text(json, options[:status]) - - elsif options[:partial] - options[:partial] = default_template_name if options[:partial] == true - if layout - render_for_text(@template.render(:text => @template.render(options), :layout => layout), options[:status]) - else - render_for_text(@template.render(options), options[:status]) - end - - elsif options[:update] - @template.send(:_evaluate_assigns_and_ivars) - - generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block) - response.content_type = Mime::JS - render_for_text(generator.to_s, options[:status]) - - elsif options[:nothing] - render_for_text(nil, options[:status]) - - else - render_for_file(default_template, options[:status], layout) - end - end - end - - # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead - # of sending it as the response body to the browser. - def render_to_string(options = nil, &block) #:doc: - render(options, &block) - ensure - response.content_type = nil - erase_render_results - reset_variables_added_to_assigns - end - - # Return a response that has no content (merely headers). The options - # argument is interpreted to be a hash of header names and values. - # This allows you to easily return a response that consists only of - # significant headers: - # - # head :created, :location => person_path(@person) - # - # It can also be used to return exceptional conditions: - # - # return head(:method_not_allowed) unless request.post? - # return head(:bad_request) unless valid_request? - # render - def head(*args) - if args.length > 2 - raise ArgumentError, "too many arguments to head" - elsif args.empty? - raise ArgumentError, "too few arguments to head" - end - options = args.extract_options! - status = interpret_status(args.shift || options.delete(:status) || :ok) - - options.each do |key, value| - headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s - end - - render :nothing => true, :status => status - end - - # Clears the rendered results, allowing for another render to be performed. - def erase_render_results #:nodoc: - response.body = nil - @performed_render = false - end - - # Clears the redirected results from the headers, resets the status to 200 and returns - # the URL that was used to redirect or nil if there was no redirected URL - # Note that +redirect_to+ will change the body of the response to indicate a redirection. - # The response body is not reset here, see +erase_render_results+ - def erase_redirect_results #:nodoc: - @performed_redirect = false - response.redirected_to = nil - response.redirected_to_method_params = nil - response.status = DEFAULT_RENDER_STATUS_CODE - response.headers.delete('Location') - end - - # Erase both render and redirect results - def erase_results #:nodoc: - erase_render_results - erase_redirect_results - end - def rewrite_options(options) #:nodoc: if defaults = default_url_options(options) defaults.merge(options) @@ -1032,73 +685,6 @@ module ActionController #:nodoc: def default_url_options(options = nil) end - # Redirects the browser to the target specified in +options+. This parameter can take one of three forms: - # - # * <tt>Hash</tt> - The URL will be generated by calling url_for with the +options+. - # * <tt>Record</tt> - The URL will be generated by calling url_for with the +options+, which will reference a named URL for that record. - # * <tt>String</tt> starting with <tt>protocol://</tt> (like <tt>http://</tt>) - Is passed straight through as the target for redirection. - # * <tt>String</tt> not containing a protocol - The current protocol and host is prepended to the string. - # * <tt>:back</tt> - Back to the page that issued the request. Useful for forms that are triggered from multiple places. - # Short-hand for <tt>redirect_to(request.env["HTTP_REFERER"])</tt> - # - # Examples: - # redirect_to :action => "show", :id => 5 - # redirect_to post - # redirect_to "http://www.rubyonrails.org" - # redirect_to "/images/screenshot.jpg" - # redirect_to articles_url - # redirect_to :back - # - # The redirection happens as a "302 Moved" header unless otherwise specified. - # - # Examples: - # redirect_to post_url(@post), :status=>:found - # redirect_to :action=>'atom', :status=>:moved_permanently - # redirect_to post_url(@post), :status=>301 - # redirect_to :action=>'atom', :status=>302 - # - # When using <tt>redirect_to :back</tt>, if there is no referrer, - # RedirectBackError will be raised. You may specify some fallback - # behavior for this case by rescuing RedirectBackError. - def redirect_to(options = {}, response_status = {}) #:doc: - raise ActionControllerError.new("Cannot redirect to nil!") if options.nil? - - if options.is_a?(Hash) && options[:status] - status = options.delete(:status) - elsif response_status[:status] - status = response_status[:status] - else - status = 302 - end - - response.redirected_to = options - logger.info("Redirected to #{options}") if logger && logger.info? - - case options - # The scheme name consist of a letter followed by any combination of - # letters, digits, and the plus ("+"), period ("."), or hyphen ("-") - # characters; and is terminated by a colon (":"). - when %r{^\w[\w\d+.-]*:.*} - redirect_to_full_url(options, status) - when String - redirect_to_full_url(request.protocol + request.host_with_port + options, status) - when :back - if referer = request.headers["Referer"] - redirect_to(referer, :status=>status) - else - raise RedirectBackError - end - else - redirect_to_full_url(url_for(options), status) - end - end - - def redirect_to_full_url(url, status) - raise DoubleRenderError if performed? - response.redirect(url, interpret_status(status)) - @performed_redirect = true - end - # Sets the etag and/or last_modified on the response and checks it against # the client request. If the request doesn't match the options provided, the # request is considered stale and should be generated from scratch. Otherwise, @@ -1174,41 +760,20 @@ module ActionController #:nodoc: end private - def render_for_file(template_path, status = nil, layout = nil, locals = {}) #:nodoc: - path = template_path.respond_to?(:path_without_format_and_extension) ? template_path.path_without_format_and_extension : template_path - logger.info("Rendering #{path}" + (status ? " (#{status})" : '')) if logger - render_for_text @template.render(:file => template_path, :locals => locals, :layout => layout), status - end - - def render_for_text(text = nil, status = nil, append_response = false) #:nodoc: - @performed_render = true - - response.status = interpret_status(status || DEFAULT_RENDER_STATUS_CODE) - - if append_response - response.body ||= '' - response.body << text.to_s - else - response.body = case text - when Proc then text - when nil then " " # Safari doesn't pass the headers of the return if the response is zero length - else text.to_s - end + def _process_options(options) + if content_type = options[:content_type] + response.content_type = content_type.to_s end - end - def validate_render_arguments(options, extra_options, has_block) - if options && (has_block && options != :update) && !options.is_a?(String) && !options.is_a?(Hash) && !options.is_a?(Symbol) - raise RenderError, "You called render with invalid options : #{options.inspect}" + if location = options[:location] + response.headers["Location"] = url_for(location) end - if !extra_options.is_a?(Hash) - raise RenderError, "You called render with invalid options : #{options.inspect}, #{extra_options.inspect}" - end + response.status = interpret_status(options[:status] || DEFAULT_RENDER_STATUS_CODE) end def initialize_template_class(response) - response.template = ActionView::Base.new(self.class.view_paths, {}, self) + @template = response.template = ActionView::Base.new(self.class.view_paths, {}, self, formats) response.template.helpers.send :include, self.class.master_helper_module response.redirected_to = nil @performed_render = @performed_redirect = false @@ -1221,7 +786,6 @@ module ActionController #:nodoc: @_response.session = request.session @_session = @_response.session - @template = @_response.template @_headers = @_response.headers end @@ -1257,23 +821,21 @@ module ActionController #:nodoc: end def perform_action - if action_methods.include?(action_name) - send(action_name) - default_render unless performed? - elsif respond_to? :method_missing - method_missing action_name - default_render unless performed? - else - begin - default_render - rescue ActionView::MissingTemplate => e - # Was the implicit template missing, or was it another template? - if e.path == default_template_name - raise UnknownAction, "No action responded to #{action_name}. Actions: #{action_methods.sort.to_sentence}", caller - else - raise e - end - end + if called = action_methods.include?(action_name) + ret = send(action_name) + elsif called = respond_to?(:method_missing) + ret = method_missing(action_name) + end + + return (performed? ? ret : default_render) if called + + begin + default_render + rescue ActionView::MissingTemplate => e + raise e unless e.path == action_name + # If the path is the same as the action_name, the action is completely missing + raise UnknownAction, "No action responded to #{action_name}. Actions: " + + "#{action_methods.sort.to_sentence}", caller end end @@ -1285,22 +847,6 @@ module ActionController #:nodoc: @action_name = (params['action'] || 'index') end - def action_methods - self.class.action_methods - end - - def self.action_methods - @action_methods ||= - # All public instance methods of this class, including ancestors - public_instance_methods(true).map { |m| m.to_s }.to_set - - # Except for public instance methods of Base and its ancestors - Base.public_instance_methods(true).map { |m| m.to_s } + - # Be sure to include shadowed public instance methods of this class - public_instance_methods(false).map { |m| m.to_s } - - # And always exclude explicitly hidden actions - hidden_actions - end - def reset_variables_added_to_assigns @template.instance_variable_set("@assigns_added", nil) end @@ -1315,6 +861,10 @@ module ActionController #:nodoc: "#{request.protocol}#{request.host}#{request.request_uri}" end + def close_session + @_session.close if @_session && @_session.respond_to?(:close) + end + def default_template(action_name = self.action_name) self.view_paths.find_template(default_template_name(action_name), default_template_format) end @@ -1326,7 +876,7 @@ module ActionController #:nodoc: action_name = strip_out_controller(action_name) end end - "#{self.controller_path}/#{action_name}" + "#{controller_path}/#{action_name}" end def strip_out_controller(path) @@ -1338,14 +888,15 @@ module ActionController #:nodoc: end def process_cleanup + close_session end end Base.class_eval do - [ Filters, Layout, Benchmarking, Rescue, Flash, MimeResponds, Helpers, + [ Filters, Layout, Renderer, Redirector, Responder, Benchmarking, Rescue, Flash, MimeResponds, Helpers, Cookies, Caching, Verification, Streaming, SessionManagement, - HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, - RecordIdentifier, RequestForgeryProtection, Translation + HttpAuthentication::Basic::ControllerMethods, RecordIdentifier, + RequestForgeryProtection, Translation ].each do |mod| include mod end diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/base/chained/benchmarking.rb index 47377e5fa9..066150f58a 100644 --- a/actionpack/lib/action_controller/benchmarking.rb +++ b/actionpack/lib/action_controller/base/chained/benchmarking.rb @@ -64,7 +64,7 @@ module ActionController #:nodoc: private def perform_action_with_benchmark - if logger + if logger && logger.info? ms = [Benchmark.ms { perform_action_without_benchmark }, 0.01].max logging_view = defined?(@view_runtime) logging_active_record = Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? diff --git a/actionpack/lib/action_controller/filters.rb b/actionpack/lib/action_controller/base/chained/filters.rb index 9022b8b279..9022b8b279 100644 --- a/actionpack/lib/action_controller/filters.rb +++ b/actionpack/lib/action_controller/base/chained/filters.rb diff --git a/actionpack/lib/action_controller/flash.rb b/actionpack/lib/action_controller/base/chained/flash.rb index 56ee9c67e2..56ee9c67e2 100644 --- a/actionpack/lib/action_controller/flash.rb +++ b/actionpack/lib/action_controller/base/chained/flash.rb diff --git a/actionpack/lib/action_controller/cookies.rb b/actionpack/lib/action_controller/base/cookies.rb index 840ceb5abd..840ceb5abd 100644 --- a/actionpack/lib/action_controller/cookies.rb +++ b/actionpack/lib/action_controller/base/cookies.rb diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/base/helpers.rb index ba65032f6a..ba65032f6a 100644 --- a/actionpack/lib/action_controller/helpers.rb +++ b/actionpack/lib/action_controller/base/helpers.rb diff --git a/actionpack/lib/action_controller/http_authentication.rb b/actionpack/lib/action_controller/base/http_authentication.rb index 5d915fda08..5d915fda08 100644 --- a/actionpack/lib/action_controller/http_authentication.rb +++ b/actionpack/lib/action_controller/base/http_authentication.rb diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/base/layout.rb index 183d56c2e8..88a15aa6ca 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/base/layout.rb @@ -2,11 +2,7 @@ module ActionController #:nodoc: module Layout #:nodoc: def self.included(base) base.extend(ClassMethods) - base.class_eval do - class << self - alias_method_chain :inherited, :layout - end - end + base.class_inheritable_accessor :layout_name, :layout_conditions end # Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in @@ -159,122 +155,90 @@ module ActionController #:nodoc: # # This will render the help action with the "help" layout instead of the controller-wide "weblog_standard" layout. module ClassMethods + extend ActiveSupport::Memoizable + # If a layout is specified, all rendered actions will have their result rendered # when the layout <tt>yield</tt>s. This layout can itself depend on instance variables assigned during action # performance and have access to them as any normal template would. def layout(template_name, conditions = {}, auto = false) add_layout_conditions(conditions) - write_inheritable_attribute(:layout, template_name) - write_inheritable_attribute(:auto_layout, auto) + self.layout_name = template_name end - def layout_conditions #:nodoc: - @layout_conditions ||= read_inheritable_attribute(:layout_conditions) + def memoized_default_layout(formats) #:nodoc: + self.layout_name || begin + layout = default_layout_name + layout.is_a?(String) ? find_layout(layout, formats) : layout + rescue ActionView::MissingTemplate + end end - def default_layout(format) #:nodoc: - layout = read_inheritable_attribute(:layout) - return layout unless read_inheritable_attribute(:auto_layout) - find_layout(layout, format) + def default_layout(*args) + (@_memoized_default_layout ||= ::ActiveSupport::ConcurrentHash.new)[args] ||= memoized_default_layout(*args) + end + + def memoized_find_layout(layout, formats) #:nodoc: + return layout if layout.nil? || layout.respond_to?(:render) + prefix = layout.to_s =~ /layouts\// ? nil : "layouts" + view_paths.find_by_parts(layout.to_s, formats, prefix) + end + + def find_layout(*args) + (@_memoized_find_layout ||= ::ActiveSupport::ConcurrentHash.new)[args] ||= memoized_find_layout(*args) end def layout_list #:nodoc: Array(view_paths).sum([]) { |path| Dir["#{path.to_str}/layouts/**/*"] } end + memoize :layout_list - def find_layout(layout, *formats) #:nodoc: - return layout if layout.respond_to?(:render) - view_paths.find_template(layout.to_s =~ /layouts\// ? layout : "layouts/#{layout}", *formats) - rescue ActionView::MissingTemplate - nil + def default_layout_name + layout_match = name.underscore.sub(/_controller$/, '') + if layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty? + superclass.default_layout_name if superclass.respond_to?(:default_layout_name) + else + layout_match + end end + memoize :default_layout_name private - def inherited_with_layout(child) - inherited_without_layout(child) - unless child.name.blank? - layout_match = child.name.underscore.sub(/_controller$/, '').sub(/^controllers\//, '') - child.layout(layout_match, {}, true) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty? - end - end - def add_layout_conditions(conditions) - write_inheritable_hash(:layout_conditions, normalize_conditions(conditions)) - end - - def normalize_conditions(conditions) - conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})} + # :except => :foo == :except => [:foo] == :except => "foo" == :except => ["foo"] + conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} } + write_inheritable_hash(:layout_conditions, conditions) end end - - # Returns the name of the active layout. If the layout was specified as a method reference (through a symbol), this method - # is called and the return value is used. Likewise if the layout was specified as an inline method (through a proc or method - # object). If the layout was defined without a directory, layouts is assumed. So <tt>layout "weblog/standard"</tt> will return - # weblog/standard, but <tt>layout "standard"</tt> will return layouts/standard. - def active_layout(passed_layout = nil) - layout = passed_layout || self.class.default_layout(default_template_format) - - active_layout = case layout - when Symbol then __send__(layout) - when Proc then layout.call(self) - else layout + + def active_layout(name) + name = self.class.default_layout(formats) if name == true + + layout_name = case name + when Symbol then __send__(name) + when Proc then name.call(self) + else name end - if active_layout - if layout = self.class.find_layout(active_layout, @template.template_format) - layout - else - raise ActionView::MissingTemplate.new(self.class.view_paths, active_layout) - end - end + self.class.find_layout(layout_name, formats) end - private - def candidate_for_layout?(options) - template = options[:template] || default_template(options[:action]) - if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty? - begin - !self.view_paths.find_template(template, default_template_format).exempt_from_layout? - rescue ActionView::MissingTemplate - true - end - end - rescue ActionView::MissingTemplate - false - end - - def pick_layout(options) - if options.has_key?(:layout) - case layout = options.delete(:layout) - when FalseClass - nil - when NilClass, TrueClass - active_layout if action_has_layout? && candidate_for_layout?(:template => default_template_name) - else - active_layout(layout) - end - else - active_layout if action_has_layout? && candidate_for_layout?(options) - end - end + def _pick_layout(layout_name, implicit = false) + return unless layout_name || implicit + layout_name = true if layout_name.nil? + active_layout(layout_name) if action_has_layout? && layout_name + end + private def action_has_layout? if conditions = self.class.layout_conditions - case - when only = conditions[:only] - only.include?(action_name) - when except = conditions[:except] - !except.include?(action_name) - else - true + if only = conditions[:only] + return only.include?(action_name) + elsif except = conditions[:except] + return !except.include?(action_name) end - else - true end + true end - def default_template_format - response.template.template_format - end end end diff --git a/actionpack/lib/action_controller/base/redirect.rb b/actionpack/lib/action_controller/base/redirect.rb new file mode 100644 index 0000000000..83af793978 --- /dev/null +++ b/actionpack/lib/action_controller/base/redirect.rb @@ -0,0 +1,91 @@ +module ActionController + class RedirectBackError < ActionControllerError #:nodoc: + DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].' + + def initialize(message = nil) + super(message || DEFAULT_MESSAGE) + end + end + + module Redirector + + # Redirects the browser to the target specified in +options+. This parameter can take one of three forms: + # + # * <tt>Hash</tt> - The URL will be generated by calling url_for with the +options+. + # * <tt>Record</tt> - The URL will be generated by calling url_for with the +options+, which will reference a named URL for that record. + # * <tt>String</tt> starting with <tt>protocol://</tt> (like <tt>http://</tt>) - Is passed straight through as the target for redirection. + # * <tt>String</tt> not containing a protocol - The current protocol and host is prepended to the string. + # * <tt>:back</tt> - Back to the page that issued the request. Useful for forms that are triggered from multiple places. + # Short-hand for <tt>redirect_to(request.env["HTTP_REFERER"])</tt> + # + # Examples: + # redirect_to :action => "show", :id => 5 + # redirect_to post + # redirect_to "http://www.rubyonrails.org" + # redirect_to "/images/screenshot.jpg" + # redirect_to articles_url + # redirect_to :back + # + # The redirection happens as a "302 Moved" header unless otherwise specified. + # + # Examples: + # redirect_to post_url(@post), :status=>:found + # redirect_to :action=>'atom', :status=>:moved_permanently + # redirect_to post_url(@post), :status=>301 + # redirect_to :action=>'atom', :status=>302 + # + # When using <tt>redirect_to :back</tt>, if there is no referrer, + # RedirectBackError will be raised. You may specify some fallback + # behavior for this case by rescuing RedirectBackError. + def redirect_to(options = {}, response_status = {}) #:doc: + raise ActionControllerError.new("Cannot redirect to nil!") if options.nil? + + if options.is_a?(Hash) && options[:status] + status = options.delete(:status) + elsif response_status[:status] + status = response_status[:status] + else + status = 302 + end + + response.redirected_to = options + logger.info("Redirected to #{options}") if logger && logger.info? + + case options + # The scheme name consist of a letter followed by any combination of + # letters, digits, and the plus ("+"), period ("."), or hyphen ("-") + # characters; and is terminated by a colon (":"). + when %r{^\w[\w\d+.-]*:.*} + redirect_to_full_url(options, status) + when String + redirect_to_full_url(request.protocol + request.host_with_port + options, status) + when :back + if referer = request.headers["Referer"] + redirect_to(referer, :status=>status) + else + raise RedirectBackError + end + else + redirect_to_full_url(url_for(options), status) + end + end + + def redirect_to_full_url(url, status) + raise DoubleRenderError if performed? + response.redirect(url, interpret_status(status)) + @performed_redirect = true + end + + # Clears the redirected results from the headers, resets the status to 200 and returns + # the URL that was used to redirect or nil if there was no redirected URL + # Note that +redirect_to+ will change the body of the response to indicate a redirection. + # The response body is not reset here, see +erase_render_results+ + def erase_redirect_results #:nodoc: + @performed_redirect = false + response.redirected_to = nil + response.redirected_to_method_params = nil + response.status = DEFAULT_RENDER_STATUS_CODE + response.headers.delete('Location') + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/base/render.rb b/actionpack/lib/action_controller/base/render.rb new file mode 100644 index 0000000000..abba059969 --- /dev/null +++ b/actionpack/lib/action_controller/base/render.rb @@ -0,0 +1,378 @@ +module ActionController + DEFAULT_RENDER_STATUS_CODE = "200 OK" + + class DoubleRenderError < ActionControllerError #:nodoc: + DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"." + + def initialize(message = nil) + super(message || DEFAULT_MESSAGE) + end + end + + module Renderer + + protected + # Renders the content that will be returned to the browser as the response body. + # + # === Rendering an action + # + # Action rendering is the most common form and the type used automatically by Action Controller when nothing else is + # specified. By default, actions are rendered within the current layout (if one exists). + # + # # Renders the template for the action "goal" within the current controller + # render :action => "goal" + # + # # Renders the template for the action "short_goal" within the current controller, + # # but without the current active layout + # render :action => "short_goal", :layout => false + # + # # Renders the template for the action "long_goal" within the current controller, + # # but with a custom layout + # render :action => "long_goal", :layout => "spectacular" + # + # === Rendering partials + # + # Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page + # without reloading. Rendering of partials from the controller makes it possible to use the same partial template in + # both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the + # controller action responding to Ajax calls). By default, the current layout is not used. + # + # # Renders the same partial with a local variable. + # render :partial => "person", :locals => { :name => "david" } + # + # # Renders the partial, making @new_person available through + # # the local variable 'person' + # render :partial => "person", :object => @new_person + # + # # Renders a collection of the same partial by making each element + # # of @winners available through the local variable "person" as it + # # builds the complete response. + # render :partial => "person", :collection => @winners + # + # # Renders a collection of partials but with a custom local variable name + # render :partial => "admin_person", :collection => @winners, :as => :person + # + # # Renders the same collection of partials, but also renders the + # # person_divider partial between each person partial. + # render :partial => "person", :collection => @winners, :spacer_template => "person_divider" + # + # # Renders a collection of partials located in a view subfolder + # # outside of our current controller. In this example we will be + # # rendering app/views/shared/_note.r(html|xml) Inside the partial + # # each element of @new_notes is available as the local var "note". + # render :partial => "shared/note", :collection => @new_notes + # + # # Renders the partial with a status code of 500 (internal error). + # render :partial => "broken", :status => 500 + # + # Note that the partial filename must also be a valid Ruby variable name, + # so e.g. 2005 and register-user are invalid. + # + # + # == Automatic etagging + # + # Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the + # response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified + # and the response body will be set to an empty string. No etag header will be inserted if it's already set. + # + # === Rendering a template + # + # Template rendering works just like action rendering except that it takes a path relative to the template root. + # The current layout is automatically applied. + # + # # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb) + # render :template => "weblog/show" + # + # # Renders the template with a local variable + # render :template => "weblog/show", :locals => {:customer => Customer.new} + # + # === Rendering a file + # + # File rendering works just like action rendering except that it takes a filesystem path. By default, the path + # is assumed to be absolute, and the current layout is not applied. + # + # # Renders the template located at the absolute filesystem path + # render :file => "/path/to/some/template.erb" + # render :file => "c:/path/to/some/template.erb" + # + # # Renders a template within the current layout, and with a 404 status code + # render :file => "/path/to/some/template.erb", :layout => true, :status => 404 + # render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404 + # + # === Rendering text + # + # Rendering of text is usually used for tests or for rendering prepared content, such as a cache. By default, text + # rendering is not done within the active layout. + # + # # Renders the clear text "hello world" with status code 200 + # render :text => "hello world!" + # + # # Renders the clear text "Explosion!" with status code 500 + # render :text => "Explosion!", :status => 500 + # + # # Renders the clear text "Hi there!" within the current active layout (if one exists) + # render :text => "Hi there!", :layout => true + # + # # Renders the clear text "Hi there!" within the layout + # # placed in "app/views/layouts/special.r(html|xml)" + # render :text => "Hi there!", :layout => "special" + # + # The <tt>:text</tt> option can also accept a Proc object, which can be used to manually control the page generation. This should + # generally be avoided, as it violates the separation between code and content, and because almost everything that can be + # done with this method can also be done more cleanly using one of the other rendering methods, most notably templates. + # + # # Renders "Hello from code!" + # render :text => proc { |response, output| output.write("Hello from code!") } + # + # === Rendering XML + # + # Rendering XML sets the content type to application/xml. + # + # # Renders '<name>David</name>' + # render :xml => {:name => "David"}.to_xml + # + # It's not necessary to call <tt>to_xml</tt> on the object you want to render, since <tt>render</tt> will + # automatically do that for you: + # + # # Also renders '<name>David</name>' + # render :xml => {:name => "David"} + # + # === Rendering JSON + # + # Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected + # that the response will be parsed (or eval'd) for use as a data structure. + # + # # Renders '{"name": "David"}' + # render :json => {:name => "David"}.to_json + # + # It's not necessary to call <tt>to_json</tt> on the object you want to render, since <tt>render</tt> will + # automatically do that for you: + # + # # Also renders '{"name": "David"}' + # render :json => {:name => "David"} + # + # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag), + # so the <tt>:callback</tt> option is provided for these cases. + # + # # Renders 'show({"name": "David"})' + # render :json => {:name => "David"}.to_json, :callback => 'show' + # + # === Rendering an inline template + # + # Rendering of an inline template works as a cross between text and action rendering where the source for the template + # is supplied inline, like text, but its interpreted with ERb or Builder, like action. By default, ERb is used for rendering + # and the current layout is not used. + # + # # Renders "hello, hello, hello, again" + # render :inline => "<%= 'hello, ' * 3 + 'again' %>" + # + # # Renders "<p>Good seeing you!</p>" using Builder + # render :inline => "xml.p { 'Good seeing you!' }", :type => :builder + # + # # Renders "hello david" + # render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" } + # + # === Rendering inline JavaScriptGenerator page updates + # + # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details), + # you can also pass the <tt>:update</tt> parameter to +render+, along with a block, to render page updates inline. + # + # render :update do |page| + # page.replace_html 'user_list', :partial => 'user', :collection => @users + # page.visual_effect :highlight, 'user_list' + # end + # + # === Rendering vanilla JavaScript + # + # In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js. + # + # # Renders "alert('hello')" and sets the mime type to text/javascript + # render :js => "alert('hello')" + # + # === Rendering with status and location headers + # All renders take the <tt>:status</tt> and <tt>:location</tt> options and turn them into headers. They can even be used together: + # + # render :xml => post.to_xml, :status => :created, :location => post_url(post) + def render(options = nil, extra_options = {}, &block) #:doc: + raise DoubleRenderError, "Can only render or redirect once per action" if performed? + + options = { :layout => true } if options.nil? + original, options = options, extra_options unless options.is_a?(Hash) + + layout_name = options.delete(:layout) + + _process_options(options) + + if block_given? + @template.send(:_evaluate_assigns_and_ivars) + + generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block) + response.content_type = Mime::JS + return render_for_text(generator.to_s) + end + + if original + return render_for_name(original, layout_name, options) unless block_given? + end + + if options.key?(:text) + return render_for_text(@template._render_text(options[:text], + _pick_layout(layout_name), options)) + end + + file, template = options.values_at(:file, :template) + if file || template + file = template.sub(/^\//, '') if template + return render_for_file(file, [layout_name, !!template], options) + end + + if action_option = options[:action] + return render_for_action(action_option, [layout_name, true], options) + end + + if inline = options[:inline] + render_for_text(@template._render_inline(inline, _pick_layout(layout_name), options)) + + elsif xml = options[:xml] + response.content_type ||= Mime::XML + render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml) + + elsif js = options[:js] + response.content_type ||= Mime::JS + render_for_text(js) + + elsif json = options[:json] + json = json.to_json unless json.is_a?(String) + json = "#{options[:callback]}(#{json})" unless options[:callback].blank? + response.content_type ||= Mime::JSON + render_for_text(json) + + elsif partial = options[:partial] + if partial == true + parts = [action_name_base, formats, controller_name, true] + elsif partial.is_a?(String) + parts = partial_parts(partial, options) + else + return render_for_text(@template._render_partial(options)) + end + + render_for_parts(parts, layout_name, options) + + elsif options[:nothing] + render_for_text(nil) + + else + render_for_parts([action_name, formats, controller_path], layout_name, options) + end + end + + def partial_parts(name, options) + segments = name.split("/") + parts = segments.pop.split(".") + + case parts.size + when 1 + parts + when 2, 3 + extension = parts.delete_at(1).to_sym + if formats.include?(extension) + self.formats.replace [extension] + end + parts.pop if parts.size == 2 + end + + path = parts.join(".") + prefix = segments[0..-1].join("/") + prefix = prefix.blank? ? controller_path : prefix + parts = [path, formats, prefix] + parts.push options[:object] || true + end + + def formats + @_request.formats.map {|f| f.symbol }.compact + end + + def action_name_base(name = action_name) + (name.is_a?(String) ? name.sub(/^#{controller_path}\//, '') : name).to_s + end + + # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead + # of sending it as the response body to the browser. + def render_to_string(options = nil, &block) #:doc: + render(options, &block) + ensure + response.content_type = nil + erase_render_results + reset_variables_added_to_assigns + end + + # Clears the rendered results, allowing for another render to be performed. + def erase_render_results #:nodoc: + response.body = nil + @performed_render = false + end + + # Erase both render and redirect results + def erase_results #:nodoc: + erase_render_results + erase_redirect_results + end + + # Return a response that has no content (merely headers). The options + # argument is interpreted to be a hash of header names and values. + # This allows you to easily return a response that consists only of + # significant headers: + # + # head :created, :location => person_path(@person) + # + # It can also be used to return exceptional conditions: + # + # return head(:method_not_allowed) unless request.post? + # return head(:bad_request) unless valid_request? + # render + def head(*args) + if args.length > 2 + raise ArgumentError, "too many arguments to head" + elsif args.empty? + raise ArgumentError, "too few arguments to head" + end + options = args.extract_options! + status = interpret_status(args.shift || options.delete(:status) || :ok) + + options.each do |key, value| + headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s + end + + render :nothing => true, :status => status + end + + private + def render_for_name(name, layout, options) + case name.to_s.index('/') + when 0 + render_for_file(name, layout, options) + when nil + render_for_action(name, layout, options) + else + render_for_file(name.sub(/^\//, ''), [layout, true], options) + end + end + + def render_for_parts(parts, layout, options = {}) + tmp = view_paths.find_by_parts(*parts) + layout = _pick_layout(*layout) unless tmp.exempt_from_layout? + + render_for_text( + @template._render_template_with_layout(tmp, layout, options, parts[3])) + end + + def render_for_file(file, layout, options) + render_for_parts([file, [request.format.to_sym]], layout, options) + end + + def render_for_action(name, layout, options) + parts = [action_name_base(name), formats, controller_name] + render_for_parts(parts, layout, options) + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/request_forgery_protection.rb b/actionpack/lib/action_controller/base/request_forgery_protection.rb index f3e6288c26..f3e6288c26 100644 --- a/actionpack/lib/action_controller/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/base/request_forgery_protection.rb diff --git a/actionpack/lib/action_controller/base/responder.rb b/actionpack/lib/action_controller/base/responder.rb new file mode 100644 index 0000000000..f83abb5a4b --- /dev/null +++ b/actionpack/lib/action_controller/base/responder.rb @@ -0,0 +1,41 @@ +module ActionController + module Responder + def self.included(klass) + klass.extend ClassMethods + end + + private + def render_for_text(text = nil, append_response = false) #:nodoc: + @performed_render = true + + if append_response + response.body ||= '' + response.body << text.to_s + else + response.body = case text + when Proc then text + when nil then " " # Safari doesn't pass the headers of the return if the response is zero length + else text.to_s + end + end + end + + def action_methods + self.class.action_methods + end + + module ClassMethods + def action_methods + @action_methods ||= + # All public instance methods of this class, including ancestors + public_instance_methods(true).map { |m| m.to_s }.to_set - + # Except for public instance methods of Base and its ancestors + Base.public_instance_methods(true).map { |m| m.to_s } + + # Be sure to include shadowed public instance methods of this class + public_instance_methods(false).map { |m| m.to_s } - + # And always exclude explicitly hidden actions + hidden_actions + end + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/streaming.rb b/actionpack/lib/action_controller/base/streaming.rb index e1786913a7..e1786913a7 100644 --- a/actionpack/lib/action_controller/streaming.rb +++ b/actionpack/lib/action_controller/base/streaming.rb diff --git a/actionpack/lib/action_controller/verification.rb b/actionpack/lib/action_controller/base/verification.rb index 7bf09ba6ea..7bf09ba6ea 100644 --- a/actionpack/lib/action_controller/verification.rb +++ b/actionpack/lib/action_controller/base/verification.rb diff --git a/actionpack/lib/action_controller/cgi_ext.rb b/actionpack/lib/action_controller/cgi/ext.rb index 406b6f06d6..558748f4bd 100644 --- a/actionpack/lib/action_controller/cgi_ext.rb +++ b/actionpack/lib/action_controller/cgi/ext.rb @@ -1,6 +1,6 @@ -require 'action_controller/cgi_ext/stdinput' -require 'action_controller/cgi_ext/query_extension' -require 'action_controller/cgi_ext/cookie' +require 'action_controller/cgi/ext/stdinput' +require 'action_controller/cgi/ext/query_extension' +require 'action_controller/cgi/ext/cookie' class CGI #:nodoc: include ActionController::CgiExt::Stdinput diff --git a/actionpack/lib/action_controller/cgi_ext/cookie.rb b/actionpack/lib/action_controller/cgi/ext/cookie.rb index 9cd19bb12d..9cd19bb12d 100644 --- a/actionpack/lib/action_controller/cgi_ext/cookie.rb +++ b/actionpack/lib/action_controller/cgi/ext/cookie.rb diff --git a/actionpack/lib/action_controller/cgi_ext/query_extension.rb b/actionpack/lib/action_controller/cgi/ext/query_extension.rb index 9620fd2873..9620fd2873 100644 --- a/actionpack/lib/action_controller/cgi_ext/query_extension.rb +++ b/actionpack/lib/action_controller/cgi/ext/query_extension.rb diff --git a/actionpack/lib/action_controller/cgi_ext/stdinput.rb b/actionpack/lib/action_controller/cgi/ext/stdinput.rb index 5e9b6784af..5e9b6784af 100644 --- a/actionpack/lib/action_controller/cgi_ext/stdinput.rb +++ b/actionpack/lib/action_controller/cgi/ext/stdinput.rb diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi/process.rb index 7e5e95e135..ffcad5666a 100644 --- a/actionpack/lib/action_controller/cgi_process.rb +++ b/actionpack/lib/action_controller/cgi/process.rb @@ -1,5 +1,3 @@ -require 'action_controller/cgi_ext' - module ActionController #:nodoc: class CGIHandler module ProperStream diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatch/dispatcher.rb index 781bc48887..e205245f13 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatch/dispatcher.rb @@ -45,7 +45,7 @@ module ActionController end cattr_accessor :middleware - self.middleware = MiddlewareStack.new do |middleware| + self.middleware = ActionDispatch::MiddlewareStack.new do |middleware| middlewares = File.join(File.dirname(__FILE__), "middlewares.rb") middleware.instance_eval(File.read(middlewares)) end diff --git a/actionpack/lib/action_controller/middlewares.rb b/actionpack/lib/action_controller/dispatch/middlewares.rb index f9cfc2b18e..3bf3dbebab 100644 --- a/actionpack/lib/action_controller/middlewares.rb +++ b/actionpack/lib/action_controller/dispatch/middlewares.rb @@ -2,10 +2,10 @@ use "Rack::Lock", :if => lambda { !ActionController::Base.allow_concurrency } -use "ActionController::Failsafe" +use "ActionDispatch::Failsafe" -["ActionController::Session::CookieStore", - "ActionController::Session::MemCacheStore", +["ActionDispatch::Session::CookieStore", + "ActionDispatch::Session::MemCacheStore", "ActiveRecord::SessionStore"].each do |store| use(store, ActionController::Base.session_options, :if => lambda { @@ -16,6 +16,6 @@ use "ActionController::Failsafe" ) end -use "ActionController::RewindableInput" -use "ActionController::ParamsParser" +use "ActionDispatch::RewindableInput" +use "ActionDispatch::ParamsParser" use "Rack::MethodOverride" diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/dispatch/rescue.rb index 4b7d1e32fd..df0a976204 100644 --- a/actionpack/lib/action_controller/rescue.rb +++ b/actionpack/lib/action_controller/dispatch/rescue.rb @@ -60,8 +60,8 @@ module ActionController #:nodoc: module ClassMethods def call_with_exception(env, exception) #:nodoc: - request = env["action_controller.rescue.request"] ||= Request.new(env) - response = env["action_controller.rescue.response"] ||= Response.new + request = env["action_controller.rescue.request"] ||= ActionDispatch::Request.new(env) + response = env["action_controller.rescue.response"] ||= ActionDispatch::Response.new new.process(request, response, :rescue_action, exception) end end @@ -125,11 +125,13 @@ module ActionController #:nodoc: @template.instance_variable_set("@exception", exception) @template.instance_variable_set("@rescues_path", RESCUES_TEMPLATE_PATH) @template.instance_variable_set("@contents", - @template.render(:file => template_path_for_local_rescue(exception))) + @template._render_template(template_path_for_local_rescue(exception))) response.content_type = Mime::HTML - render_for_file(rescues_path("layout"), - response_code_for_rescue(exception)) + response.status = interpret_status(response_code_for_rescue(exception)) + + content = @template._render_template(rescues_path("layout")) + render_for_text(content) end def rescue_action_without_handler(exception) @@ -157,7 +159,7 @@ module ActionController #:nodoc: end def rescues_path(template_name) - RESCUES_TEMPLATE_PATH["rescues/#{template_name}.erb"] + RESCUES_TEMPLATE_PATH.find_template("rescues/#{template_name}.erb") end def template_path_for_local_rescue(exception) diff --git a/actionpack/lib/action_controller/templates/rescues/_request_and_response.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/_request_and_response.erb index 64b34650b1..64b34650b1 100644 --- a/actionpack/lib/action_controller/templates/rescues/_request_and_response.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/_request_and_response.erb diff --git a/actionpack/lib/action_controller/templates/rescues/_trace.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/_trace.erb index bb2d8375bd..bb2d8375bd 100644 --- a/actionpack/lib/action_controller/templates/rescues/_trace.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/_trace.erb diff --git a/actionpack/lib/action_controller/templates/rescues/diagnostics.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/diagnostics.erb index 669da1b26e..95be64511d 100644 --- a/actionpack/lib/action_controller/templates/rescues/diagnostics.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/diagnostics.erb @@ -6,6 +6,5 @@ </h1> <pre><%=h @exception.clean_message %></pre> -<%= render :file => @rescues_path["rescues/_trace.erb"] %> - -<%= render :file => @rescues_path["rescues/_request_and_response.erb"] %> +<%= @template._render_template(@rescues_path.find_template("rescues/_trace.erb")) %> +<%= @template._render_template(@rescues_path.find_template("rescues/_request_and_response.erb")) %>
\ No newline at end of file diff --git a/actionpack/lib/action_controller/templates/rescues/layout.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/layout.erb index 4a04742e40..4a04742e40 100644 --- a/actionpack/lib/action_controller/templates/rescues/layout.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/layout.erb diff --git a/actionpack/lib/action_controller/templates/rescues/missing_template.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/missing_template.erb index dbfdf76947..dbfdf76947 100644 --- a/actionpack/lib/action_controller/templates/rescues/missing_template.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/missing_template.erb diff --git a/actionpack/lib/action_controller/templates/rescues/routing_error.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/routing_error.erb index ccfa858cce..ccfa858cce 100644 --- a/actionpack/lib/action_controller/templates/rescues/routing_error.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/routing_error.erb diff --git a/actionpack/lib/action_controller/templates/rescues/template_error.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/template_error.erb index 2e34e03bd5..2e34e03bd5 100644 --- a/actionpack/lib/action_controller/templates/rescues/template_error.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/template_error.erb diff --git a/actionpack/lib/action_controller/templates/rescues/unknown_action.erb b/actionpack/lib/action_controller/dispatch/templates/rescues/unknown_action.erb index 683379da10..683379da10 100644 --- a/actionpack/lib/action_controller/templates/rescues/unknown_action.erb +++ b/actionpack/lib/action_controller/dispatch/templates/rescues/unknown_action.erb diff --git a/actionpack/lib/action_controller/failsafe.rb b/actionpack/lib/action_controller/failsafe.rb deleted file mode 100644 index 567581142c..0000000000 --- a/actionpack/lib/action_controller/failsafe.rb +++ /dev/null @@ -1,52 +0,0 @@ -module ActionController - class Failsafe - cattr_accessor :error_file_path - self.error_file_path = Rails.public_path if defined?(Rails.public_path) - - def initialize(app) - @app = app - end - - def call(env) - @app.call(env) - rescue Exception => exception - # Reraise exception in test environment - if env["rack.test"] - raise exception - else - failsafe_response(exception) - end - end - - private - def failsafe_response(exception) - log_failsafe_exception(exception) - [500, {'Content-Type' => 'text/html'}, failsafe_response_body] - rescue Exception => failsafe_error # Logger or IO errors - $stderr.puts "Error during failsafe response: #{failsafe_error}" - end - - def failsafe_response_body - error_path = "#{self.class.error_file_path}/500.html" - if File.exist?(error_path) - File.read(error_path) - else - "<html><body><h1>500 Internal Server Error</h1></body></html>" - end - end - - def log_failsafe_exception(exception) - message = "/!\\ FAILSAFE /!\\ #{Time.now}\n Status: 500 Internal Server Error\n" - message << " #{exception}\n #{exception.backtrace.join("\n ")}" if exception - failsafe_logger.fatal(message) - end - - def failsafe_logger - if defined?(Rails) && Rails.logger - Rails.logger - else - Logger.new($stderr) - end - end - end -end diff --git a/actionpack/lib/action_controller/headers.rb b/actionpack/lib/action_controller/headers.rb deleted file mode 100644 index 139669c66f..0000000000 --- a/actionpack/lib/action_controller/headers.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'active_support/memoizable' - -module ActionController - module Http - class Headers < ::Hash - extend ActiveSupport::Memoizable - - def initialize(*args) - if args.size == 1 && args[0].is_a?(Hash) - super() - update(args[0]) - else - super - end - end - - def [](header_name) - if include?(header_name) - super - else - super(env_name(header_name)) - end - end - - private - # Converts a HTTP header name to an environment variable name. - def env_name(header_name) - "HTTP_#{header_name.upcase.gsub(/-/, '_')}" - end - memoize :env_name - end - end -end diff --git a/actionpack/lib/action_controller/middleware_stack.rb b/actionpack/lib/action_controller/middleware_stack.rb deleted file mode 100644 index dbc2fda41e..0000000000 --- a/actionpack/lib/action_controller/middleware_stack.rb +++ /dev/null @@ -1,109 +0,0 @@ -module ActionController - class MiddlewareStack < Array - class Middleware - def self.new(klass, *args, &block) - if klass.is_a?(self) - klass - else - super - end - end - - attr_reader :args, :block - - def initialize(klass, *args, &block) - @klass = klass - - options = args.extract_options! - if options.has_key?(:if) - @conditional = options.delete(:if) - else - @conditional = true - end - args << options unless options.empty? - - @args = args - @block = block - end - - def klass - if @klass.is_a?(Class) - @klass - else - @klass.to_s.constantize - end - rescue NameError - @klass - end - - def active? - if @conditional.respond_to?(:call) - @conditional.call - else - @conditional - end - end - - def ==(middleware) - case middleware - when Middleware - klass == middleware.klass - when Class - klass == middleware - else - klass == middleware.to_s.constantize - end - end - - def inspect - str = klass.to_s - args.each { |arg| str += ", #{arg.inspect}" } - str - end - - def build(app) - if block - klass.new(app, *args, &block) - else - klass.new(app, *args) - end - end - end - - def initialize(*args, &block) - super(*args) - block.call(self) if block_given? - end - - def insert(index, *args, &block) - index = self.index(index) unless index.is_a?(Integer) - middleware = Middleware.new(*args, &block) - super(index, middleware) - end - - alias_method :insert_before, :insert - - def insert_after(index, *args, &block) - index = self.index(index) unless index.is_a?(Integer) - insert(index + 1, *args, &block) - end - - def swap(target, *args, &block) - insert_before(target, *args, &block) - delete(target) - end - - def use(*args, &block) - middleware = Middleware.new(*args, &block) - push(middleware) - end - - def active - find_all { |middleware| middleware.active? } - end - - def build(app) - active.reverse.inject(app) { |a, e| e.build(a) } - end - end -end diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime/responds.rb index 4b3d14c2d4..bac225ab2a 100644 --- a/actionpack/lib/action_controller/mime_responds.rb +++ b/actionpack/lib/action_controller/mime/responds.rb @@ -127,7 +127,7 @@ module ActionController #:nodoc: @order << mime_type @responses[mime_type] ||= Proc.new do - @response.template.template_format = mime_type.to_sym + @response.template.formats = [mime_type.to_sym] @response.content_type = mime_type.to_s block_given? ? block.call : @controller.send(:render, :action => @controller.action_name) end diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb deleted file mode 100644 index 7af8510b6b..0000000000 --- a/actionpack/lib/action_controller/mime_type.rb +++ /dev/null @@ -1,212 +0,0 @@ -require 'set' - -module Mime - SET = [] - EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? } - LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? } - - def self.[](type) - Type.lookup_by_extension(type) - end - - # Encapsulates the notion of a mime type. Can be used at render time, for example, with: - # - # class PostsController < ActionController::Base - # def show - # @post = Post.find(params[:id]) - # - # respond_to do |format| - # format.html - # format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"] } - # format.xml { render :xml => @people.to_xml } - # end - # end - # end - class Type - @@html_types = Set.new [:html, :all] - cattr_reader :html_types - - # These are the content types which browsers can generate without using ajax, flash, etc - # i.e. following a link, getting an image or posting a form. CSRF protection - # only needs to protect against these types. - @@browser_generated_types = Set.new [:html, :url_encoded_form, :multipart_form, :text] - cattr_reader :browser_generated_types - - - @@unverifiable_types = Set.new [:text, :json, :csv, :xml, :rss, :atom, :yaml] - def self.unverifiable_types - ActiveSupport::Deprecation.warn("unverifiable_types is deprecated and has no effect", caller) - @@unverifiable_types - end - - # A simple helper class used in parsing the accept header - class AcceptItem #:nodoc: - attr_accessor :order, :name, :q - - def initialize(order, name, q=nil) - @order = order - @name = name.strip - q ||= 0.0 if @name == Mime::ALL # default wilcard match to end of list - @q = ((q || 1.0).to_f * 100).to_i - end - - def to_s - @name - end - - def <=>(item) - result = item.q <=> q - result = order <=> item.order if result == 0 - result - end - - def ==(item) - name == (item.respond_to?(:name) ? item.name : item) - end - end - - class << self - def lookup(string) - LOOKUP[string] - end - - def lookup_by_extension(extension) - EXTENSION_LOOKUP[extension] - end - - # Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for - # rendering different HTML versions depending on the user agent, like an iPhone. - def register_alias(string, symbol, extension_synonyms = []) - register(string, symbol, [], extension_synonyms, true) - end - - def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) - Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) } - - SET << Mime.const_get(symbol.to_s.upcase) - - ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup - ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last } - end - - def parse(accept_header) - if accept_header !~ /,/ - [Mime::Type.lookup(accept_header)] - else - # keep track of creation order to keep the subsequent sort stable - list = [] - accept_header.split(/,/).each_with_index do |header, index| - params, q = header.split(/;\s*q=/) - if params - params.strip! - list << AcceptItem.new(index, params, q) unless params.empty? - end - end - list.sort! - - # Take care of the broken text/xml entry by renaming or deleting it - text_xml = list.index("text/xml") - app_xml = list.index(Mime::XML.to_s) - - if text_xml && app_xml - # set the q value to the max of the two - list[app_xml].q = [list[text_xml].q, list[app_xml].q].max - - # make sure app_xml is ahead of text_xml in the list - if app_xml > text_xml - list[app_xml], list[text_xml] = list[text_xml], list[app_xml] - app_xml, text_xml = text_xml, app_xml - end - - # delete text_xml from the list - list.delete_at(text_xml) - - elsif text_xml - list[text_xml].name = Mime::XML.to_s - end - - # Look for more specific XML-based types and sort them ahead of app/xml - - if app_xml - idx = app_xml - app_xml_type = list[app_xml] - - while(idx < list.length) - type = list[idx] - break if type.q < app_xml_type.q - if type.name =~ /\+xml$/ - list[app_xml], list[idx] = list[idx], list[app_xml] - app_xml = idx - end - idx += 1 - end - end - - list.map! { |i| Mime::Type.lookup(i.name) }.uniq! - list - end - end - end - - def initialize(string, symbol = nil, synonyms = []) - @symbol, @synonyms = symbol, synonyms - @string = string - end - - def to_s - @string - end - - def to_str - to_s - end - - def to_sym - @symbol || @string.to_sym - end - - def ===(list) - if list.is_a?(Array) - (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) } - else - super - end - end - - def ==(mime_type) - return false if mime_type.blank? - (@synonyms + [ self ]).any? do |synonym| - synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym - end - end - - def =~(mime_type) - return false if mime_type.blank? - regexp = Regexp.new(Regexp.quote(mime_type.to_s)) - (@synonyms + [ self ]).any? do |synonym| - synonym.to_s =~ regexp - end - end - - # Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See - # ActionController::RequestForgeryProtection. - def verify_request? - @@browser_generated_types.include?(to_sym) - end - - def html? - @@html_types.include?(to_sym) || @string =~ /html/ - end - - private - def method_missing(method, *args) - if method.to_s =~ /(\w+)\?$/ - $1.downcase.to_sym == to_sym - else - super - end - end - end -end - -require 'action_controller/mime_types' diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb deleted file mode 100644 index 2d7fba1173..0000000000 --- a/actionpack/lib/action_controller/mime_types.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Build list of Mime types for HTTP responses -# http://www.iana.org/assignments/media-types/ - -Mime::Type.register "*/*", :all -Mime::Type.register "text/plain", :text, [], %w(txt) -Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml ) -Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript ) -Mime::Type.register "text/css", :css -Mime::Type.register "text/calendar", :ics -Mime::Type.register "text/csv", :csv -Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml ) -Mime::Type.register "application/rss+xml", :rss -Mime::Type.register "application/atom+xml", :atom -Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml ) - -Mime::Type.register "multipart/form-data", :multipart_form -Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form - -# http://www.ietf.org/rfc/rfc4627.txt -# http://www.json.org/JSONRequest.html -Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
\ No newline at end of file diff --git a/actionpack/lib/action_controller/params_parser.rb b/actionpack/lib/action_controller/params_parser.rb deleted file mode 100644 index d269fe07fa..0000000000 --- a/actionpack/lib/action_controller/params_parser.rb +++ /dev/null @@ -1,71 +0,0 @@ -module ActionController - class ParamsParser - ActionController::Base.param_parsers[Mime::XML] = :xml_simple - ActionController::Base.param_parsers[Mime::JSON] = :json - - def initialize(app) - @app = app - end - - def call(env) - if params = parse_formatted_parameters(env) - env["action_controller.request.request_parameters"] = params - end - - @app.call(env) - end - - private - def parse_formatted_parameters(env) - request = Request.new(env) - - return false if request.content_length.zero? - - mime_type = content_type_from_legacy_post_data_format_header(env) || request.content_type - strategy = ActionController::Base.param_parsers[mime_type] - - return false unless strategy - - case strategy - when Proc - strategy.call(request.raw_post) - when :xml_simple, :xml_node - body = request.raw_post - body.blank? ? {} : Hash.from_xml(body).with_indifferent_access - when :yaml - YAML.load(request.raw_post) - when :json - body = request.raw_post - if body.blank? - {} - else - data = ActiveSupport::JSON.decode(body) - data = {:_json => data} unless data.is_a?(Hash) - data.with_indifferent_access - end - else - false - end - rescue Exception => e # YAML, XML or Ruby code block errors - raise - { "body" => request.raw_post, - "content_type" => request.content_type, - "content_length" => request.content_length, - "exception" => "#{e.message} (#{e.class})", - "backtrace" => e.backtrace } - end - - def content_type_from_legacy_post_data_format_header(env) - if x_post_format = env['HTTP_X_POST_DATA_FORMAT'] - case x_post_format.to_s.downcase - when 'yaml' - return Mime::YAML - when 'xml' - return Mime::XML - end - end - - nil - end - end -end diff --git a/actionpack/lib/action_controller/rack_ext.rb b/actionpack/lib/action_controller/rack_ext.rb deleted file mode 100644 index 2ba6654e3d..0000000000 --- a/actionpack/lib/action_controller/rack_ext.rb +++ /dev/null @@ -1,3 +0,0 @@ -require 'action_controller/rack_ext/lock' -require 'action_controller/rack_ext/multipart' -require 'action_controller/rack_ext/parse_query' diff --git a/actionpack/lib/action_controller/rack_ext/lock.rb b/actionpack/lib/action_controller/rack_ext/lock.rb deleted file mode 100644 index 9bf1889065..0000000000 --- a/actionpack/lib/action_controller/rack_ext/lock.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Rack - # Rack::Lock was commited to Rack core - # http://github.com/rack/rack/commit/7409b0c - # Remove this when Rack 1.0 is released - unless defined? Lock - class Lock - FLAG = 'rack.multithread'.freeze - - def initialize(app, lock = Mutex.new) - @app, @lock = app, lock - end - - def call(env) - old, env[FLAG] = env[FLAG], false - @lock.synchronize { @app.call(env) } - ensure - env[FLAG] = old - end - end - end -end diff --git a/actionpack/lib/action_controller/rack_ext/multipart.rb b/actionpack/lib/action_controller/rack_ext/multipart.rb deleted file mode 100644 index 3b142307e9..0000000000 --- a/actionpack/lib/action_controller/rack_ext/multipart.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Rack - module Utils - module Multipart - class << self - def parse_multipart_with_rewind(env) - result = parse_multipart_without_rewind(env) - - begin - env['rack.input'].rewind if env['rack.input'].respond_to?(:rewind) - rescue Errno::ESPIPE - # Handles exceptions raised by input streams that cannot be rewound - # such as when using plain CGI under Apache - end - - result - end - - alias_method_chain :parse_multipart, :rewind - end - end - end -end diff --git a/actionpack/lib/action_controller/rack_ext/parse_query.rb b/actionpack/lib/action_controller/rack_ext/parse_query.rb deleted file mode 100644 index 2f21a57770..0000000000 --- a/actionpack/lib/action_controller/rack_ext/parse_query.rb +++ /dev/null @@ -1,18 +0,0 @@ -# Rack does not automatically cleanup Safari 2 AJAX POST body -# This has not yet been commited to Rack, please +1 this ticket: -# http://rack.lighthouseapp.com/projects/22435/tickets/19 - -module Rack - module Utils - alias_method :parse_query_without_ajax_body_cleanup, :parse_query - module_function :parse_query_without_ajax_body_cleanup - - def parse_query(qs, d = '&;') - qs = qs.dup - qs.chop! if qs[-1] == 0 - qs.gsub!(/&_=$/, '') - parse_query_without_ajax_body_cleanup(qs, d) - end - module_function :parse_query - end -end diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 742d290ad6..6bda27e23a 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,4 +1,4 @@ -module ActionController +module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or # Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate # the view actions to a higher logical level. Example: diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb deleted file mode 100755 index a8729bb6f5..0000000000 --- a/actionpack/lib/action_controller/request.rb +++ /dev/null @@ -1,474 +0,0 @@ -require 'tempfile' -require 'stringio' -require 'strscan' - -require 'active_support/memoizable' -require 'action_controller/cgi_ext' - -module ActionController - class Request < Rack::Request - - %w[ AUTH_TYPE GATEWAY_INTERFACE - PATH_TRANSLATED REMOTE_HOST - REMOTE_IDENT REMOTE_USER REMOTE_ADDR - SERVER_NAME SERVER_PROTOCOL - - HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING - HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM - HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env| - define_method(env.sub(/^HTTP_/n, '').downcase) do - @env[env] - end - end - - def key?(key) - @env.key?(key) - end - - HTTP_METHODS = %w(get head put post delete options) - HTTP_METHOD_LOOKUP = HTTP_METHODS.inject({}) { |h, m| h[m] = h[m.upcase] = m.to_sym; h } - - # Returns the true HTTP request \method as a lowercase symbol, such as - # <tt>:get</tt>. If the request \method is not listed in the HTTP_METHODS - # constant above, an UnknownHttpMethod exception is raised. - def request_method - @request_method ||= HTTP_METHOD_LOOKUP[super] || raise(UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}") - end - - # Returns the HTTP request \method used for action processing as a - # lowercase symbol, such as <tt>:post</tt>. (Unlike #request_method, this - # method returns <tt>:get</tt> for a HEAD request because the two are - # functionally equivalent from the application's perspective.) - def method - request_method == :head ? :get : request_method - end - - # Is this a GET (or HEAD) request? Equivalent to <tt>request.method == :get</tt>. - def get? - method == :get - end - - # Is this a POST request? Equivalent to <tt>request.method == :post</tt>. - def post? - request_method == :post - end - - # Is this a PUT request? Equivalent to <tt>request.method == :put</tt>. - def put? - request_method == :put - end - - # Is this a DELETE request? Equivalent to <tt>request.method == :delete</tt>. - def delete? - request_method == :delete - end - - # Is this a HEAD request? Since <tt>request.method</tt> sees HEAD as <tt>:get</tt>, - # this \method checks the actual HTTP \method directly. - def head? - request_method == :head - end - - # Provides access to the request's HTTP headers, for example: - # - # request.headers["Content-Type"] # => "text/plain" - def headers - @headers ||= ActionController::Http::Headers.new(@env) - end - - # Returns the content length of the request as an integer. - def content_length - super.to_i - end - - # The MIME type of the HTTP request, such as Mime::XML. - # - # For backward compatibility, the post \format is extracted from the - # X-Post-Data-Format HTTP header if present. - def content_type - @content_type ||= begin - if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/ - Mime::Type.lookup($1.strip.downcase) - else - nil - end - end - end - - # Returns the accepted MIME type for the request. - def accepts - @accepts ||= begin - header = @env['HTTP_ACCEPT'].to_s.strip - - if header.empty? - [content_type, Mime::ALL].compact - else - Mime::Type.parse(header) - end - end - end - - def if_modified_since - if since = env['HTTP_IF_MODIFIED_SINCE'] - Time.rfc2822(since) rescue nil - end - end - - def if_none_match - env['HTTP_IF_NONE_MATCH'] - end - - def not_modified?(modified_at) - if_modified_since && modified_at && if_modified_since >= modified_at - end - - def etag_matches?(etag) - if_none_match && if_none_match == etag - end - - # Check response freshness (Last-Modified and ETag) against request - # If-Modified-Since and If-None-Match conditions. If both headers are - # supplied, both must match, or the request is not considered fresh. - def fresh?(response) - case - when if_modified_since && if_none_match - not_modified?(response.last_modified) && etag_matches?(response.etag) - when if_modified_since - not_modified?(response.last_modified) - when if_none_match - etag_matches?(response.etag) - else - false - end - end - - ONLY_ALL = [Mime::ALL].freeze - - # Returns the Mime type for the \format used in the request. - # - # GET /posts/5.xml | request.format => Mime::XML - # GET /posts/5.xhtml | request.format => Mime::HTML - # GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt> - - def format - @format ||= - if parameters[:format] - Mime[parameters[:format]] - elsif Base.use_accept_header && !(accepts == ONLY_ALL) - accepts.first - elsif xhr? then Mime::JS - else Mime::HTML - end - end - - def formats - @formats = - if Base.use_accept_header - Array(Mime[parameters[:format]] || accepts) - else - [format] - end - end - - # Sets the \format by string extension, which can be used to force custom formats - # that are not controlled by the extension. - # - # class ApplicationController < ActionController::Base - # before_filter :adjust_format_for_iphone - # - # private - # def adjust_format_for_iphone - # request.format = :iphone if request.env["HTTP_USER_AGENT"][/iPhone/] - # end - # end - def format=(extension) - parameters[:format] = extension.to_s - @format = Mime::Type.lookup_by_extension(parameters[:format]) - end - - # Returns a symbolized version of the <tt>:format</tt> parameter of the request. - # If no \format is given it returns <tt>:js</tt>for Ajax requests and <tt>:html</tt> - # otherwise. - def template_format - parameter_format = parameters[:format] - - if parameter_format - parameter_format - elsif xhr? - :js - else - :html - end - end - - def cache_format - parameters[:format] - end - - # Returns true if the request's "X-Requested-With" header contains - # "XMLHttpRequest". (The Prototype Javascript library sends this header with - # every Ajax request.) - def xml_http_request? - !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i) - end - alias xhr? :xml_http_request? - - # Which IP addresses are "trusted proxies" that can be stripped from - # the right-hand-side of X-Forwarded-For - TRUSTED_PROXIES = /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i - - # Determines originating IP address. REMOTE_ADDR is the standard - # but will fail if the user is behind a proxy. HTTP_CLIENT_IP and/or - # HTTP_X_FORWARDED_FOR are set by proxies so check for these if - # REMOTE_ADDR is a proxy. HTTP_X_FORWARDED_FOR may be a comma- - # delimited list in the case of multiple chained proxies; the last - # address which is not trusted is the originating IP. - def remote_ip - remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].scan(/[^,\s]+/) - - unless remote_addr_list.blank? - not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES} - return not_trusted_addrs.first unless not_trusted_addrs.empty? - end - remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',') - - if @env.include? 'HTTP_CLIENT_IP' - if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP']) - # We don't know which came from the proxy, and which from the user - raise ActionControllerError.new(<<EOM) -IP spoofing attack?! -HTTP_CLIENT_IP=#{@env['HTTP_CLIENT_IP'].inspect} -HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect} -EOM - end - - return @env['HTTP_CLIENT_IP'] - end - - if remote_ips - while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip - remote_ips.pop - end - - return remote_ips.last.strip - end - - @env['REMOTE_ADDR'] - end - - # Returns the lowercase name of the HTTP server software. - def server_software - (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil - end - - # Returns the complete URL used for this request. - def url - protocol + host_with_port + request_uri - end - - # Returns 'https://' if this is an SSL request and 'http://' otherwise. - def protocol - ssl? ? 'https://' : 'http://' - end - - # Is this an SSL request? - def ssl? - @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' - end - - # Returns the \host for this request, such as "example.com". - def raw_host_with_port - if forwarded = env["HTTP_X_FORWARDED_HOST"] - forwarded.split(/,\s?/).last - else - env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}" - end - end - - # Returns the host for this request, such as example.com. - def host - raw_host_with_port.sub(/:\d+$/, '') - end - - # Returns a \host:\port string for this request, such as "example.com" or - # "example.com:8080". - def host_with_port - "#{host}#{port_string}" - end - - # Returns the port number of this request as an integer. - def port - if raw_host_with_port =~ /:(\d+)$/ - $1.to_i - else - standard_port - end - end - - # Returns the standard \port number for this request's protocol. - def standard_port - case protocol - when 'https://' then 443 - else 80 - end - end - - # Returns a \port suffix like ":8080" if the \port number of this request - # is not the default HTTP \port 80 or HTTPS \port 443. - def port_string - port == standard_port ? '' : ":#{port}" - end - - # Returns the \domain part of a \host, such as "rubyonrails.org" in "www.rubyonrails.org". You can specify - # a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk". - def domain(tld_length = 1) - return nil unless named_host?(host) - - host.split('.').last(1 + tld_length).join('.') - end - - # Returns all the \subdomains as an array, so <tt>["dev", "www"]</tt> would be - # returned for "dev.www.rubyonrails.org". You can specify a different <tt>tld_length</tt>, - # such as 2 to catch <tt>["www"]</tt> instead of <tt>["www", "rubyonrails"]</tt> - # in "www.rubyonrails.co.uk". - def subdomains(tld_length = 1) - return [] unless named_host?(host) - parts = host.split('.') - parts[0..-(tld_length+2)] - end - - # Returns the query string, accounting for server idiosyncrasies. - def query_string - @env['QUERY_STRING'].present? ? @env['QUERY_STRING'] : (@env['REQUEST_URI'].split('?', 2)[1] || '') - end - - # Returns the request URI, accounting for server idiosyncrasies. - # WEBrick includes the full URL. IIS leaves REQUEST_URI blank. - def request_uri - if uri = @env['REQUEST_URI'] - # Remove domain, which webrick puts into the request_uri. - (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri - else - # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO. - uri = @env['PATH_INFO'].to_s - - if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) - uri = uri.sub(/#{script_filename}\//, '') - end - - env_qs = @env['QUERY_STRING'].to_s - uri += "?#{env_qs}" unless env_qs.empty? - - if uri.blank? - @env.delete('REQUEST_URI') - else - @env['REQUEST_URI'] = uri - end - end - end - - # Returns the interpreted \path to requested resource after all the installation - # directory of this application was taken into account. - def path - path = request_uri.to_s[/\A[^\?]*/] - path.sub!(/\A#{ActionController::Base.relative_url_root}/, '') - path - end - - # Read the request \body. This is useful for web services that need to - # work with raw requests directly. - def raw_post - unless @env.include? 'RAW_POST_DATA' - @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i) - body.rewind if body.respond_to?(:rewind) - end - @env['RAW_POST_DATA'] - end - - # Returns both GET and POST \parameters in a single hash. - def parameters - @parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access - end - alias_method :params, :parameters - - def path_parameters=(parameters) #:nodoc: - @env["rack.routing_args"] = parameters - @symbolized_path_parameters = @parameters = nil - end - - # The same as <tt>path_parameters</tt> with explicitly symbolized keys. - def symbolized_path_parameters - @symbolized_path_parameters ||= path_parameters.symbolize_keys - end - - # Returns a hash with the \parameters used to form the \path of the request. - # Returned hash keys are strings: - # - # {'action' => 'my_action', 'controller' => 'my_controller'} - # - # See <tt>symbolized_path_parameters</tt> for symbolized keys. - def path_parameters - @env["rack.routing_args"] ||= {} - end - - # The request body is an IO input stream. If the RAW_POST_DATA environment - # variable is already set, wrap it in a StringIO. - def body - if raw_post = @env['RAW_POST_DATA'] - raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding) - StringIO.new(raw_post) - else - @env['rack.input'] - end - end - - def form_data? - FORM_DATA_MEDIA_TYPES.include?(content_type.to_s) - end - - # Override Rack's GET method to support nested query strings - def GET - @env["action_controller.request.query_parameters"] ||= UrlEncodedPairParser.parse_query_parameters(query_string) - end - alias_method :query_parameters, :GET - - # Override Rack's POST method to support nested query strings - def POST - @env["action_controller.request.request_parameters"] ||= UrlEncodedPairParser.parse_hash_parameters(super) - end - alias_method :request_parameters, :POST - - def body_stream #:nodoc: - @env['rack.input'] - end - - def session - @env['rack.session'] ||= {} - end - - def session=(session) #:nodoc: - @env['rack.session'] = session - end - - def reset_session - @env['rack.session'] = {} - end - - def session_options - @env['rack.session.options'] ||= {} - end - - def session_options=(options) - @env['rack.session.options'] = options - end - - def server_port - @env['SERVER_PORT'].to_i - end - - private - def named_host?(host) - !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) - end - end -end diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb deleted file mode 100644 index 27860a6207..0000000000 --- a/actionpack/lib/action_controller/response.rb +++ /dev/null @@ -1,255 +0,0 @@ -require 'digest/md5' - -module ActionController # :nodoc: - # Represents an HTTP response generated by a controller action. One can use - # an ActionController::Response object to retrieve the current state - # of the response, or customize the response. An Response object can - # either represent a "real" HTTP response (i.e. one that is meant to be sent - # back to the web browser) or a test response (i.e. one that is generated - # from integration tests). See CgiResponse and TestResponse, respectively. - # - # Response is mostly a Ruby on Rails framework implement detail, and - # should never be used directly in controllers. Controllers should use the - # methods defined in ActionController::Base instead. For example, if you want - # to set the HTTP response's content MIME type, then use - # ActionControllerBase#headers instead of Response#headers. - # - # Nevertheless, integration tests may want to inspect controller responses in - # more detail, and that's when Response can be useful for application - # developers. Integration test methods such as - # ActionController::Integration::Session#get and - # ActionController::Integration::Session#post return objects of type - # TestResponse (which are of course also of type Response). - # - # For example, the following demo integration "test" prints the body of the - # controller response to the console: - # - # class DemoControllerTest < ActionController::IntegrationTest - # def test_print_root_path_to_console - # get('/') - # puts @response.body - # end - # end - class Response < Rack::Response - DEFAULT_HEADERS = { "Cache-Control" => "no-cache" } - attr_accessor :request - - attr_accessor :session, :assigns, :template, :layout - attr_accessor :redirected_to, :redirected_to_method_params - - delegate :default_charset, :to => 'ActionController::Base' - - def initialize - @status = 200 - @header = DEFAULT_HEADERS.dup - - @writer = lambda { |x| @body << x } - @block = nil - - @body = "", - @session, @assigns = [], [] - end - - def location; headers['Location'] end - def location=(url) headers['Location'] = url end - - - # Sets the HTTP response's content MIME type. For example, in the controller - # you could write this: - # - # response.content_type = "text/plain" - # - # If a character set has been defined for this response (see charset=) then - # the character set information will also be included in the content type - # information. - def content_type=(mime_type) - self.headers["Content-Type"] = - if mime_type =~ /charset/ || (c = charset).nil? - mime_type.to_s - else - "#{mime_type}; charset=#{c}" - end - end - - # Returns the response's content MIME type, or nil if content type has been set. - def content_type - content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0] - content_type.blank? ? nil : content_type - end - - # Set the charset of the Content-Type header. Set to nil to remove it. - # If no content type is set, it defaults to HTML. - def charset=(charset) - headers["Content-Type"] = - if charset - "#{content_type || Mime::HTML}; charset=#{charset}" - else - content_type || Mime::HTML.to_s - end - end - - def charset - charset = String(headers["Content-Type"] || headers["type"]).split(";")[1] - charset.blank? ? nil : charset.strip.split("=")[1] - end - - def last_modified - if last = headers['Last-Modified'] - Time.httpdate(last) - end - end - - def last_modified? - headers.include?('Last-Modified') - end - - def last_modified=(utc_time) - headers['Last-Modified'] = utc_time.httpdate - end - - def etag - headers['ETag'] - end - - def etag? - headers.include?('ETag') - end - - def etag=(etag) - if etag.blank? - headers.delete('ETag') - else - headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") - end - end - - def redirect(url, status) - self.status = status - self.location = url.gsub(/[\r\n]/, '') - self.body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>" - end - - def sending_file? - headers["Content-Transfer-Encoding"] == "binary" - end - - def assign_default_content_type_and_charset! - self.content_type ||= Mime::HTML - self.charset ||= default_charset unless sending_file? - end - - def prepare! - assign_default_content_type_and_charset! - handle_conditional_get! - set_content_length! - convert_content_type! - convert_language! - convert_expires! - convert_cookies! - end - - def each(&callback) - if @body.respond_to?(:call) - @writer = lambda { |x| callback.call(x) } - @body.call(self, self) - elsif @body.is_a?(String) - @body.each_line(&callback) - else - @body.each(&callback) - end - - @writer = callback - @block.call(self) if @block - end - - def write(str) - @writer.call str.to_s - str - end - - # Over Rack::Response#set_cookie to add HttpOnly option - def set_cookie(key, value) - case value - when Hash - domain = "; domain=" + value[:domain] if value[:domain] - path = "; path=" + value[:path] if value[:path] - # According to RFC 2109, we need dashes here. - # N.B.: cgi.rb uses spaces... - expires = "; expires=" + value[:expires].clone.gmtime. - strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires] - secure = "; secure" if value[:secure] - httponly = "; HttpOnly" if value[:http_only] - value = value[:value] - end - value = [value] unless Array === value - cookie = ::Rack::Utils.escape(key) + "=" + - value.map { |v| ::Rack::Utils.escape v }.join("&") + - "#{domain}#{path}#{expires}#{secure}#{httponly}" - - case self["Set-Cookie"] - when Array - self["Set-Cookie"] << cookie - when String - self["Set-Cookie"] = [self["Set-Cookie"], cookie] - when nil - self["Set-Cookie"] = cookie - end - end - - private - def handle_conditional_get! - if etag? || last_modified? - set_conditional_cache_control! - elsif nonempty_ok_response? - self.etag = body - - if request && request.etag_matches?(etag) - self.status = '304 Not Modified' - self.body = '' - end - - set_conditional_cache_control! - end - end - - def nonempty_ok_response? - ok = !status || status.to_s[0..2] == '200' - ok && body.is_a?(String) && !body.empty? - end - - def set_conditional_cache_control! - if headers['Cache-Control'] == DEFAULT_HEADERS['Cache-Control'] - headers['Cache-Control'] = 'private, max-age=0, must-revalidate' - end - end - - def convert_content_type! - headers['Content-Type'] ||= "text/html" - headers['Content-Type'] += "; charset=" + headers.delete('charset') if headers['charset'] - end - - # Don't set the Content-Length for block-based bodies as that would mean - # reading it all into memory. Not nice for, say, a 2GB streaming file. - def set_content_length! - if status && status.to_s[0..2] == '204' - headers.delete('Content-Length') - elsif length = headers['Content-Length'] - headers['Content-Length'] = length.to_s - elsif !body.respond_to?(:call) && (!status || status.to_s[0..2] != '304') - headers["Content-Length"] = body.size.to_s - end - end - - def convert_language! - headers["Content-Language"] = headers.delete("language") if headers["language"] - end - - def convert_expires! - headers["Expires"] = headers.delete("") if headers["expires"] - end - - def convert_cookies! - headers['Set-Cookie'] = Array(headers['Set-Cookie']).compact - end - end -end diff --git a/actionpack/lib/action_controller/rewindable_input.rb b/actionpack/lib/action_controller/rewindable_input.rb deleted file mode 100644 index cedfb7fd75..0000000000 --- a/actionpack/lib/action_controller/rewindable_input.rb +++ /dev/null @@ -1,28 +0,0 @@ -module ActionController - class RewindableInput - class RewindableIO < ActiveSupport::BasicObject - def initialize(io) - @io = io - @rewindable = io.is_a?(::StringIO) - end - - def method_missing(method, *args, &block) - unless @rewindable - @io = ::StringIO.new(@io.read) - @rewindable = true - end - - @io.__send__(method, *args, &block) - end - end - - def initialize(app) - @app = app - end - - def call(env) - env['rack.input'] = RewindableIO.new(env['rack.input']) - @app.call(env) - end - end -end diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb index 924d1aa6bd..924d1aa6bd 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb diff --git a/actionpack/lib/action_controller/url_rewriter.rb b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb index bb6cb437b7..bb6cb437b7 100644 --- a/actionpack/lib/action_controller/url_rewriter.rb +++ b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/routing/resources.rb index e8988aa737..e8988aa737 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/routing/resources.rb diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 044ace7de1..70cd1f642d 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -428,7 +428,7 @@ module ActionController end def call(env) - request = Request.new(env) + request = ActionDispatch::Request.new(env) app = Routing::Routes.recognize(request) app.call(env).to_a end diff --git a/actionpack/lib/action_controller/session/abstract_store.rb b/actionpack/lib/action_controller/session/abstract_store.rb deleted file mode 100644 index 9434c2e05e..0000000000 --- a/actionpack/lib/action_controller/session/abstract_store.rb +++ /dev/null @@ -1,168 +0,0 @@ -require 'rack/utils' - -module ActionController - module Session - class AbstractStore - ENV_SESSION_KEY = 'rack.session'.freeze - ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze - - HTTP_COOKIE = 'HTTP_COOKIE'.freeze - SET_COOKIE = 'Set-Cookie'.freeze - - class SessionHash < Hash - def initialize(by, env) - super() - @by = by - @env = env - @loaded = false - end - - def id - load! unless @loaded - @id - end - - def session_id - ActiveSupport::Deprecation.warn( - "ActionController::Session::AbstractStore::SessionHash#session_id" + - "has been deprecated.Please use #id instead.", caller) - id - end - - def [](key) - load! unless @loaded - super - end - - def []=(key, value) - load! unless @loaded - super - end - - def to_hash - h = {}.replace(self) - h.delete_if { |k,v| v.nil? } - h - end - - def data - ActiveSupport::Deprecation.warn( - "ActionController::Session::AbstractStore::SessionHash#data" + - "has been deprecated.Please use #to_hash instead.", caller) - to_hash - end - - private - def loaded? - @loaded - end - - def load! - @id, session = @by.send(:load_session, @env) - replace(session) - @loaded = true - end - end - - DEFAULT_OPTIONS = { - :key => '_session_id', - :path => '/', - :domain => nil, - :expire_after => nil, - :secure => false, - :httponly => true, - :cookie_only => true - } - - def initialize(app, options = {}) - # Process legacy CGI options - options = options.symbolize_keys - if options.has_key?(:session_path) - options[:path] = options.delete(:session_path) - end - if options.has_key?(:session_key) - options[:key] = options.delete(:session_key) - end - if options.has_key?(:session_http_only) - options[:httponly] = options.delete(:session_http_only) - end - - @app = app - @default_options = DEFAULT_OPTIONS.merge(options) - @key = @default_options[:key] - @cookie_only = @default_options[:cookie_only] - end - - def call(env) - session = SessionHash.new(self, env) - - env[ENV_SESSION_KEY] = session - env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup - - response = @app.call(env) - - session_data = env[ENV_SESSION_KEY] - options = env[ENV_SESSION_OPTIONS_KEY] - - if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after] - session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?) - - if session_data.is_a?(AbstractStore::SessionHash) - sid = session_data.id - else - sid = generate_sid - end - - unless set_session(env, sid, session_data.to_hash) - return response - end - - cookie = Rack::Utils.escape(@key) + '=' + Rack::Utils.escape(sid) - cookie << "; domain=#{options[:domain]}" if options[:domain] - cookie << "; path=#{options[:path]}" if options[:path] - if options[:expire_after] - expiry = Time.now + options[:expire_after] - cookie << "; expires=#{expiry.httpdate}" - end - cookie << "; Secure" if options[:secure] - cookie << "; HttpOnly" if options[:httponly] - - headers = response[1] - case a = headers[SET_COOKIE] - when Array - a << cookie - when String - headers[SET_COOKIE] = [a, cookie] - when nil - headers[SET_COOKIE] = cookie - end - end - - response - end - - private - def generate_sid - ActiveSupport::SecureRandom.hex(16) - end - - def load_session(env) - request = Rack::Request.new(env) - sid = request.cookies[@key] - unless @cookie_only - sid ||= request.params[@key] - end - sid, session = get_session(env, sid) - [sid, session] - end - - def get_session(env, sid) - raise '#get_session needs to be implemented.' - end - - def set_session(env, sid, session_data) - raise '#set_session needs to be implemented.' - end - end - end -end diff --git a/actionpack/lib/action_controller/session/cookie_store.rb b/actionpack/lib/action_controller/session/cookie_store.rb deleted file mode 100644 index 5a728d1877..0000000000 --- a/actionpack/lib/action_controller/session/cookie_store.rb +++ /dev/null @@ -1,224 +0,0 @@ -module ActionController - module Session - # This cookie-based session store is the Rails default. Sessions typically - # contain at most a user_id and flash message; both fit within the 4K cookie - # size limit. Cookie-based sessions are dramatically faster than the - # alternatives. - # - # If you have more than 4K of session data or don't want your data to be - # visible to the user, pick another session store. - # - # CookieOverflow is raised if you attempt to store more than 4K of data. - # - # A message digest is included with the cookie to ensure data integrity: - # a user cannot alter his +user_id+ without knowing the secret key - # included in the hash. New apps are generated with a pregenerated secret - # in config/environment.rb. Set your own for old apps you're upgrading. - # - # Session options: - # - # * <tt>:secret</tt>: An application-wide key string or block returning a - # string called per generated digest. The block is called with the - # CGI::Session instance as an argument. It's important that the secret - # is not vulnerable to a dictionary attack. Therefore, you should choose - # a secret consisting of random numbers and letters and more than 30 - # characters. Examples: - # - # :secret => '449fe2e7daee471bffae2fd8dc02313d' - # :secret => Proc.new { User.current_user.secret_key } - # - # * <tt>:digest</tt>: The message digest algorithm used to verify session - # integrity defaults to 'SHA1' but may be any digest provided by OpenSSL, - # such as 'MD5', 'RIPEMD160', 'SHA256', etc. - # - # To generate a secret key for an existing application, run - # "rake secret" and set the key in config/environment.rb. - # - # Note that changing digest or secret invalidates all existing sessions! - class CookieStore - # Cookies can typically store 4096 bytes. - MAX = 4096 - SECRET_MIN_LENGTH = 30 # characters - - DEFAULT_OPTIONS = { - :key => '_session_id', - :domain => nil, - :path => "/", - :expire_after => nil, - :httponly => true - }.freeze - - ENV_SESSION_KEY = "rack.session".freeze - ENV_SESSION_OPTIONS_KEY = "rack.session.options".freeze - HTTP_SET_COOKIE = "Set-Cookie".freeze - - # Raised when storing more than 4K of session data. - class CookieOverflow < StandardError; end - - def initialize(app, options = {}) - # Process legacy CGI options - options = options.symbolize_keys - if options.has_key?(:session_path) - options[:path] = options.delete(:session_path) - end - if options.has_key?(:session_key) - options[:key] = options.delete(:session_key) - end - if options.has_key?(:session_http_only) - options[:httponly] = options.delete(:session_http_only) - end - - @app = app - - # The session_key option is required. - ensure_session_key(options[:key]) - @key = options.delete(:key).freeze - - # The secret option is required. - ensure_secret_secure(options[:secret]) - @secret = options.delete(:secret).freeze - - @digest = options.delete(:digest) || 'SHA1' - @verifier = verifier_for(@secret, @digest) - - @default_options = DEFAULT_OPTIONS.merge(options).freeze - - freeze - end - - def call(env) - env[ENV_SESSION_KEY] = AbstractStore::SessionHash.new(self, env) - env[ENV_SESSION_OPTIONS_KEY] = @default_options - - status, headers, body = @app.call(env) - - session_data = env[ENV_SESSION_KEY] - options = env[ENV_SESSION_OPTIONS_KEY] - - if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after] - session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?) - session_data = marshal(session_data.to_hash) - - raise CookieOverflow if session_data.size > MAX - - cookie = Hash.new - cookie[:value] = session_data - unless options[:expire_after].nil? - cookie[:expires] = Time.now + options[:expire_after] - end - - cookie = build_cookie(@key, cookie.merge(options)) - case headers[HTTP_SET_COOKIE] - when Array - headers[HTTP_SET_COOKIE] << cookie - when String - headers[HTTP_SET_COOKIE] = [headers[HTTP_SET_COOKIE], cookie] - when nil - headers[HTTP_SET_COOKIE] = cookie - end - end - - [status, headers, body] - end - - private - # Should be in Rack::Utils soon - def build_cookie(key, value) - case value - when Hash - domain = "; domain=" + value[:domain] if value[:domain] - path = "; path=" + value[:path] if value[:path] - # According to RFC 2109, we need dashes here. - # N.B.: cgi.rb uses spaces... - expires = "; expires=" + value[:expires].clone.gmtime. - strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires] - secure = "; secure" if value[:secure] - httponly = "; httponly" if value[:httponly] - value = value[:value] - end - value = [value] unless Array === value - cookie = Rack::Utils.escape(key) + "=" + - value.map { |v| Rack::Utils.escape(v) }.join("&") + - "#{domain}#{path}#{expires}#{secure}#{httponly}" - end - - def load_session(env) - request = Rack::Request.new(env) - session_data = request.cookies[@key] - data = unmarshal(session_data) || persistent_session_id!({}) - [data[:session_id], data] - end - - # Marshal a session hash into safe cookie data. Include an integrity hash. - def marshal(session) - @verifier.generate(persistent_session_id!(session)) - end - - # Unmarshal cookie data to a hash and verify its integrity. - def unmarshal(cookie) - persistent_session_id!(@verifier.verify(cookie)) if cookie - rescue ActiveSupport::MessageVerifier::InvalidSignature - nil - end - - def ensure_session_key(key) - if key.blank? - raise ArgumentError, 'A key is required to write a ' + - 'cookie containing the session data. Use ' + - 'config.action_controller.session = { :key => ' + - '"_myapp_session", :secret => "some secret phrase" } in ' + - 'config/environment.rb' - end - end - - # To prevent users from using something insecure like "Password" we make sure that the - # secret they've provided is at least 30 characters in length. - def ensure_secret_secure(secret) - # There's no way we can do this check if they've provided a proc for the - # secret. - return true if secret.is_a?(Proc) - - if secret.blank? - raise ArgumentError, "A secret is required to generate an " + - "integrity hash for cookie session data. Use " + - "config.action_controller.session = { :key => " + - "\"_myapp_session\", :secret => \"some secret phrase of at " + - "least #{SECRET_MIN_LENGTH} characters\" } " + - "in config/environment.rb" - end - - if secret.length < SECRET_MIN_LENGTH - raise ArgumentError, "Secret should be something secure, " + - "like \"#{ActiveSupport::SecureRandom.hex(16)}\". The value you " + - "provided, \"#{secret}\", is shorter than the minimum length " + - "of #{SECRET_MIN_LENGTH} characters" - end - end - - def verifier_for(secret, digest) - key = secret.respond_to?(:call) ? secret.call : secret - ActiveSupport::MessageVerifier.new(key, digest) - end - - def generate_sid - ActiveSupport::SecureRandom.hex(16) - end - - def persistent_session_id!(data) - (data ||= {}).merge!(inject_persistent_session_id(data)) - end - - def inject_persistent_session_id(data) - requires_session_id?(data) ? { :session_id => generate_sid } : {} - end - - def requires_session_id?(data) - if data - data.respond_to?(:key?) && !data.key?(:session_id) - else - true - end - end - end - end -end diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session/management.rb index b556f04649..ffce8e1bd1 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session/management.rb @@ -26,7 +26,7 @@ module ActionController #:nodoc: if defined? @@session_store @@session_store else - Session::CookieStore + ActionDispatch::Session::CookieStore end end diff --git a/actionpack/lib/action_controller/session/mem_cache_store.rb b/actionpack/lib/action_controller/session/mem_cache_store.rb deleted file mode 100644 index f745715a97..0000000000 --- a/actionpack/lib/action_controller/session/mem_cache_store.rb +++ /dev/null @@ -1,51 +0,0 @@ -begin - require_library_or_gem 'memcache' - - module ActionController - module Session - class MemCacheStore < AbstractStore - def initialize(app, options = {}) - # Support old :expires option - options[:expire_after] ||= options[:expires] - - super - - @default_options = { - :namespace => 'rack:session', - :memcache_server => 'localhost:11211' - }.merge(@default_options) - - @pool = options[:cache] || MemCache.new(@default_options[:memcache_server], @default_options) - unless @pool.servers.any? { |s| s.alive? } - raise "#{self} unable to find server during initialization." - end - @mutex = Mutex.new - - super - end - - private - def get_session(env, sid) - sid ||= generate_sid - begin - session = @pool.get(sid) || {} - rescue MemCache::MemCacheError, Errno::ECONNREFUSED - session = {} - end - [sid, session] - end - - def set_session(env, sid, session_data) - options = env['rack.session.options'] - expiry = options[:expire_after] || 0 - @pool.set(sid, session_data, expiry) - return true - rescue MemCache::MemCacheError, Errno::ECONNREFUSED - return false - end - end - end - end -rescue LoadError - # MemCache wasn't available so neither can the store be -end diff --git a/actionpack/lib/action_controller/status_codes.rb b/actionpack/lib/action_controller/status_codes.rb deleted file mode 100644 index 4977c79491..0000000000 --- a/actionpack/lib/action_controller/status_codes.rb +++ /dev/null @@ -1,88 +0,0 @@ -module ActionController - module StatusCodes #:nodoc: - # Defines the standard HTTP status codes, by integer, with their - # corresponding default message texts. - # Source: http://www.iana.org/assignments/http-status-codes - STATUS_CODES = { - 100 => "Continue", - 101 => "Switching Protocols", - 102 => "Processing", - - 200 => "OK", - 201 => "Created", - 202 => "Accepted", - 203 => "Non-Authoritative Information", - 204 => "No Content", - 205 => "Reset Content", - 206 => "Partial Content", - 207 => "Multi-Status", - 226 => "IM Used", - - 300 => "Multiple Choices", - 301 => "Moved Permanently", - 302 => "Found", - 303 => "See Other", - 304 => "Not Modified", - 305 => "Use Proxy", - 307 => "Temporary Redirect", - - 400 => "Bad Request", - 401 => "Unauthorized", - 402 => "Payment Required", - 403 => "Forbidden", - 404 => "Not Found", - 405 => "Method Not Allowed", - 406 => "Not Acceptable", - 407 => "Proxy Authentication Required", - 408 => "Request Timeout", - 409 => "Conflict", - 410 => "Gone", - 411 => "Length Required", - 412 => "Precondition Failed", - 413 => "Request Entity Too Large", - 414 => "Request-URI Too Long", - 415 => "Unsupported Media Type", - 416 => "Requested Range Not Satisfiable", - 417 => "Expectation Failed", - 422 => "Unprocessable Entity", - 423 => "Locked", - 424 => "Failed Dependency", - 426 => "Upgrade Required", - - 500 => "Internal Server Error", - 501 => "Not Implemented", - 502 => "Bad Gateway", - 503 => "Service Unavailable", - 504 => "Gateway Timeout", - 505 => "HTTP Version Not Supported", - 507 => "Insufficient Storage", - 510 => "Not Extended" - } - - # Provides a symbol-to-fixnum lookup for converting a symbol (like - # :created or :not_implemented) into its corresponding HTTP status - # code (like 200 or 501). - SYMBOL_TO_STATUS_CODE = STATUS_CODES.inject({}) do |hash, (code, message)| - hash[message.gsub(/ /, "").underscore.to_sym] = code - hash - end - - # Given a status parameter, determine whether it needs to be converted - # to a string. If it is a fixnum, use the STATUS_CODES hash to lookup - # the default message. If it is a symbol, use the SYMBOL_TO_STATUS_CODE - # hash to convert it. - def interpret_status(status) - case status - when Fixnum then - "#{status} #{STATUS_CODES[status]}".strip - when Symbol then - interpret_status(SYMBOL_TO_STATUS_CODE[status] || - "500 Unknown Status #{status.inspect}") - else - status.to_s - end - end - private :interpret_status - - end -end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/assertions/dom_assertions.rb b/actionpack/lib/action_controller/testing/assertions/dom.rb index 5ffe5f1883..5ffe5f1883 100644 --- a/actionpack/lib/action_controller/assertions/dom_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/dom.rb diff --git a/actionpack/lib/action_controller/assertions/model_assertions.rb b/actionpack/lib/action_controller/testing/assertions/model.rb index 3a7b39b106..3a7b39b106 100644 --- a/actionpack/lib/action_controller/assertions/model_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/model.rb diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/testing/assertions/response.rb index 5976090273..ca0a9bbf52 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/response.rb @@ -11,7 +11,7 @@ module ActionController # # You can also pass an explicit status number like assert_response(501) # or its symbolic equivalent assert_response(:not_implemented). - # See ActionController::StatusCodes for a full list. + # See ActionDispatch::StatusCodes for a full list. # # ==== Examples # @@ -27,7 +27,7 @@ module ActionController assert_block("") { true } # to count the assertion elsif type.is_a?(Fixnum) && @response.response_code == type assert_block("") { true } # to count the assertion - elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type] + elsif type.is_a?(Symbol) && @response.response_code == ActionDispatch::StatusCodes::SYMBOL_TO_STATUS_CODE[type] assert_block("") { true } # to count the assertion else if @response.error? diff --git a/actionpack/lib/action_controller/assertions/routing_assertions.rb b/actionpack/lib/action_controller/testing/assertions/routing.rb index 5101751cea..5101751cea 100644 --- a/actionpack/lib/action_controller/assertions/routing_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/routing.rb diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/testing/assertions/selector.rb index 0d56ea5ef7..0d56ea5ef7 100644 --- a/actionpack/lib/action_controller/assertions/selector_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/selector.rb diff --git a/actionpack/lib/action_controller/assertions/tag_assertions.rb b/actionpack/lib/action_controller/testing/assertions/tag.rb index 80249e0e83..80249e0e83 100644 --- a/actionpack/lib/action_controller/assertions/tag_assertions.rb +++ b/actionpack/lib/action_controller/testing/assertions/tag.rb diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/testing/integration.rb index 163ba84a3e..0da23f9dc8 100644 --- a/actionpack/lib/action_controller/integration.rb +++ b/actionpack/lib/action_controller/testing/integration.rb @@ -316,7 +316,7 @@ module ActionController @html_document = nil @status = status.to_i - @status_message = StatusCodes::STATUS_CODES[@status] + @status_message = ActionDispatch::StatusCodes::STATUS_CODES[@status] @headers = Rack::Utils::HeaderHash.new(headers) @@ -335,7 +335,7 @@ module ActionController else # Decorate responses from Rack Middleware and Rails Metal # as an Response for the purposes of integration testing - @response = Response.new + @response = ActionDispatch::Response.new @response.status = status.to_s @response.headers.replace(@headers) @response.body = @body @@ -374,7 +374,7 @@ module ActionController "SERVER_PORT" => https? ? "443" : "80", "HTTPS" => https? ? "on" : "off" } - UrlRewriter.new(Request.new(env), {}) + UrlRewriter.new(ActionDispatch::Request.new(env), {}) end def name_with_prefix(prefix, name) diff --git a/actionpack/lib/action_controller/performance_test.rb b/actionpack/lib/action_controller/testing/performance.rb index d88180087d..d88180087d 100644 --- a/actionpack/lib/action_controller/performance_test.rb +++ b/actionpack/lib/action_controller/testing/performance.rb diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/testing/process.rb index 4b5fc3a3c1..38e37c7a18 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -1,5 +1,5 @@ module ActionController #:nodoc: - class TestRequest < Request #:nodoc: + class TestRequest < ActionDispatch::Request #:nodoc: attr_accessor :cookies, :session_options attr_accessor :query_parameters, :path, :session attr_accessor :host @@ -271,7 +271,7 @@ module ActionController #:nodoc: # controller actions. # # See Response for more information on controller response objects. - class TestResponse < Response + class TestResponse < ActionDispatch::Response include TestResponseBehavior def recycle! diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/testing/test_case.rb index 0b0d0c799b..c14785ba83 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/testing/test_case.rb @@ -1,5 +1,5 @@ require 'active_support/test_case' -require 'action_controller/test_process' +require 'action_controller/testing/process' module ActionController # Superclass for ActionController functional tests. Functional tests allow you to diff --git a/actionpack/lib/action_controller/uploaded_file.rb b/actionpack/lib/action_controller/uploaded_file.rb deleted file mode 100644 index 376ba3621a..0000000000 --- a/actionpack/lib/action_controller/uploaded_file.rb +++ /dev/null @@ -1,44 +0,0 @@ -module ActionController - module UploadedFile - def self.included(base) - base.class_eval do - attr_accessor :original_path, :content_type - alias_method :local_path, :path - end - end - - def self.extended(object) - object.class_eval do - attr_accessor :original_path, :content_type - alias_method :local_path, :path - end - end - - # Take the basename of the upload's original filename. - # This handles the full Windows paths given by Internet Explorer - # (and perhaps other broken user agents) without affecting - # those which give the lone filename. - # The Windows regexp is adapted from Perl's File::Basename. - def original_filename - unless defined? @original_filename - @original_filename = - unless original_path.blank? - if original_path =~ /^(?:.*[:\\\/])?(.*)/m - $1 - else - File.basename original_path - end - end - end - @original_filename - end - end - - class UploadedStringIO < StringIO - include UploadedFile - end - - class UploadedTempfile < Tempfile - include UploadedFile - end -end diff --git a/actionpack/lib/action_controller/url_encoded_pair_parser.rb b/actionpack/lib/action_controller/url_encoded_pair_parser.rb deleted file mode 100644 index 57594c4259..0000000000 --- a/actionpack/lib/action_controller/url_encoded_pair_parser.rb +++ /dev/null @@ -1,155 +0,0 @@ -module ActionController - class UrlEncodedPairParser < StringScanner #:nodoc: - class << self - def parse_query_parameters(query_string) - return {} if query_string.blank? - - pairs = query_string.split('&').collect do |chunk| - next if chunk.empty? - key, value = chunk.split('=', 2) - next if key.empty? - value = value.nil? ? nil : CGI.unescape(value) - [ CGI.unescape(key), value ] - end.compact - - new(pairs).result - end - - def parse_hash_parameters(params) - parser = new - - params = params.dup - until params.empty? - for key, value in params - if key.blank? - params.delete(key) - elsif value.is_a?(Array) - parser.parse(key, get_typed_value(value.shift)) - params.delete(key) if value.empty? - else - parser.parse(key, get_typed_value(value)) - params.delete(key) - end - end - end - - parser.result - end - - private - def get_typed_value(value) - case value - when String - value - when NilClass - '' - when Array - value.map { |v| get_typed_value(v) } - when Hash - if value.has_key?(:tempfile) && value[:filename].any? - upload = value[:tempfile] - upload.extend(UploadedFile) - upload.original_path = value[:filename] - upload.content_type = value[:type] - upload - else - nil - end - else - raise "Unknown form value: #{value.inspect}" - end - end - end - - attr_reader :top, :parent, :result - - def initialize(pairs = []) - super('') - @result = {} - pairs.each { |key, value| parse(key, value) } - end - - KEY_REGEXP = %r{([^\[\]=&]+)} - BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]} - - # Parse the query string - def parse(key, value) - self.string = key - @top, @parent = result, nil - - # First scan the bare key - key = scan(KEY_REGEXP) or return - key = post_key_check(key) - - # Then scan as many nestings as present - until eos? - r = scan(BRACKETED_KEY_REGEXP) or return - key = self[1] - key = post_key_check(key) - end - - bind(key, value) - end - - private - # After we see a key, we must look ahead to determine our next action. Cases: - # - # [] follows the key. Then the value must be an array. - # = follows the key. (A value comes next) - # & or the end of string follows the key. Then the key is a flag. - # otherwise, a hash follows the key. - def post_key_check(key) - if scan(/\[\]/) # a[b][] indicates that b is an array - container(key, Array) - nil - elsif check(/\[[^\]]/) # a[b] indicates that a is a hash - container(key, Hash) - nil - else # End of key? We do nothing. - key - end - end - - # Add a container to the stack. - def container(key, klass) - type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass) - value = bind(key, klass.new) - type_conflict! klass, value unless value.is_a?(klass) - push(value) - end - - # Push a value onto the 'stack', which is actually only the top 2 items. - def push(value) - @parent, @top = @top, value - end - - # Bind a key (which may be nil for items in an array) to the provided value. - def bind(key, value) - if top.is_a? Array - if key - if top[-1].is_a?(Hash) && ! top[-1].key?(key) - top[-1][key] = value - else - top << {key => value}.with_indifferent_access - end - push top.last - return top[key] - else - top << value - return value - end - elsif top.is_a? Hash - key = CGI.unescape(key) - parent << (@top = {}) if top.key?(key) && parent.is_a?(Array) - top[key] ||= value - return top[key] - else - raise ArgumentError, "Don't know what to do: top is #{top.inspect}" - end - end - - def type_conflict!(klass, value) - raise TypeError, "Conflicting types for parameter containers. Expected an instance of #{klass} but found an instance of #{value.class}. This can be caused by colliding Array and Hash parameters like qs[]=value&qs[key]=value. (The parameters received were #{value.inspect}.)" - end - end -end |