Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2596

Show
Ignore:
Timestamp:
10/15/05 01:00:25 (3 years ago)
Author:
ulysses
Message:

Fix Request#host_with_port to use the standard port when Rails is behind a proxy.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r2572 r2596  
    11*SVN* 
     2 
     3* Fix Request#host_with_port to use the standard port when Rails is behind a proxy. [Nicholas Seckar] 
    24 
    35* Escape query strings in the href attribute of URLs created by url_helper.  #2333 [Michael Schuerig <michael@schuerig.de>] 
  • trunk/actionpack/lib/action_controller/cgi_process.rb

    r2274 r2596  
    8080 
    8181    def host 
    82       env["HTTP_X_FORWARDED_HOST"] || @cgi.host.to_s.split(":").first || '' 
     82      env["HTTP_X_FORWARDED_HOST"] || ($1 if env['HTTP_HOST'] && /^(.*):\d+$/ =~ env['HTTP_HOST']) || @cgi.host.to_s.split(":").first || '' 
     83    end 
     84     
     85    def port 
     86      env["HTTP_X_FORWARDED_HOST"] ? standard_port : (port_from_http_host || super) 
     87    end 
     88     
     89    def port_from_http_host 
     90      $1.to_i if env['HTTP_HOST'] && /:(\d+)$/ =~ env['HTTP_HOST'] 
    8391    end 
    8492     
  • trunk/actionpack/lib/action_controller/request.rb

    r2270 r2596  
    175175      @port_as_int ||= env['SERVER_PORT'].to_i 
    176176    end 
     177     
     178    # Returns the standard port number for this request's protocol 
     179    def standard_port 
     180      case protocol 
     181        when 'https://' then 443 
     182        else 80 
     183      end 
     184    end 
    177185 
    178186    # Returns a port suffix like ":8080" if the port number of this request 
    179187    # is not the default HTTP port 80 or HTTPS port 443. 
    180188    def port_string 
    181       (protocol == 'http://' && port == 80) || (protocol == 'https://' && port == 443) ? '' : ":#{port}" 
     189      (port == standard_port) ? '' : ":#{port}" 
    182190    end 
    183191 
     
    185193    # example.com:8080. 
    186194    def host_with_port 
    187       env['HTTP_HOST'] || host + port_string 
     195      host + port_string 
    188196    end 
    189197   
  • trunk/actionpack/test/controller/cgi_test.rb

    r2389 r2596  
    11require File.dirname(__FILE__) + '/../abstract_unit' 
     2require 'action_controller/cgi_process' 
     3require 'action_controller/cgi_ext/cgi_ext' 
     4 
     5 
     6require 'stringio' 
    27 
    38class CGITest < Test::Unit::TestCase 
     
    304309    end 
    305310end 
     311 
     312 
     313class CGIRequestTest < Test::Unit::TestCase 
     314  def setup 
     315    @request_hash = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"} 
     316    @fake_cgi = Struct.new(:env_table).new(@request_hash) 
     317    @request = ActionController::CgiRequest.new(@fake_cgi) 
     318  end 
     319   
     320  def test_proxy_request 
     321    assert_equal 'glu.ttono.us', @request.host_with_port 
     322  end 
     323   
     324  def test_http_host 
     325    @request_hash.delete "HTTP_X_FORWARDED_HOST" 
     326    @request_hash['HTTP_HOST'] = "rubyonrails.org:8080" 
     327    assert_equal "rubyonrails.org:8080", @request.host_with_port 
     328  end 
     329   
     330end 
  • trunk/actionpack/test/controller/request_test.rb

    r2270 r2596  
    202202 
    203203  def test_host_with_port 
    204     @request.env['HTTP_HOST'] = "rubyonrails.org:8080" 
    205     assert_equal "rubyonrails.org:8080", @request.host_with_port 
    206     @request.env['HTTP_HOST'] = nil 
    207      
    208204    @request.host = "rubyonrails.org" 
    209205    @request.port = 80