Skip to content

Commit

Permalink
Memoize request accessors on the Rack env so other request objects ha…
Browse files Browse the repository at this point in the history
…ve access to the same cache [#1668 state:resolved]
  • Loading branch information
josh committed Jan 4, 2009
1 parent ed2e776 commit f00e86d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -384,8 +384,8 @@ class Base
class << self
def call(env)
# HACK: For global rescue to have access to the original request and response
request = env["actioncontroller.rescue.request"] ||= Request.new(env)
response = env["actioncontroller.rescue.response"] ||= Response.new
request = env["action_controller.rescue.request"] ||= Request.new(env)
response = env["action_controller.rescue.response"] ||= Response.new
process(request, response)
end

Expand Down
20 changes: 8 additions & 12 deletions actionpack/lib/action_controller/request.rb
Expand Up @@ -19,6 +19,7 @@ class SessionFixationAttempt < StandardError #:nodoc:

def initialize(env)
@env = env
@parser = ActionController::RequestParser.new(env)
end

%w[ AUTH_TYPE GATEWAY_INTERFACE PATH_INFO
Expand Down Expand Up @@ -92,16 +93,15 @@ def headers

# Returns the content length of the request as an integer.
def content_length
@env['CONTENT_LENGTH'].to_i
@env["action_controller.request.content_length"] ||= @env['CONTENT_LENGTH'].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(parser.content_type_without_parameters)
Mime::Type.lookup(@parser.content_type_without_parameters)
end
memoize :content_type

Expand Down Expand Up @@ -389,7 +389,7 @@ def path
# Read the request \body. This is useful for web services that need to
# work with raw requests directly.
def raw_post
parser.raw_post
@parser.raw_post
end

# Returns both GET and POST \parameters in a single hash.
Expand Down Expand Up @@ -418,7 +418,7 @@ def path_parameters
end

def body
parser.body
@parser.body
end

def remote_addr
Expand All @@ -431,11 +431,11 @@ def referrer
alias referer referrer

def query_parameters
@query_parameters ||= parser.query_parameters
@parser.query_parameters
end

def request_parameters
@request_parameters ||= parser.request_parameters
@parser.request_parameters
end

def body_stream #:nodoc:
Expand All @@ -451,7 +451,7 @@ def session
end

def session=(session) #:nodoc:
@session = session
@env['rack.session'] = session
end

def reset_session
Expand All @@ -474,9 +474,5 @@ def server_port
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end

def parser
@parser ||= ActionController::RequestParser.new(@env)
end
end
end
7 changes: 4 additions & 3 deletions actionpack/lib/action_controller/request_parser.rb
Expand Up @@ -2,14 +2,15 @@ module ActionController
class RequestParser
def initialize(env)
@env = env
freeze
end

def request_parameters
@request_parameters ||= parse_formatted_request_parameters
@env["action_controller.request_parser.request_parameters"] ||= parse_formatted_request_parameters
end

def query_parameters
@query_parameters ||= self.class.parse_query_parameters(query_string)
@env["action_controller.request_parser.query_parameters"] ||= self.class.parse_query_parameters(query_string)
end

# Returns the query string, accounting for server idiosyncrasies.
Expand Down Expand Up @@ -90,7 +91,7 @@ def parse_formatted_request_parameters
end

def content_length
@content_length ||= @env['CONTENT_LENGTH'].to_i
@env["action_controller.request.content_length"] ||= @env['CONTENT_LENGTH'].to_i
end

# The raw content type string. Use when you need parameters such as
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/rescue.rb
Expand Up @@ -60,8 +60,8 @@ def self.included(base) #:nodoc:

module ClassMethods
def call_with_exception(env, exception) #:nodoc:
request = env["actioncontroller.rescue.request"] ||= Request.new(env)
response = env["actioncontroller.rescue.response"] ||= Response.new
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
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/request_test.rb
Expand Up @@ -391,8 +391,8 @@ def test_user_agent
end

def test_parameters
@request.instance_eval { @request_parameters = { "foo" => 1 } }
@request.instance_eval { @query_parameters = { "bar" => 2 } }
@request.stubs(:request_parameters).returns({ "foo" => 1 })
@request.stubs(:query_parameters).returns({ "bar" => 2 })

assert_equal({"foo" => 1, "bar" => 2}, @request.parameters)
assert_equal({"foo" => 1}, @request.request_parameters)
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/rescue_test.rb
Expand Up @@ -383,8 +383,8 @@ def test_block_rescue_handler_with_argument_as_string

def test_rescue_dispatcher_exceptions
env = @request.env
env["actioncontroller.rescue.request"] = @request
env["actioncontroller.rescue.response"] = @response
env["action_controller.rescue.request"] = @request
env["action_controller.rescue.response"] = @response

RescueController.call_with_exception(env, ActionController::RoutingError.new("Route not found"))
assert_equal "no way", @response.body
Expand Down

0 comments on commit f00e86d

Please sign in to comment.