From 76540822609415dc5cfa8ea31bfafea602373a27 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 9 Feb 2006 20:05:11 +0000 Subject: Major components cleanup and speedup. Closes #3527. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3563 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/request.rb | 51 +++++++++++++++-------------- 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'actionpack/lib/action_controller/request.rb') diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 8b5c480626..0daa229448 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -3,14 +3,18 @@ module ActionController class AbstractRequest cattr_accessor :relative_url_root + # Returns the hash of environment variables for this request, + # such as { 'RAILS_ENV' => 'production' }. + attr_reader :env + # Returns both GET and POST parameters in a single hash. def parameters - @parameters ||= request_parameters.merge(query_parameters).merge(path_parameters).with_indifferent_access + @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access end # Returns the HTTP request method as a lowercase symbol (:get, for example) def method - env['REQUEST_METHOD'].downcase.to_sym + @request_method ||= @env['REQUEST_METHOD'].downcase.to_sym end # Is this a GET request? Equivalent to request.method == :get @@ -52,10 +56,10 @@ module ActionController # X-Post-Data-Format HTTP header if present. def post_format @post_format ||= - if env['HTTP_X_POST_DATA_FORMAT'] - env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym + if @env['HTTP_X_POST_DATA_FORMAT'] + @env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym else - case env['CONTENT_TYPE'].to_s.downcase + case @env['CONTENT_TYPE'].to_s.downcase when 'application/xml', 'text/xml' then :xml when 'application/x-yaml', 'text/x-yaml' then :yaml else :url_encoded @@ -82,7 +86,7 @@ module ActionController # "XMLHttpRequest". (The Prototype Javascript library sends this header with # every Ajax request.) def xml_http_request? - not /XMLHttpRequest/i.match(env['HTTP_X_REQUESTED_WITH']).nil? + not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? end alias xhr? :xml_http_request? @@ -93,17 +97,17 @@ module ActionController # delimited list in the case of multiple chained proxies; the first is # the originating IP. def remote_ip - return env['HTTP_CLIENT_IP'] if env.include? 'HTTP_CLIENT_IP' + return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' - if env.include? 'HTTP_X_FORWARDED_FOR' then - remote_ips = env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| + if @env.include? 'HTTP_X_FORWARDED_FOR' then + remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i end return remote_ips.first.strip unless remote_ips.empty? end - env['REMOTE_ADDR'] + @env['REMOTE_ADDR'] end # Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify @@ -127,19 +131,19 @@ module ActionController # This is useful for services such as REST, XMLRPC and SOAP # which communicate over HTTP POST but don't use the traditional parameter format. def raw_post - env['RAW_POST_DATA'] + @env['RAW_POST_DATA'] end # Returns the request URI correctly, taking into account the idiosyncracies # of the various servers. def request_uri - if uri = env['REQUEST_URI'] + if uri = @env['REQUEST_URI'] (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri. else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME - script_filename = env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) - uri = env['PATH_INFO'] + script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) + uri = @env['PATH_INFO'] uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil? - unless (env_qs = env['QUERY_STRING']).nil? || env_qs.empty? + unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? uri << '?' << env_qs end uri @@ -153,7 +157,7 @@ module ActionController # Is this an SSL request? def ssl? - env['HTTPS'] == 'on' || env['HTTP_X_FORWARDED_PROTO'] == 'https' + @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' end # Returns the interpreted path to requested resource after all the installation directory of this application was taken into account @@ -169,13 +173,13 @@ module ActionController # Returns the path minus the web server relative installation directory. # This method returns nil unless the web server is apache. def relative_url_root - @@relative_url_root ||= server_software == 'apache' ? env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') : '' + @@relative_url_root ||= server_software == 'apache' ? @env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') : '' end # Returns the port number of this request as an integer. def port - @port_as_int ||= env['SERVER_PORT'].to_i + @port_as_int ||= @env['SERVER_PORT'].to_i end # Returns the standard port number for this request's protocol @@ -213,7 +217,7 @@ module ActionController # Returns the lowercase name of the HTTP server software. def server_software - (env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ env['SERVER_SOFTWARE']) ? $1.downcase : nil + (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil end #-- @@ -225,11 +229,6 @@ module ActionController def request_parameters #:nodoc: end - # Returns the hash of environment variables for this request, - # such as { 'RAILS_ENV' => 'production' }. - def env - end - # Returns the host for this request, such as example.com. def host end @@ -240,6 +239,10 @@ module ActionController def session #:nodoc: end + def session=(session) #:nodoc: + @session = session + end + def reset_session #:nodoc: end end -- cgit v1.2.3