Instiki 0.16.3: Rails 2.3.0
Instiki now runs on the Rails 2.3.0 Candidate Release. Among other improvements, this means that it now automagically selects between WEBrick and Mongrel. Just run ./instiki --daemon
This commit is contained in:
parent
43aadecc99
commit
4e14ccc74d
893 changed files with 71965 additions and 28511 deletions
|
@ -1,69 +0,0 @@
|
|||
require 'test/unit/assertions'
|
||||
|
||||
module ActionController #:nodoc:
|
||||
# In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
|
||||
# can be used against. These collections are:
|
||||
#
|
||||
# * assigns: Instance variables assigned in the action that are available for the view.
|
||||
# * session: Objects being saved in the session.
|
||||
# * flash: The flash objects currently in the session.
|
||||
# * cookies: Cookies being sent to the user on this request.
|
||||
#
|
||||
# These collections can be used just like any other hash:
|
||||
#
|
||||
# assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
|
||||
# assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
|
||||
# assert flash.empty? # makes sure that there's nothing in the flash
|
||||
#
|
||||
# For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To
|
||||
# appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing.
|
||||
# So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work.
|
||||
#
|
||||
# On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
|
||||
#
|
||||
# For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
|
||||
# action call which can then be asserted against.
|
||||
#
|
||||
# == Manipulating the request collections
|
||||
#
|
||||
# The collections described above link to the response, so you can test if what the actions were expected to do happened. But
|
||||
# sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions
|
||||
# and cookies, though. For sessions, you just do:
|
||||
#
|
||||
# @request.session[:key] = "value"
|
||||
#
|
||||
# For cookies, you need to manually create the cookie, like this:
|
||||
#
|
||||
# @request.cookies["key"] = CGI::Cookie.new("key", "value")
|
||||
#
|
||||
# == Testing named routes
|
||||
#
|
||||
# If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case.
|
||||
# Example:
|
||||
#
|
||||
# assert_redirected_to page_url(:title => 'foo')
|
||||
module Assertions
|
||||
def self.included(klass)
|
||||
%w(response selector tag dom routing model).each do |kind|
|
||||
require "action_controller/assertions/#{kind}_assertions"
|
||||
klass.module_eval { include const_get("#{kind.camelize}Assertions") }
|
||||
end
|
||||
end
|
||||
|
||||
def clean_backtrace(&block)
|
||||
yield
|
||||
rescue Test::Unit::AssertionFailedError => error
|
||||
framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions"))
|
||||
error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path }
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Test #:nodoc:
|
||||
module Unit #:nodoc:
|
||||
class TestCase #:nodoc:
|
||||
include ActionController::Assertions
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,6 +11,7 @@ module ActionController
|
|||
# assert_valid(model)
|
||||
#
|
||||
def assert_valid(record)
|
||||
::ActiveSupport::Deprecation.warn("assert_valid is deprecated. Use assert record.valid? instead", caller)
|
||||
clean_backtrace do
|
||||
assert record.valid?, record.errors.full_messages.join("\n")
|
||||
end
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
require 'rexml/document'
|
||||
require 'html/document'
|
||||
|
||||
module ActionController
|
||||
module Assertions
|
||||
# A small suite of assertions that test responses from Rails applications.
|
||||
|
@ -19,7 +16,7 @@ module ActionController
|
|||
# ==== Examples
|
||||
#
|
||||
# # assert that the response was a redirection
|
||||
# assert_response :redirect
|
||||
# assert_response :redirect
|
||||
#
|
||||
# # assert that the response code was status code 401 (unauthorized)
|
||||
# assert_response 401
|
||||
|
@ -44,7 +41,7 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
# Assert that the redirection options passed in match those of the redirect called in the latest action.
|
||||
# Assert that the redirection options passed in match those of the redirect called in the latest action.
|
||||
# This match can be partial, such that assert_redirected_to(:controller => "weblog") will also
|
||||
# match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.
|
||||
#
|
||||
|
@ -63,12 +60,12 @@ module ActionController
|
|||
clean_backtrace do
|
||||
assert_response(:redirect, message)
|
||||
return true if options == @response.redirected_to
|
||||
|
||||
|
||||
# Support partial arguments for hash redirections
|
||||
if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash)
|
||||
return true if options.all? {|(key, value)| @response.redirected_to[key] == value}
|
||||
end
|
||||
|
||||
|
||||
redirected_to_after_normalisation = normalize_argument_to_redirection(@response.redirected_to)
|
||||
options_after_normalisation = normalize_argument_to_redirection(options)
|
||||
|
||||
|
@ -78,29 +75,59 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
# Asserts that the request was rendered with the appropriate template file.
|
||||
# Asserts that the request was rendered with the appropriate template file or partials
|
||||
#
|
||||
# ==== Examples
|
||||
#
|
||||
# # assert that the "new" view template was rendered
|
||||
# assert_template "new"
|
||||
#
|
||||
def assert_template(expected = nil, message=nil)
|
||||
# # assert that the "_customer" partial was rendered twice
|
||||
# assert_template :partial => '_customer', :count => 2
|
||||
#
|
||||
# # assert that no partials were rendered
|
||||
# assert_template :partial => false
|
||||
#
|
||||
def assert_template(options = {}, message = nil)
|
||||
clean_backtrace do
|
||||
rendered = @response.rendered_template.to_s
|
||||
msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
|
||||
assert_block(msg) do
|
||||
if expected.nil?
|
||||
@response.rendered_template.blank?
|
||||
case options
|
||||
when NilClass, String
|
||||
rendered = @response.rendered[:template].to_s
|
||||
msg = build_message(message,
|
||||
"expecting <?> but rendering with <?>",
|
||||
options, rendered)
|
||||
assert_block(msg) do
|
||||
if options.nil?
|
||||
@response.rendered[:template].blank?
|
||||
else
|
||||
rendered.to_s.match(options)
|
||||
end
|
||||
end
|
||||
when Hash
|
||||
if expected_partial = options[:partial]
|
||||
partials = @response.rendered[:partials]
|
||||
if expected_count = options[:count]
|
||||
found = partials.detect { |p, _| p.to_s.match(expected_partial) }
|
||||
actual_count = found.nil? ? 0 : found.second
|
||||
msg = build_message(message,
|
||||
"expecting ? to be rendered ? time(s) but rendered ? time(s)",
|
||||
expected_partial, expected_count, actual_count)
|
||||
assert(actual_count == expected_count.to_i, msg)
|
||||
else
|
||||
msg = build_message(message,
|
||||
"expecting partial <?> but action rendered <?>",
|
||||
options[:partial], partials.keys)
|
||||
assert(partials.keys.any? { |p| p.to_s.match(expected_partial) }, msg)
|
||||
end
|
||||
else
|
||||
rendered.to_s.match(expected)
|
||||
assert @response.rendered[:partials].empty?,
|
||||
"Expected no partials to be rendered"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Proxy to to_param if the object will respond to it.
|
||||
def parameterize(value)
|
||||
value.respond_to?(:to_param) ? value.to_param : value
|
||||
|
|
|
@ -134,7 +134,7 @@ module ActionController
|
|||
path = "/#{path}" unless path.first == '/'
|
||||
|
||||
# Assume given controller
|
||||
request = ActionController::TestRequest.new({}, {}, nil)
|
||||
request = ActionController::TestRequest.new
|
||||
request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
|
||||
request.path = path
|
||||
|
||||
|
|
|
@ -3,9 +3,6 @@
|
|||
# Under MIT and/or CC By license.
|
||||
#++
|
||||
|
||||
require 'rexml/document'
|
||||
require 'html/document'
|
||||
|
||||
module ActionController
|
||||
module Assertions
|
||||
unless const_defined?(:NO_STRIP)
|
||||
|
@ -112,20 +109,27 @@ module ActionController
|
|||
# starting from (and including) that element and all its children in
|
||||
# depth-first order.
|
||||
#
|
||||
# If no element if specified, calling +assert_select+ will select from the
|
||||
# response HTML. Calling #assert_select inside an +assert_select+ block will
|
||||
# run the assertion for each element selected by the enclosing assertion.
|
||||
# If no element if specified, calling +assert_select+ selects from the
|
||||
# response HTML unless +assert_select+ is called from within an +assert_select+ block.
|
||||
#
|
||||
# When called with a block +assert_select+ passes an array of selected elements
|
||||
# to the block. Calling +assert_select+ from the block, with no element specified,
|
||||
# runs the assertion on the complete set of elements selected by the enclosing assertion.
|
||||
# Alternatively the array may be iterated through so that +assert_select+ can be called
|
||||
# separately for each element.
|
||||
#
|
||||
#
|
||||
# ==== Example
|
||||
# assert_select "ol>li" do |elements|
|
||||
# If the response contains two ordered lists, each with four list elements then:
|
||||
# assert_select "ol" do |elements|
|
||||
# elements.each do |element|
|
||||
# assert_select element, "li"
|
||||
# assert_select element, "li", 4
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Or for short:
|
||||
# assert_select "ol>li" do
|
||||
# assert_select "li"
|
||||
# will pass, as will:
|
||||
# assert_select "ol" do
|
||||
# assert_select "li", 8
|
||||
# end
|
||||
#
|
||||
# The selector may be a CSS selector expression (String), an expression
|
||||
|
@ -405,6 +409,7 @@ module ActionController
|
|||
if rjs_type
|
||||
if rjs_type == :insert
|
||||
position = args.shift
|
||||
id = args.shift
|
||||
insertion = "insert_#{position}".to_sym
|
||||
raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion]
|
||||
statement = "(#{RJS_STATEMENTS[insertion]})"
|
||||
|
@ -590,7 +595,7 @@ module ActionController
|
|||
def response_from_page_or_rjs()
|
||||
content_type = @response.content_type
|
||||
|
||||
if content_type && content_type =~ /text\/javascript/
|
||||
if content_type && Mime::JS =~ content_type
|
||||
body = @response.body.dup
|
||||
root = HTML::Node.new(nil)
|
||||
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
require 'rexml/document'
|
||||
require 'html/document'
|
||||
|
||||
module ActionController
|
||||
module Assertions
|
||||
# Pair of assertions to testing elements in the HTML output of the response.
|
||||
|
@ -127,4 +124,4 @@ module ActionController
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,3 @@
|
|||
require 'action_controller/mime_type'
|
||||
require 'action_controller/request'
|
||||
require 'action_controller/response'
|
||||
require 'action_controller/routing'
|
||||
require 'action_controller/resources'
|
||||
require 'action_controller/url_rewriter'
|
||||
require 'action_controller/status_codes'
|
||||
require 'action_view'
|
||||
require 'drb'
|
||||
require 'set'
|
||||
|
||||
module ActionController #:nodoc:
|
||||
|
@ -173,8 +164,8 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# Other options for session storage are:
|
||||
#
|
||||
# * ActiveRecordStore - Sessions are stored in your database, which works better than PStore with multiple app servers and,
|
||||
# unlike CookieStore, hides your session contents from the user. To use ActiveRecordStore, set
|
||||
# * ActiveRecord::SessionStore - Sessions are stored in your database, which works better than PStore with multiple app servers and,
|
||||
# unlike CookieStore, hides your session contents from the user. To use ActiveRecord::SessionStore, set
|
||||
#
|
||||
# config.action_controller.session_store = :active_record_store
|
||||
#
|
||||
|
@ -263,7 +254,7 @@ module ActionController #:nodoc:
|
|||
cattr_reader :protected_instance_variables
|
||||
# Controller specific instance variables which will not be accessible inside views.
|
||||
@@protected_instance_variables = %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller
|
||||
@action_name @before_filter_chain_aborted @action_cache_path @_session @_cookies @_headers @_params
|
||||
@action_name @before_filter_chain_aborted @action_cache_path @_session @_headers @_params
|
||||
@_flash @_response)
|
||||
|
||||
# Prepends all the URL-generating helpers from AssetHelper. This makes it possible to easily move javascripts, stylesheets,
|
||||
|
@ -310,10 +301,7 @@ module ActionController #:nodoc:
|
|||
# A YAML parser is also available and can be turned on with:
|
||||
#
|
||||
# ActionController::Base.param_parsers[Mime::YAML] = :yaml
|
||||
@@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
|
||||
Mime::URL_ENCODED_FORM => :url_encoded_form,
|
||||
Mime::XML => :xml_simple,
|
||||
Mime::JSON => :json }
|
||||
@@param_parsers = {}
|
||||
cattr_accessor :param_parsers
|
||||
|
||||
# Controls the default charset for all renders.
|
||||
|
@ -336,6 +324,10 @@ module ActionController #:nodoc:
|
|||
# sets it to <tt>:authenticity_token</tt> by default.
|
||||
cattr_accessor :request_forgery_protection_token
|
||||
|
||||
# Controls the IP Spoofing check when determining the remote IP.
|
||||
@@ip_spoofing_check = true
|
||||
cattr_accessor :ip_spoofing_check
|
||||
|
||||
# Indicates whether or not optimise the generated named
|
||||
# route helper methods
|
||||
cattr_accessor :optimise_named_routes
|
||||
|
@ -387,6 +379,13 @@ module ActionController #:nodoc:
|
|||
attr_accessor :action_name
|
||||
|
||||
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
|
||||
process(request, response)
|
||||
end
|
||||
|
||||
# Factory for the standard create, process loop where the controller is discarded after processing.
|
||||
def process(request, response) #:nodoc:
|
||||
new.process(request, response)
|
||||
|
@ -507,7 +506,7 @@ module ActionController #:nodoc:
|
|||
protected :filter_parameters
|
||||
end
|
||||
|
||||
delegate :exempt_from_layout, :to => 'ActionView::Base'
|
||||
delegate :exempt_from_layout, :to => 'ActionView::Template'
|
||||
end
|
||||
|
||||
public
|
||||
|
@ -529,7 +528,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def send_response
|
||||
response.prepare! unless component_request?
|
||||
response.prepare!
|
||||
response
|
||||
end
|
||||
|
||||
|
@ -645,7 +644,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def session_enabled?
|
||||
request.session_options && request.session_options[:disabled] != false
|
||||
ActiveSupport::Deprecation.warn("Sessions are now lazy loaded. So if you don't access them, consider them disabled.", caller)
|
||||
end
|
||||
|
||||
self.view_paths = []
|
||||
|
@ -864,20 +863,28 @@ module ActionController #:nodoc:
|
|||
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?
|
||||
return render(:file => default_template_name, :layout => true)
|
||||
elsif !extra_options.is_a?(Hash)
|
||||
raise RenderError, "You called render with invalid options : #{options.inspect}, #{extra_options.inspect}"
|
||||
else
|
||||
if options == :update
|
||||
options = extra_options.merge({ :update => true })
|
||||
elsif !options.is_a?(Hash)
|
||||
raise RenderError, "You called render with invalid options : #{options.inspect}"
|
||||
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
|
||||
|
||||
response.layout = layout = pick_layout(options)
|
||||
logger.info("Rendering template within #{layout}") if logger && layout
|
||||
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
|
||||
|
@ -902,7 +909,7 @@ module ActionController #:nodoc:
|
|||
render_for_text(@template.render(options.merge(:layout => layout)), options[:status])
|
||||
|
||||
elsif action_name = options[:action]
|
||||
render_for_file(default_template_name(action_name.to_s), options[:status], layout)
|
||||
render_for_file(default_template(action_name.to_s), options[:status], layout)
|
||||
|
||||
elsif xml = options[:xml]
|
||||
response.content_type ||= Mime::XML
|
||||
|
@ -937,7 +944,7 @@ module ActionController #:nodoc:
|
|||
render_for_text(nil, options[:status])
|
||||
|
||||
else
|
||||
render_for_file(default_template_name, options[:status], layout)
|
||||
render_for_file(default_template, options[:status], layout)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -994,7 +1001,7 @@ module ActionController #:nodoc:
|
|||
@performed_redirect = false
|
||||
response.redirected_to = nil
|
||||
response.redirected_to_method_params = nil
|
||||
response.headers['Status'] = DEFAULT_RENDER_STATUS_CODE
|
||||
response.status = DEFAULT_RENDER_STATUS_CODE
|
||||
response.headers.delete('Location')
|
||||
end
|
||||
|
||||
|
@ -1115,7 +1122,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
# Sets the etag, last_modified, or both on the response and renders a
|
||||
# "304 Not Modified" response if the request is already fresh.
|
||||
# "304 Not Modified" response if the request is already fresh.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
|
@ -1123,8 +1130,8 @@ module ActionController #:nodoc:
|
|||
# @article = Article.find(params[:id])
|
||||
# fresh_when(:etag => @article, :last_modified => @article.created_at.utc)
|
||||
# end
|
||||
#
|
||||
# This will render the show template if the request isn't sending a matching etag or
|
||||
#
|
||||
# This will render the show template if the request isn't sending a matching etag or
|
||||
# If-Modified-Since header and just a "304 Not Modified" response if there's a match.
|
||||
def fresh_when(options)
|
||||
options.assert_valid_keys(:etag, :last_modified)
|
||||
|
@ -1164,20 +1171,19 @@ module ActionController #:nodoc:
|
|||
def reset_session #:doc:
|
||||
request.reset_session
|
||||
@_session = request.session
|
||||
response.session = @_session
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def render_for_file(template_path, status = nil, layout = nil, locals = {}) #:nodoc:
|
||||
logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger
|
||||
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.headers['Status'] = interpret_status(status || DEFAULT_RENDER_STATUS_CODE)
|
||||
response.status = interpret_status(status || DEFAULT_RENDER_STATUS_CODE)
|
||||
|
||||
if append_response
|
||||
response.body ||= ''
|
||||
|
@ -1191,6 +1197,16 @@ module ActionController #:nodoc:
|
|||
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}"
|
||||
end
|
||||
|
||||
if !extra_options.is_a?(Hash)
|
||||
raise RenderError, "You called render with invalid options : #{options.inspect}, #{extra_options.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_template_class(response)
|
||||
response.template = ActionView::Base.new(self.class.view_paths, {}, self)
|
||||
response.template.helpers.send :include, self.class.master_helper_module
|
||||
|
@ -1199,7 +1215,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def assign_shortcuts(request, response)
|
||||
@_request, @_params, @_cookies = request, request.parameters, request.cookies
|
||||
@_request, @_params = request, request.parameters
|
||||
|
||||
@_response = response
|
||||
@_response.session = request.session
|
||||
|
@ -1217,11 +1233,10 @@ module ActionController #:nodoc:
|
|||
def log_processing
|
||||
if logger && logger.info?
|
||||
log_processing_for_request_id
|
||||
log_processing_for_session_id
|
||||
log_processing_for_parameters
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def log_processing_for_request_id
|
||||
request_id = "\n\nProcessing #{self.class.name}\##{action_name} "
|
||||
request_id << "to #{params[:format]} " if params[:format]
|
||||
|
@ -1230,17 +1245,10 @@ module ActionController #:nodoc:
|
|||
logger.info(request_id)
|
||||
end
|
||||
|
||||
def log_processing_for_session_id
|
||||
if @_session && @_session.respond_to?(:session_id) && @_session.respond_to?(:dbman) &&
|
||||
!@_session.dbman.is_a?(CGI::Session::CookieStore)
|
||||
logger.info " Session ID: #{@_session.session_id}"
|
||||
end
|
||||
end
|
||||
|
||||
def log_processing_for_parameters
|
||||
parameters = respond_to?(:filter_parameters) ? filter_parameters(params) : params.dup
|
||||
parameters = parameters.except!(:controller, :action, :format, :_method)
|
||||
|
||||
|
||||
logger.info " Parameters: #{parameters.inspect}" unless parameters.empty?
|
||||
end
|
||||
|
||||
|
@ -1255,10 +1263,17 @@ module ActionController #:nodoc:
|
|||
elsif respond_to? :method_missing
|
||||
method_missing action_name
|
||||
default_render unless performed?
|
||||
elsif template_exists?
|
||||
default_render
|
||||
else
|
||||
raise UnknownAction, "No action responded to #{action_name}. Actions: #{action_methods.sort.to_sentence}", caller
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1270,11 +1285,6 @@ module ActionController #:nodoc:
|
|||
@action_name = (params['action'] || 'index')
|
||||
end
|
||||
|
||||
def assign_default_content_type_and_charset
|
||||
response.assign_default_content_type_and_charset!
|
||||
end
|
||||
deprecate :assign_default_content_type_and_charset => :'response.assign_default_content_type_and_charset!'
|
||||
|
||||
def action_methods
|
||||
self.class.action_methods
|
||||
end
|
||||
|
@ -1305,14 +1315,8 @@ module ActionController #:nodoc:
|
|||
"#{request.protocol}#{request.host}#{request.request_uri}"
|
||||
end
|
||||
|
||||
def close_session
|
||||
@_session.close if @_session && @_session.respond_to?(:close)
|
||||
end
|
||||
|
||||
def template_exists?(template_name = default_template_name)
|
||||
@template.send(:_pick_template, template_name) ? true : false
|
||||
rescue ActionView::MissingTemplate
|
||||
false
|
||||
def default_template(action_name = self.action_name)
|
||||
self.view_paths.find_template(default_template_name(action_name), default_template_format)
|
||||
end
|
||||
|
||||
def default_template_name(action_name = self.action_name)
|
||||
|
@ -1334,7 +1338,16 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def process_cleanup
|
||||
close_session
|
||||
end
|
||||
end
|
||||
|
||||
Base.class_eval do
|
||||
[ Filters, Layout, Benchmarking, Rescue, Flash, MimeResponds, Helpers,
|
||||
Cookies, Caching, Verification, Streaming, SessionManagement,
|
||||
HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods,
|
||||
RecordIdentifier, RequestForgeryProtection, Translation
|
||||
].each do |mod|
|
||||
include mod
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,8 +23,8 @@ module ActionController #:nodoc:
|
|||
def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
|
||||
if logger && logger.level == log_level
|
||||
result = nil
|
||||
seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
|
||||
logger.add(log_level, "#{title} (#{('%.1f' % (seconds * 1000))}ms)")
|
||||
ms = Benchmark.ms { result = use_silence ? silence { yield } : yield }
|
||||
logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)")
|
||||
result
|
||||
else
|
||||
yield
|
||||
|
@ -48,7 +48,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
render_output = nil
|
||||
@view_runtime = Benchmark::realtime { render_output = render_without_benchmark(options, extra_options, &block) }
|
||||
@view_runtime = Benchmark.ms { render_output = render_without_benchmark(options, extra_options, &block) }
|
||||
|
||||
if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
||||
@db_rt_before_render = db_runtime
|
||||
|
@ -65,11 +65,11 @@ module ActionController #:nodoc:
|
|||
private
|
||||
def perform_action_with_benchmark
|
||||
if logger
|
||||
seconds = [ Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001 ].max
|
||||
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?
|
||||
|
||||
log_message = "Completed in #{sprintf("%.0f", seconds * 1000)}ms"
|
||||
log_message = 'Completed in %.0fms' % ms
|
||||
|
||||
if logging_view || logging_active_record
|
||||
log_message << " ("
|
||||
|
@ -83,25 +83,25 @@ module ActionController #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
log_message << " | #{headers["Status"]}"
|
||||
log_message << " | #{response.status}"
|
||||
log_message << " [#{complete_request_uri rescue "unknown"}]"
|
||||
|
||||
logger.info(log_message)
|
||||
response.headers["X-Runtime"] = "#{sprintf("%.0f", seconds * 1000)}ms"
|
||||
response.headers["X-Runtime"] = "%.0f" % ms
|
||||
else
|
||||
perform_action_without_benchmark
|
||||
end
|
||||
end
|
||||
|
||||
def view_runtime
|
||||
"View: %.0f" % (@view_runtime * 1000)
|
||||
"View: %.0f" % @view_runtime
|
||||
end
|
||||
|
||||
def active_record_runtime
|
||||
db_runtime = ActiveRecord::Base.connection.reset_runtime
|
||||
db_runtime += @db_rt_before_render if @db_rt_before_render
|
||||
db_runtime += @db_rt_after_render if @db_rt_after_render
|
||||
"DB: %.0f" % (db_runtime * 1000)
|
||||
"DB: %.0f" % db_runtime
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,13 +2,6 @@ require 'fileutils'
|
|||
require 'uri'
|
||||
require 'set'
|
||||
|
||||
require 'action_controller/caching/pages'
|
||||
require 'action_controller/caching/actions'
|
||||
require 'action_controller/caching/sql_cache'
|
||||
require 'action_controller/caching/sweeping'
|
||||
require 'action_controller/caching/fragments'
|
||||
|
||||
|
||||
module ActionController #:nodoc:
|
||||
# Caching is a cheap way of speeding up slow applications by keeping the result of calculations, renderings, and database calls
|
||||
# around for subsequent requests. Action Controller affords you three approaches in varying levels of granularity: Page, Action, Fragment.
|
||||
|
@ -31,6 +24,11 @@ module ActionController #:nodoc:
|
|||
# ActionController::Base.cache_store = :mem_cache_store, "localhost"
|
||||
# ActionController::Base.cache_store = MyOwnStore.new("parameter")
|
||||
module Caching
|
||||
autoload :Actions, 'action_controller/caching/actions'
|
||||
autoload :Fragments, 'action_controller/caching/fragments'
|
||||
autoload :Pages, 'action_controller/caching/pages'
|
||||
autoload :Sweeping, 'action_controller/caching/sweeping'
|
||||
|
||||
def self.included(base) #:nodoc:
|
||||
base.class_eval do
|
||||
@@cache_store = nil
|
||||
|
@ -42,7 +40,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
include Pages, Actions, Fragments
|
||||
include Sweeping, SqlCache if defined?(ActiveRecord)
|
||||
include Sweeping if defined?(ActiveRecord)
|
||||
|
||||
@@perform_caching = true
|
||||
cattr_accessor :perform_caching
|
||||
|
@ -63,10 +61,9 @@ module ActionController #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
private
|
||||
def cache_configured?
|
||||
self.class.cache_configured?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -113,7 +113,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def caching_allowed(controller)
|
||||
controller.request.get? && controller.response.headers['Status'].to_i == 200
|
||||
controller.request.get? && controller.response.status.to_i == 200
|
||||
end
|
||||
|
||||
def cache_layout?
|
||||
|
|
|
@ -10,23 +10,23 @@ module ActionController #:nodoc:
|
|||
# <%= render :partial => "topic", :collection => Topic.find(:all) %>
|
||||
# <% end %>
|
||||
#
|
||||
# This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would
|
||||
# be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>.
|
||||
#
|
||||
# This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using
|
||||
# This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would
|
||||
# be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>.
|
||||
#
|
||||
# This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using
|
||||
# <tt>caches_action</tt>, so we also have the option to qualify the name of the cached fragment with something like:
|
||||
#
|
||||
# <% cache(:action => "list", :action_suffix => "all_topics") do %>
|
||||
#
|
||||
# That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a
|
||||
# different suffix. Note that the URL doesn't have to really exist or be callable - the url_for system is just used to generate unique
|
||||
# cache names that we can refer to when we need to expire the cache.
|
||||
#
|
||||
# That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a
|
||||
# different suffix. Note that the URL doesn't have to really exist or be callable - the url_for system is just used to generate unique
|
||||
# cache names that we can refer to when we need to expire the cache.
|
||||
#
|
||||
# The expiration call for this example is:
|
||||
#
|
||||
#
|
||||
# expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")
|
||||
module Fragments
|
||||
# Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,
|
||||
# Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,
|
||||
# writing, or expiring a cached fragment. If the key is a hash, the generated key is the return
|
||||
# value of url_for on that hash (without the protocol). All keys are prefixed with "views/" and uses
|
||||
# ActiveSupport::Cache.expand_cache_key for the expansion.
|
||||
|
@ -50,7 +50,7 @@ module ActionController #:nodoc:
|
|||
|
||||
# Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
|
||||
def write_fragment(key, content, options = nil)
|
||||
return unless cache_configured?
|
||||
return content unless cache_configured?
|
||||
|
||||
key = fragment_cache_key(key)
|
||||
|
||||
|
@ -83,15 +83,23 @@ module ActionController #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
# Name can take one of three forms:
|
||||
# * String: This would normally take the form of a path like "pages/45/notes"
|
||||
# * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 }
|
||||
# * Regexp: Will destroy all the matched fragments, example:
|
||||
# %r{pages/\d*/notes}
|
||||
# Ensure you do not specify start and finish in the regex (^$) because
|
||||
# the actual filename matched looks like ./cache/filename/path.cache
|
||||
# Regexp expiration is only supported on caches that can iterate over
|
||||
# all keys (unlike memcached).
|
||||
# Removes fragments from the cache.
|
||||
#
|
||||
# +key+ can take one of three forms:
|
||||
# * String - This would normally take the form of a path, like
|
||||
# <tt>"pages/45/notes"</tt>.
|
||||
# * Hash - Treated as an implicit call to +url_for+, like
|
||||
# <tt>{:controller => "pages", :action => "notes", :id => 45}</tt>
|
||||
# * Regexp - Will remove any fragment that matches, so
|
||||
# <tt>%r{pages/\d*/notes}</tt> might remove all notes. Make sure you
|
||||
# don't use anchors in the regex (<tt>^</tt> or <tt>$</tt>) because
|
||||
# the actual filename matched looks like
|
||||
# <tt>./cache/filename/path.cache</tt>. Note: Regexp expiration is
|
||||
# only supported on caches that can iterate over all keys (unlike
|
||||
# memcached).
|
||||
#
|
||||
# +options+ is passed through to the cache store's <tt>delete</tt>
|
||||
# method (or <tt>delete_matched</tt>, for Regexp keys.)
|
||||
def expire_fragment(key, options = nil)
|
||||
return unless cache_configured?
|
||||
|
||||
|
|
|
@ -33,28 +33,26 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# Additionally, you can expire caches using Sweepers that act on changes in the model to determine when a cache is supposed to be
|
||||
# expired.
|
||||
#
|
||||
# == Setting the cache directory
|
||||
#
|
||||
# The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
|
||||
# For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>RAILS_ROOT + "/public"</tt>). Changing
|
||||
# this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
|
||||
# web server to look in the new location for cached files.
|
||||
#
|
||||
# == Setting the cache extension
|
||||
#
|
||||
# Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
|
||||
# order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
|
||||
# If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
|
||||
# extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
|
||||
module Pages
|
||||
def self.included(base) #:nodoc:
|
||||
base.extend(ClassMethods)
|
||||
base.class_eval do
|
||||
@@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
|
||||
##
|
||||
# :singleton-method:
|
||||
# The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
|
||||
# For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>RAILS_ROOT + "/public"</tt>). Changing
|
||||
# this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
|
||||
# web server to look in the new location for cached files.
|
||||
cattr_accessor :page_cache_directory
|
||||
|
||||
@@page_cache_extension = '.html'
|
||||
##
|
||||
# :singleton-method:
|
||||
# Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
|
||||
# order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
|
||||
# If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
|
||||
# extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
|
||||
cattr_accessor :page_cache_extension
|
||||
end
|
||||
end
|
||||
|
@ -147,7 +145,7 @@ module ActionController #:nodoc:
|
|||
|
||||
private
|
||||
def caching_allowed
|
||||
request.get? && response.headers['Status'].to_i == 200
|
||||
request.get? && response.status.to_i == 200
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
module ActionController #:nodoc:
|
||||
module Caching
|
||||
module SqlCache
|
||||
def self.included(base) #:nodoc:
|
||||
if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:cache)
|
||||
base.alias_method_chain :perform_action, :caching
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def perform_action_with_caching
|
||||
ActiveRecord::Base.cache do
|
||||
perform_action_without_caching
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -87,9 +87,9 @@ module ActionController #:nodoc:
|
|||
__send__(action_callback_method_name) if respond_to?(action_callback_method_name, true)
|
||||
end
|
||||
|
||||
def method_missing(method, *arguments)
|
||||
def method_missing(method, *arguments, &block)
|
||||
return if @controller.nil?
|
||||
@controller.__send__(method, *arguments)
|
||||
@controller.__send__(method, *arguments, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +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/session'
|
||||
|
||||
class CGI #:nodoc:
|
||||
include ActionController::CgiExt::Stdinput
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'delegate'
|
||||
|
||||
CGI.module_eval { remove_const "Cookie" }
|
||||
|
||||
# TODO: document how this differs from stdlib CGI::Cookie
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
require 'digest/md5'
|
||||
require 'cgi/session'
|
||||
require 'cgi/session/pstore'
|
||||
|
||||
class CGI #:nodoc:
|
||||
# * Expose the CGI instance to session stores.
|
||||
# * Don't require 'digest/md5' whenever a new session id is generated.
|
||||
class Session #:nodoc:
|
||||
def self.generate_unique_id(constant = nil)
|
||||
ActiveSupport::SecureRandom.hex(16)
|
||||
end
|
||||
|
||||
# Make the CGI instance available to session stores.
|
||||
attr_reader :cgi
|
||||
attr_reader :dbman
|
||||
alias_method :initialize_without_cgi_reader, :initialize
|
||||
def initialize(cgi, options = {})
|
||||
@cgi = cgi
|
||||
initialize_without_cgi_reader(cgi, options)
|
||||
end
|
||||
|
||||
private
|
||||
# Create a new session id.
|
||||
def create_new_id
|
||||
@new_session = true
|
||||
self.class.generate_unique_id
|
||||
end
|
||||
|
||||
# * Don't require 'digest/md5' whenever a new session is started.
|
||||
class PStore #:nodoc:
|
||||
def initialize(session, option={})
|
||||
dir = option['tmpdir'] || Dir::tmpdir
|
||||
prefix = option['prefix'] || ''
|
||||
id = session.session_id
|
||||
md5 = Digest::MD5.hexdigest(id)[0,16]
|
||||
path = dir+"/"+prefix+md5
|
||||
path.untaint
|
||||
if File::exist?(path)
|
||||
@hash = nil
|
||||
else
|
||||
unless session.new_session
|
||||
raise CGI::Session::NoSession, "uninitialized session"
|
||||
end
|
||||
@hash = {}
|
||||
end
|
||||
@p = ::PStore.new(path)
|
||||
@p.transaction do |p|
|
||||
File.chmod(0600, p.path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,185 +1,72 @@
|
|||
require 'action_controller/cgi_ext'
|
||||
require 'action_controller/session/cookie_store'
|
||||
|
||||
module ActionController #:nodoc:
|
||||
class Base
|
||||
# Process a request extracted from a CGI object and return a response. Pass false as <tt>session_options</tt> to disable
|
||||
# sessions (large performance increase if sessions are not needed). The <tt>session_options</tt> are the same as for CGI::Session:
|
||||
#
|
||||
# * <tt>:database_manager</tt> - standard options are CGI::Session::FileStore, CGI::Session::MemoryStore, and CGI::Session::PStore
|
||||
# (default). Additionally, there is CGI::Session::DRbStore and CGI::Session::ActiveRecordStore. Read more about these in
|
||||
# lib/action_controller/session.
|
||||
# * <tt>:session_key</tt> - the parameter name used for the session id. Defaults to '_session_id'.
|
||||
# * <tt>:session_id</tt> - the session id to use. If not provided, then it is retrieved from the +session_key+ cookie, or
|
||||
# automatically generated for a new session.
|
||||
# * <tt>:new_session</tt> - if true, force creation of a new session. If not set, a new session is only created if none currently
|
||||
# exists. If false, a new session is never created, and if none currently exists and the +session_id+ option is not set,
|
||||
# an ArgumentError is raised.
|
||||
# * <tt>:session_expires</tt> - the time the current session expires, as a Time object. If not set, the session will continue
|
||||
# indefinitely.
|
||||
# * <tt>:session_domain</tt> - the hostname domain for which this session is valid. If not set, defaults to the hostname of the
|
||||
# server.
|
||||
# * <tt>:session_secure</tt> - if +true+, this session will only work over HTTPS.
|
||||
# * <tt>:session_path</tt> - the path for which this session applies. Defaults to the directory of the CGI script.
|
||||
# * <tt>:cookie_only</tt> - if +true+ (the default), session IDs will only be accepted from cookies and not from
|
||||
# the query string or POST parameters. This protects against session fixation attacks.
|
||||
def self.process_cgi(cgi = CGI.new, session_options = {})
|
||||
new.process_cgi(cgi, session_options)
|
||||
end
|
||||
|
||||
def process_cgi(cgi, session_options = {}) #:nodoc:
|
||||
process(CgiRequest.new(cgi, session_options), CgiResponse.new(cgi)).out
|
||||
end
|
||||
end
|
||||
|
||||
class CgiRequest < AbstractRequest #:nodoc:
|
||||
attr_accessor :cgi, :session_options
|
||||
class SessionFixationAttempt < StandardError #:nodoc:
|
||||
end
|
||||
|
||||
DEFAULT_SESSION_OPTIONS = {
|
||||
:database_manager => CGI::Session::CookieStore, # store data in cookie
|
||||
:prefix => "ruby_sess.", # prefix session file names
|
||||
:session_path => "/", # available to all paths in app
|
||||
:session_key => "_session_id",
|
||||
:cookie_only => true,
|
||||
:session_http_only=> true
|
||||
}
|
||||
|
||||
def initialize(cgi, session_options = {})
|
||||
@cgi = cgi
|
||||
@session_options = session_options
|
||||
@env = @cgi.__send__(:env_table)
|
||||
super()
|
||||
end
|
||||
|
||||
def query_string
|
||||
qs = @cgi.query_string if @cgi.respond_to?(:query_string)
|
||||
if !qs.blank?
|
||||
qs
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def body_stream #:nodoc:
|
||||
@cgi.stdinput
|
||||
end
|
||||
|
||||
def cookies
|
||||
@cgi.cookies.freeze
|
||||
end
|
||||
|
||||
def session
|
||||
unless defined?(@session)
|
||||
if @session_options == false
|
||||
@session = Hash.new
|
||||
else
|
||||
stale_session_check! do
|
||||
if cookie_only? && query_parameters[session_options_with_string_keys['session_key']]
|
||||
raise SessionFixationAttempt
|
||||
end
|
||||
case value = session_options_with_string_keys['new_session']
|
||||
when true
|
||||
@session = new_session
|
||||
when false
|
||||
begin
|
||||
@session = CGI::Session.new(@cgi, session_options_with_string_keys)
|
||||
# CGI::Session raises ArgumentError if 'new_session' == false
|
||||
# and no session cookie or query param is present.
|
||||
rescue ArgumentError
|
||||
@session = Hash.new
|
||||
end
|
||||
when nil
|
||||
@session = CGI::Session.new(@cgi, session_options_with_string_keys)
|
||||
else
|
||||
raise ArgumentError, "Invalid new_session option: #{value}"
|
||||
end
|
||||
@session['__valid_session']
|
||||
end
|
||||
end
|
||||
end
|
||||
@session
|
||||
end
|
||||
|
||||
def reset_session
|
||||
@session.delete if defined?(@session) && @session.is_a?(CGI::Session)
|
||||
@session = new_session
|
||||
end
|
||||
|
||||
def method_missing(method_id, *arguments)
|
||||
@cgi.__send__(method_id, *arguments) rescue super
|
||||
end
|
||||
|
||||
private
|
||||
# Delete an old session if it exists then create a new one.
|
||||
def new_session
|
||||
if @session_options == false
|
||||
Hash.new
|
||||
else
|
||||
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => false)).delete rescue nil
|
||||
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => true))
|
||||
class CGIHandler
|
||||
module ProperStream
|
||||
def each
|
||||
while line = gets
|
||||
yield line
|
||||
end
|
||||
end
|
||||
|
||||
def cookie_only?
|
||||
session_options_with_string_keys['cookie_only']
|
||||
end
|
||||
|
||||
def stale_session_check!
|
||||
yield
|
||||
rescue ArgumentError => argument_error
|
||||
if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
|
||||
begin
|
||||
# Note that the regexp does not allow $1 to end with a ':'
|
||||
$1.constantize
|
||||
rescue LoadError, NameError => const_error
|
||||
raise ActionController::SessionRestoreError, <<-end_msg
|
||||
Session contains objects whose class definition isn\'t available.
|
||||
Remember to require the classes for all objects kept in the session.
|
||||
(Original exception: #{const_error.message} [#{const_error.class}])
|
||||
end_msg
|
||||
end
|
||||
|
||||
retry
|
||||
def read(*args)
|
||||
if args.empty?
|
||||
super || ""
|
||||
else
|
||||
raise
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def session_options_with_string_keys
|
||||
@session_options_with_string_keys ||= DEFAULT_SESSION_OPTIONS.merge(@session_options).stringify_keys
|
||||
end
|
||||
end
|
||||
|
||||
class CgiResponse < AbstractResponse #:nodoc:
|
||||
def initialize(cgi)
|
||||
@cgi = cgi
|
||||
super()
|
||||
end
|
||||
|
||||
def out(output = $stdout)
|
||||
output.binmode if output.respond_to?(:binmode)
|
||||
output.sync = false if output.respond_to?(:sync=)
|
||||
def self.dispatch_cgi(app, cgi, out = $stdout)
|
||||
env = cgi.__send__(:env_table)
|
||||
env.delete "HTTP_CONTENT_LENGTH"
|
||||
|
||||
cgi.stdinput.extend ProperStream
|
||||
|
||||
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
|
||||
|
||||
env.update({
|
||||
"rack.version" => [0,1],
|
||||
"rack.input" => cgi.stdinput,
|
||||
"rack.errors" => $stderr,
|
||||
"rack.multithread" => false,
|
||||
"rack.multiprocess" => true,
|
||||
"rack.run_once" => false,
|
||||
"rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
|
||||
})
|
||||
|
||||
env["QUERY_STRING"] ||= ""
|
||||
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
|
||||
env["REQUEST_PATH"] ||= "/"
|
||||
env.delete "PATH_INFO" if env["PATH_INFO"] == ""
|
||||
|
||||
status, headers, body = app.call(env)
|
||||
begin
|
||||
output.write(@cgi.header(@headers))
|
||||
out.binmode if out.respond_to?(:binmode)
|
||||
out.sync = false if out.respond_to?(:sync=)
|
||||
|
||||
if @cgi.__send__(:env_table)['REQUEST_METHOD'] == 'HEAD'
|
||||
return
|
||||
elsif @body.respond_to?(:call)
|
||||
# Flush the output now in case the @body Proc uses
|
||||
# #syswrite.
|
||||
output.flush if output.respond_to?(:flush)
|
||||
@body.call(self, output)
|
||||
else
|
||||
output.write(@body)
|
||||
end
|
||||
headers['Status'] = status.to_s
|
||||
out.write(cgi.header(headers))
|
||||
|
||||
output.flush if output.respond_to?(:flush)
|
||||
rescue Errno::EPIPE, Errno::ECONNRESET
|
||||
# lost connection to parent process, ignore output
|
||||
body.each { |part|
|
||||
out.write part
|
||||
out.flush if out.respond_to?(:flush)
|
||||
}
|
||||
ensure
|
||||
body.close if body.respond_to?(:close)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class CgiRequest #:nodoc:
|
||||
DEFAULT_SESSION_OPTIONS = {
|
||||
:database_manager => nil,
|
||||
:prefix => "ruby_sess.",
|
||||
:session_path => "/",
|
||||
:session_key => "_session_id",
|
||||
:cookie_only => true,
|
||||
:session_http_only => true
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,169 +0,0 @@
|
|||
module ActionController #:nodoc:
|
||||
# Components allow you to call other actions for their rendered response while executing another action. You can either delegate
|
||||
# the entire response rendering or you can mix a partial response in with your other content.
|
||||
#
|
||||
# class WeblogController < ActionController::Base
|
||||
# # Performs a method and then lets hello_world output its render
|
||||
# def delegate_action
|
||||
# do_other_stuff_before_hello_world
|
||||
# render_component :controller => "greeter", :action => "hello_world", :params => { :person => "david" }
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# class GreeterController < ActionController::Base
|
||||
# def hello_world
|
||||
# render :text => "#{params[:person]} says, Hello World!"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# The same can be done in a view to do a partial rendering:
|
||||
#
|
||||
# Let's see a greeting:
|
||||
# <%= render_component :controller => "greeter", :action => "hello_world" %>
|
||||
#
|
||||
# It is also possible to specify the controller as a class constant, bypassing the inflector
|
||||
# code to compute the controller class at runtime:
|
||||
#
|
||||
# <%= render_component :controller => GreeterController, :action => "hello_world" %>
|
||||
#
|
||||
# == When to use components
|
||||
#
|
||||
# Components should be used with care. They're significantly slower than simply splitting reusable parts into partials and
|
||||
# conceptually more complicated. Don't use components as a way of separating concerns inside a single application. Instead,
|
||||
# reserve components to those rare cases where you truly have reusable view and controller elements that can be employed
|
||||
# across many applications at once.
|
||||
#
|
||||
# So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters.
|
||||
module Components
|
||||
def self.included(base) #:nodoc:
|
||||
base.class_eval do
|
||||
include InstanceMethods
|
||||
include ActiveSupport::Deprecation
|
||||
extend ClassMethods
|
||||
helper HelperMethods
|
||||
|
||||
# If this controller was instantiated to process a component request,
|
||||
# +parent_controller+ points to the instantiator of this controller.
|
||||
attr_accessor :parent_controller
|
||||
|
||||
alias_method_chain :process_cleanup, :components
|
||||
alias_method_chain :set_session_options, :components
|
||||
alias_method_chain :flash, :components
|
||||
|
||||
alias_method :component_request?, :parent_controller
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Track parent controller to identify component requests
|
||||
def process_with_components(request, response, parent_controller = nil) #:nodoc:
|
||||
controller = new
|
||||
controller.parent_controller = parent_controller
|
||||
controller.process(request, response)
|
||||
end
|
||||
end
|
||||
|
||||
module HelperMethods
|
||||
def render_component(options)
|
||||
@controller.__send__(:render_component_as_string, options)
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# Extracts the action_name from the request parameters and performs that action.
|
||||
def process_with_components(request, response, method = :perform_action, *arguments) #:nodoc:
|
||||
flash.discard if component_request?
|
||||
process_without_components(request, response, method, *arguments)
|
||||
end
|
||||
|
||||
protected
|
||||
# Renders the component specified as the response for the current method
|
||||
def render_component(options) #:doc:
|
||||
component_logging(options) do
|
||||
render_for_text(component_response(options, true).body, response.headers["Status"])
|
||||
end
|
||||
end
|
||||
deprecate :render_component => "Please install render_component plugin from http://github.com/rails/render_component/tree/master"
|
||||
|
||||
# Returns the component response as a string
|
||||
def render_component_as_string(options) #:doc:
|
||||
component_logging(options) do
|
||||
response = component_response(options, false)
|
||||
|
||||
if redirected = response.redirected_to
|
||||
render_component_as_string(redirected)
|
||||
else
|
||||
response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
deprecate :render_component_as_string => "Please install render_component plugin from http://github.com/rails/render_component/tree/master"
|
||||
|
||||
def flash_with_components(refresh = false) #:nodoc:
|
||||
if !defined?(@_flash) || refresh
|
||||
@_flash =
|
||||
if defined?(@parent_controller)
|
||||
@parent_controller.flash
|
||||
else
|
||||
flash_without_components
|
||||
end
|
||||
end
|
||||
@_flash
|
||||
end
|
||||
|
||||
private
|
||||
def component_response(options, reuse_response)
|
||||
klass = component_class(options)
|
||||
request = request_for_component(klass.controller_name, options)
|
||||
new_response = reuse_response ? response : response.dup
|
||||
|
||||
klass.process_with_components(request, new_response, self)
|
||||
end
|
||||
|
||||
# determine the controller class for the component request
|
||||
def component_class(options)
|
||||
if controller = options[:controller]
|
||||
controller.is_a?(Class) ? controller : "#{controller.camelize}Controller".constantize
|
||||
else
|
||||
self.class
|
||||
end
|
||||
end
|
||||
|
||||
# Create a new request object based on the current request.
|
||||
# The new request inherits the session from the current request,
|
||||
# bypassing any session options set for the component controller's class
|
||||
def request_for_component(controller_name, options)
|
||||
new_request = request.dup
|
||||
new_request.session = request.session
|
||||
|
||||
new_request.instance_variable_set(
|
||||
:@parameters,
|
||||
(options[:params] || {}).with_indifferent_access.update(
|
||||
"controller" => controller_name, "action" => options[:action], "id" => options[:id]
|
||||
)
|
||||
)
|
||||
|
||||
new_request
|
||||
end
|
||||
|
||||
def component_logging(options)
|
||||
if logger
|
||||
logger.info "Start rendering component (#{options.inspect}): "
|
||||
result = yield
|
||||
logger.info "\n\nEnd of component rendering"
|
||||
result
|
||||
else
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def set_session_options_with_components(request)
|
||||
set_session_options_without_components(request) unless component_request?
|
||||
end
|
||||
|
||||
def process_cleanup_with_components
|
||||
process_cleanup_without_components unless component_request?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -64,43 +64,31 @@ module ActionController #:nodoc:
|
|||
|
||||
# Returns the value of the cookie by +name+, or +nil+ if no such cookie exists.
|
||||
def [](name)
|
||||
cookie = @cookies[name.to_s]
|
||||
if cookie && cookie.respond_to?(:value)
|
||||
cookie.size > 1 ? cookie.value : cookie.value[0]
|
||||
end
|
||||
super(name.to_s)
|
||||
end
|
||||
|
||||
# Sets the cookie named +name+. The second argument may be the very cookie
|
||||
# value, or a hash of options as documented above.
|
||||
def []=(name, options)
|
||||
def []=(key, options)
|
||||
if options.is_a?(Hash)
|
||||
options = options.inject({}) { |options, pair| options[pair.first.to_s] = pair.last; options }
|
||||
options["name"] = name.to_s
|
||||
options.symbolize_keys!
|
||||
else
|
||||
options = { "name" => name.to_s, "value" => options }
|
||||
options = { :value => options }
|
||||
end
|
||||
|
||||
set_cookie(options)
|
||||
options[:path] = "/" unless options.has_key?(:path)
|
||||
super(key.to_s, options[:value])
|
||||
@controller.response.set_cookie(key, options)
|
||||
end
|
||||
|
||||
# Removes the cookie on the client machine by setting the value to an empty string
|
||||
# and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in
|
||||
# an options hash to delete cookies with extra data such as a <tt>:path</tt>.
|
||||
def delete(name, options = {})
|
||||
options.stringify_keys!
|
||||
set_cookie(options.merge("name" => name.to_s, "value" => "", "expires" => Time.at(0)))
|
||||
def delete(key, options = {})
|
||||
options.symbolize_keys!
|
||||
options[:path] = "/" unless options.has_key?(:path)
|
||||
super(key.to_s)
|
||||
@controller.response.delete_cookie(key, options)
|
||||
end
|
||||
|
||||
private
|
||||
# Builds a CGI::Cookie object and adds the cookie to the response headers.
|
||||
#
|
||||
# The path of the cookie defaults to "/" if there's none in +options+, and
|
||||
# everything is passed to the CGI::Cookie constructor.
|
||||
def set_cookie(options) #:doc:
|
||||
options["path"] = "/" unless options["path"]
|
||||
cookie = CGI::Cookie.new(options)
|
||||
@controller.logger.info "Cookie set: #{cookie}" unless @controller.logger.nil?
|
||||
@controller.response.headers["cookie"] << cookie
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,23 +2,14 @@ module ActionController
|
|||
# Dispatches requests to the appropriate controller and takes care of
|
||||
# reloading the app after each request when Dependencies.load? is true.
|
||||
class Dispatcher
|
||||
@@guard = Mutex.new
|
||||
|
||||
class << self
|
||||
def define_dispatcher_callbacks(cache_classes)
|
||||
unless cache_classes
|
||||
# Development mode callbacks
|
||||
before_dispatch :reload_application
|
||||
after_dispatch :cleanup_application
|
||||
end
|
||||
|
||||
# Common callbacks
|
||||
to_prepare :load_application_controller do
|
||||
begin
|
||||
require_dependency 'application' unless defined?(::ApplicationController)
|
||||
rescue LoadError => error
|
||||
raise unless error.message =~ /application\.rb/
|
||||
end
|
||||
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
|
||||
end
|
||||
|
||||
if defined?(ActiveRecord)
|
||||
|
@ -33,8 +24,7 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
# Backward-compatible class method takes CGI-specific args. Deprecated
|
||||
# in favor of Dispatcher.new(output, request, response).dispatch.
|
||||
# DEPRECATE: Remove CGI support
|
||||
def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout)
|
||||
new(output).dispatch_cgi(cgi, session_options)
|
||||
end
|
||||
|
@ -52,92 +42,49 @@ module ActionController
|
|||
callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
|
||||
@prepare_dispatch_callbacks.replace_or_append!(callback)
|
||||
end
|
||||
|
||||
# If the block raises, send status code as a last-ditch response.
|
||||
def failsafe_response(fallback_output, status, originating_exception = nil)
|
||||
yield
|
||||
rescue Exception => exception
|
||||
begin
|
||||
log_failsafe_exception(status, originating_exception || exception)
|
||||
body = failsafe_response_body(status)
|
||||
fallback_output.write "Status: #{status}\r\nContent-Type: text/html\r\n\r\n#{body}"
|
||||
nil
|
||||
rescue Exception => failsafe_error # Logger or IO errors
|
||||
$stderr.puts "Error during failsafe response: #{failsafe_error}"
|
||||
$stderr.puts "(originally #{originating_exception})" if originating_exception
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def failsafe_response_body(status)
|
||||
error_path = "#{error_file_path}/#{status.to_s[0..3]}.html"
|
||||
|
||||
if File.exist?(error_path)
|
||||
File.read(error_path)
|
||||
else
|
||||
"<html><body><h1>#{status}</h1></body></html>"
|
||||
end
|
||||
end
|
||||
|
||||
def log_failsafe_exception(status, exception)
|
||||
message = "/!\\ FAILSAFE /!\\ #{Time.now}\n Status: #{status}\n"
|
||||
message << " #{exception}\n #{exception.backtrace.join("\n ")}" if exception
|
||||
failsafe_logger.fatal message
|
||||
end
|
||||
|
||||
def failsafe_logger
|
||||
if defined?(::RAILS_DEFAULT_LOGGER) && !::RAILS_DEFAULT_LOGGER.nil?
|
||||
::RAILS_DEFAULT_LOGGER
|
||||
else
|
||||
Logger.new($stderr)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cattr_accessor :error_file_path
|
||||
self.error_file_path = Rails.public_path if defined?(Rails.public_path)
|
||||
cattr_accessor :middleware
|
||||
self.middleware = MiddlewareStack.new do |middleware|
|
||||
middlewares = File.join(File.dirname(__FILE__), "middlewares.rb")
|
||||
middleware.instance_eval(File.read(middlewares))
|
||||
end
|
||||
|
||||
include ActiveSupport::Callbacks
|
||||
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch
|
||||
|
||||
# DEPRECATE: Remove arguments, since they are only used by CGI
|
||||
def initialize(output = $stdout, request = nil, response = nil)
|
||||
@output, @request, @response = output, request, response
|
||||
@output = output
|
||||
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
|
||||
end
|
||||
|
||||
def dispatch_unlocked
|
||||
def dispatch
|
||||
begin
|
||||
run_callbacks :before_dispatch
|
||||
handle_request
|
||||
Routing::Routes.call(@env)
|
||||
rescue Exception => exception
|
||||
failsafe_rescue exception
|
||||
if controller ||= (::ApplicationController rescue Base)
|
||||
controller.call_with_exception(@env, exception).to_a
|
||||
else
|
||||
raise exception
|
||||
end
|
||||
ensure
|
||||
run_callbacks :after_dispatch, :enumerator => :reverse_each
|
||||
end
|
||||
end
|
||||
|
||||
def dispatch
|
||||
if ActionController::Base.allow_concurrency
|
||||
dispatch_unlocked
|
||||
else
|
||||
@@guard.synchronize do
|
||||
dispatch_unlocked
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DEPRECATE: Remove CGI support
|
||||
def dispatch_cgi(cgi, session_options)
|
||||
if cgi ||= self.class.failsafe_response(@output, '400 Bad Request') { CGI.new }
|
||||
@request = CgiRequest.new(cgi, session_options)
|
||||
@response = CgiResponse.new(cgi)
|
||||
dispatch
|
||||
end
|
||||
rescue Exception => exception
|
||||
failsafe_rescue exception
|
||||
CGIHandler.dispatch_cgi(self, cgi, @output)
|
||||
end
|
||||
|
||||
def call(env)
|
||||
@request = RackRequest.new(env)
|
||||
@response = RackResponse.new(@request)
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
def _call(env)
|
||||
@env = env
|
||||
dispatch
|
||||
end
|
||||
|
||||
|
@ -146,8 +93,6 @@ module ActionController
|
|||
run_callbacks :prepare_dispatch
|
||||
|
||||
Routing::Routes.reload
|
||||
ActionController::Base.view_paths.reload!
|
||||
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.clear
|
||||
end
|
||||
|
||||
# Cleanup the application by clearing out loaded classes so they can
|
||||
|
@ -162,35 +107,10 @@ module ActionController
|
|||
Base.logger.flush
|
||||
end
|
||||
|
||||
def mark_as_test_request!
|
||||
@test_request = true
|
||||
self
|
||||
end
|
||||
|
||||
def test_request?
|
||||
@test_request
|
||||
end
|
||||
|
||||
def checkin_connections
|
||||
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
|
||||
return if test_request?
|
||||
return if @env.key?("rack.test")
|
||||
ActiveRecord::Base.clear_active_connections!
|
||||
end
|
||||
|
||||
protected
|
||||
def handle_request
|
||||
@controller = Routing::Routes.recognize(@request)
|
||||
@controller.process(@request, @response).out(@output)
|
||||
end
|
||||
|
||||
def failsafe_rescue(exception)
|
||||
self.class.failsafe_response(@output, '500 Internal Server Error', exception) do
|
||||
if @controller ||= defined?(::ApplicationController) ? ::ApplicationController : Base
|
||||
@controller.process_with_exception(@request, @response, exception).out(@output)
|
||||
else
|
||||
raise exception
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
52
vendor/rails/actionpack/lib/action_controller/failsafe.rb
vendored
Normal file
52
vendor/rails/actionpack/lib/action_controller/failsafe.rb
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
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
|
|
@ -4,20 +4,22 @@ module ActionController #:nodoc:
|
|||
# action that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can
|
||||
# then expose the flash to its template. Actually, that exposure is automatically done. Example:
|
||||
#
|
||||
# class WeblogController < ActionController::Base
|
||||
# class PostsController < ActionController::Base
|
||||
# def create
|
||||
# # save post
|
||||
# flash[:notice] = "Successfully created post"
|
||||
# redirect_to :action => "display", :params => { :id => post.id }
|
||||
# redirect_to posts_path(@post)
|
||||
# end
|
||||
#
|
||||
# def display
|
||||
# def show
|
||||
# # doesn't need to assign the flash notice to the template, that's done automatically
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# display.erb
|
||||
# <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
|
||||
# show.html.erb
|
||||
# <% if flash[:notice] %>
|
||||
# <div class="notice"><%= flash[:notice] %></div>
|
||||
# <% end %>
|
||||
#
|
||||
# This example just places a string in the flash, but you can put any object in there. And of course, you can put as
|
||||
# many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
|
||||
|
@ -27,55 +29,54 @@ module ActionController #:nodoc:
|
|||
def self.included(base)
|
||||
base.class_eval do
|
||||
include InstanceMethods
|
||||
alias_method_chain :assign_shortcuts, :flash
|
||||
alias_method_chain :reset_session, :flash
|
||||
alias_method_chain :perform_action, :flash
|
||||
alias_method_chain :reset_session, :flash
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
class FlashNow #:nodoc:
|
||||
def initialize(flash)
|
||||
@flash = flash
|
||||
end
|
||||
|
||||
|
||||
def []=(k, v)
|
||||
@flash[k] = v
|
||||
@flash.discard(k)
|
||||
v
|
||||
end
|
||||
|
||||
|
||||
def [](k)
|
||||
@flash[k]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class FlashHash < Hash
|
||||
def initialize #:nodoc:
|
||||
super
|
||||
@used = {}
|
||||
end
|
||||
|
||||
|
||||
def []=(k, v) #:nodoc:
|
||||
keep(k)
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
def update(h) #:nodoc:
|
||||
h.keys.each { |k| keep(k) }
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
alias :merge! :update
|
||||
|
||||
|
||||
def replace(h) #:nodoc:
|
||||
@used = {}
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
# Sets a flash that will not be available to the next action, only to the current.
|
||||
#
|
||||
# flash.now[:message] = "Hello current action"
|
||||
#
|
||||
#
|
||||
# This method enables you to use the flash as a central messaging system in your app.
|
||||
# When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
|
||||
# When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
|
||||
|
@ -85,7 +86,7 @@ module ActionController #:nodoc:
|
|||
def now
|
||||
FlashNow.new(self)
|
||||
end
|
||||
|
||||
|
||||
# Keeps either the entire current flash or a specific flash entry available for the next action:
|
||||
#
|
||||
# flash.keep # keeps the entire flash
|
||||
|
@ -93,7 +94,7 @@ module ActionController #:nodoc:
|
|||
def keep(k = nil)
|
||||
use(k, false)
|
||||
end
|
||||
|
||||
|
||||
# Marks the entire flash or a single flash entry to be discarded by the end of the current action:
|
||||
#
|
||||
# flash.discard # discard the entire flash at the end of the current action
|
||||
|
@ -101,12 +102,12 @@ module ActionController #:nodoc:
|
|||
def discard(k = nil)
|
||||
use(k)
|
||||
end
|
||||
|
||||
|
||||
# Mark for removal entries that were kept, and delete unkept ones.
|
||||
#
|
||||
# This method is called automatically by filters, so you generally don't need to care about it.
|
||||
def sweep #:nodoc:
|
||||
keys.each do |k|
|
||||
keys.each do |k|
|
||||
unless @used[k]
|
||||
use(k)
|
||||
else
|
||||
|
@ -118,7 +119,7 @@ module ActionController #:nodoc:
|
|||
# clean up after keys that could have been left over by calling reject! or shift on the flash
|
||||
(@used.keys - keys).each{ |k| @used.delete(k) }
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
|
||||
# use() # marks the entire flash as used
|
||||
|
@ -136,37 +137,27 @@ module ActionController #:nodoc:
|
|||
|
||||
module InstanceMethods #:nodoc:
|
||||
protected
|
||||
def perform_action_with_flash
|
||||
perform_action_without_flash
|
||||
remove_instance_variable(:@_flash) if defined? @_flash
|
||||
end
|
||||
|
||||
def reset_session_with_flash
|
||||
reset_session_without_flash
|
||||
remove_instance_variable(:@_flash)
|
||||
flash(:refresh)
|
||||
remove_instance_variable(:@_flash) if defined? @_flash
|
||||
end
|
||||
|
||||
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
|
||||
# <tt>flash["notice"] = "hello"</tt> to put a new one.
|
||||
# Note that if sessions are disabled only flash.now will work.
|
||||
def flash(refresh = false) #:doc:
|
||||
if !defined?(@_flash) || refresh
|
||||
@_flash =
|
||||
if session.is_a?(Hash)
|
||||
# don't put flash in session if disabled
|
||||
FlashHash.new
|
||||
else
|
||||
# otherwise, session is a CGI::Session or a TestSession
|
||||
# so make sure it gets retrieved from/saved to session storage after request processing
|
||||
session["flash"] ||= FlashHash.new
|
||||
end
|
||||
|
||||
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to
|
||||
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
|
||||
# to put a new one.
|
||||
def flash #:doc:
|
||||
unless defined? @_flash
|
||||
@_flash = session["flash"] ||= FlashHash.new
|
||||
@_flash.sweep
|
||||
end
|
||||
|
||||
@_flash
|
||||
end
|
||||
|
||||
private
|
||||
def assign_shortcuts_with_flash(request, response) #:nodoc:
|
||||
assign_shortcuts_without_flash(request, response)
|
||||
flash(:refresh)
|
||||
flash.sweep if @_session && !component_request?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
require 'active_support/dependencies'
|
||||
|
||||
# FIXME: helper { ... } is broken on Ruby 1.9
|
||||
module ActionController #:nodoc:
|
||||
module Helpers #:nodoc:
|
||||
HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers")
|
||||
|
||||
def self.included(base)
|
||||
# Initialize the base module to aggregate its helpers.
|
||||
base.class_inheritable_accessor :master_helper_module
|
||||
base.master_helper_module = Module.new
|
||||
|
||||
# Set the default directory for helpers
|
||||
base.class_inheritable_accessor :helpers_dir
|
||||
base.helpers_dir = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers")
|
||||
|
||||
# Extend base with class methods to declare helpers.
|
||||
base.extend(ClassMethods)
|
||||
|
||||
|
@ -88,8 +92,8 @@ module ActionController #:nodoc:
|
|||
# When the argument is a module it will be included directly in the template class.
|
||||
# helper FooHelper # => includes FooHelper
|
||||
#
|
||||
# When the argument is the symbol <tt>:all</tt>, the controller will include all helpers from
|
||||
# <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT.
|
||||
# When the argument is the symbol <tt>:all</tt>, the controller will include all helpers beneath
|
||||
# <tt>ActionController::Base.helpers_dir</tt> (defaults to <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT).
|
||||
# helper :all
|
||||
#
|
||||
# Additionally, the +helper+ class method can receive and evaluate a block, making the methods defined available
|
||||
|
@ -159,9 +163,9 @@ module ActionController #:nodoc:
|
|||
def helper_method(*methods)
|
||||
methods.flatten.each do |method|
|
||||
master_helper_module.module_eval <<-end_eval
|
||||
def #{method}(*args, &block)
|
||||
controller.send(%(#{method}), *args, &block)
|
||||
end
|
||||
def #{method}(*args, &block) # def current_user(*args, &block)
|
||||
controller.send(%(#{method}), *args, &block) # controller.send(%(current_user), *args, &block)
|
||||
end # end
|
||||
end_eval
|
||||
end
|
||||
end
|
||||
|
@ -213,8 +217,8 @@ module ActionController #:nodoc:
|
|||
|
||||
# Extract helper names from files in app/helpers/**/*.rb
|
||||
def all_application_helpers
|
||||
extract = /^#{Regexp.quote(HELPERS_DIR)}\/?(.*)_helper.rb$/
|
||||
Dir["#{HELPERS_DIR}/**/*_helper.rb"].map { |file| file.sub extract, '\1' }
|
||||
extract = /^#{Regexp.quote(helpers_dir)}\/?(.*)_helper.rb$/
|
||||
Dir["#{helpers_dir}/**/*_helper.rb"].map { |file| file.sub extract, '\1' }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,42 +1,42 @@
|
|||
module ActionController
|
||||
module HttpAuthentication
|
||||
# Makes it dead easy to do HTTP Basic authentication.
|
||||
#
|
||||
#
|
||||
# Simple Basic example:
|
||||
#
|
||||
#
|
||||
# class PostsController < ApplicationController
|
||||
# USER_NAME, PASSWORD = "dhh", "secret"
|
||||
#
|
||||
#
|
||||
# before_filter :authenticate, :except => [ :index ]
|
||||
#
|
||||
#
|
||||
# def index
|
||||
# render :text => "Everyone can see me!"
|
||||
# end
|
||||
#
|
||||
#
|
||||
# def edit
|
||||
# render :text => "I'm only accessible if you know the password"
|
||||
# end
|
||||
#
|
||||
#
|
||||
# private
|
||||
# def authenticate
|
||||
# authenticate_or_request_with_http_basic do |user_name, password|
|
||||
# authenticate_or_request_with_http_basic do |user_name, password|
|
||||
# user_name == USER_NAME && password == PASSWORD
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
#
|
||||
# Here is a more advanced Basic example where only Atom feeds and the XML API is protected by HTTP authentication,
|
||||
#
|
||||
#
|
||||
# Here is a more advanced Basic example where only Atom feeds and the XML API is protected by HTTP authentication,
|
||||
# the regular HTML interface is protected by a session approach:
|
||||
#
|
||||
#
|
||||
# class ApplicationController < ActionController::Base
|
||||
# before_filter :set_account, :authenticate
|
||||
#
|
||||
#
|
||||
# protected
|
||||
# def set_account
|
||||
# @account = Account.find_by_url_name(request.subdomains.first)
|
||||
# end
|
||||
#
|
||||
#
|
||||
# def authenticate
|
||||
# case request.format
|
||||
# when Mime::XML, Mime::ATOM
|
||||
|
@ -54,24 +54,48 @@ module ActionController
|
|||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
#
|
||||
#
|
||||
# In your integration tests, you can do something like this:
|
||||
#
|
||||
#
|
||||
# def test_access_granted_from_xml
|
||||
# get(
|
||||
# "/notes/1.xml", nil,
|
||||
# "/notes/1.xml", nil,
|
||||
# :authorization => ActionController::HttpAuthentication::Basic.encode_credentials(users(:dhh).name, users(:dhh).password)
|
||||
# )
|
||||
#
|
||||
#
|
||||
# assert_equal 200, status
|
||||
# end
|
||||
#
|
||||
#
|
||||
#
|
||||
# Simple Digest example:
|
||||
#
|
||||
# class PostsController < ApplicationController
|
||||
# USERS = {"dhh" => "secret"}
|
||||
#
|
||||
# before_filter :authenticate, :except => [:index]
|
||||
#
|
||||
# def index
|
||||
# render :text => "Everyone can see me!"
|
||||
# end
|
||||
#
|
||||
# def edit
|
||||
# render :text => "I'm only accessible if you know the password"
|
||||
# end
|
||||
#
|
||||
# private
|
||||
# def authenticate
|
||||
# authenticate_or_request_with_http_digest(realm) do |username|
|
||||
# USERS[username]
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# NOTE: The +authenticate_or_request_with_http_digest+ block must return the user's password so the framework can appropriately
|
||||
# hash it to check the user's credentials. Returning +nil+ will cause authentication to fail.
|
||||
#
|
||||
# On shared hosts, Apache sometimes doesn't pass authentication headers to
|
||||
# FCGI instances. If your environment matches this description and you cannot
|
||||
# authenticate, try this rule in your Apache setup:
|
||||
#
|
||||
#
|
||||
# RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
|
||||
module Basic
|
||||
extend self
|
||||
|
@ -99,14 +123,14 @@ module ActionController
|
|||
def user_name_and_password(request)
|
||||
decode_credentials(request).split(/:/, 2)
|
||||
end
|
||||
|
||||
|
||||
def authorization(request)
|
||||
request.env['HTTP_AUTHORIZATION'] ||
|
||||
request.env['X-HTTP_AUTHORIZATION'] ||
|
||||
request.env['X_HTTP_AUTHORIZATION'] ||
|
||||
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
|
||||
end
|
||||
|
||||
|
||||
def decode_credentials(request)
|
||||
ActiveSupport::Base64.decode64(authorization(request).split.last || '')
|
||||
end
|
||||
|
@ -120,5 +144,131 @@ module ActionController
|
|||
controller.__send__ :render, :text => "HTTP Basic: Access denied.\n", :status => :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
module Digest
|
||||
extend self
|
||||
|
||||
module ControllerMethods
|
||||
def authenticate_or_request_with_http_digest(realm = "Application", &password_procedure)
|
||||
authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm)
|
||||
end
|
||||
|
||||
# Authenticate with HTTP Digest, returns true or false
|
||||
def authenticate_with_http_digest(realm = "Application", &password_procedure)
|
||||
HttpAuthentication::Digest.authenticate(self, realm, &password_procedure)
|
||||
end
|
||||
|
||||
# Render output including the HTTP Digest authentication header
|
||||
def request_http_digest_authentication(realm = "Application", message = nil)
|
||||
HttpAuthentication::Digest.authentication_request(self, realm, message)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns false on a valid response, true otherwise
|
||||
def authenticate(controller, realm, &password_procedure)
|
||||
authorization(controller.request) && validate_digest_response(controller.request, realm, &password_procedure)
|
||||
end
|
||||
|
||||
def authorization(request)
|
||||
request.env['HTTP_AUTHORIZATION'] ||
|
||||
request.env['X-HTTP_AUTHORIZATION'] ||
|
||||
request.env['X_HTTP_AUTHORIZATION'] ||
|
||||
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
|
||||
end
|
||||
|
||||
# Raises error unless the request credentials response value matches the expected value.
|
||||
def validate_digest_response(request, realm, &password_procedure)
|
||||
credentials = decode_credentials_header(request)
|
||||
valid_nonce = validate_nonce(request, credentials[:nonce])
|
||||
|
||||
if valid_nonce && realm == credentials[:realm] && opaque(request.session.session_id) == credentials[:opaque]
|
||||
password = password_procedure.call(credentials[:username])
|
||||
expected = expected_response(request.env['REQUEST_METHOD'], request.url, credentials, password)
|
||||
expected == credentials[:response]
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the expected response for a request of +http_method+ to +uri+ with the decoded +credentials+ and the expected +password+
|
||||
def expected_response(http_method, uri, credentials, password)
|
||||
ha1 = ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
|
||||
ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':'))
|
||||
::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':'))
|
||||
end
|
||||
|
||||
def encode_credentials(http_method, credentials, password)
|
||||
credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password)
|
||||
"Digest " + credentials.sort_by {|x| x[0].to_s }.inject([]) {|a, v| a << "#{v[0]}='#{v[1]}'" }.join(', ')
|
||||
end
|
||||
|
||||
def decode_credentials_header(request)
|
||||
decode_credentials(authorization(request))
|
||||
end
|
||||
|
||||
def decode_credentials(header)
|
||||
header.to_s.gsub(/^Digest\s+/,'').split(',').inject({}) do |hash, pair|
|
||||
key, value = pair.split('=', 2)
|
||||
hash[key.strip.to_sym] = value.to_s.gsub(/^"|"$/,'').gsub(/'/, '')
|
||||
hash
|
||||
end
|
||||
end
|
||||
|
||||
def authentication_header(controller, realm)
|
||||
session_id = controller.request.session.session_id
|
||||
controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce(session_id)}", opaque="#{opaque(session_id)}")
|
||||
end
|
||||
|
||||
def authentication_request(controller, realm, message = nil)
|
||||
message ||= "HTTP Digest: Access denied.\n"
|
||||
authentication_header(controller, realm)
|
||||
controller.__send__ :render, :text => message, :status => :unauthorized
|
||||
end
|
||||
|
||||
# Uses an MD5 digest based on time to generate a value to be used only once.
|
||||
#
|
||||
# A server-specified data string which should be uniquely generated each time a 401 response is made.
|
||||
# It is recommended that this string be base64 or hexadecimal data.
|
||||
# Specifically, since the string is passed in the header lines as a quoted string, the double-quote character is not allowed.
|
||||
#
|
||||
# The contents of the nonce are implementation dependent.
|
||||
# The quality of the implementation depends on a good choice.
|
||||
# A nonce might, for example, be constructed as the base 64 encoding of
|
||||
#
|
||||
# => time-stamp H(time-stamp ":" ETag ":" private-key)
|
||||
#
|
||||
# where time-stamp is a server-generated time or other non-repeating value,
|
||||
# ETag is the value of the HTTP ETag header associated with the requested entity,
|
||||
# and private-key is data known only to the server.
|
||||
# With a nonce of this form a server would recalculate the hash portion after receiving the client authentication header and
|
||||
# reject the request if it did not match the nonce from that header or
|
||||
# if the time-stamp value is not recent enough. In this way the server can limit the time of the nonce's validity.
|
||||
# The inclusion of the ETag prevents a replay request for an updated version of the resource.
|
||||
# (Note: including the IP address of the client in the nonce would appear to offer the server the ability
|
||||
# to limit the reuse of the nonce to the same client that originally got it.
|
||||
# However, that would break proxy farms, where requests from a single user often go through different proxies in the farm.
|
||||
# Also, IP address spoofing is not that hard.)
|
||||
#
|
||||
# An implementation might choose not to accept a previously used nonce or a previously used digest, in order to
|
||||
# protect against a replay attack. Or, an implementation might choose to use one-time nonces or digests for
|
||||
# POST or PUT requests and a time-stamp for GET requests. For more details on the issues involved see Section 4
|
||||
# of this document.
|
||||
#
|
||||
# The nonce is opaque to the client.
|
||||
def nonce(session_id, time = Time.now)
|
||||
t = time.to_i
|
||||
hashed = [t, session_id]
|
||||
digest = ::Digest::MD5.hexdigest(hashed.join(":"))
|
||||
Base64.encode64("#{t}:#{digest}").gsub("\n", '')
|
||||
end
|
||||
|
||||
def validate_nonce(request, value)
|
||||
t = Base64.decode64(value).split(":").first.to_i
|
||||
nonce(request.session.session_id, t) == value && (t - Time.now.to_i).abs <= 10 * 60
|
||||
end
|
||||
|
||||
# Opaque based on digest of session_id
|
||||
def opaque(session_id)
|
||||
Base64.encode64(::Digest::MD5::hexdigest(session_id)).gsub("\n", '')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
require 'active_support/test_case'
|
||||
require 'action_controller/dispatcher'
|
||||
require 'action_controller/test_process'
|
||||
|
||||
require 'stringio'
|
||||
require 'uri'
|
||||
require 'active_support/test_case'
|
||||
|
||||
module ActionController
|
||||
module Integration #:nodoc:
|
||||
|
@ -12,19 +9,26 @@ module ActionController
|
|||
# multiple sessions and run them side-by-side, you can also mimic (to some
|
||||
# limited extent) multiple simultaneous users interacting with your system.
|
||||
#
|
||||
# Typically, you will instantiate a new session using IntegrationTest#open_session,
|
||||
# rather than instantiating Integration::Session directly.
|
||||
# Typically, you will instantiate a new session using
|
||||
# IntegrationTest#open_session, rather than instantiating
|
||||
# Integration::Session directly.
|
||||
class Session
|
||||
include Test::Unit::Assertions
|
||||
include ActionController::Assertions
|
||||
include ActionController::TestCase::Assertions
|
||||
include ActionController::TestProcess
|
||||
|
||||
# Rack application to use
|
||||
attr_accessor :application
|
||||
|
||||
# The integer HTTP status code of the last request.
|
||||
attr_reader :status
|
||||
|
||||
# The status message that accompanied the status code of the last request.
|
||||
attr_reader :status_message
|
||||
|
||||
# The body of the last request.
|
||||
attr_reader :body
|
||||
|
||||
# The URI of the last request.
|
||||
attr_reader :path
|
||||
|
||||
|
@ -60,7 +64,8 @@ module ActionController
|
|||
end
|
||||
|
||||
# Create and initialize a new Session instance.
|
||||
def initialize
|
||||
def initialize(app = nil)
|
||||
@application = app || ActionController::Dispatcher.new
|
||||
reset!
|
||||
end
|
||||
|
||||
|
@ -79,11 +84,13 @@ module ActionController
|
|||
|
||||
self.host = "www.example.com"
|
||||
self.remote_addr = "127.0.0.1"
|
||||
self.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
|
||||
self.accept = "text/xml,application/xml,application/xhtml+xml," +
|
||||
"text/html;q=0.9,text/plain;q=0.8,image/png," +
|
||||
"*/*;q=0.5"
|
||||
|
||||
unless defined? @named_routes_configured
|
||||
# install the named routes in this session instance.
|
||||
klass = class<<self; self; end
|
||||
klass = class << self; self; end
|
||||
Routing::Routes.install_helpers(klass)
|
||||
|
||||
# the helpers are made protected by default--we make them public for
|
||||
|
@ -97,7 +104,7 @@ module ActionController
|
|||
#
|
||||
# session.https!
|
||||
# session.https!(false)
|
||||
def https!(flag=true)
|
||||
def https!(flag = true)
|
||||
@https = flag
|
||||
end
|
||||
|
||||
|
@ -122,7 +129,7 @@ module ActionController
|
|||
# performed on the location header.
|
||||
def follow_redirect!
|
||||
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
|
||||
get(interpret_uri(headers['location'].first))
|
||||
get(interpret_uri(headers['location']))
|
||||
status
|
||||
end
|
||||
|
||||
|
@ -167,17 +174,21 @@ module ActionController
|
|||
|
||||
# Performs a GET request with the given parameters.
|
||||
#
|
||||
# - +path+: The URI (as a String) on which you want to perform a GET request.
|
||||
# - +parameters+: The HTTP parameters that you want to pass. This may be +nil+,
|
||||
# - +path+: The URI (as a String) on which you want to perform a GET
|
||||
# request.
|
||||
# - +parameters+: The HTTP parameters that you want to pass. This may
|
||||
# be +nil+,
|
||||
# a Hash, or a String that is appropriately encoded
|
||||
# (<tt>application/x-www-form-urlencoded</tt> or <tt>multipart/form-data</tt>).
|
||||
# (<tt>application/x-www-form-urlencoded</tt> or
|
||||
# <tt>multipart/form-data</tt>).
|
||||
# - +headers+: Additional HTTP headers to pass, as a Hash. The keys will
|
||||
# automatically be upcased, with the prefix 'HTTP_' added if needed.
|
||||
#
|
||||
# This method returns an AbstractResponse object, which one can use to inspect
|
||||
# the details of the response. Furthermore, if this method was called from an
|
||||
# ActionController::IntegrationTest object, then that object's <tt>@response</tt>
|
||||
# instance variable will point to the same response object.
|
||||
# This method returns an Response object, which one can use to
|
||||
# inspect the details of the response. Furthermore, if this method was
|
||||
# called from an ActionController::IntegrationTest object, then that
|
||||
# object's <tt>@response</tt> instance variable will point to the same
|
||||
# response object.
|
||||
#
|
||||
# You can also perform POST, PUT, DELETE, and HEAD requests with +post+,
|
||||
# +put+, +delete+, and +head+.
|
||||
|
@ -185,22 +196,26 @@ module ActionController
|
|||
process :get, path, parameters, headers
|
||||
end
|
||||
|
||||
# Performs a POST request with the given parameters. See get() for more details.
|
||||
# Performs a POST request with the given parameters. See get() for more
|
||||
# details.
|
||||
def post(path, parameters = nil, headers = nil)
|
||||
process :post, path, parameters, headers
|
||||
end
|
||||
|
||||
# Performs a PUT request with the given parameters. See get() for more details.
|
||||
# Performs a PUT request with the given parameters. See get() for more
|
||||
# details.
|
||||
def put(path, parameters = nil, headers = nil)
|
||||
process :put, path, parameters, headers
|
||||
end
|
||||
|
||||
# Performs a DELETE request with the given parameters. See get() for more details.
|
||||
# Performs a DELETE request with the given parameters. See get() for
|
||||
# more details.
|
||||
def delete(path, parameters = nil, headers = nil)
|
||||
process :delete, path, parameters, headers
|
||||
end
|
||||
|
||||
# Performs a HEAD request with the given parameters. See get() for more details.
|
||||
# Performs a HEAD request with the given parameters. See get() for more
|
||||
# details.
|
||||
def head(path, parameters = nil, headers = nil)
|
||||
process :head, path, parameters, headers
|
||||
end
|
||||
|
@ -215,8 +230,7 @@ module ActionController
|
|||
def xml_http_request(request_method, path, parameters = nil, headers = nil)
|
||||
headers ||= {}
|
||||
headers['X-Requested-With'] = 'XMLHttpRequest'
|
||||
headers['Accept'] ||= 'text/javascript, text/html, application/xml, text/xml, */*'
|
||||
|
||||
headers['Accept'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
||||
process(request_method, path, parameters, headers)
|
||||
end
|
||||
alias xhr :xml_http_request
|
||||
|
@ -224,7 +238,9 @@ module ActionController
|
|||
# Returns the URL for the given options, according to the rules specified
|
||||
# in the application's routes.
|
||||
def url_for(options)
|
||||
controller ? controller.url_for(options) : generic_url_rewriter.rewrite(options)
|
||||
controller ?
|
||||
controller.url_for(options) :
|
||||
generic_url_rewriter.rewrite(options)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -250,17 +266,35 @@ module ActionController
|
|||
data = nil
|
||||
end
|
||||
|
||||
env["QUERY_STRING"] ||= ""
|
||||
|
||||
data = data.is_a?(IO) ? data : StringIO.new(data || '')
|
||||
|
||||
env.update(
|
||||
"REQUEST_METHOD" => method.to_s.upcase,
|
||||
"REQUEST_METHOD" => method.to_s.upcase,
|
||||
"SERVER_NAME" => host,
|
||||
"SERVER_PORT" => (https? ? "443" : "80"),
|
||||
"HTTPS" => https? ? "on" : "off",
|
||||
"rack.url_scheme" => https? ? "https" : "http",
|
||||
"SCRIPT_NAME" => "",
|
||||
|
||||
"REQUEST_URI" => path,
|
||||
"PATH_INFO" => path,
|
||||
"HTTP_HOST" => host,
|
||||
"REMOTE_ADDR" => remote_addr,
|
||||
"SERVER_PORT" => (https? ? "443" : "80"),
|
||||
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
|
||||
"CONTENT_LENGTH" => data ? data.length.to_s : nil,
|
||||
"HTTP_COOKIE" => encode_cookies,
|
||||
"HTTPS" => https? ? "on" : "off",
|
||||
"HTTP_ACCEPT" => accept
|
||||
"HTTP_ACCEPT" => accept,
|
||||
|
||||
"rack.version" => [0,1],
|
||||
"rack.input" => data,
|
||||
"rack.errors" => StringIO.new,
|
||||
"rack.multithread" => true,
|
||||
"rack.multiprocess" => true,
|
||||
"rack.run_once" => false,
|
||||
|
||||
"rack.test" => true
|
||||
)
|
||||
|
||||
(headers || {}).each do |key, value|
|
||||
|
@ -269,54 +303,67 @@ module ActionController
|
|||
env[key] = value
|
||||
end
|
||||
|
||||
unless ActionController::Base.respond_to?(:clear_last_instantiation!)
|
||||
ActionController::Base.module_eval { include ControllerCapture }
|
||||
[ControllerCapture, ActionController::ProcessWithTest].each do |mod|
|
||||
unless ActionController::Base < mod
|
||||
ActionController::Base.class_eval { include mod }
|
||||
end
|
||||
end
|
||||
|
||||
ActionController::Base.clear_last_instantiation!
|
||||
|
||||
env['rack.input'] = data.is_a?(IO) ? data : StringIO.new(data || '')
|
||||
@status, @headers, result_body = ActionController::Dispatcher.new.mark_as_test_request!.call(env)
|
||||
app = @application
|
||||
# Rack::Lint doesn't accept String headers or bodies in Ruby 1.9
|
||||
unless RUBY_VERSION >= '1.9.0' && Rack.release <= '0.9.0'
|
||||
app = Rack::Lint.new(app)
|
||||
end
|
||||
|
||||
status, headers, body = app.call(env)
|
||||
@request_count += 1
|
||||
|
||||
@controller = ActionController::Base.last_instantiation
|
||||
@request = @controller.request
|
||||
@response = @controller.response
|
||||
|
||||
# Decorate the response with the standard behavior of the TestResponse
|
||||
# so that things like assert_response can be used in integration
|
||||
# tests.
|
||||
@response.extend(TestResponseBehavior)
|
||||
|
||||
@html_document = nil
|
||||
|
||||
# Inject status back in for backwords compatibility with CGI
|
||||
@headers['Status'] = @status
|
||||
@status = status.to_i
|
||||
@status_message = StatusCodes::STATUS_CODES[@status]
|
||||
|
||||
@status, @status_message = @status.split(/ /)
|
||||
@status = @status.to_i
|
||||
@headers = Rack::Utils::HeaderHash.new(headers)
|
||||
|
||||
cgi_headers = Hash.new { |h,k| h[k] = [] }
|
||||
@headers.each do |key, value|
|
||||
cgi_headers[key.downcase] << value
|
||||
end
|
||||
cgi_headers['set-cookie'] = cgi_headers['set-cookie'].first
|
||||
@headers = cgi_headers
|
||||
|
||||
@response.headers['cookie'] ||= []
|
||||
(@headers['set-cookie'] || []).each do |cookie|
|
||||
(@headers['Set-Cookie'] || []).each do |cookie|
|
||||
name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
|
||||
@cookies[name] = value
|
||||
|
||||
# Fake CGI cookie header
|
||||
# DEPRECATE: Use response.headers["Set-Cookie"] instead
|
||||
@response.headers['cookie'] << CGI::Cookie::new("name" => name, "value" => value)
|
||||
end
|
||||
|
||||
return status
|
||||
@body = ""
|
||||
if body.is_a?(String)
|
||||
@body << body
|
||||
else
|
||||
body.each { |part| @body << part }
|
||||
end
|
||||
|
||||
if @controller = ActionController::Base.last_instantiation
|
||||
@request = @controller.request
|
||||
@response = @controller.response
|
||||
@controller.send(:set_test_assigns)
|
||||
else
|
||||
# Decorate responses from Rack Middleware and Rails Metal
|
||||
# as an Response for the purposes of integration testing
|
||||
@response = Response.new
|
||||
@response.status = status.to_s
|
||||
@response.headers.replace(@headers)
|
||||
@response.body = @body
|
||||
end
|
||||
|
||||
# Decorate the response with the standard behavior of the
|
||||
# TestResponse so that things like assert_response can be
|
||||
# used in integration tests.
|
||||
@response.extend(TestResponseBehavior)
|
||||
|
||||
return @status
|
||||
rescue MultiPartNeededException
|
||||
boundary = "----------XnJLe9ZIbbGUYtzPQJ16u1"
|
||||
status = process(method, path, multipart_body(parameters, boundary), (headers || {}).merge({"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}"}))
|
||||
status = process(method, path,
|
||||
multipart_body(parameters, boundary),
|
||||
(headers || {}).merge(
|
||||
{"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}"}))
|
||||
return status
|
||||
end
|
||||
|
||||
|
@ -338,7 +385,7 @@ module ActionController
|
|||
"SERVER_PORT" => https? ? "443" : "80",
|
||||
"HTTPS" => https? ? "on" : "off"
|
||||
}
|
||||
ActionController::UrlRewriter.new(ActionController::RackRequest.new(env), {})
|
||||
UrlRewriter.new(Request.new(env), {})
|
||||
end
|
||||
|
||||
def name_with_prefix(prefix, name)
|
||||
|
@ -352,9 +399,13 @@ module ActionController
|
|||
raise MultiPartNeededException
|
||||
elsif Hash === parameters
|
||||
return nil if parameters.empty?
|
||||
parameters.map { |k,v| requestify(v, name_with_prefix(prefix, k)) }.join("&")
|
||||
parameters.map { |k,v|
|
||||
requestify(v, name_with_prefix(prefix, k))
|
||||
}.join("&")
|
||||
elsif Array === parameters
|
||||
parameters.map { |v| requestify(v, name_with_prefix(prefix, "")) }.join("&")
|
||||
parameters.map { |v|
|
||||
requestify(v, name_with_prefix(prefix, ""))
|
||||
}.join("&")
|
||||
elsif prefix.nil?
|
||||
parameters
|
||||
else
|
||||
|
@ -380,7 +431,7 @@ module ActionController
|
|||
def multipart_body(params, boundary)
|
||||
multipart_requestify(params).map do |key, value|
|
||||
if value.respond_to?(:original_filename)
|
||||
File.open(value.path) do |f|
|
||||
File.open(value.path, "rb") do |f|
|
||||
f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
|
||||
|
||||
<<-EOF
|
||||
|
@ -460,8 +511,8 @@ EOF
|
|||
# By default, a single session is automatically created for you, but you
|
||||
# can use this method to open multiple sessions that ought to be tested
|
||||
# simultaneously.
|
||||
def open_session
|
||||
session = Integration::Session.new
|
||||
def open_session(application = nil)
|
||||
session = Integration::Session.new(application)
|
||||
|
||||
# delegate the fixture accessors back to the test instance
|
||||
extras = Module.new { attr_accessor :delegate, :test_result }
|
||||
|
@ -469,12 +520,16 @@ EOF
|
|||
self.class.fixture_table_names.each do |table_name|
|
||||
name = table_name.tr(".", "_")
|
||||
next unless respond_to?(name)
|
||||
extras.__send__(:define_method, name) { |*args| delegate.send(name, *args) }
|
||||
extras.__send__(:define_method, name) { |*args|
|
||||
delegate.send(name, *args)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# delegate add_assertion to the test case
|
||||
extras.__send__(:define_method, :add_assertion) { test_result.add_assertion }
|
||||
extras.__send__(:define_method, :add_assertion) {
|
||||
test_result.add_assertion
|
||||
}
|
||||
session.extend(extras)
|
||||
session.delegate = self
|
||||
session.test_result = @_result
|
||||
|
@ -602,7 +657,8 @@ EOF
|
|||
# would potentially have to set their values for both Test::Unit::TestCase
|
||||
# ActionController::IntegrationTest, since by the time the value is set on
|
||||
# TestCase, IntegrationTest has already been defined and cannot inherit
|
||||
# changes to those variables. So, we make those two attributes copy-on-write.
|
||||
# changes to those variables. So, we make those two attributes
|
||||
# copy-on-write.
|
||||
|
||||
class << self
|
||||
def use_transactional_fixtures=(flag) #:nodoc:
|
||||
|
|
|
@ -175,13 +175,18 @@ module ActionController #:nodoc:
|
|||
def default_layout(format) #:nodoc:
|
||||
layout = read_inheritable_attribute(:layout)
|
||||
return layout unless read_inheritable_attribute(:auto_layout)
|
||||
@default_layout ||= {}
|
||||
@default_layout[format] ||= default_layout_with_format(format, layout)
|
||||
@default_layout[format]
|
||||
find_layout(layout, format)
|
||||
end
|
||||
|
||||
def layout_list #:nodoc:
|
||||
Array(view_paths).sum([]) { |path| Dir["#{path}/layouts/**/*"] }
|
||||
Array(view_paths).sum([]) { |path| Dir["#{path.to_str}/layouts/**/*"] }
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -200,15 +205,6 @@ module ActionController #:nodoc:
|
|||
def normalize_conditions(conditions)
|
||||
conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})}
|
||||
end
|
||||
|
||||
def default_layout_with_format(format, layout)
|
||||
list = layout_list
|
||||
if list.grep(%r{layouts/#{layout}\.#{format}(\.[a-z][0-9a-z]*)+$}).empty?
|
||||
(!list.grep(%r{layouts/#{layout}\.([a-z][0-9a-z]*)+$}).empty? && format == :html) ? layout : nil
|
||||
else
|
||||
layout
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the name of the active layout. If the layout was specified as a method reference (through a symbol), this method
|
||||
|
@ -217,28 +213,34 @@ module ActionController #:nodoc:
|
|||
# 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 String then layout
|
||||
when Symbol then __send__(layout)
|
||||
when Proc then layout.call(self)
|
||||
else layout
|
||||
end
|
||||
|
||||
# Explicitly passed layout names with slashes are looked up relative to the template root,
|
||||
# but auto-discovered layouts derived from a nested controller will contain a slash, though be relative
|
||||
# to the 'layouts' directory so we have to check the file system to infer which case the layout name came from.
|
||||
if active_layout
|
||||
if active_layout.include?('/') && ! layout_directory?(active_layout)
|
||||
active_layout
|
||||
if layout = self.class.find_layout(active_layout, @template.template_format)
|
||||
layout
|
||||
else
|
||||
"layouts/#{active_layout}"
|
||||
raise ActionView::MissingTemplate.new(self.class.view_paths, active_layout)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def candidate_for_layout?(options)
|
||||
options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty? &&
|
||||
!@template.__send__(:_exempt_from_layout?, options[:template] || default_template_name(options[:action]))
|
||||
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)
|
||||
|
@ -247,7 +249,7 @@ module ActionController #:nodoc:
|
|||
when FalseClass
|
||||
nil
|
||||
when NilClass, TrueClass
|
||||
active_layout if action_has_layout? && !@template.__send__(:_exempt_from_layout?, default_template_name)
|
||||
active_layout if action_has_layout? && candidate_for_layout?(:template => default_template_name)
|
||||
else
|
||||
active_layout(layout)
|
||||
end
|
||||
|
@ -271,12 +273,6 @@ module ActionController #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
def layout_directory?(layout_name)
|
||||
@template.__send__(:_pick_template, "#{File.join('layouts', layout_name)}.#{@template.template_format}") ? true : false
|
||||
rescue ActionView::MissingTemplate
|
||||
false
|
||||
end
|
||||
|
||||
def default_template_format
|
||||
response.template.template_format
|
||||
end
|
||||
|
|
109
vendor/rails/actionpack/lib/action_controller/middleware_stack.rb
vendored
Normal file
109
vendor/rails/actionpack/lib/action_controller/middleware_stack.rb
vendored
Normal file
|
@ -0,0 +1,109 @@
|
|||
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
|
22
vendor/rails/actionpack/lib/action_controller/middlewares.rb
vendored
Normal file
22
vendor/rails/actionpack/lib/action_controller/middlewares.rb
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
use "Rack::Lock", :if => lambda {
|
||||
!ActionController::Base.allow_concurrency
|
||||
}
|
||||
|
||||
use "ActionController::Failsafe"
|
||||
|
||||
["ActionController::Session::CookieStore",
|
||||
"ActionController::Session::MemCacheStore",
|
||||
"ActiveRecord::SessionStore"].each do |store|
|
||||
use(store, ActionController::Base.session_options,
|
||||
:if => lambda {
|
||||
if session_store = ActionController::Base.session_store
|
||||
session_store.name == store
|
||||
end
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
use "ActionController::RewindableInput"
|
||||
use "ActionController::ParamsParser"
|
||||
use "Rack::MethodOverride"
|
||||
use "Rack::Head"
|
|
@ -143,12 +143,27 @@ module ActionController #:nodoc:
|
|||
custom(@mime_type_priority.first, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def self.generate_method_for_mime(mime)
|
||||
sym = mime.is_a?(Symbol) ? mime : mime.to_sym
|
||||
const = sym.to_s.upcase
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{sym}(&block) # def html(&block)
|
||||
custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
|
||||
end # end
|
||||
RUBY
|
||||
end
|
||||
|
||||
Mime::SET.each do |mime|
|
||||
generate_method_for_mime(mime)
|
||||
end
|
||||
|
||||
def method_missing(symbol, &block)
|
||||
mime_constant = symbol.to_s.upcase
|
||||
|
||||
if Mime::SET.include?(Mime.const_get(mime_constant))
|
||||
custom(Mime.const_get(mime_constant), &block)
|
||||
mime_constant = Mime.const_get(symbol.to_s.upcase)
|
||||
|
||||
if Mime::SET.include?(mime_constant)
|
||||
self.class.generate_method_for_mime(mime_constant)
|
||||
send(symbol, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
|
|
|
@ -176,6 +176,14 @@ module Mime
|
|||
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?
|
||||
|
|
71
vendor/rails/actionpack/lib/action_controller/params_parser.rb
vendored
Normal file
71
vendor/rails/actionpack/lib/action_controller/params_parser.rb
vendored
Normal file
|
@ -0,0 +1,71 @@
|
|||
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
|
|
@ -1,4 +1,3 @@
|
|||
require 'action_controller/integration'
|
||||
require 'active_support/testing/performance'
|
||||
require 'active_support/testing/default'
|
||||
|
||||
|
|
|
@ -36,12 +36,11 @@ module ActionController
|
|||
#
|
||||
# * <tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt>
|
||||
# * <tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt>
|
||||
# * <tt>formatted_polymorphic_url</tt>, <tt>formatted_polymorphic_path</tt>
|
||||
#
|
||||
# Example usage:
|
||||
#
|
||||
# edit_polymorphic_path(@post) # => "/posts/1/edit"
|
||||
# formatted_polymorphic_path([@post, :pdf]) # => "/posts/1.pdf"
|
||||
# polymorphic_path(@post, :format => :pdf) # => "/posts/1.pdf"
|
||||
module PolymorphicRoutes
|
||||
# Constructs a call to a named RESTful route for the given record and returns the
|
||||
# resulting URL string. For example:
|
||||
|
@ -55,7 +54,7 @@ module ActionController
|
|||
# ==== Options
|
||||
#
|
||||
# * <tt>:action</tt> - Specifies the action prefix for the named route:
|
||||
# <tt>:new</tt>, <tt>:edit</tt>, or <tt>:formatted</tt>. Default is no prefix.
|
||||
# <tt>:new</tt> or <tt>:edit</tt>. Default is no prefix.
|
||||
# * <tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or <tt>:url</tt>.
|
||||
# Default is <tt>:url</tt>.
|
||||
#
|
||||
|
@ -78,9 +77,8 @@ module ActionController
|
|||
end
|
||||
|
||||
record = extract_record(record_or_hash_or_array)
|
||||
format = extract_format(record_or_hash_or_array, options)
|
||||
namespace = extract_namespace(record_or_hash_or_array)
|
||||
|
||||
|
||||
args = case record_or_hash_or_array
|
||||
when Hash; [ record_or_hash_or_array ]
|
||||
when Array; record_or_hash_or_array.dup
|
||||
|
@ -100,11 +98,10 @@ module ActionController
|
|||
end
|
||||
|
||||
args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
|
||||
args << format if format
|
||||
|
||||
|
||||
named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
|
||||
|
||||
url_options = options.except(:action, :routing_type, :format)
|
||||
url_options = options.except(:action, :routing_type)
|
||||
unless url_options.empty?
|
||||
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
|
||||
end
|
||||
|
@ -119,21 +116,37 @@ module ActionController
|
|||
polymorphic_url(record_or_hash_or_array, options)
|
||||
end
|
||||
|
||||
%w(edit new formatted).each do |action|
|
||||
%w(edit new).each do |action|
|
||||
module_eval <<-EOT, __FILE__, __LINE__
|
||||
def #{action}_polymorphic_url(record_or_hash, options = {})
|
||||
polymorphic_url(record_or_hash, options.merge(:action => "#{action}"))
|
||||
end
|
||||
|
||||
def #{action}_polymorphic_path(record_or_hash, options = {})
|
||||
polymorphic_url(record_or_hash, options.merge(:action => "#{action}", :routing_type => :path))
|
||||
end
|
||||
def #{action}_polymorphic_url(record_or_hash, options = {}) # def edit_polymorphic_url(record_or_hash, options = {})
|
||||
polymorphic_url( # polymorphic_url(
|
||||
record_or_hash, # record_or_hash,
|
||||
options.merge(:action => "#{action}")) # options.merge(:action => "edit"))
|
||||
end # end
|
||||
#
|
||||
def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_polymorphic_path(record_or_hash, options = {})
|
||||
polymorphic_url( # polymorphic_url(
|
||||
record_or_hash, # record_or_hash,
|
||||
options.merge(:action => "#{action}", :routing_type => :path)) # options.merge(:action => "edit", :routing_type => :path))
|
||||
end # end
|
||||
EOT
|
||||
end
|
||||
|
||||
def formatted_polymorphic_url(record_or_hash, options = {})
|
||||
ActiveSupport::Deprecation.warn("formatted_polymorphic_url has been deprecated. Please pass :format to the polymorphic_url method instead", caller)
|
||||
options[:format] = record_or_hash.pop if Array === record_or_hash
|
||||
polymorphic_url(record_or_hash, options)
|
||||
end
|
||||
|
||||
def formatted_polymorphic_path(record_or_hash, options = {})
|
||||
ActiveSupport::Deprecation.warn("formatted_polymorphic_path has been deprecated. Please pass :format to the polymorphic_path method instead", caller)
|
||||
options[:format] = record_or_hash.pop if record_or_hash === Array
|
||||
polymorphic_url(record_or_hash, options.merge(:routing_type => :path))
|
||||
end
|
||||
|
||||
private
|
||||
def action_prefix(options)
|
||||
options[:action] ? "#{options[:action]}_" : options[:format] ? "formatted_" : ""
|
||||
options[:action] ? "#{options[:action]}_" : ''
|
||||
end
|
||||
|
||||
def routing_type(options)
|
||||
|
@ -171,17 +184,7 @@ module ActionController
|
|||
else record_or_hash_or_array
|
||||
end
|
||||
end
|
||||
|
||||
def extract_format(record_or_hash_or_array, options)
|
||||
if options[:action].to_s == "formatted" && record_or_hash_or_array.is_a?(Array)
|
||||
record_or_hash_or_array.pop
|
||||
elsif options[:format]
|
||||
options[:format]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Remove the first symbols from the array and return the url prefix
|
||||
# implied by those symbols.
|
||||
def extract_namespace(record_or_hash_or_array)
|
||||
|
|
3
vendor/rails/actionpack/lib/action_controller/rack_ext.rb
vendored
Normal file
3
vendor/rails/actionpack/lib/action_controller/rack_ext.rb
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
require 'action_controller/rack_ext/lock'
|
||||
require 'action_controller/rack_ext/multipart'
|
||||
require 'action_controller/rack_ext/parse_query'
|
21
vendor/rails/actionpack/lib/action_controller/rack_ext/lock.rb
vendored
Normal file
21
vendor/rails/actionpack/lib/action_controller/rack_ext/lock.rb
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
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
|
22
vendor/rails/actionpack/lib/action_controller/rack_ext/multipart.rb
vendored
Normal file
22
vendor/rails/actionpack/lib/action_controller/rack_ext/multipart.rb
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
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
|
17
vendor/rails/actionpack/lib/action_controller/rack_ext/parse_query.rb
vendored
Normal file
17
vendor/rails/actionpack/lib/action_controller/rack_ext/parse_query.rb
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
# 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
|
||||
parse_query_without_ajax_body_cleanup(qs, d)
|
||||
end
|
||||
module_function :parse_query
|
||||
end
|
||||
end
|
|
@ -1,303 +0,0 @@
|
|||
require 'action_controller/cgi_ext'
|
||||
require 'action_controller/session/cookie_store'
|
||||
|
||||
module ActionController #:nodoc:
|
||||
class RackRequest < AbstractRequest #:nodoc:
|
||||
attr_accessor :session_options
|
||||
attr_reader :cgi
|
||||
|
||||
class SessionFixationAttempt < StandardError #:nodoc:
|
||||
end
|
||||
|
||||
DEFAULT_SESSION_OPTIONS = {
|
||||
:database_manager => CGI::Session::CookieStore, # store data in cookie
|
||||
:prefix => "ruby_sess.", # prefix session file names
|
||||
:session_path => "/", # available to all paths in app
|
||||
:session_key => "_session_id",
|
||||
:cookie_only => true,
|
||||
:session_http_only=> true
|
||||
}
|
||||
|
||||
def initialize(env, session_options = DEFAULT_SESSION_OPTIONS)
|
||||
@session_options = session_options
|
||||
@env = env
|
||||
@cgi = CGIWrapper.new(self)
|
||||
super()
|
||||
end
|
||||
|
||||
%w[ AUTH_TYPE GATEWAY_INTERFACE PATH_INFO
|
||||
PATH_TRANSLATED REMOTE_HOST
|
||||
REMOTE_IDENT REMOTE_USER SCRIPT_NAME
|
||||
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 query_string
|
||||
qs = super
|
||||
if !qs.blank?
|
||||
qs
|
||||
else
|
||||
@env['QUERY_STRING']
|
||||
end
|
||||
end
|
||||
|
||||
def body_stream #:nodoc:
|
||||
@env['rack.input']
|
||||
end
|
||||
|
||||
def key?(key)
|
||||
@env.key?(key)
|
||||
end
|
||||
|
||||
def cookies
|
||||
return {} unless @env["HTTP_COOKIE"]
|
||||
|
||||
unless @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
|
||||
@env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
|
||||
@env["rack.request.cookie_hash"] = CGI::Cookie::parse(@env["rack.request.cookie_string"])
|
||||
end
|
||||
|
||||
@env["rack.request.cookie_hash"]
|
||||
end
|
||||
|
||||
def server_port
|
||||
@env['SERVER_PORT'].to_i
|
||||
end
|
||||
|
||||
def server_software
|
||||
@env['SERVER_SOFTWARE'].split("/").first
|
||||
end
|
||||
|
||||
def session
|
||||
unless defined?(@session)
|
||||
if @session_options == false
|
||||
@session = Hash.new
|
||||
else
|
||||
stale_session_check! do
|
||||
if cookie_only? && query_parameters[session_options_with_string_keys['session_key']]
|
||||
raise SessionFixationAttempt
|
||||
end
|
||||
case value = session_options_with_string_keys['new_session']
|
||||
when true
|
||||
@session = new_session
|
||||
when false
|
||||
begin
|
||||
@session = CGI::Session.new(@cgi, session_options_with_string_keys)
|
||||
# CGI::Session raises ArgumentError if 'new_session' == false
|
||||
# and no session cookie or query param is present.
|
||||
rescue ArgumentError
|
||||
@session = Hash.new
|
||||
end
|
||||
when nil
|
||||
@session = CGI::Session.new(@cgi, session_options_with_string_keys)
|
||||
else
|
||||
raise ArgumentError, "Invalid new_session option: #{value}"
|
||||
end
|
||||
@session['__valid_session']
|
||||
end
|
||||
end
|
||||
end
|
||||
@session
|
||||
end
|
||||
|
||||
def reset_session
|
||||
@session.delete if defined?(@session) && @session.is_a?(CGI::Session)
|
||||
@session = new_session
|
||||
end
|
||||
|
||||
private
|
||||
# Delete an old session if it exists then create a new one.
|
||||
def new_session
|
||||
if @session_options == false
|
||||
Hash.new
|
||||
else
|
||||
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => false)).delete rescue nil
|
||||
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => true))
|
||||
end
|
||||
end
|
||||
|
||||
def cookie_only?
|
||||
session_options_with_string_keys['cookie_only']
|
||||
end
|
||||
|
||||
def stale_session_check!
|
||||
yield
|
||||
rescue ArgumentError => argument_error
|
||||
if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
|
||||
begin
|
||||
# Note that the regexp does not allow $1 to end with a ':'
|
||||
$1.constantize
|
||||
rescue LoadError, NameError => const_error
|
||||
raise ActionController::SessionRestoreError, <<-end_msg
|
||||
Session contains objects whose class definition isn\'t available.
|
||||
Remember to require the classes for all objects kept in the session.
|
||||
(Original exception: #{const_error.message} [#{const_error.class}])
|
||||
end_msg
|
||||
end
|
||||
|
||||
retry
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def session_options_with_string_keys
|
||||
@session_options_with_string_keys ||= DEFAULT_SESSION_OPTIONS.merge(@session_options).stringify_keys
|
||||
end
|
||||
end
|
||||
|
||||
class RackResponse < AbstractResponse #:nodoc:
|
||||
def initialize(request)
|
||||
@cgi = request.cgi
|
||||
@writer = lambda { |x| @body << x }
|
||||
@block = nil
|
||||
super()
|
||||
end
|
||||
|
||||
# Retrieve status from instance variable if has already been delete
|
||||
def status
|
||||
@status || super
|
||||
end
|
||||
|
||||
def out(output = $stdout, &block)
|
||||
# Nasty hack because CGI sessions are closed after the normal
|
||||
# prepare! statement
|
||||
set_cookies!
|
||||
|
||||
@block = block
|
||||
@status = headers.delete("Status")
|
||||
if [204, 304].include?(status.to_i)
|
||||
headers.delete("Content-Type")
|
||||
[status, headers.to_hash, []]
|
||||
else
|
||||
[status, headers.to_hash, self]
|
||||
end
|
||||
end
|
||||
alias to_a out
|
||||
|
||||
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
|
||||
|
||||
def close
|
||||
@body.close if @body.respond_to?(:close)
|
||||
end
|
||||
|
||||
def empty?
|
||||
@block == nil && @body.empty?
|
||||
end
|
||||
|
||||
def prepare!
|
||||
super
|
||||
|
||||
convert_language!
|
||||
convert_expires!
|
||||
set_status!
|
||||
# set_cookies!
|
||||
end
|
||||
|
||||
private
|
||||
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_content_type!
|
||||
super
|
||||
headers['Content-Type'] = headers.delete('type') || "text/html"
|
||||
headers['Content-Type'] += "; charset=" + headers.delete('charset') if headers['charset']
|
||||
end
|
||||
|
||||
def set_content_length!
|
||||
super
|
||||
headers["Content-Length"] = headers["Content-Length"].to_s if headers["Content-Length"]
|
||||
end
|
||||
|
||||
def set_status!
|
||||
self.status ||= "200 OK"
|
||||
end
|
||||
|
||||
def set_cookies!
|
||||
# Convert 'cookie' header to 'Set-Cookie' headers.
|
||||
# Because Set-Cookie header can appear more the once in the response body,
|
||||
# we store it in a line break separated string that will be translated to
|
||||
# multiple Set-Cookie header by the handler.
|
||||
if cookie = headers.delete('cookie')
|
||||
cookies = []
|
||||
|
||||
case cookie
|
||||
when Array then cookie.each { |c| cookies << c.to_s }
|
||||
when Hash then cookie.each { |_, c| cookies << c.to_s }
|
||||
else cookies << cookie.to_s
|
||||
end
|
||||
|
||||
@cgi.output_cookies.each { |c| cookies << c.to_s } if @cgi.output_cookies
|
||||
|
||||
headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].flatten.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class CGIWrapper < ::CGI
|
||||
attr_reader :output_cookies
|
||||
|
||||
def initialize(request, *args)
|
||||
@request = request
|
||||
@args = *args
|
||||
@input = request.body
|
||||
|
||||
super *args
|
||||
end
|
||||
|
||||
def params
|
||||
@params ||= @request.params
|
||||
end
|
||||
|
||||
def cookies
|
||||
@request.cookies
|
||||
end
|
||||
|
||||
def query_string
|
||||
@request.query_string
|
||||
end
|
||||
|
||||
# Used to wrap the normal args variable used inside CGI.
|
||||
def args
|
||||
@args
|
||||
end
|
||||
|
||||
# Used to wrap the normal env_table variable used inside CGI.
|
||||
def env_table
|
||||
@request.env
|
||||
end
|
||||
|
||||
# Used to wrap the normal stdinput variable used inside CGI.
|
||||
def stdinput
|
||||
@input
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,39 +3,42 @@ require 'stringio'
|
|||
require 'strscan'
|
||||
|
||||
require 'active_support/memoizable'
|
||||
require 'action_controller/cgi_ext'
|
||||
|
||||
module ActionController
|
||||
# CgiRequest and TestRequest provide concrete implementations.
|
||||
class AbstractRequest
|
||||
extend ActiveSupport::Memoizable
|
||||
class Request < Rack::Request
|
||||
|
||||
def self.relative_url_root=(relative_url_root)
|
||||
ActiveSupport::Deprecation.warn(
|
||||
"ActionController::AbstractRequest.relative_url_root= has been renamed." +
|
||||
"You can now set it with config.action_controller.relative_url_root=", caller)
|
||||
ActionController::Base.relative_url_root=relative_url_root
|
||||
%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 }
|
||||
|
||||
# The hash of environment variables for this request,
|
||||
# such as { 'RAILS_ENV' => 'production' }.
|
||||
attr_reader :env
|
||||
|
||||
# The true HTTP request \method as a lowercase symbol, such as <tt>:get</tt>.
|
||||
# UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS.
|
||||
# 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
|
||||
method = @env['REQUEST_METHOD']
|
||||
method = parameters[:_method] if method == 'POST' && !parameters[:_method].blank?
|
||||
|
||||
HTTP_METHOD_LOOKUP[method] || raise(UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
|
||||
@request_method ||= HTTP_METHOD_LOOKUP[super] || raise(UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
|
||||
end
|
||||
memoize :request_method
|
||||
|
||||
# The HTTP request \method as a lowercase symbol, such as <tt>:get</tt>.
|
||||
# Note, HEAD is returned as <tt>:get</tt> since the two are functionally
|
||||
# equivalent from the application's perspective.
|
||||
# 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
|
||||
|
@ -70,43 +73,46 @@ module ActionController
|
|||
#
|
||||
# request.headers["Content-Type"] # => "text/plain"
|
||||
def headers
|
||||
ActionController::Http::Headers.new(@env)
|
||||
@headers ||= ActionController::Http::Headers.new(@env)
|
||||
end
|
||||
memoize :headers
|
||||
|
||||
# Returns the content length of the request as an integer.
|
||||
def content_length
|
||||
@env['CONTENT_LENGTH'].to_i
|
||||
super.to_i
|
||||
end
|
||||
memoize :content_length
|
||||
|
||||
# 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
|
||||
Mime::Type.lookup(content_type_without_parameters)
|
||||
@content_type ||= begin
|
||||
if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/
|
||||
Mime::Type.lookup($1.strip.downcase)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
memoize :content_type
|
||||
|
||||
# Returns the accepted MIME type for the request.
|
||||
def accepts
|
||||
header = @env['HTTP_ACCEPT'].to_s.strip
|
||||
@accepts ||= begin
|
||||
header = @env['HTTP_ACCEPT'].to_s.strip
|
||||
|
||||
if header.empty?
|
||||
[content_type, Mime::ALL].compact
|
||||
else
|
||||
Mime::Type.parse(header)
|
||||
if header.empty?
|
||||
[content_type, Mime::ALL].compact
|
||||
else
|
||||
Mime::Type.parse(header)
|
||||
end
|
||||
end
|
||||
end
|
||||
memoize :accepts
|
||||
|
||||
def if_modified_since
|
||||
if since = env['HTTP_IF_MODIFIED_SINCE']
|
||||
Time.rfc2822(since) rescue nil
|
||||
end
|
||||
end
|
||||
memoize :if_modified_since
|
||||
|
||||
def if_none_match
|
||||
env['HTTP_IF_NONE_MATCH']
|
||||
|
@ -125,15 +131,15 @@ module ActionController
|
|||
# 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
|
||||
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
|
||||
|
||||
# Returns the Mime type for the \format used in the request.
|
||||
|
@ -209,7 +215,7 @@ module ActionController
|
|||
# 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'].split(',').collect(&:strip)
|
||||
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}
|
||||
|
@ -218,7 +224,7 @@ module ActionController
|
|||
remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',')
|
||||
|
||||
if @env.include? 'HTTP_CLIENT_IP'
|
||||
if remote_ips && !remote_ips.include?(@env['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?!
|
||||
|
@ -240,26 +246,21 @@ EOM
|
|||
|
||||
@env['REMOTE_ADDR']
|
||||
end
|
||||
memoize :remote_ip
|
||||
|
||||
# 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
|
||||
memoize :server_software
|
||||
|
||||
|
||||
# Returns the complete URL used for this request.
|
||||
def url
|
||||
protocol + host_with_port + request_uri
|
||||
end
|
||||
memoize :url
|
||||
|
||||
# Returns 'https://' if this is an SSL request and 'http://' otherwise.
|
||||
def protocol
|
||||
ssl? ? 'https://' : 'http://'
|
||||
end
|
||||
memoize :protocol
|
||||
|
||||
# Is this an SSL request?
|
||||
def ssl?
|
||||
|
@ -271,7 +272,7 @@ EOM
|
|||
if forwarded = env["HTTP_X_FORWARDED_HOST"]
|
||||
forwarded.split(/,\s?/).last
|
||||
else
|
||||
env['HTTP_HOST'] || env['SERVER_NAME'] || "#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
|
||||
env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -279,14 +280,12 @@ EOM
|
|||
def host
|
||||
raw_host_with_port.sub(/:\d+$/, '')
|
||||
end
|
||||
memoize :host
|
||||
|
||||
# Returns a \host:\port string for this request, such as "example.com" or
|
||||
# "example.com:8080".
|
||||
def host_with_port
|
||||
"#{host}#{port_string}"
|
||||
end
|
||||
memoize :host_with_port
|
||||
|
||||
# Returns the port number of this request as an integer.
|
||||
def port
|
||||
|
@ -296,7 +295,6 @@ EOM
|
|||
standard_port
|
||||
end
|
||||
end
|
||||
memoize :port
|
||||
|
||||
# Returns the standard \port number for this request's protocol.
|
||||
def standard_port
|
||||
|
@ -332,13 +330,8 @@ EOM
|
|||
|
||||
# Returns the query string, accounting for server idiosyncrasies.
|
||||
def query_string
|
||||
if uri = @env['REQUEST_URI']
|
||||
uri.split('?', 2)[1] || ''
|
||||
else
|
||||
@env['QUERY_STRING'] || ''
|
||||
end
|
||||
@env['QUERY_STRING'].present? ? @env['QUERY_STRING'] : (@env['REQUEST_URI'].split('?', 2)[1] || '')
|
||||
end
|
||||
memoize :query_string
|
||||
|
||||
# Returns the request URI, accounting for server idiosyncrasies.
|
||||
# WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
|
||||
|
@ -364,36 +357,33 @@ EOM
|
|||
end
|
||||
end
|
||||
end
|
||||
memoize :request_uri
|
||||
|
||||
# Returns the interpreted \path to requested resource after all the installation
|
||||
# directory of this application was taken into account.
|
||||
def path
|
||||
path = (uri = request_uri) ? uri.split('?').first.to_s : ''
|
||||
|
||||
# Cut off the path to the installation directory if given
|
||||
path.sub!(%r/^#{ActionController::Base.relative_url_root}/, '')
|
||||
path || ''
|
||||
path = request_uri.to_s[/\A[^\?]*/]
|
||||
path.sub!(/\A#{ActionController::Base.relative_url_root}/, '')
|
||||
path
|
||||
end
|
||||
memoize :path
|
||||
|
||||
# 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(content_length)
|
||||
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']
|
||||
@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:
|
||||
@path_parameters = parameters
|
||||
@env["rack.routing_args"] = parameters
|
||||
@symbolized_path_parameters = @parameters = nil
|
||||
end
|
||||
|
||||
|
@ -409,464 +399,67 @@ EOM
|
|||
#
|
||||
# See <tt>symbolized_path_parameters</tt> for symbolized keys.
|
||||
def path_parameters
|
||||
@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']
|
||||
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
|
||||
body_stream
|
||||
@env['rack.input']
|
||||
end
|
||||
end
|
||||
|
||||
def remote_addr
|
||||
@env['REMOTE_ADDR']
|
||||
def form_data?
|
||||
FORM_DATA_MEDIA_TYPES.include?(content_type.to_s)
|
||||
end
|
||||
|
||||
def referrer
|
||||
@env['HTTP_REFERER']
|
||||
# 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 referer referrer
|
||||
alias_method :query_parameters, :GET
|
||||
|
||||
|
||||
def query_parameters
|
||||
@query_parameters ||= self.class.parse_query_parameters(query_string)
|
||||
# Override Rack's POST method to support nested query strings
|
||||
def POST
|
||||
@env["action_controller.request.request_parameters"] ||= UrlEncodedPairParser.parse_hash_parameters(super)
|
||||
end
|
||||
|
||||
def request_parameters
|
||||
@request_parameters ||= parse_formatted_request_parameters
|
||||
end
|
||||
|
||||
|
||||
#--
|
||||
# Must be implemented in the concrete request
|
||||
#++
|
||||
alias_method :request_parameters, :POST
|
||||
|
||||
def body_stream #:nodoc:
|
||||
@env['rack.input']
|
||||
end
|
||||
|
||||
def cookies #:nodoc:
|
||||
end
|
||||
|
||||
def session #:nodoc:
|
||||
def session
|
||||
@env['rack.session'] ||= {}
|
||||
end
|
||||
|
||||
def session=(session) #:nodoc:
|
||||
@session = session
|
||||
@env['rack.session'] = session
|
||||
end
|
||||
|
||||
def reset_session #:nodoc:
|
||||
def reset_session
|
||||
@env['rack.session'] = {}
|
||||
end
|
||||
|
||||
protected
|
||||
# The raw content type string. Use when you need parameters such as
|
||||
# charset or boundary which aren't included in the content_type MIME type.
|
||||
# Overridden by the X-POST_DATA_FORMAT header for backward compatibility.
|
||||
def content_type_with_parameters
|
||||
content_type_from_legacy_post_data_format_header ||
|
||||
env['CONTENT_TYPE'].to_s
|
||||
end
|
||||
def session_options
|
||||
@env['rack.session.options'] ||= {}
|
||||
end
|
||||
|
||||
# The raw content type string with its parameters stripped off.
|
||||
def content_type_without_parameters
|
||||
self.class.extract_content_type_without_parameters(content_type_with_parameters)
|
||||
end
|
||||
memoize :content_type_without_parameters
|
||||
def session_options=(options)
|
||||
@env['rack.session.options'] = options
|
||||
end
|
||||
|
||||
def server_port
|
||||
@env['SERVER_PORT'].to_i
|
||||
end
|
||||
|
||||
private
|
||||
def content_type_from_legacy_post_data_format_header
|
||||
if x_post_format = @env['HTTP_X_POST_DATA_FORMAT']
|
||||
case x_post_format.to_s.downcase
|
||||
when 'yaml'; 'application/x-yaml'
|
||||
when 'xml'; 'application/xml'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_formatted_request_parameters
|
||||
return {} if content_length.zero?
|
||||
|
||||
content_type, boundary = self.class.extract_multipart_boundary(content_type_with_parameters)
|
||||
|
||||
# Don't parse params for unknown requests.
|
||||
return {} if content_type.blank?
|
||||
|
||||
mime_type = Mime::Type.lookup(content_type)
|
||||
strategy = ActionController::Base.param_parsers[mime_type]
|
||||
|
||||
# Only multipart form parsing expects a stream.
|
||||
body = (strategy && strategy != :multipart_form) ? raw_post : self.body
|
||||
|
||||
case strategy
|
||||
when Proc
|
||||
strategy.call(body)
|
||||
when :url_encoded_form
|
||||
self.class.clean_up_ajax_request_body! body
|
||||
self.class.parse_query_parameters(body)
|
||||
when :multipart_form
|
||||
self.class.parse_multipart_form_parameters(body, boundary, content_length, env)
|
||||
when :xml_simple, :xml_node
|
||||
body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
|
||||
when :yaml
|
||||
YAML.load(body)
|
||||
when :json
|
||||
if body.blank?
|
||||
{}
|
||||
else
|
||||
data = ActiveSupport::JSON.decode(body)
|
||||
data = {:_json => data} unless data.is_a?(Hash)
|
||||
data.with_indifferent_access
|
||||
end
|
||||
else
|
||||
{}
|
||||
end
|
||||
rescue Exception => e # YAML, XML or Ruby code block errors
|
||||
raise
|
||||
{ "body" => body,
|
||||
"content_type" => content_type_with_parameters,
|
||||
"content_length" => content_length,
|
||||
"exception" => "#{e.message} (#{e.class})",
|
||||
"backtrace" => e.backtrace }
|
||||
end
|
||||
|
||||
def named_host?(host)
|
||||
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
UrlEncodedPairParser.new(pairs).result
|
||||
end
|
||||
|
||||
def parse_request_parameters(params)
|
||||
parser = UrlEncodedPairParser.new
|
||||
|
||||
params = params.dup
|
||||
until params.empty?
|
||||
for key, value in params
|
||||
if key.blank?
|
||||
params.delete key
|
||||
elsif !key.include?('[')
|
||||
# much faster to test for the most common case first (GET)
|
||||
# and avoid the call to build_deep_hash
|
||||
parser.result[key] = get_typed_value(value[0])
|
||||
params.delete key
|
||||
elsif value.is_a?(Array)
|
||||
parser.parse(key, get_typed_value(value.shift))
|
||||
params.delete key if value.empty?
|
||||
else
|
||||
raise TypeError, "Expected array, found #{value.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
parser.result
|
||||
end
|
||||
|
||||
def parse_multipart_form_parameters(body, boundary, body_size, env)
|
||||
parse_request_parameters(read_multipart(body, boundary, body_size, env))
|
||||
end
|
||||
|
||||
def extract_multipart_boundary(content_type_with_parameters)
|
||||
if content_type_with_parameters =~ MULTIPART_BOUNDARY
|
||||
['multipart/form-data', $1.dup]
|
||||
else
|
||||
extract_content_type_without_parameters(content_type_with_parameters)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_content_type_without_parameters(content_type_with_parameters)
|
||||
$1.strip.downcase if content_type_with_parameters =~ /^([^,\;]*)/
|
||||
end
|
||||
|
||||
def clean_up_ajax_request_body!(body)
|
||||
body.chop! if body[-1] == 0
|
||||
body.gsub!(/&_=$/, '')
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def get_typed_value(value)
|
||||
case value
|
||||
when String
|
||||
value
|
||||
when NilClass
|
||||
''
|
||||
when Array
|
||||
value.map { |v| get_typed_value(v) }
|
||||
else
|
||||
if value.respond_to? :original_filename
|
||||
# Uploaded file
|
||||
if value.original_filename
|
||||
value
|
||||
# Multipart param
|
||||
else
|
||||
result = value.read
|
||||
value.rewind
|
||||
result
|
||||
end
|
||||
# Unknown value, neither string nor multipart.
|
||||
else
|
||||
raise "Unknown form value: #{value.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
|
||||
|
||||
EOL = "\015\012"
|
||||
|
||||
def read_multipart(body, boundary, body_size, env)
|
||||
params = Hash.new([])
|
||||
boundary = "--" + boundary
|
||||
quoted_boundary = Regexp.quote(boundary)
|
||||
buf = ""
|
||||
bufsize = 10 * 1024
|
||||
boundary_end=""
|
||||
|
||||
# start multipart/form-data
|
||||
body.binmode if defined? body.binmode
|
||||
case body
|
||||
when File
|
||||
body.set_encoding(Encoding::BINARY) if body.respond_to?(:set_encoding)
|
||||
when StringIO
|
||||
body.string.force_encoding(Encoding::BINARY) if body.string.respond_to?(:force_encoding)
|
||||
end
|
||||
boundary_size = boundary.size + EOL.size
|
||||
body_size -= boundary_size
|
||||
status = body.read(boundary_size)
|
||||
if nil == status
|
||||
raise EOFError, "no content body"
|
||||
elsif boundary + EOL != status
|
||||
raise EOFError, "bad content body"
|
||||
end
|
||||
|
||||
loop do
|
||||
head = nil
|
||||
content =
|
||||
if 10240 < body_size
|
||||
UploadedTempfile.new("CGI")
|
||||
else
|
||||
UploadedStringIO.new
|
||||
end
|
||||
content.binmode if defined? content.binmode
|
||||
|
||||
until head and /#{quoted_boundary}(?:#{EOL}|--)/n.match(buf)
|
||||
|
||||
if (not head) and /#{EOL}#{EOL}/n.match(buf)
|
||||
buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
|
||||
head = $1.dup
|
||||
""
|
||||
end
|
||||
next
|
||||
end
|
||||
|
||||
if head and ( (EOL + boundary + EOL).size < buf.size )
|
||||
content.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
|
||||
buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
|
||||
end
|
||||
|
||||
c = if bufsize < body_size
|
||||
body.read(bufsize)
|
||||
else
|
||||
body.read(body_size)
|
||||
end
|
||||
if c.nil? || c.empty?
|
||||
raise EOFError, "bad content body"
|
||||
end
|
||||
buf.concat(c)
|
||||
body_size -= c.size
|
||||
end
|
||||
|
||||
buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do
|
||||
content.print $1
|
||||
if "--" == $2
|
||||
body_size = -1
|
||||
end
|
||||
boundary_end = $2.dup
|
||||
""
|
||||
end
|
||||
|
||||
content.rewind
|
||||
|
||||
head =~ /Content-Disposition:.* filename=(?:"((?:\\.|[^\"])*)"|([^;]*))/ni
|
||||
if filename = $1 || $2
|
||||
if /Mac/ni.match(env['HTTP_USER_AGENT']) and
|
||||
/Mozilla/ni.match(env['HTTP_USER_AGENT']) and
|
||||
(not /MSIE/ni.match(env['HTTP_USER_AGENT']))
|
||||
filename = CGI.unescape(filename)
|
||||
end
|
||||
content.original_path = filename.dup
|
||||
end
|
||||
|
||||
head =~ /Content-Type: ([^\r]*)/ni
|
||||
content.content_type = $1.dup if $1
|
||||
|
||||
head =~ /Content-Disposition:.* name="?([^\";]*)"?/ni
|
||||
name = $1.dup if $1
|
||||
|
||||
if params.has_key?(name)
|
||||
params[name].push(content)
|
||||
else
|
||||
params[name] = [content]
|
||||
end
|
||||
break if body_size == -1
|
||||
end
|
||||
raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
|
||||
|
||||
begin
|
||||
body.rewind if body.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
|
||||
|
||||
params
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class UrlEncodedPairParser < StringScanner #:nodoc:
|
||||
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
|
||||
push top.last
|
||||
value = top[key]
|
||||
end
|
||||
else
|
||||
top << 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
|
||||
|
||||
return value
|
||||
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
|
||||
|
||||
module UploadedFile
|
||||
def self.included(base)
|
||||
base.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
|
||||
|
|
|
@ -5,8 +5,6 @@ module ActionController #:nodoc:
|
|||
module RequestForgeryProtection
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
class_inheritable_accessor :request_forgery_protection_options
|
||||
self.request_forgery_protection_options = {}
|
||||
helper_method :form_authenticity_token
|
||||
helper_method :protect_against_forgery?
|
||||
end
|
||||
|
@ -14,7 +12,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
# Protecting controller actions from CSRF attacks by ensuring that all forms are coming from the current web application, not a
|
||||
# forged link from another site, is done by embedding a token based on the session (which an attacker wouldn't know) in all
|
||||
# forged link from another site, is done by embedding a token based on a random string stored in the session (which an attacker wouldn't know) in all
|
||||
# forms and Ajax requests generated by Rails and then verifying the authenticity of that token in the controller. Only
|
||||
# HTML/JavaScript requests are checked, so this will not protect your XML API (presumably you'll have a different authentication
|
||||
# scheme there anyway). Also, GET requests are not protected as these should be idempotent anyway.
|
||||
|
@ -57,12 +55,8 @@ module ActionController #:nodoc:
|
|||
# Example:
|
||||
#
|
||||
# class FooController < ApplicationController
|
||||
# # uses the cookie session store (then you don't need a separate :secret)
|
||||
# protect_from_forgery :except => :index
|
||||
#
|
||||
# # uses one of the other session stores that uses a session_id value.
|
||||
# protect_from_forgery :secret => 'my-little-pony', :except => :index
|
||||
#
|
||||
# # you can disable csrf protection on controller-by-controller basis:
|
||||
# skip_before_filter :verify_authenticity_token
|
||||
# end
|
||||
|
@ -70,13 +64,12 @@ module ActionController #:nodoc:
|
|||
# Valid Options:
|
||||
#
|
||||
# * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified.
|
||||
# * <tt>:secret</tt> - Custom salt used to generate the <tt>form_authenticity_token</tt>.
|
||||
# Leave this off if you are using the cookie session store.
|
||||
# * <tt>:digest</tt> - Message digest used for hashing. Defaults to 'SHA1'.
|
||||
def protect_from_forgery(options = {})
|
||||
self.request_forgery_protection_token ||= :authenticity_token
|
||||
before_filter :verify_authenticity_token, :only => options.delete(:only), :except => options.delete(:except)
|
||||
request_forgery_protection_options.update(options)
|
||||
if options[:secret] || options[:digest]
|
||||
ActiveSupport::Deprecation.warn("protect_from_forgery only takes :only and :except options now. :digest and :secret have no effect", caller)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -90,7 +83,7 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# * is the format restricted? By default, only HTML and AJAX requests are checked.
|
||||
# * is it a GET request? Gets should be safe and idempotent
|
||||
# * Does the form_authenticity_token match the given _token value from the params?
|
||||
# * Does the form_authenticity_token match the given token value from the params?
|
||||
def verified_request?
|
||||
!protect_against_forgery? ||
|
||||
request.method == :get ||
|
||||
|
@ -105,34 +98,9 @@ module ActionController #:nodoc:
|
|||
# Sets the token value for the current session. Pass a <tt>:secret</tt> option
|
||||
# in +protect_from_forgery+ to add a custom salt to the hash.
|
||||
def form_authenticity_token
|
||||
@form_authenticity_token ||= if !session.respond_to?(:session_id)
|
||||
raise InvalidAuthenticityToken, "Request Forgery Protection requires a valid session. Use #allow_forgery_protection to disable it, or use a valid session."
|
||||
elsif request_forgery_protection_options[:secret]
|
||||
authenticity_token_from_session_id
|
||||
elsif session.respond_to?(:dbman) && session.dbman.respond_to?(:generate_digest)
|
||||
authenticity_token_from_cookie_session
|
||||
else
|
||||
raise InvalidAuthenticityToken, "No :secret given to the #protect_from_forgery call. Set that or use a session store capable of generating its own keys (Cookie Session Store)."
|
||||
end
|
||||
session[:_csrf_token] ||= ActiveSupport::SecureRandom.base64(32)
|
||||
end
|
||||
|
||||
# Generates a unique digest using the session_id and the CSRF secret.
|
||||
def authenticity_token_from_session_id
|
||||
key = if request_forgery_protection_options[:secret].respond_to?(:call)
|
||||
request_forgery_protection_options[:secret].call(@session)
|
||||
else
|
||||
request_forgery_protection_options[:secret]
|
||||
end
|
||||
digest = request_forgery_protection_options[:digest] ||= 'SHA1'
|
||||
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(digest), key.to_s, session.session_id.to_s)
|
||||
end
|
||||
|
||||
# No secret was given, so assume this is a cookie session store.
|
||||
def authenticity_token_from_cookie_session
|
||||
session[:csrf_id] ||= CGI::Session.generate_unique_id
|
||||
session.dbman.generate_digest(session[:csrf_id])
|
||||
end
|
||||
|
||||
|
||||
def protect_against_forgery?
|
||||
allow_forgery_protection && request_forgery_protection_token
|
||||
end
|
||||
|
|
|
@ -1,169 +0,0 @@
|
|||
require 'optparse'
|
||||
require 'action_controller/integration'
|
||||
|
||||
module ActionController
|
||||
class RequestProfiler
|
||||
# Wrap up the integration session runner.
|
||||
class Sandbox
|
||||
include Integration::Runner
|
||||
|
||||
def self.benchmark(n, script)
|
||||
new(script).benchmark(n)
|
||||
end
|
||||
|
||||
def initialize(script_path)
|
||||
@quiet = false
|
||||
define_run_method(script_path)
|
||||
reset!
|
||||
end
|
||||
|
||||
def benchmark(n, profiling = false)
|
||||
@quiet = true
|
||||
print ' '
|
||||
|
||||
result = Benchmark.realtime do
|
||||
n.times do |i|
|
||||
run(profiling)
|
||||
print_progress(i)
|
||||
end
|
||||
end
|
||||
|
||||
puts
|
||||
result
|
||||
ensure
|
||||
@quiet = false
|
||||
end
|
||||
|
||||
def say(message)
|
||||
puts " #{message}" unless @quiet
|
||||
end
|
||||
|
||||
private
|
||||
def define_run_method(script_path)
|
||||
script = File.read(script_path)
|
||||
|
||||
source = <<-end_source
|
||||
def run(profiling = false)
|
||||
if profiling
|
||||
RubyProf.resume do
|
||||
#{script}
|
||||
end
|
||||
else
|
||||
#{script}
|
||||
end
|
||||
|
||||
old_request_count = request_count
|
||||
reset!
|
||||
self.request_count = old_request_count
|
||||
end
|
||||
end_source
|
||||
|
||||
instance_eval source, script_path, 1
|
||||
end
|
||||
|
||||
def print_progress(i)
|
||||
print "\n " if i % 60 == 0
|
||||
print ' ' if i % 10 == 0
|
||||
print '.'
|
||||
$stdout.flush
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize(options = {})
|
||||
@options = default_options.merge(options)
|
||||
end
|
||||
|
||||
|
||||
def self.run(args = nil, options = {})
|
||||
profiler = new(options)
|
||||
profiler.parse_options(args) if args
|
||||
profiler.run
|
||||
end
|
||||
|
||||
def run
|
||||
sandbox = Sandbox.new(options[:script])
|
||||
|
||||
puts 'Warming up once'
|
||||
|
||||
elapsed = warmup(sandbox)
|
||||
puts '%.2f sec, %d requests, %d req/sec' % [elapsed, sandbox.request_count, sandbox.request_count / elapsed]
|
||||
puts "\n#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x"
|
||||
|
||||
options[:benchmark] ? benchmark(sandbox) : profile(sandbox)
|
||||
end
|
||||
|
||||
def profile(sandbox)
|
||||
load_ruby_prof
|
||||
|
||||
benchmark(sandbox, true)
|
||||
results = RubyProf.stop
|
||||
|
||||
show_profile_results results
|
||||
results
|
||||
end
|
||||
|
||||
def benchmark(sandbox, profiling = false)
|
||||
sandbox.request_count = 0
|
||||
elapsed = sandbox.benchmark(options[:n], profiling).to_f
|
||||
count = sandbox.request_count.to_i
|
||||
puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed]
|
||||
end
|
||||
|
||||
def warmup(sandbox)
|
||||
Benchmark.realtime { sandbox.run(false) }
|
||||
end
|
||||
|
||||
def default_options
|
||||
{ :n => 100, :open => 'open %s &' }
|
||||
end
|
||||
|
||||
# Parse command-line options
|
||||
def parse_options(args)
|
||||
OptionParser.new do |opt|
|
||||
opt.banner = "USAGE: #{$0} [options] [session script path]"
|
||||
|
||||
opt.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v| options[:n] = v.to_i if v }
|
||||
opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| options[:benchmark] = v }
|
||||
opt.on('-m', '--measure [mode]', 'Which ruby-prof measure mode to use: process_time, wall_time, cpu_time, allocations, or memory. Defaults to process_time.') { |v| options[:measure] = v }
|
||||
opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| options[:open] = v }
|
||||
opt.on('-h', '--help', 'Show this help') { puts opt; exit }
|
||||
|
||||
opt.parse args
|
||||
|
||||
if args.empty?
|
||||
puts opt
|
||||
exit
|
||||
end
|
||||
options[:script] = args.pop
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def load_ruby_prof
|
||||
begin
|
||||
gem 'ruby-prof', '>= 0.6.1'
|
||||
require 'ruby-prof'
|
||||
if mode = options[:measure]
|
||||
RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
|
||||
end
|
||||
rescue LoadError
|
||||
abort '`gem install ruby-prof` to use the profiler'
|
||||
end
|
||||
end
|
||||
|
||||
def show_profile_results(results)
|
||||
File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
|
||||
RubyProf::GraphHtmlPrinter.new(results).print(file)
|
||||
`#{options[:open] % file.path}` if options[:open]
|
||||
end
|
||||
|
||||
File.open "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file|
|
||||
RubyProf::FlatPrinter.new(results).print(file)
|
||||
`#{options[:open] % file.path}` if options[:open]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,13 +1,19 @@
|
|||
module ActionController #:nodoc:
|
||||
# Actions that fail to perform as expected throw exceptions. These exceptions can either be rescued for the public view
|
||||
# (with a nice user-friendly explanation) or for the developers view (with tons of debugging information). The developers view
|
||||
# is already implemented by the Action Controller, but the public view should be tailored to your specific application.
|
||||
#
|
||||
# The default behavior for public exceptions is to render a static html file with the name of the error code thrown. If no such
|
||||
# file exists, an empty response is sent with the correct status code.
|
||||
# Actions that fail to perform as expected throw exceptions. These
|
||||
# exceptions can either be rescued for the public view (with a nice
|
||||
# user-friendly explanation) or for the developers view (with tons of
|
||||
# debugging information). The developers view is already implemented by
|
||||
# the Action Controller, but the public view should be tailored to your
|
||||
# specific application.
|
||||
#
|
||||
# You can override what constitutes a local request by overriding the <tt>local_request?</tt> method in your own controller.
|
||||
# Custom rescue behavior is achieved by overriding the <tt>rescue_action_in_public</tt> and <tt>rescue_action_locally</tt> methods.
|
||||
# The default behavior for public exceptions is to render a static html
|
||||
# file with the name of the error code thrown. If no such file exists, an
|
||||
# empty response is sent with the correct status code.
|
||||
#
|
||||
# You can override what constitutes a local request by overriding the
|
||||
# <tt>local_request?</tt> method in your own controller. Custom rescue
|
||||
# behavior is achieved by overriding the <tt>rescue_action_in_public</tt>
|
||||
# and <tt>rescue_action_locally</tt> methods.
|
||||
module Rescue
|
||||
LOCALHOST = '127.0.0.1'.freeze
|
||||
|
||||
|
@ -32,6 +38,9 @@ module ActionController #:nodoc:
|
|||
'ActionView::TemplateError' => 'template_error'
|
||||
}
|
||||
|
||||
RESCUES_TEMPLATE_PATH = ActionView::Template::EagerPath.new(
|
||||
File.join(File.dirname(__FILE__), "templates"))
|
||||
|
||||
def self.included(base) #:nodoc:
|
||||
base.cattr_accessor :rescue_responses
|
||||
base.rescue_responses = Hash.new(DEFAULT_RESCUE_RESPONSE)
|
||||
|
@ -50,47 +59,60 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
module ClassMethods
|
||||
def process_with_exception(request, response, exception) #:nodoc:
|
||||
def call_with_exception(env, exception) #:nodoc:
|
||||
request = env["action_controller.rescue.request"] ||= Request.new(env)
|
||||
response = env["action_controller.rescue.response"] ||= Response.new
|
||||
new.process(request, response, :rescue_action, exception)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
# Exception handler called when the performance of an action raises an exception.
|
||||
# Exception handler called when the performance of an action raises
|
||||
# an exception.
|
||||
def rescue_action(exception)
|
||||
rescue_with_handler(exception) || rescue_action_without_handler(exception)
|
||||
rescue_with_handler(exception) ||
|
||||
rescue_action_without_handler(exception)
|
||||
end
|
||||
|
||||
# Overwrite to implement custom logging of errors. By default logs as fatal.
|
||||
# Overwrite to implement custom logging of errors. By default
|
||||
# logs as fatal.
|
||||
def log_error(exception) #:doc:
|
||||
ActiveSupport::Deprecation.silence do
|
||||
if ActionView::TemplateError === exception
|
||||
logger.fatal(exception.to_s)
|
||||
else
|
||||
logger.fatal(
|
||||
"\n\n#{exception.class} (#{exception.message}):\n " +
|
||||
clean_backtrace(exception).join("\n ") +
|
||||
"\n\n"
|
||||
"\n#{exception.class} (#{exception.message}):\n " +
|
||||
clean_backtrace(exception).join("\n ") + "\n\n"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>). By
|
||||
# default will call render_optional_error_file. Override this method to provide more user friendly error messages.
|
||||
# Overwrite to implement public exception handling (for requests
|
||||
# answering false to <tt>local_request?</tt>). By default will call
|
||||
# render_optional_error_file. Override this method to provide more
|
||||
# user friendly error messages.
|
||||
def rescue_action_in_public(exception) #:doc:
|
||||
render_optional_error_file response_code_for_rescue(exception)
|
||||
end
|
||||
|
||||
# Attempts to render a static error page based on the <tt>status_code</tt> thrown,
|
||||
# or just return headers if no such file exists. For example, if a 500 error is
|
||||
# being handled Rails will first attempt to render the file at <tt>public/500.html</tt>.
|
||||
# If the file doesn't exist, the body of the response will be left empty.
|
||||
|
||||
# Attempts to render a static error page based on the
|
||||
# <tt>status_code</tt> thrown, or just return headers if no such file
|
||||
# exists. At first, it will try to render a localized static page.
|
||||
# For example, if a 500 error is being handled Rails and locale is :da,
|
||||
# it will first attempt to render the file at <tt>public/500.da.html</tt>
|
||||
# then attempt to render <tt>public/500.html</tt>. If none of them exist,
|
||||
# the body of the response will be left empty.
|
||||
def render_optional_error_file(status_code)
|
||||
status = interpret_status(status_code)
|
||||
locale_path = "#{Rails.public_path}/#{status[0,3]}.#{I18n.locale}.html" if I18n.locale
|
||||
path = "#{Rails.public_path}/#{status[0,3]}.html"
|
||||
if File.exist?(path)
|
||||
render :file => path, :status => status
|
||||
|
||||
if locale_path && File.exist?(locale_path)
|
||||
render :file => locale_path, :status => status, :content_type => Mime::HTML
|
||||
elsif File.exist?(path)
|
||||
render :file => path, :status => status, :content_type => Mime::HTML
|
||||
else
|
||||
head status
|
||||
end
|
||||
|
@ -107,11 +129,13 @@ module ActionController #:nodoc:
|
|||
# a controller action.
|
||||
def rescue_action_locally(exception)
|
||||
@template.instance_variable_set("@exception", exception)
|
||||
@template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub")))
|
||||
@template.instance_variable_set("@contents", @template.render(:file => template_path_for_local_rescue(exception)))
|
||||
@template.instance_variable_set("@rescues_path", RESCUES_TEMPLATE_PATH)
|
||||
@template.instance_variable_set("@contents",
|
||||
@template.render(:file => template_path_for_local_rescue(exception)))
|
||||
|
||||
response.content_type = Mime::HTML
|
||||
render_for_file(rescues_path("layout"), response_code_for_rescue(exception))
|
||||
render_for_file(rescues_path("layout"),
|
||||
response_code_for_rescue(exception))
|
||||
end
|
||||
|
||||
def rescue_action_without_handler(exception)
|
||||
|
@ -139,7 +163,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def rescues_path(template_name)
|
||||
"#{File.dirname(__FILE__)}/templates/rescues/#{template_name}.erb"
|
||||
RESCUES_TEMPLATE_PATH["rescues/#{template_name}.erb"]
|
||||
end
|
||||
|
||||
def template_path_for_local_rescue(exception)
|
||||
|
@ -151,13 +175,9 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def clean_backtrace(exception)
|
||||
if backtrace = exception.backtrace
|
||||
if defined?(RAILS_ROOT)
|
||||
backtrace.map { |line| line.sub RAILS_ROOT, '' }
|
||||
else
|
||||
backtrace
|
||||
end
|
||||
end
|
||||
defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) ?
|
||||
Rails.backtrace_cleaner.clean(exception.backtrace) :
|
||||
exception.backtrace
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,7 +42,7 @@ module ActionController
|
|||
#
|
||||
# Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
|
||||
module Resources
|
||||
INHERITABLE_OPTIONS = :namespace, :shallow, :actions
|
||||
INHERITABLE_OPTIONS = :namespace, :shallow
|
||||
|
||||
class Resource #:nodoc:
|
||||
DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
|
||||
|
@ -119,7 +119,7 @@ module ActionController
|
|||
end
|
||||
|
||||
def has_action?(action)
|
||||
!DEFAULT_ACTIONS.include?(action) || @options[:actions].nil? || @options[:actions].include?(action)
|
||||
!DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -135,22 +135,27 @@ module ActionController
|
|||
end
|
||||
|
||||
def set_allowed_actions
|
||||
only = @options.delete(:only)
|
||||
except = @options.delete(:except)
|
||||
only, except = @options.values_at(:only, :except)
|
||||
@allowed_actions ||= {}
|
||||
|
||||
if only && except
|
||||
raise ArgumentError, 'Please supply either :only or :except, not both.'
|
||||
elsif only == :all || except == :none
|
||||
options[:actions] = DEFAULT_ACTIONS
|
||||
if only == :all || except == :none
|
||||
only = nil
|
||||
except = []
|
||||
elsif only == :none || except == :all
|
||||
options[:actions] = []
|
||||
elsif only
|
||||
options[:actions] = DEFAULT_ACTIONS & Array(only).map(&:to_sym)
|
||||
elsif except
|
||||
options[:actions] = DEFAULT_ACTIONS - Array(except).map(&:to_sym)
|
||||
else
|
||||
# leave options[:actions] alone
|
||||
only = []
|
||||
except = nil
|
||||
end
|
||||
|
||||
if only
|
||||
@allowed_actions[:only] = Array(only).map(&:to_sym)
|
||||
elsif except
|
||||
@allowed_actions[:except] = Array(except).map(&:to_sym)
|
||||
end
|
||||
end
|
||||
|
||||
def action_allowed?(action)
|
||||
only, except = @allowed_actions.values_at(:only, :except)
|
||||
(!only || only.include?(action)) && (!except || !except.include?(action))
|
||||
end
|
||||
|
||||
def set_prefixes
|
||||
|
@ -283,7 +288,12 @@ module ActionController
|
|||
# * <tt>:new</tt> - Same as <tt>:collection</tt>, but for actions that operate on the new \resource action.
|
||||
# * <tt>:controller</tt> - Specify the controller name for the routes.
|
||||
# * <tt>:singular</tt> - Specify the singular name used in the member routes.
|
||||
# * <tt>:requirements</tt> - Set custom routing parameter requirements.
|
||||
# * <tt>:requirements</tt> - Set custom routing parameter requirements; this is a hash of either
|
||||
# regular expressions (which must match for the route to match) or extra parameters. For example:
|
||||
#
|
||||
# map.resource :profile, :path_prefix => ':name', :requirements => { :name => /[a-zA-Z]+/, :extra => 'value' }
|
||||
#
|
||||
# will only match if the first part is alphabetic, and will pass the parameter :extra to the controller.
|
||||
# * <tt>:conditions</tt> - Specify custom routing recognition conditions. \Resources sets the <tt>:method</tt> value for the method-specific routes.
|
||||
# * <tt>:as</tt> - Specify a different \resource name to use in the URL path. For example:
|
||||
# # products_path == '/productos'
|
||||
|
@ -398,8 +408,6 @@ module ActionController
|
|||
# # --> POST /posts/1/comments (maps to the CommentsController#create action)
|
||||
# # --> PUT /posts/1/comments/1 (fails)
|
||||
#
|
||||
# The <tt>:only</tt> and <tt>:except</tt> options are inherited by any nested resource(s).
|
||||
#
|
||||
# If <tt>map.resources</tt> is called with multiple resources, they all get the same options applied.
|
||||
#
|
||||
# Examples:
|
||||
|
@ -535,9 +543,9 @@ module ActionController
|
|||
|
||||
with_options :controller => resource.controller do |map|
|
||||
map_collection_actions(map, resource)
|
||||
map_default_singleton_actions(map, resource)
|
||||
map_new_actions(map, resource)
|
||||
map_member_actions(map, resource)
|
||||
map_default_singleton_actions(map, resource)
|
||||
|
||||
map_associations(resource, options)
|
||||
|
||||
|
@ -639,10 +647,8 @@ module ActionController
|
|||
formatted_route_path = "#{route_path}.:format"
|
||||
|
||||
if route_name && @set.named_routes[route_name.to_sym].nil?
|
||||
map.named_route(route_name, route_path, action_options)
|
||||
map.named_route("formatted_#{route_name}", formatted_route_path, action_options)
|
||||
map.named_route(route_name, formatted_route_path, action_options)
|
||||
else
|
||||
map.connect(route_path, action_options)
|
||||
map.connect(formatted_route_path, action_options)
|
||||
end
|
||||
end
|
||||
|
@ -669,7 +675,3 @@ module ActionController
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ActionController::Routing::RouteSet::Mapper
|
||||
include ActionController::Resources
|
||||
end
|
||||
|
|
|
@ -1,24 +1,25 @@
|
|||
require 'digest/md5'
|
||||
|
||||
module ActionController # :nodoc:
|
||||
# Represents an HTTP response generated by a controller action. One can use an
|
||||
# ActionController::AbstractResponse object to retrieve the current state of the
|
||||
# response, or customize the response. An AbstractResponse 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.
|
||||
# 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.
|
||||
#
|
||||
# AbstractResponse 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
|
||||
# AbstractResponse#headers.
|
||||
# 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 AbstractResponse 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 AbstractResponse).
|
||||
# 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:
|
||||
|
@ -29,25 +30,25 @@ module ActionController # :nodoc:
|
|||
# puts @response.body
|
||||
# end
|
||||
# end
|
||||
class AbstractResponse
|
||||
class Response < Rack::Response
|
||||
DEFAULT_HEADERS = { "Cache-Control" => "no-cache" }
|
||||
attr_accessor :request
|
||||
|
||||
# The body content (e.g. HTML) of the response, as a String.
|
||||
attr_accessor :body
|
||||
# The headers of the response, as a Hash. It maps header names to header values.
|
||||
attr_accessor :headers
|
||||
attr_accessor :session, :cookies, :assigns, :template, :layout
|
||||
attr_accessor :session, :assigns, :template, :layout
|
||||
attr_accessor :redirected_to, :redirected_to_method_params
|
||||
|
||||
delegate :default_charset, :to => 'ActionController::Base'
|
||||
|
||||
def initialize
|
||||
@body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
|
||||
end
|
||||
@status = 200
|
||||
@header = DEFAULT_HEADERS.dup
|
||||
|
||||
def status; headers['Status'] end
|
||||
def status=(status) headers['Status'] = status end
|
||||
@writer = lambda { |x| @body << x }
|
||||
@block = nil
|
||||
|
||||
@body = "",
|
||||
@session, @assigns = [], []
|
||||
end
|
||||
|
||||
def location; headers['Location'] end
|
||||
def location=(url) headers['Location'] = url end
|
||||
|
@ -109,13 +110,17 @@ module ActionController # :nodoc:
|
|||
def etag
|
||||
headers['ETag']
|
||||
end
|
||||
|
||||
|
||||
def etag?
|
||||
headers.include?('ETag')
|
||||
end
|
||||
|
||||
|
||||
def etag=(etag)
|
||||
headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(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)
|
||||
|
@ -138,26 +143,77 @@ module ActionController # :nodoc:
|
|||
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
|
||||
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
|
||||
if request && request.etag_matches?(etag)
|
||||
self.status = '304 Not Modified'
|
||||
self.body = ''
|
||||
end
|
||||
|
||||
set_conditional_cache_control!
|
||||
end
|
||||
set_conditional_cache_control!
|
||||
end
|
||||
end
|
||||
|
||||
def nonempty_ok_response?
|
||||
ok = !status || status[0..2] == '200'
|
||||
ok = !status || status.to_s[0..2] == '200'
|
||||
ok && body.is_a?(String) && !body.empty?
|
||||
end
|
||||
|
||||
|
@ -168,23 +224,32 @@ module ActionController # :nodoc:
|
|||
end
|
||||
|
||||
def convert_content_type!
|
||||
if content_type = headers.delete("Content-Type")
|
||||
self.headers["type"] = content_type
|
||||
end
|
||||
if content_type = headers.delete("Content-type")
|
||||
self.headers["type"] = content_type
|
||||
end
|
||||
if content_type = headers.delete("content-type")
|
||||
self.headers["type"] = 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
|
||||
|
||||
# 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!
|
||||
unless body.respond_to?(:call) || (status && status[0..2] == '304')
|
||||
self.headers["Content-Length"] ||= body.size
|
||||
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
|
||||
|
|
28
vendor/rails/actionpack/lib/action_controller/rewindable_input.rb
vendored
Normal file
28
vendor/rails/actionpack/lib/action_controller/rewindable_input.rb
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
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
|
|
@ -1,6 +1,5 @@
|
|||
require 'cgi'
|
||||
require 'uri'
|
||||
require 'action_controller/polymorphic_routes'
|
||||
require 'action_controller/routing/optimisations'
|
||||
require 'action_controller/routing/routing_ext'
|
||||
require 'action_controller/routing/route'
|
||||
|
@ -84,9 +83,11 @@ module ActionController
|
|||
# This sets up +blog+ as the default controller if no other is specified.
|
||||
# This means visiting '/' would invoke the blog controller.
|
||||
#
|
||||
# More formally, you can define defaults in a route with the <tt>:defaults</tt> key.
|
||||
# More formally, you can include arbitrary parameters in the route, thus:
|
||||
#
|
||||
# map.connect ':controller/:action/:id', :action => 'show', :defaults => { :page => 'Dashboard' }
|
||||
# map.connect ':controller/:action/:id', :action => 'show', :page => 'Dashboard'
|
||||
#
|
||||
# This will pass the :page parameter to all incoming requests that match this route.
|
||||
#
|
||||
# Note: The default routes, as provided by the Rails generator, make all actions in every
|
||||
# controller accessible via GET requests. You should consider removing them or commenting
|
||||
|
@ -192,9 +193,8 @@ module ActionController
|
|||
#
|
||||
# map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'
|
||||
#
|
||||
# will glob all remaining parts of the route that were not recognized earlier. This idiom
|
||||
# must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in
|
||||
# this case.
|
||||
# will glob all remaining parts of the route that were not recognized earlier.
|
||||
# The globbed values are in <tt>params[:path]</tt> as an array of path segments.
|
||||
#
|
||||
# == Route conditions
|
||||
#
|
||||
|
|
|
@ -34,6 +34,8 @@ module ActionController
|
|||
def segment_for(string)
|
||||
segment =
|
||||
case string
|
||||
when /\A\.(:format)?\//
|
||||
OptionalFormatSegment.new
|
||||
when /\A:(\w+)/
|
||||
key = $1.to_sym
|
||||
key == :controller ? ControllerSegment.new(key) : DynamicSegment.new(key)
|
||||
|
|
|
@ -65,7 +65,7 @@ module ActionController
|
|||
# rather than triggering the expensive logic in +url_for+.
|
||||
class PositionalArguments < Optimiser
|
||||
def guard_conditions
|
||||
number_of_arguments = route.segment_keys.size
|
||||
number_of_arguments = route.required_segment_keys.size
|
||||
# if they're using foo_url(:id=>2) it's one
|
||||
# argument, but we don't want to generate /foos/id2
|
||||
if number_of_arguments == 1
|
||||
|
|
|
@ -56,7 +56,7 @@ module ActionController
|
|||
result = recognize_optimized(path, environment) and return result
|
||||
|
||||
# Route was not recognized. Try to find out why (maybe wrong verb).
|
||||
allows = HTTP_METHODS.select { |verb| routes.find { |r| r.recognize(path, :method => verb) } }
|
||||
allows = HTTP_METHODS.select { |verb| routes.find { |r| r.recognize(path, environment.merge(:method => verb)) } }
|
||||
|
||||
if environment[:method] && !HTTP_METHODS.include?(environment[:method])
|
||||
raise NotImplemented.new(*allows)
|
||||
|
|
|
@ -35,6 +35,11 @@ module ActionController
|
|||
segment.key if segment.respond_to? :key
|
||||
end.compact
|
||||
end
|
||||
|
||||
def required_segment_keys
|
||||
required_segments = segments.select {|seg| (!seg.optional? && !seg.is_a?(DividerSegment)) || seg.is_a?(PathSegment) }
|
||||
required_segments.collect { |seg| seg.key if seg.respond_to?(:key)}.compact
|
||||
end
|
||||
|
||||
# Build a query string from the keys of the given hash. If +only_keys+
|
||||
# is given (as an array), only the keys indicated will be used to build
|
||||
|
@ -122,6 +127,16 @@ module ActionController
|
|||
super
|
||||
end
|
||||
|
||||
def generate(options, hash, expire_on = {})
|
||||
path, hash = generate_raw(options, hash, expire_on)
|
||||
append_query_string(path, hash, extra_keys(options))
|
||||
end
|
||||
|
||||
def generate_extras(options, hash, expire_on = {})
|
||||
path, hash = generate_raw(options, hash, expire_on)
|
||||
[path, extra_keys(options)]
|
||||
end
|
||||
|
||||
private
|
||||
def requirement_for(key)
|
||||
return requirements[key] if requirements.key? key
|
||||
|
@ -150,11 +165,6 @@ module ActionController
|
|||
# the query string. (Never use keys from the recalled request when building the
|
||||
# query string.)
|
||||
|
||||
method_decl = "def generate(#{args})\npath, hash = generate_raw(options, hash, expire_on)\nappend_query_string(path, hash, extra_keys(options))\nend"
|
||||
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
|
||||
|
||||
method_decl = "def generate_extras(#{args})\npath, hash = generate_raw(options, hash, expire_on)\n[path, extra_keys(options)]\nend"
|
||||
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
|
||||
raw_method
|
||||
end
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ module ActionController
|
|||
# Mapper instances have relatively few instance methods, in order to avoid
|
||||
# clashes with named routes.
|
||||
class Mapper #:doc:
|
||||
include ActionController::Resources
|
||||
|
||||
def initialize(set) #:nodoc:
|
||||
@set = set
|
||||
end
|
||||
|
@ -136,13 +138,17 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
def named_helper_module_eval(code, *args)
|
||||
@module.module_eval(code, *args)
|
||||
end
|
||||
|
||||
def define_hash_access(route, name, kind, options)
|
||||
selector = hash_access_name(name, kind)
|
||||
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(options = nil)
|
||||
options ? #{options.inspect}.merge(options) : #{options.inspect}
|
||||
end
|
||||
protected :#{selector}
|
||||
named_helper_module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(options = nil) # def hash_for_users_url(options = nil)
|
||||
options ? #{options.inspect}.merge(options) : #{options.inspect} # options ? {:only_path=>false}.merge(options) : {:only_path=>false}
|
||||
end # end
|
||||
protected :#{selector} # protected :hash_for_users_url
|
||||
end_eval
|
||||
helpers << selector
|
||||
end
|
||||
|
@ -166,33 +172,44 @@ module ActionController
|
|||
#
|
||||
# foo_url(bar, baz, bang, :sort_by => 'baz')
|
||||
#
|
||||
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(*args)
|
||||
|
||||
#{generate_optimisation_block(route, kind)}
|
||||
|
||||
opts = if args.empty? || Hash === args.first
|
||||
args.first || {}
|
||||
else
|
||||
options = args.extract_options!
|
||||
args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
|
||||
h[k] = v
|
||||
h
|
||||
end
|
||||
options.merge(args)
|
||||
end
|
||||
|
||||
url_for(#{hash_access_method}(opts))
|
||||
end
|
||||
protected :#{selector}
|
||||
named_helper_module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(*args) # def users_url(*args)
|
||||
#
|
||||
#{generate_optimisation_block(route, kind)} # #{generate_optimisation_block(route, kind)}
|
||||
#
|
||||
opts = if args.empty? || Hash === args.first # opts = if args.empty? || Hash === args.first
|
||||
args.first || {} # args.first || {}
|
||||
else # else
|
||||
options = args.extract_options! # options = args.extract_options!
|
||||
args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)| # args = args.zip([]).inject({}) do |h, (v, k)|
|
||||
h[k] = v # h[k] = v
|
||||
h # h
|
||||
end # end
|
||||
options.merge(args) # options.merge(args)
|
||||
end # end
|
||||
#
|
||||
url_for(#{hash_access_method}(opts)) # url_for(hash_for_users_url(opts))
|
||||
#
|
||||
end # end
|
||||
#Add an alias to support the now deprecated formatted_* URL. # #Add an alias to support the now deprecated formatted_* URL.
|
||||
def formatted_#{selector}(*args) # def formatted_users_url(*args)
|
||||
ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
|
||||
"formatted_#{selector}() has been deprecated. " + # "formatted_users_url() has been deprecated. " +
|
||||
"Please pass format to the standard " + # "Please pass format to the standard " +
|
||||
"#{selector} method instead.", caller) # "users_url method instead.", caller)
|
||||
#{selector}(*args) # users_url(*args)
|
||||
end # end
|
||||
protected :#{selector} # protected :users_url
|
||||
end_eval
|
||||
helpers << selector
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :routes, :named_routes, :configuration_file
|
||||
attr_accessor :routes, :named_routes, :configuration_files
|
||||
|
||||
def initialize
|
||||
self.configuration_files = []
|
||||
|
||||
self.routes = []
|
||||
self.named_routes = NamedRouteCollection.new
|
||||
|
||||
|
@ -206,7 +223,6 @@ module ActionController
|
|||
end
|
||||
|
||||
def draw
|
||||
clear!
|
||||
yield Mapper.new(self)
|
||||
install_helpers
|
||||
end
|
||||
|
@ -230,8 +246,22 @@ module ActionController
|
|||
routes.empty?
|
||||
end
|
||||
|
||||
def add_configuration_file(path)
|
||||
self.configuration_files << path
|
||||
end
|
||||
|
||||
# Deprecated accessor
|
||||
def configuration_file=(path)
|
||||
add_configuration_file(path)
|
||||
end
|
||||
|
||||
# Deprecated accessor
|
||||
def configuration_file
|
||||
configuration_files
|
||||
end
|
||||
|
||||
def load!
|
||||
Routing.use_controllers! nil # Clear the controller cache so we may discover new ones
|
||||
Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones
|
||||
clear!
|
||||
load_routes!
|
||||
end
|
||||
|
@ -240,24 +270,39 @@ module ActionController
|
|||
alias reload! load!
|
||||
|
||||
def reload
|
||||
if @routes_last_modified && configuration_file
|
||||
mtime = File.stat(configuration_file).mtime
|
||||
# if it hasn't been changed, then just return
|
||||
return if mtime == @routes_last_modified
|
||||
# if it has changed then record the new time and fall to the load! below
|
||||
@routes_last_modified = mtime
|
||||
if configuration_files.any? && @routes_last_modified
|
||||
if routes_changed_at == @routes_last_modified
|
||||
return # routes didn't change, don't reload
|
||||
else
|
||||
@routes_last_modified = routes_changed_at
|
||||
end
|
||||
end
|
||||
|
||||
load!
|
||||
end
|
||||
|
||||
def load_routes!
|
||||
if configuration_file
|
||||
load configuration_file
|
||||
@routes_last_modified = File.stat(configuration_file).mtime
|
||||
if configuration_files.any?
|
||||
configuration_files.each { |config| load(config) }
|
||||
@routes_last_modified = routes_changed_at
|
||||
else
|
||||
add_route ":controller/:action/:id"
|
||||
end
|
||||
end
|
||||
|
||||
def routes_changed_at
|
||||
routes_changed_at = nil
|
||||
|
||||
configuration_files.each do |config|
|
||||
config_changed_at = File.stat(config).mtime
|
||||
|
||||
if routes_changed_at.nil? || config_changed_at > routes_changed_at
|
||||
routes_changed_at = config_changed_at
|
||||
end
|
||||
end
|
||||
|
||||
routes_changed_at
|
||||
end
|
||||
|
||||
def add_route(path, options = {})
|
||||
route = builder.build(path, options)
|
||||
|
@ -359,7 +404,7 @@ module ActionController
|
|||
end
|
||||
|
||||
# don't use the recalled keys when determining which routes to check
|
||||
routes = routes_by_controller[controller][action][options.keys.sort_by { |x| x.object_id }]
|
||||
routes = routes_by_controller[controller][action][options.reject {|k,v| !v}.keys.sort_by { |x| x.object_id }]
|
||||
|
||||
routes.each do |route|
|
||||
results = route.__send__(method, options, merged, expire_on)
|
||||
|
@ -382,6 +427,12 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
def call(env)
|
||||
request = Request.new(env)
|
||||
app = Routing::Routes.recognize(request)
|
||||
app.call(env).to_a
|
||||
end
|
||||
|
||||
def recognize(request)
|
||||
params = recognize_path(request.path, extract_request_environment(request))
|
||||
request.path_parameters = params.with_indifferent_access
|
||||
|
|
|
@ -308,5 +308,36 @@ module ActionController
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The OptionalFormatSegment allows for any resource route to have an optional
|
||||
# :format, which decreases the amount of routes created by 50%.
|
||||
class OptionalFormatSegment < DynamicSegment
|
||||
|
||||
def initialize(key = nil, options = {})
|
||||
super(:format, {:optional => true}.merge(options))
|
||||
end
|
||||
|
||||
def interpolation_chunk
|
||||
"." + super
|
||||
end
|
||||
|
||||
def regexp_chunk
|
||||
'(\.[^/?\.]+)?'
|
||||
end
|
||||
|
||||
def to_s
|
||||
'(.:format)?'
|
||||
end
|
||||
|
||||
#the value should not include the period (.)
|
||||
def match_extraction(next_capture)
|
||||
%[
|
||||
if (m = match[#{next_capture}])
|
||||
params[:#{key}] = URI.unescape(m.from(1))
|
||||
end
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
168
vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb
vendored
Normal file
168
vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb
vendored
Normal file
|
@ -0,0 +1,168 @@
|
|||
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
|
|
@ -1,340 +0,0 @@
|
|||
require 'cgi'
|
||||
require 'cgi/session'
|
||||
require 'digest/md5'
|
||||
|
||||
class CGI
|
||||
class Session
|
||||
attr_reader :data
|
||||
|
||||
# Return this session's underlying Session instance. Useful for the DB-backed session stores.
|
||||
def model
|
||||
@dbman.model if @dbman
|
||||
end
|
||||
|
||||
|
||||
# A session store backed by an Active Record class. A default class is
|
||||
# provided, but any object duck-typing to an Active Record Session class
|
||||
# with text +session_id+ and +data+ attributes is sufficient.
|
||||
#
|
||||
# The default assumes a +sessions+ tables with columns:
|
||||
# +id+ (numeric primary key),
|
||||
# +session_id+ (text, or longtext if your session data exceeds 65K), and
|
||||
# +data+ (text or longtext; careful if your session data exceeds 65KB).
|
||||
# The +session_id+ column should always be indexed for speedy lookups.
|
||||
# Session data is marshaled to the +data+ column in Base64 format.
|
||||
# If the data you write is larger than the column's size limit,
|
||||
# ActionController::SessionOverflowError will be raised.
|
||||
#
|
||||
# You may configure the table name, primary key, and data column.
|
||||
# For example, at the end of <tt>config/environment.rb</tt>:
|
||||
# CGI::Session::ActiveRecordStore::Session.table_name = 'legacy_session_table'
|
||||
# CGI::Session::ActiveRecordStore::Session.primary_key = 'session_id'
|
||||
# CGI::Session::ActiveRecordStore::Session.data_column_name = 'legacy_session_data'
|
||||
# Note that setting the primary key to the +session_id+ frees you from
|
||||
# having a separate +id+ column if you don't want it. However, you must
|
||||
# set <tt>session.model.id = session.session_id</tt> by hand! A before filter
|
||||
# on ApplicationController is a good place.
|
||||
#
|
||||
# Since the default class is a simple Active Record, you get timestamps
|
||||
# for free if you add +created_at+ and +updated_at+ datetime columns to
|
||||
# the +sessions+ table, making periodic session expiration a snap.
|
||||
#
|
||||
# You may provide your own session class implementation, whether a
|
||||
# feature-packed Active Record or a bare-metal high-performance SQL
|
||||
# store, by setting
|
||||
# CGI::Session::ActiveRecordStore.session_class = MySessionClass
|
||||
# You must implement these methods:
|
||||
# self.find_by_session_id(session_id)
|
||||
# initialize(hash_of_session_id_and_data)
|
||||
# attr_reader :session_id
|
||||
# attr_accessor :data
|
||||
# save
|
||||
# destroy
|
||||
#
|
||||
# The example SqlBypass class is a generic SQL session store. You may
|
||||
# use it as a basis for high-performance database-specific stores.
|
||||
class ActiveRecordStore
|
||||
# The default Active Record class.
|
||||
class Session < ActiveRecord::Base
|
||||
# Customizable data column name. Defaults to 'data'.
|
||||
cattr_accessor :data_column_name
|
||||
self.data_column_name = 'data'
|
||||
|
||||
before_save :marshal_data!
|
||||
before_save :raise_on_session_data_overflow!
|
||||
|
||||
class << self
|
||||
# Don't try to reload ARStore::Session in dev mode.
|
||||
def reloadable? #:nodoc:
|
||||
false
|
||||
end
|
||||
|
||||
def data_column_size_limit
|
||||
@data_column_size_limit ||= columns_hash[@@data_column_name].limit
|
||||
end
|
||||
|
||||
# Hook to set up sessid compatibility.
|
||||
def find_by_session_id(session_id)
|
||||
setup_sessid_compatibility!
|
||||
find_by_session_id(session_id)
|
||||
end
|
||||
|
||||
def marshal(data) ActiveSupport::Base64.encode64(Marshal.dump(data)) if data end
|
||||
def unmarshal(data) Marshal.load(ActiveSupport::Base64.decode64(data)) if data end
|
||||
|
||||
def create_table!
|
||||
connection.execute <<-end_sql
|
||||
CREATE TABLE #{table_name} (
|
||||
id INTEGER PRIMARY KEY,
|
||||
#{connection.quote_column_name('session_id')} TEXT UNIQUE,
|
||||
#{connection.quote_column_name(@@data_column_name)} TEXT(255)
|
||||
)
|
||||
end_sql
|
||||
end
|
||||
|
||||
def drop_table!
|
||||
connection.execute "DROP TABLE #{table_name}"
|
||||
end
|
||||
|
||||
private
|
||||
# Compatibility with tables using sessid instead of session_id.
|
||||
def setup_sessid_compatibility!
|
||||
# Reset column info since it may be stale.
|
||||
reset_column_information
|
||||
if columns_hash['sessid']
|
||||
def self.find_by_session_id(*args)
|
||||
find_by_sessid(*args)
|
||||
end
|
||||
|
||||
define_method(:session_id) { sessid }
|
||||
define_method(:session_id=) { |session_id| self.sessid = session_id }
|
||||
else
|
||||
def self.find_by_session_id(session_id)
|
||||
find :first, :conditions => ["session_id #{attribute_condition(session_id)}", session_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Lazy-unmarshal session state.
|
||||
def data
|
||||
@data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
|
||||
end
|
||||
|
||||
attr_writer :data
|
||||
|
||||
# Has the session been loaded yet?
|
||||
def loaded?
|
||||
!! @data
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def marshal_data!
|
||||
return false if !loaded?
|
||||
write_attribute(@@data_column_name, self.class.marshal(self.data))
|
||||
end
|
||||
|
||||
# Ensures that the data about to be stored in the database is not
|
||||
# larger than the data storage column. Raises
|
||||
# ActionController::SessionOverflowError.
|
||||
def raise_on_session_data_overflow!
|
||||
return false if !loaded?
|
||||
limit = self.class.data_column_size_limit
|
||||
if loaded? and limit and read_attribute(@@data_column_name).size > limit
|
||||
raise ActionController::SessionOverflowError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# A barebones session store which duck-types with the default session
|
||||
# store but bypasses Active Record and issues SQL directly. This is
|
||||
# an example session model class meant as a basis for your own classes.
|
||||
#
|
||||
# The database connection, table name, and session id and data columns
|
||||
# are configurable class attributes. Marshaling and unmarshaling
|
||||
# are implemented as class methods that you may override. By default,
|
||||
# marshaling data is
|
||||
#
|
||||
# ActiveSupport::Base64.encode64(Marshal.dump(data))
|
||||
#
|
||||
# and unmarshaling data is
|
||||
#
|
||||
# Marshal.load(ActiveSupport::Base64.decode64(data))
|
||||
#
|
||||
# This marshaling behavior is intended to store the widest range of
|
||||
# binary session data in a +text+ column. For higher performance,
|
||||
# store in a +blob+ column instead and forgo the Base64 encoding.
|
||||
class SqlBypass
|
||||
# Use the ActiveRecord::Base.connection by default.
|
||||
cattr_accessor :connection
|
||||
|
||||
# The table name defaults to 'sessions'.
|
||||
cattr_accessor :table_name
|
||||
@@table_name = 'sessions'
|
||||
|
||||
# The session id field defaults to 'session_id'.
|
||||
cattr_accessor :session_id_column
|
||||
@@session_id_column = 'session_id'
|
||||
|
||||
# The data field defaults to 'data'.
|
||||
cattr_accessor :data_column
|
||||
@@data_column = 'data'
|
||||
|
||||
class << self
|
||||
|
||||
def connection
|
||||
@@connection ||= ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
# Look up a session by id and unmarshal its data if found.
|
||||
def find_by_session_id(session_id)
|
||||
if record = @@connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{@@connection.quote(session_id)}")
|
||||
new(:session_id => session_id, :marshaled_data => record['data'])
|
||||
end
|
||||
end
|
||||
|
||||
def marshal(data) ActiveSupport::Base64.encode64(Marshal.dump(data)) if data end
|
||||
def unmarshal(data) Marshal.load(ActiveSupport::Base64.decode64(data)) if data end
|
||||
|
||||
def create_table!
|
||||
@@connection.execute <<-end_sql
|
||||
CREATE TABLE #{table_name} (
|
||||
id INTEGER PRIMARY KEY,
|
||||
#{@@connection.quote_column_name(session_id_column)} TEXT UNIQUE,
|
||||
#{@@connection.quote_column_name(data_column)} TEXT
|
||||
)
|
||||
end_sql
|
||||
end
|
||||
|
||||
def drop_table!
|
||||
@@connection.execute "DROP TABLE #{table_name}"
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :session_id
|
||||
attr_writer :data
|
||||
|
||||
# Look for normal and marshaled data, self.find_by_session_id's way of
|
||||
# telling us to postpone unmarshaling until the data is requested.
|
||||
# We need to handle a normal data attribute in case of a new record.
|
||||
def initialize(attributes)
|
||||
@session_id, @data, @marshaled_data = attributes[:session_id], attributes[:data], attributes[:marshaled_data]
|
||||
@new_record = @marshaled_data.nil?
|
||||
end
|
||||
|
||||
def new_record?
|
||||
@new_record
|
||||
end
|
||||
|
||||
# Lazy-unmarshal session state.
|
||||
def data
|
||||
unless @data
|
||||
if @marshaled_data
|
||||
@data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil
|
||||
else
|
||||
@data = {}
|
||||
end
|
||||
end
|
||||
@data
|
||||
end
|
||||
|
||||
def loaded?
|
||||
!! @data
|
||||
end
|
||||
|
||||
def save
|
||||
return false if !loaded?
|
||||
marshaled_data = self.class.marshal(data)
|
||||
|
||||
if @new_record
|
||||
@new_record = false
|
||||
@@connection.update <<-end_sql, 'Create session'
|
||||
INSERT INTO #{@@table_name} (
|
||||
#{@@connection.quote_column_name(@@session_id_column)},
|
||||
#{@@connection.quote_column_name(@@data_column)} )
|
||||
VALUES (
|
||||
#{@@connection.quote(session_id)},
|
||||
#{@@connection.quote(marshaled_data)} )
|
||||
end_sql
|
||||
else
|
||||
@@connection.update <<-end_sql, 'Update session'
|
||||
UPDATE #{@@table_name}
|
||||
SET #{@@connection.quote_column_name(@@data_column)}=#{@@connection.quote(marshaled_data)}
|
||||
WHERE #{@@connection.quote_column_name(@@session_id_column)}=#{@@connection.quote(session_id)}
|
||||
end_sql
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
unless @new_record
|
||||
@@connection.delete <<-end_sql, 'Destroy session'
|
||||
DELETE FROM #{@@table_name}
|
||||
WHERE #{@@connection.quote_column_name(@@session_id_column)}=#{@@connection.quote(session_id)}
|
||||
end_sql
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# The class used for session storage. Defaults to
|
||||
# CGI::Session::ActiveRecordStore::Session.
|
||||
cattr_accessor :session_class
|
||||
self.session_class = Session
|
||||
|
||||
# Find or instantiate a session given a CGI::Session.
|
||||
def initialize(session, option = nil)
|
||||
session_id = session.session_id
|
||||
unless @session = ActiveRecord::Base.silence { @@session_class.find_by_session_id(session_id) }
|
||||
unless session.new_session
|
||||
raise CGI::Session::NoSession, 'uninitialized session'
|
||||
end
|
||||
@session = @@session_class.new(:session_id => session_id, :data => {})
|
||||
# session saving can be lazy again, because of improved component implementation
|
||||
# therefore next line gets commented out:
|
||||
# @session.save
|
||||
end
|
||||
end
|
||||
|
||||
# Access the underlying session model.
|
||||
def model
|
||||
@session
|
||||
end
|
||||
|
||||
# Restore session state. The session model handles unmarshaling.
|
||||
def restore
|
||||
if @session
|
||||
@session.data
|
||||
end
|
||||
end
|
||||
|
||||
# Save session store.
|
||||
def update
|
||||
if @session
|
||||
ActiveRecord::Base.silence { @session.save }
|
||||
end
|
||||
end
|
||||
|
||||
# Save and close the session store.
|
||||
def close
|
||||
if @session
|
||||
update
|
||||
@session = nil
|
||||
end
|
||||
end
|
||||
|
||||
# Delete and close the session store.
|
||||
def delete
|
||||
if @session
|
||||
ActiveRecord::Base.silence { @session.destroy }
|
||||
@session = nil
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def logger
|
||||
ActionController::Base.logger rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,167 +1,224 @@
|
|||
require 'cgi'
|
||||
require 'cgi/session'
|
||||
require 'openssl' # to generate the HMAC message digest
|
||||
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
|
||||
|
||||
# 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.
|
||||
# TamperedWithCookie is raised if the data integrity check fails.
|
||||
#
|
||||
# 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 CGI::Session::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
|
||||
|
||||
# Raised when storing more than 4K of session data.
|
||||
class CookieOverflow < StandardError; end
|
||||
ENV_SESSION_KEY = "rack.session".freeze
|
||||
ENV_SESSION_OPTIONS_KEY = "rack.session.options".freeze
|
||||
HTTP_SET_COOKIE = "Set-Cookie".freeze
|
||||
|
||||
# Raised when the cookie fails its integrity check.
|
||||
class TamperedWithCookie < StandardError; end
|
||||
# Raised when storing more than 4K of session data.
|
||||
class CookieOverflow < StandardError; end
|
||||
|
||||
# Called from CGI::Session only.
|
||||
def initialize(session, options = {})
|
||||
# The session_key option is required.
|
||||
if options['session_key'].blank?
|
||||
raise ArgumentError, 'A session_key is required to write a cookie containing the session data. Use config.action_controller.session = { :session_key => "_myapp_session", :secret => "some secret phrase" } in config/environment.rb'
|
||||
end
|
||||
|
||||
# The secret option is required.
|
||||
ensure_secret_secure(options['secret'])
|
||||
|
||||
# Keep the session and its secret on hand so we can read and write cookies.
|
||||
@session, @secret = session, options['secret']
|
||||
|
||||
# Message digest defaults to SHA1.
|
||||
@digest = options['digest'] || 'SHA1'
|
||||
|
||||
# Default cookie options derived from session settings.
|
||||
@cookie_options = {
|
||||
'name' => options['session_key'],
|
||||
'path' => options['session_path'],
|
||||
'domain' => options['session_domain'],
|
||||
'expires' => options['session_expires'],
|
||||
'secure' => options['session_secure'],
|
||||
'http_only' => options['session_http_only']
|
||||
}
|
||||
|
||||
# Set no_hidden and no_cookies since the session id is unused and we
|
||||
# set our own data cookie.
|
||||
options['no_hidden'] = true
|
||||
options['no_cookies'] = true
|
||||
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, %Q{A secret is required to generate an integrity hash for cookie session data. Use config.action_controller.session = { :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, %Q{Secret should be something secure, like "#{CGI::Session.generate_unique_id}". The value you provided, "#{secret}", is shorter than the minimum length of #{SECRET_MIN_LENGTH} characters}
|
||||
end
|
||||
end
|
||||
|
||||
# Restore session data from the cookie.
|
||||
def restore
|
||||
@original = read_cookie
|
||||
@data = unmarshal(@original) || {}
|
||||
end
|
||||
|
||||
# Wait until close to write the session data cookie.
|
||||
def update; end
|
||||
|
||||
# Write the session data cookie if it was loaded and has changed.
|
||||
def close
|
||||
if defined?(@data) && !@data.blank?
|
||||
updated = marshal(@data)
|
||||
raise CookieOverflow if updated.size > MAX
|
||||
write_cookie('value' => updated) unless updated == @original
|
||||
end
|
||||
end
|
||||
|
||||
# Delete the session data by setting an expired cookie with no data.
|
||||
def delete
|
||||
@data = nil
|
||||
clear_old_cookie_value
|
||||
write_cookie('value' => nil, 'expires' => 1.year.ago)
|
||||
end
|
||||
|
||||
# Generate the HMAC keyed message digest. Uses SHA1 by default.
|
||||
def generate_digest(data)
|
||||
key = @secret.respond_to?(:call) ? @secret.call(@session) : @secret
|
||||
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), key, data)
|
||||
end
|
||||
|
||||
private
|
||||
# Marshal a session hash into safe cookie data. Include an integrity hash.
|
||||
def marshal(session)
|
||||
data = ActiveSupport::Base64.encode64s(Marshal.dump(session))
|
||||
"#{data}--#{generate_digest(data)}"
|
||||
end
|
||||
|
||||
# Unmarshal cookie data to a hash and verify its integrity.
|
||||
def unmarshal(cookie)
|
||||
if cookie
|
||||
data, digest = cookie.split('--')
|
||||
|
||||
# Do two checks to transparently support old double-escaped data.
|
||||
unless digest == generate_digest(data) || digest == generate_digest(data = CGI.unescape(data))
|
||||
delete
|
||||
raise TamperedWithCookie
|
||||
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
|
||||
|
||||
Marshal.load(ActiveSupport::Base64.decode64(data))
|
||||
@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
|
||||
end
|
||||
|
||||
# Read the session data cookie.
|
||||
def read_cookie
|
||||
@session.cgi.cookies[@cookie_options['name']].first
|
||||
end
|
||||
def call(env)
|
||||
env[ENV_SESSION_KEY] = AbstractStore::SessionHash.new(self, env)
|
||||
env[ENV_SESSION_OPTIONS_KEY] = @default_options
|
||||
|
||||
# CGI likes to make you hack.
|
||||
def write_cookie(options)
|
||||
cookie = CGI::Cookie.new(@cookie_options.merge(options))
|
||||
@session.cgi.send :instance_variable_set, '@output_cookies', [cookie]
|
||||
end
|
||||
status, headers, body = @app.call(env)
|
||||
|
||||
# Clear cookie value so subsequent new_session doesn't reload old data.
|
||||
def clear_old_cookie_value
|
||||
@session.cgi.cookies[@cookie_options['name']].clear
|
||||
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
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This is a really simple session storage daemon, basically just a hash,
|
||||
# which is enabled for DRb access.
|
||||
|
||||
require 'drb'
|
||||
|
||||
session_hash = Hash.new
|
||||
session_hash.instance_eval { @mutex = Mutex.new }
|
||||
|
||||
class <<session_hash
|
||||
def []=(key, value)
|
||||
@mutex.synchronize do
|
||||
super(key, value)
|
||||
end
|
||||
end
|
||||
|
||||
def [](key)
|
||||
@mutex.synchronize do
|
||||
super(key)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(key)
|
||||
@mutex.synchronize do
|
||||
super(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
DRb.start_service('druby://127.0.0.1:9192', session_hash)
|
||||
DRb.thread.join
|
|
@ -1,35 +0,0 @@
|
|||
require 'cgi'
|
||||
require 'cgi/session'
|
||||
require 'drb'
|
||||
|
||||
class CGI #:nodoc:all
|
||||
class Session
|
||||
class DRbStore
|
||||
@@session_data = DRbObject.new(nil, 'druby://localhost:9192')
|
||||
|
||||
def initialize(session, option=nil)
|
||||
@session_id = session.session_id
|
||||
end
|
||||
|
||||
def restore
|
||||
@h = @@session_data[@session_id] || {}
|
||||
end
|
||||
|
||||
def update
|
||||
@@session_data[@session_id] = @h
|
||||
end
|
||||
|
||||
def close
|
||||
update
|
||||
end
|
||||
|
||||
def delete
|
||||
@@session_data.delete(@session_id)
|
||||
end
|
||||
|
||||
def data
|
||||
@@session_data[@session_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,95 +1,48 @@
|
|||
# cgi/session/memcached.rb - persistent storage of marshalled session data
|
||||
#
|
||||
# == Overview
|
||||
#
|
||||
# This file provides the CGI::Session::MemCache class, which builds
|
||||
# persistence of storage data on top of the MemCache library. See
|
||||
# cgi/session.rb for more details on session storage managers.
|
||||
#
|
||||
|
||||
begin
|
||||
require 'cgi/session'
|
||||
require_library_or_gem 'memcache'
|
||||
|
||||
class CGI
|
||||
class Session
|
||||
# MemCache-based session storage class.
|
||||
#
|
||||
# This builds upon the top-level MemCache class provided by the
|
||||
# library file memcache.rb. Session data is marshalled and stored
|
||||
# in a memcached cache.
|
||||
class MemCacheStore
|
||||
def check_id(id) #:nodoc:#
|
||||
/[^0-9a-zA-Z]+/ =~ id.to_s ? false : true
|
||||
end
|
||||
module ActionController
|
||||
module Session
|
||||
class MemCacheStore < AbstractStore
|
||||
def initialize(app, options = {})
|
||||
# Support old :expires option
|
||||
options[:expire_after] ||= options[:expires]
|
||||
|
||||
# Create a new CGI::Session::MemCache instance
|
||||
#
|
||||
# This constructor is used internally by CGI::Session. The
|
||||
# user does not generally need to call it directly.
|
||||
#
|
||||
# +session+ is the session for which this instance is being
|
||||
# created. The session id must only contain alphanumeric
|
||||
# characters; automatically generated session ids observe
|
||||
# this requirement.
|
||||
#
|
||||
# +options+ is a hash of options for the initializer. The
|
||||
# following options are recognized:
|
||||
#
|
||||
# cache:: an instance of a MemCache client to use as the
|
||||
# session cache.
|
||||
#
|
||||
# expires:: an expiry time value to use for session entries in
|
||||
# the session cache. +expires+ is interpreted in seconds
|
||||
# relative to the current time if it’s less than 60*60*24*30
|
||||
# (30 days), or as an absolute Unix time (e.g., Time#to_i) if
|
||||
# greater. If +expires+ is +0+, or not passed on +options+,
|
||||
# the entry will never expire.
|
||||
#
|
||||
# This session's memcache entry will be created if it does
|
||||
# not exist, or retrieved if it does.
|
||||
def initialize(session, options = {})
|
||||
id = session.session_id
|
||||
unless check_id(id)
|
||||
raise ArgumentError, "session_id '%s' is invalid" % id
|
||||
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
|
||||
@cache = options['cache'] || MemCache.new('localhost')
|
||||
@expires = options['expires'] || 0
|
||||
@session_key = "session:#{id}"
|
||||
@session_data = {}
|
||||
# Add this key to the store if haven't done so yet
|
||||
unless @cache.get(@session_key)
|
||||
@cache.add(@session_key, @session_data, @expires)
|
||||
@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
|
||||
end
|
||||
|
||||
# Restore session state from the session's memcache entry.
|
||||
#
|
||||
# Returns the session state as a hash.
|
||||
def restore
|
||||
@session_data = @cache[@session_key] || {}
|
||||
end
|
||||
|
||||
# Save session state to the session's memcache entry.
|
||||
def update
|
||||
@cache.set(@session_key, @session_data, @expires)
|
||||
end
|
||||
|
||||
# Update and close the session's memcache entry.
|
||||
def close
|
||||
update
|
||||
end
|
||||
|
||||
# Delete the session's memcache entry.
|
||||
def delete
|
||||
@cache.delete(@session_key)
|
||||
@session_data = {}
|
||||
end
|
||||
|
||||
def data
|
||||
@session_data
|
||||
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
|
||||
|
|
|
@ -1,17 +1,8 @@
|
|||
require 'action_controller/session/cookie_store'
|
||||
require 'action_controller/session/drb_store'
|
||||
require 'action_controller/session/mem_cache_store'
|
||||
if Object.const_defined?(:ActiveRecord)
|
||||
require 'action_controller/session/active_record_store'
|
||||
end
|
||||
|
||||
module ActionController #:nodoc:
|
||||
module SessionManagement #:nodoc:
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend ClassMethods
|
||||
alias_method_chain :process, :session_management_support
|
||||
alias_method_chain :process_cleanup, :session_management_support
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -19,144 +10,45 @@ module ActionController #:nodoc:
|
|||
# Set the session store to be used for keeping the session data between requests.
|
||||
# By default, sessions are stored in browser cookies (<tt>:cookie_store</tt>),
|
||||
# but you can also specify one of the other included stores (<tt>:active_record_store</tt>,
|
||||
# <tt>:p_store</tt>, <tt>:drb_store</tt>, <tt>:mem_cache_store</tt>, or
|
||||
# <tt>:memory_store</tt>) or your own custom class.
|
||||
# <tt>:mem_cache_store</tt>, or your own custom class.
|
||||
def session_store=(store)
|
||||
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] =
|
||||
store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store
|
||||
if store == :active_record_store
|
||||
self.session_store = ActiveRecord::SessionStore
|
||||
else
|
||||
@@session_store = store.is_a?(Symbol) ?
|
||||
Session.const_get(store.to_s.camelize) :
|
||||
store
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the session store class currently used.
|
||||
def session_store
|
||||
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager]
|
||||
if defined? @@session_store
|
||||
@@session_store
|
||||
else
|
||||
Session::CookieStore
|
||||
end
|
||||
end
|
||||
|
||||
def session=(options = {})
|
||||
self.session_store = nil if options.delete(:disabled)
|
||||
session_options.merge!(options)
|
||||
end
|
||||
|
||||
# Returns the hash used to configure the session. Example use:
|
||||
#
|
||||
# ActionController::Base.session_options[:session_secure] = true # session only available over HTTPS
|
||||
# ActionController::Base.session_options[:secure] = true # session only available over HTTPS
|
||||
def session_options
|
||||
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
|
||||
@session_options ||= {}
|
||||
end
|
||||
|
||||
# Specify how sessions ought to be managed for a subset of the actions on
|
||||
# the controller. Like filters, you can specify <tt>:only</tt> and
|
||||
# <tt>:except</tt> clauses to restrict the subset, otherwise options
|
||||
# apply to all actions on this controller.
|
||||
#
|
||||
# The session options are inheritable, as well, so if you specify them in
|
||||
# a parent controller, they apply to controllers that extend the parent.
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# # turn off session management for all actions.
|
||||
# session :off
|
||||
#
|
||||
# # turn off session management for all actions _except_ foo and bar.
|
||||
# session :off, :except => %w(foo bar)
|
||||
#
|
||||
# # turn off session management for only the foo and bar actions.
|
||||
# session :off, :only => %w(foo bar)
|
||||
#
|
||||
# # the session will only work over HTTPS, but only for the foo action
|
||||
# session :only => :foo, :session_secure => true
|
||||
#
|
||||
# # the session by default uses HttpOnly sessions for security reasons.
|
||||
# # this can be switched off.
|
||||
# session :only => :foo, :session_http_only => false
|
||||
#
|
||||
# # the session will only be disabled for 'foo', and only if it is
|
||||
# # requested as a web service
|
||||
# session :off, :only => :foo,
|
||||
# :if => Proc.new { |req| req.parameters[:ws] }
|
||||
#
|
||||
# # the session will be disabled for non html/ajax requests
|
||||
# session :off,
|
||||
# :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
|
||||
#
|
||||
# # turn the session back on, useful when it was turned off in the
|
||||
# # application controller, and you need it on in another controller
|
||||
# session :on
|
||||
#
|
||||
# All session options described for ActionController::Base.process_cgi
|
||||
# are valid arguments.
|
||||
|
||||
def session(*args)
|
||||
options = args.extract_options!
|
||||
|
||||
options[:disabled] = false if args.delete(:on)
|
||||
options[:disabled] = true if !args.empty?
|
||||
options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only]
|
||||
options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except]
|
||||
if options[:only] && options[:except]
|
||||
raise ArgumentError, "only one of either :only or :except are allowed"
|
||||
end
|
||||
|
||||
write_inheritable_array(:session_options, [options])
|
||||
end
|
||||
|
||||
# So we can declare session options in the Rails initializer.
|
||||
alias_method :session=, :session
|
||||
|
||||
def cached_session_options #:nodoc:
|
||||
@session_options ||= read_inheritable_attribute(:session_options) || []
|
||||
end
|
||||
|
||||
def session_options_for(request, action) #:nodoc:
|
||||
if (session_options = cached_session_options).empty?
|
||||
{}
|
||||
else
|
||||
options = {}
|
||||
|
||||
action = action.to_s
|
||||
session_options.each do |opts|
|
||||
next if opts[:if] && !opts[:if].call(request)
|
||||
if opts[:only] && opts[:only].include?(action)
|
||||
options.merge!(opts)
|
||||
elsif opts[:except] && !opts[:except].include?(action)
|
||||
options.merge!(opts)
|
||||
elsif !opts[:only] && !opts[:except]
|
||||
options.merge!(opts)
|
||||
end
|
||||
end
|
||||
|
||||
if options.empty? then options
|
||||
else
|
||||
options.delete :only
|
||||
options.delete :except
|
||||
options.delete :if
|
||||
options[:disabled] ? false : options
|
||||
end
|
||||
end
|
||||
ActiveSupport::Deprecation.warn(
|
||||
"Disabling sessions for a single controller has been deprecated. " +
|
||||
"Sessions are now lazy loaded. So if you don't access them, " +
|
||||
"consider them off. You can still modify the session cookie " +
|
||||
"options with request.session_options.", caller)
|
||||
end
|
||||
end
|
||||
|
||||
def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc:
|
||||
set_session_options(request)
|
||||
process_without_session_management_support(request, response, method, *arguments)
|
||||
end
|
||||
|
||||
private
|
||||
def set_session_options(request)
|
||||
request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index")
|
||||
end
|
||||
|
||||
def process_cleanup_with_session_management_support
|
||||
clear_persistent_model_associations
|
||||
process_cleanup_without_session_management_support
|
||||
end
|
||||
|
||||
# Clear cached associations in session data so they don't overflow
|
||||
# the database field. Only applies to ActiveRecordStore since there
|
||||
# is not a standard way to iterate over session data.
|
||||
def clear_persistent_model_associations #:doc:
|
||||
if defined?(@_session) && @_session.respond_to?(:data)
|
||||
session_data = @_session.data
|
||||
|
||||
if session_data && session_data.respond_to?(:each_value)
|
||||
session_data.each_value do |obj|
|
||||
obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,7 +24,8 @@ module ActionController #:nodoc:
|
|||
# Options:
|
||||
# * <tt>:filename</tt> - suggests a filename for the browser to use.
|
||||
# Defaults to <tt>File.basename(path)</tt>.
|
||||
# * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'.
|
||||
# * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'. You can specify
|
||||
# either a string or a symbol for a registered type register with <tt>Mime::Type.register</tt>, for example :json
|
||||
# * <tt>:length</tt> - used to manually override the length (in bytes) of the content that
|
||||
# is going to be sent to the client. Defaults to <tt>File.size(path)</tt>.
|
||||
# * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
|
||||
|
@ -107,7 +108,8 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# Options:
|
||||
# * <tt>:filename</tt> - suggests a filename for the browser to use.
|
||||
# * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'.
|
||||
# * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'. You can specify
|
||||
# either a string or a symbol for a registered type register with <tt>Mime::Type.register</tt>, for example :json
|
||||
# * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
|
||||
# Valid values are 'inline' and 'attachment' (default).
|
||||
# * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
|
||||
|
@ -143,9 +145,16 @@ module ActionController #:nodoc:
|
|||
|
||||
disposition <<= %(; filename="#{options[:filename]}") if options[:filename]
|
||||
|
||||
content_type = options[:type]
|
||||
if content_type.is_a?(Symbol)
|
||||
raise ArgumentError, "Unknown MIME type #{options[:type]}" unless Mime::EXTENSION_LOOKUP.has_key?(content_type.to_s)
|
||||
content_type = Mime::Type.lookup_by_extension(content_type.to_s)
|
||||
end
|
||||
content_type = content_type.to_s.strip # fixes a problem with extra '\r' with some browsers
|
||||
|
||||
headers.update(
|
||||
'Content-Length' => options[:length],
|
||||
'Content-Type' => options[:type].to_s.strip, # fixes a problem with extra '\r' with some browsers
|
||||
'Content-Type' => content_type,
|
||||
'Content-Disposition' => disposition,
|
||||
'Content-Transfer-Encoding' => 'binary'
|
||||
)
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
</h1>
|
||||
<pre><%=h @exception.clean_message %></pre>
|
||||
|
||||
<%= render(:file => @rescues_path + "/_trace.erb") %>
|
||||
<%= render :file => @rescues_path["rescues/_trace.erb"] %>
|
||||
|
||||
<%= render(:file => @rescues_path + "/_request_and_response.erb") %>
|
||||
<%= render :file => @rescues_path["rescues/_request_and_response.erb"] %>
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
<% @real_exception = @exception
|
||||
@exception = @exception.original_exception || @exception %>
|
||||
<%= render(:file => @rescues_path + "/_trace.erb") %>
|
||||
<%= render :file => @rescues_path["rescues/_trace.erb"] %>
|
||||
<% @exception = @real_exception %>
|
||||
|
||||
<%= render(:file => @rescues_path + "/_request_and_response.erb") %>
|
||||
<%= render :file => @rescues_path["rescues/_request_and_response.erb"] %>
|
||||
|
|
|
@ -1,20 +1,7 @@
|
|||
require 'active_support/test_case'
|
||||
require 'action_controller/test_process'
|
||||
|
||||
module ActionController
|
||||
class NonInferrableControllerError < ActionControllerError
|
||||
def initialize(name)
|
||||
@name = name
|
||||
super "Unable to determine the controller to test from #{name}. " +
|
||||
"You'll need to specify it using 'tests YourController' in your " +
|
||||
"test case definition. This could mean that #{inferred_controller_name} does not exist " +
|
||||
"or it contains syntax errors"
|
||||
end
|
||||
|
||||
def inferred_controller_name
|
||||
@name.sub(/Test$/, '')
|
||||
end
|
||||
end
|
||||
|
||||
# Superclass for ActionController functional tests. Functional tests allow you to
|
||||
# test a single controller action per test method. This should not be confused with
|
||||
# integration tests (see ActionController::IntegrationTest), which are more like
|
||||
|
@ -74,7 +61,65 @@ module ActionController
|
|||
# class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
|
||||
# tests WidgetController
|
||||
# end
|
||||
#
|
||||
# == Testing controller internals
|
||||
#
|
||||
# In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
|
||||
# can be used against. These collections are:
|
||||
#
|
||||
# * assigns: Instance variables assigned in the action that are available for the view.
|
||||
# * session: Objects being saved in the session.
|
||||
# * flash: The flash objects currently in the session.
|
||||
# * cookies: Cookies being sent to the user on this request.
|
||||
#
|
||||
# These collections can be used just like any other hash:
|
||||
#
|
||||
# assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
|
||||
# assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
|
||||
# assert flash.empty? # makes sure that there's nothing in the flash
|
||||
#
|
||||
# For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To
|
||||
# appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing.
|
||||
# So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work.
|
||||
#
|
||||
# On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
|
||||
#
|
||||
# For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
|
||||
# action call which can then be asserted against.
|
||||
#
|
||||
# == Manipulating the request collections
|
||||
#
|
||||
# The collections described above link to the response, so you can test if what the actions were expected to do happened. But
|
||||
# sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions
|
||||
# and cookies, though. For sessions, you just do:
|
||||
#
|
||||
# @request.session[:key] = "value"
|
||||
# @request.cookies["key"] = "value"
|
||||
#
|
||||
# == Testing named routes
|
||||
#
|
||||
# If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case.
|
||||
# Example:
|
||||
#
|
||||
# assert_redirected_to page_url(:title => 'foo')
|
||||
class TestCase < ActiveSupport::TestCase
|
||||
include TestProcess
|
||||
|
||||
module Assertions
|
||||
%w(response selector tag dom routing model).each do |kind|
|
||||
include ActionController::Assertions.const_get("#{kind.camelize}Assertions")
|
||||
end
|
||||
|
||||
def clean_backtrace(&block)
|
||||
yield
|
||||
rescue ActiveSupport::TestCase::Assertion => error
|
||||
framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions"))
|
||||
error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path }
|
||||
raise
|
||||
end
|
||||
end
|
||||
include Assertions
|
||||
|
||||
# When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline
|
||||
# (bystepping the regular exception handling from rescue_action). If the request.remote_addr is anything else, the regular
|
||||
# rescue_action process takes place. This means you can test your rescue_action code by setting remote_addr to something else
|
||||
|
@ -82,17 +127,18 @@ module ActionController
|
|||
#
|
||||
# The exception is stored in the exception accessor for further inspection.
|
||||
module RaiseActionExceptions
|
||||
attr_accessor :exception
|
||||
protected
|
||||
attr_accessor :exception
|
||||
|
||||
def rescue_action_without_handler(e)
|
||||
self.exception = e
|
||||
|
||||
if request.remote_addr == "0.0.0.0"
|
||||
raise(e)
|
||||
else
|
||||
super(e)
|
||||
def rescue_action_without_handler(e)
|
||||
self.exception = e
|
||||
|
||||
if request.remote_addr == "0.0.0.0"
|
||||
raise(e)
|
||||
else
|
||||
super(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
setup :setup_controller_request_and_response
|
||||
|
@ -107,7 +153,7 @@ module ActionController
|
|||
end
|
||||
|
||||
def controller_class=(new_class)
|
||||
prepare_controller_class(new_class)
|
||||
prepare_controller_class(new_class) if new_class
|
||||
write_inheritable_attribute(:controller_class, new_class)
|
||||
end
|
||||
|
||||
|
@ -122,7 +168,7 @@ module ActionController
|
|||
def determine_default_controller_class(name)
|
||||
name.sub(/Test$/, '').constantize
|
||||
rescue NameError
|
||||
raise NonInferrableControllerError.new(name)
|
||||
nil
|
||||
end
|
||||
|
||||
def prepare_controller_class(new_class)
|
||||
|
@ -131,17 +177,23 @@ module ActionController
|
|||
end
|
||||
|
||||
def setup_controller_request_and_response
|
||||
@controller = self.class.controller_class.new
|
||||
@controller.request = @request = TestRequest.new
|
||||
@request = TestRequest.new
|
||||
@response = TestResponse.new
|
||||
|
||||
@controller.params = {}
|
||||
@controller.send(:initialize_current_url)
|
||||
if klass = self.class.controller_class
|
||||
@controller ||= klass.new rescue nil
|
||||
end
|
||||
|
||||
if @controller
|
||||
@controller.request = @request
|
||||
@controller.params = {}
|
||||
@controller.send(:initialize_current_url)
|
||||
end
|
||||
end
|
||||
|
||||
# Cause the action to be rescued according to the regular rules for rescue_action when the visitor is not local
|
||||
def rescue_action_in_public!
|
||||
@request.remote_addr = '208.77.188.166' # example.com
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,51 +1,21 @@
|
|||
require 'action_controller/assertions'
|
||||
require 'action_controller/test_case'
|
||||
|
||||
module ActionController #:nodoc:
|
||||
class Base
|
||||
attr_reader :assigns
|
||||
|
||||
# Process a test request called with a TestRequest object.
|
||||
def self.process_test(request)
|
||||
new.process_test(request)
|
||||
end
|
||||
|
||||
def process_test(request) #:nodoc:
|
||||
process(request, TestResponse.new)
|
||||
end
|
||||
|
||||
def process_with_test(*args)
|
||||
returning process_without_test(*args) do
|
||||
@assigns = {}
|
||||
(instance_variable_names - @@protected_instance_variables).each do |var|
|
||||
value = instance_variable_get(var)
|
||||
@assigns[var[1..-1]] = value
|
||||
response.template.assigns[var[1..-1]] = value if response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
alias_method_chain :process, :test
|
||||
end
|
||||
|
||||
class TestRequest < AbstractRequest #:nodoc:
|
||||
class TestRequest < Request #:nodoc:
|
||||
attr_accessor :cookies, :session_options
|
||||
attr_accessor :query_parameters, :request_parameters, :path, :session
|
||||
attr_accessor :host, :user_agent
|
||||
attr_accessor :query_parameters, :path, :session
|
||||
attr_accessor :host
|
||||
|
||||
def initialize(query_parameters = nil, request_parameters = nil, session = nil)
|
||||
@query_parameters = query_parameters || {}
|
||||
@request_parameters = request_parameters || {}
|
||||
@session = session || TestSession.new
|
||||
def initialize
|
||||
super(Rack::MockRequest.env_for("/"))
|
||||
|
||||
@query_parameters = {}
|
||||
@session = TestSession.new
|
||||
|
||||
initialize_containers
|
||||
initialize_default_values
|
||||
|
||||
super()
|
||||
initialize_containers
|
||||
end
|
||||
|
||||
def reset_session
|
||||
@session = TestSession.new
|
||||
@session.reset
|
||||
end
|
||||
|
||||
# Wraps raw_post in a StringIO.
|
||||
|
@ -56,12 +26,15 @@ module ActionController #:nodoc:
|
|||
# Either the RAW_POST_DATA environment variable or the URL-encoded request
|
||||
# parameters.
|
||||
def raw_post
|
||||
env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters) { |b| b.force_encoding(Encoding::BINARY) if b.respond_to?(:force_encoding) }
|
||||
@env['RAW_POST_DATA'] ||= begin
|
||||
data = url_encoded_request_parameters
|
||||
data.force_encoding(Encoding::BINARY) if data.respond_to?(:force_encoding)
|
||||
data
|
||||
end
|
||||
end
|
||||
|
||||
def port=(number)
|
||||
@env["SERVER_PORT"] = number.to_i
|
||||
port(true)
|
||||
end
|
||||
|
||||
def action=(action_name)
|
||||
|
@ -75,8 +48,6 @@ module ActionController #:nodoc:
|
|||
@env["REQUEST_URI"] = value
|
||||
@request_uri = nil
|
||||
@path = nil
|
||||
request_uri(true)
|
||||
path(true)
|
||||
end
|
||||
|
||||
def request_uri=(uri)
|
||||
|
@ -84,9 +55,13 @@ module ActionController #:nodoc:
|
|||
@path = uri.split("?").first
|
||||
end
|
||||
|
||||
def request_method=(method)
|
||||
@request_method = method
|
||||
end
|
||||
|
||||
def accept=(mime_types)
|
||||
@env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
|
||||
accepts(true)
|
||||
@accepts = nil
|
||||
end
|
||||
|
||||
def if_modified_since=(last_modified)
|
||||
|
@ -102,11 +77,11 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def request_uri(*args)
|
||||
@request_uri || super
|
||||
@request_uri || super()
|
||||
end
|
||||
|
||||
def path(*args)
|
||||
@path || super
|
||||
@path || super()
|
||||
end
|
||||
|
||||
def assign_parameters(controller_path, action, parameters)
|
||||
|
@ -126,26 +101,30 @@ module ActionController #:nodoc:
|
|||
path_parameters[key.to_s] = value
|
||||
end
|
||||
end
|
||||
raw_post # populate env['RAW_POST_DATA']
|
||||
@parameters = nil # reset TestRequest#parameters to use the new path_parameters
|
||||
end
|
||||
|
||||
def recycle!
|
||||
self.request_parameters = {}
|
||||
self.query_parameters = {}
|
||||
self.path_parameters = {}
|
||||
unmemoize_all
|
||||
@headers, @request_method, @accepts, @content_type = nil, nil, nil, nil
|
||||
end
|
||||
|
||||
def user_agent=(user_agent)
|
||||
@env['HTTP_USER_AGENT'] = user_agent
|
||||
end
|
||||
|
||||
private
|
||||
def initialize_containers
|
||||
@env, @cookies = {}, {}
|
||||
@cookies = {}
|
||||
end
|
||||
|
||||
def initialize_default_values
|
||||
@host = "test.host"
|
||||
@request_uri = "/"
|
||||
@user_agent = "Rails Testing"
|
||||
self.remote_addr = "0.0.0.0"
|
||||
@env['HTTP_USER_AGENT'] = "Rails Testing"
|
||||
@env['REMOTE_ADDR'] = "0.0.0.0"
|
||||
@env["SERVER_PORT"] = 80
|
||||
@env['REQUEST_METHOD'] = "GET"
|
||||
end
|
||||
|
@ -167,7 +146,7 @@ module ActionController #:nodoc:
|
|||
module TestResponseBehavior #:nodoc:
|
||||
# The response code of the request
|
||||
def response_code
|
||||
status[0,3].to_i rescue 0
|
||||
status.to_s[0,3].to_i rescue 0
|
||||
end
|
||||
|
||||
# Returns a String to ensure compatibility with Net::HTTPResponse
|
||||
|
@ -201,6 +180,11 @@ module ActionController #:nodoc:
|
|||
|
||||
alias_method :server_error?, :error?
|
||||
|
||||
# Was there a client client?
|
||||
def client_error?
|
||||
(400..499).include?(response_code)
|
||||
end
|
||||
|
||||
# Returns the redirection location or nil
|
||||
def redirect_url
|
||||
headers['Location']
|
||||
|
@ -217,8 +201,8 @@ module ActionController #:nodoc:
|
|||
|
||||
# Returns the template of the file which was used to
|
||||
# render this response (or nil)
|
||||
def rendered_template
|
||||
template.instance_variable_get(:@_first_render)
|
||||
def rendered
|
||||
template.instance_variable_get(:@_rendered)
|
||||
end
|
||||
|
||||
# A shortcut to the flash. Returns an empty hash if no session flash exists.
|
||||
|
@ -228,7 +212,7 @@ module ActionController #:nodoc:
|
|||
|
||||
# Do we have a flash?
|
||||
def has_flash?
|
||||
!session['flash'].empty?
|
||||
!flash.empty?
|
||||
end
|
||||
|
||||
# Do we have a flash that has contents?
|
||||
|
@ -256,11 +240,16 @@ module ActionController #:nodoc:
|
|||
!template_objects[name].nil?
|
||||
end
|
||||
|
||||
# Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs
|
||||
# Returns the response cookies, converted to a Hash of (name => value) pairs
|
||||
#
|
||||
# assert_equal ['AuthorOfNewPage'], r.cookies['author'].value
|
||||
# assert_equal 'AuthorOfNewPage', r.cookies['author']
|
||||
def cookies
|
||||
headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash }
|
||||
cookies = {}
|
||||
Array(headers['Set-Cookie']).each do |cookie|
|
||||
key, value = cookie.split(";").first.split("=")
|
||||
cookies[key] = value
|
||||
end
|
||||
cookies
|
||||
end
|
||||
|
||||
# Returns binary content (downloadable file), converted to a String
|
||||
|
@ -281,48 +270,72 @@ module ActionController #:nodoc:
|
|||
# TestResponse, which represent the HTTP response results of the requested
|
||||
# controller actions.
|
||||
#
|
||||
# See AbstractResponse for more information on controller response objects.
|
||||
class TestResponse < AbstractResponse
|
||||
# See Response for more information on controller response objects.
|
||||
class TestResponse < Response
|
||||
include TestResponseBehavior
|
||||
|
||||
|
||||
def recycle!
|
||||
headers.delete('ETag')
|
||||
headers.delete('Last-Modified')
|
||||
end
|
||||
end
|
||||
|
||||
class TestSession #:nodoc:
|
||||
class TestSession < Hash #:nodoc:
|
||||
attr_accessor :session_id
|
||||
|
||||
def initialize(attributes = nil)
|
||||
@session_id = ''
|
||||
@attributes = attributes.nil? ? nil : attributes.stringify_keys
|
||||
@saved_attributes = nil
|
||||
reset_session_id
|
||||
replace_attributes(attributes)
|
||||
end
|
||||
|
||||
def reset
|
||||
reset_session_id
|
||||
replace_attributes({ })
|
||||
end
|
||||
|
||||
def data
|
||||
@attributes ||= @saved_attributes || {}
|
||||
to_hash
|
||||
end
|
||||
|
||||
def [](key)
|
||||
data[key.to_s]
|
||||
super(key.to_s)
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
data[key.to_s] = value
|
||||
super(key.to_s, value)
|
||||
end
|
||||
|
||||
def update
|
||||
@saved_attributes = @attributes
|
||||
def update(hash = nil)
|
||||
if hash.nil?
|
||||
ActiveSupport::Deprecation.warn('use replace instead', caller)
|
||||
replace({})
|
||||
else
|
||||
super(hash)
|
||||
end
|
||||
end
|
||||
|
||||
def delete
|
||||
@attributes = nil
|
||||
def delete(key = nil)
|
||||
if key.nil?
|
||||
ActiveSupport::Deprecation.warn('use clear instead', caller)
|
||||
clear
|
||||
else
|
||||
super(key.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def close
|
||||
update
|
||||
delete
|
||||
ActiveSupport::Deprecation.warn('sessions should no longer be closed', caller)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reset_session_id
|
||||
@session_id = ''
|
||||
end
|
||||
|
||||
def replace_attributes(attributes = nil)
|
||||
attributes ||= {}
|
||||
replace(attributes.stringify_keys)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -333,10 +346,10 @@ module ActionController #:nodoc:
|
|||
# a file upload.
|
||||
#
|
||||
# Usage example, within a functional test:
|
||||
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
|
||||
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
|
||||
#
|
||||
# Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
|
||||
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
|
||||
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
|
||||
require 'tempfile'
|
||||
class TestUploadedFile
|
||||
# The filename, *not* including the path, of the "uploaded" file
|
||||
|
@ -368,20 +381,33 @@ module ActionController #:nodoc:
|
|||
|
||||
module TestProcess
|
||||
def self.included(base)
|
||||
# execute the request simulating a specific HTTP method and set/volley the response
|
||||
# TODO: this should be un-DRY'ed for the sake of API documentation.
|
||||
%w( get post put delete head ).each do |method|
|
||||
base.class_eval <<-EOV, __FILE__, __LINE__
|
||||
def #{method}(action, parameters = nil, session = nil, flash = nil)
|
||||
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
|
||||
process(action, parameters, session, flash)
|
||||
end
|
||||
EOV
|
||||
# Executes a request simulating GET HTTP method and set/volley the response
|
||||
def get(action, parameters = nil, session = nil, flash = nil)
|
||||
process(action, parameters, session, flash, "GET")
|
||||
end
|
||||
|
||||
# Executes a request simulating POST HTTP method and set/volley the response
|
||||
def post(action, parameters = nil, session = nil, flash = nil)
|
||||
process(action, parameters, session, flash, "POST")
|
||||
end
|
||||
|
||||
# Executes a request simulating PUT HTTP method and set/volley the response
|
||||
def put(action, parameters = nil, session = nil, flash = nil)
|
||||
process(action, parameters, session, flash, "PUT")
|
||||
end
|
||||
|
||||
# Executes a request simulating DELETE HTTP method and set/volley the response
|
||||
def delete(action, parameters = nil, session = nil, flash = nil)
|
||||
process(action, parameters, session, flash, "DELETE")
|
||||
end
|
||||
|
||||
# Executes a request simulating HEAD HTTP method and set/volley the response
|
||||
def head(action, parameters = nil, session = nil, flash = nil)
|
||||
process(action, parameters, session, flash, "HEAD")
|
||||
end
|
||||
end
|
||||
|
||||
# execute the request and set/volley the response
|
||||
def process(action, parameters = nil, session = nil, flash = nil)
|
||||
def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
|
||||
# Sanity check for required instance variables so we can give an
|
||||
# understandable error message.
|
||||
%w(@controller @request @response).each do |iv_name|
|
||||
|
@ -394,7 +420,7 @@ module ActionController #:nodoc:
|
|||
@response.recycle!
|
||||
|
||||
@html_document = nil
|
||||
@request.env['REQUEST_METHOD'] ||= "GET"
|
||||
@request.env['REQUEST_METHOD'] = http_method
|
||||
|
||||
@request.action = action.to_s
|
||||
|
||||
|
@ -404,12 +430,14 @@ module ActionController #:nodoc:
|
|||
@request.session = ActionController::TestSession.new(session) unless session.nil?
|
||||
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
|
||||
build_request_uri(action, parameters)
|
||||
@controller.process(@request, @response)
|
||||
|
||||
Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
|
||||
@controller.process_with_test(@request, @response)
|
||||
end
|
||||
|
||||
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
|
||||
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
||||
@request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
|
||||
@request.env['HTTP_ACCEPT'] = [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
||||
returning __send__(request_method, action, parameters, session, flash) do
|
||||
@request.env.delete 'HTTP_X_REQUESTED_WITH'
|
||||
@request.env.delete 'HTTP_ACCEPT'
|
||||
|
@ -426,7 +454,7 @@ module ActionController #:nodoc:
|
|||
end
|
||||
|
||||
def session
|
||||
@response.session
|
||||
@request.session
|
||||
end
|
||||
|
||||
def flash
|
||||
|
@ -464,15 +492,15 @@ module ActionController #:nodoc:
|
|||
html_document.find_all(conditions)
|
||||
end
|
||||
|
||||
def method_missing(selector, *args)
|
||||
if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
|
||||
@controller.send(selector, *args)
|
||||
def method_missing(selector, *args, &block)
|
||||
if @controller && ActionController::Routing::Routes.named_routes.helpers.include?(selector)
|
||||
@controller.send(selector, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
# Shortcut for <tt>ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)</tt>:
|
||||
# Shortcut for <tt>ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + path, type)</tt>:
|
||||
#
|
||||
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
|
||||
#
|
||||
|
@ -481,11 +509,8 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
|
||||
def fixture_file_upload(path, mime_type = nil, binary = false)
|
||||
ActionController::TestUploadedFile.new(
|
||||
Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
|
||||
mime_type,
|
||||
binary
|
||||
)
|
||||
fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
|
||||
ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
|
||||
end
|
||||
|
||||
# A helper to make it easier to test different route configurations.
|
||||
|
@ -520,12 +545,24 @@ module ActionController #:nodoc:
|
|||
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Test
|
||||
module Unit
|
||||
class TestCase #:nodoc:
|
||||
include ActionController::TestProcess
|
||||
module ProcessWithTest #:nodoc:
|
||||
def self.included(base)
|
||||
base.class_eval { attr_reader :assigns }
|
||||
end
|
||||
|
||||
def process_with_test(*args)
|
||||
process(*args).tap { set_test_assigns }
|
||||
end
|
||||
|
||||
private
|
||||
def set_test_assigns
|
||||
@assigns = {}
|
||||
(instance_variable_names - self.class.protected_instance_variables).each do |var|
|
||||
name, value = var[1..-1], instance_variable_get(var)
|
||||
@assigns[name] = value
|
||||
response.template.assigns[name] = value if response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
44
vendor/rails/actionpack/lib/action_controller/uploaded_file.rb
vendored
Normal file
44
vendor/rails/actionpack/lib/action_controller/uploaded_file.rb
vendored
Normal file
|
@ -0,0 +1,44 @@
|
|||
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
|
155
vendor/rails/actionpack/lib/action_controller/url_encoded_pair_parser.rb
vendored
Normal file
155
vendor/rails/actionpack/lib/action_controller/url_encoded_pair_parser.rb
vendored
Normal file
|
@ -0,0 +1,155 @@
|
|||
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].blank?
|
||||
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
|
|
@ -92,15 +92,12 @@ module ActionController
|
|||
# end
|
||||
# end
|
||||
module UrlWriter
|
||||
# The default options for urls written by this writer. Typically a <tt>:host</tt>
|
||||
# pair is provided.
|
||||
mattr_accessor :default_url_options
|
||||
self.default_url_options = {}
|
||||
|
||||
def self.included(base) #:nodoc:
|
||||
ActionController::Routing::Routes.install_helpers(base)
|
||||
base.mattr_accessor :default_url_options
|
||||
base.default_url_options ||= default_url_options
|
||||
|
||||
# The default options for urls written by this writer. Typically a <tt>:host</tt> pair is provided.
|
||||
base.default_url_options ||= {}
|
||||
end
|
||||
|
||||
# Generate a url based on the options provided, default_url_options and the
|
||||
|
|
16
vendor/rails/actionpack/lib/action_controller/vendor/html-scanner.rb
vendored
Normal file
16
vendor/rails/actionpack/lib/action_controller/vendor/html-scanner.rb
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
$LOAD_PATH << "#{File.dirname(__FILE__)}/html-scanner"
|
||||
|
||||
module HTML
|
||||
autoload :CDATA, 'html/node'
|
||||
autoload :Document, 'html/document'
|
||||
autoload :FullSanitizer, 'html/sanitizer'
|
||||
autoload :LinkSanitizer, 'html/sanitizer'
|
||||
autoload :Node, 'html/node'
|
||||
autoload :Sanitizer, 'html/sanitizer'
|
||||
autoload :Selector, 'html/selector'
|
||||
autoload :Tag, 'html/node'
|
||||
autoload :Text, 'html/node'
|
||||
autoload :Tokenizer, 'html/tokenizer'
|
||||
autoload :Version, 'html/version'
|
||||
autoload :WhiteListSanitizer, 'html/sanitizer'
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue