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

Changeset 6316

Show
Ignore:
Timestamp:
03/04/07 20:32:38 (2 years ago)
Author:
david
Message:

Added Request#url that returns the complete URL used for the request [DHH]

Files:

Legend:

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

    r6306 r6316  
    11*SVN* 
     2 
     3* Added Request#url that returns the complete URL used for the request [DHH] 
    24 
    35* Extract dynamic scaffolding into a plugin.  #7700 [Josh Peek] 
  • trunk/actionpack/lib/action_controller/request.rb

    r6158 r6316  
    99    # such as { 'RAILS_ENV' => 'production' }. 
    1010    attr_reader :env 
    11  
    12     # Returns both GET and POST parameters in a single hash. 
    13     def parameters 
    14       @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access 
    15     end 
    1611 
    1712    # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get 
     
    127122    end 
    128123 
     124    # Returns the lowercase name of the HTTP server software. 
     125    def server_software 
     126      (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 
     127    end 
     128 
     129 
     130    # Returns the complete URL used for this request 
     131    def url 
     132      request.protocol + request.host_with_port + request.request_uri 
     133    end 
     134 
     135    # Return 'https://' if this is an SSL request and 'http://' otherwise. 
     136    def protocol 
     137      ssl? ? 'https://' : 'http://' 
     138    end 
     139 
     140    # Is this an SSL request? 
     141    def ssl? 
     142      @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 
     143    end 
     144 
     145    # Returns the host for this request, such as example.com. 
     146    def host 
     147    end 
     148 
     149    # Returns a host:port string for this request, such as example.com or 
     150    # example.com:8080. 
     151    def host_with_port 
     152      host + port_string 
     153    end 
     154 
     155    # Returns the port number of this request as an integer. 
     156    def port 
     157      @port_as_int ||= @env['SERVER_PORT'].to_i 
     158    end 
     159 
     160    # Returns the standard port number for this request's protocol 
     161    def standard_port 
     162      case protocol 
     163        when 'https://' then 443 
     164        else 80 
     165      end 
     166    end 
     167 
     168    # Returns a port suffix like ":8080" if the port number of this request 
     169    # is not the default HTTP port 80 or HTTPS port 443. 
     170    def port_string 
     171      (port == standard_port) ? '' : ":#{port}" 
     172    end 
     173 
    129174    # Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify 
    130175    # a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk". 
     
    142187      parts = host.split('.') 
    143188      parts[0..-(tld_length+2)] 
    144     end 
    145  
    146     # Receive the raw post data. 
    147     # This is useful for services such as REST, XMLRPC and SOAP 
    148     # which communicate over HTTP POST but don't use the traditional parameter format. 
    149     def raw_post 
    150       @env['RAW_POST_DATA'] 
    151189    end 
    152190 
     
    169207    end 
    170208 
    171     # Return 'https://' if this is an SSL request and 'http://' otherwise. 
    172     def protocol 
    173       ssl? ? 'https://' : 'http://' 
    174     end 
    175  
    176     # Is this an SSL request? 
    177     def ssl? 
    178       @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' 
    179     end 
    180  
    181209    # Returns the interpreted path to requested resource after all the installation directory of this application was taken into account 
    182210    def path 
     
    203231    end 
    204232 
    205     # Returns the port number of this request as an integer. 
    206     def port 
    207       @port_as_int ||= @env['SERVER_PORT'].to_i 
    208     end 
    209  
    210     # Returns the standard port number for this request's protocol 
    211     def standard_port 
    212       case protocol 
    213         when 'https://' then 443 
    214         else 80 
    215       end 
    216     end 
    217  
    218     # Returns a port suffix like ":8080" if the port number of this request 
    219     # is not the default HTTP port 80 or HTTPS port 443. 
    220     def port_string 
    221       (port == standard_port) ? '' : ":#{port}" 
    222     end 
    223  
    224     # Returns a host:port string for this request, such as example.com or 
    225     # example.com:8080. 
    226     def host_with_port 
    227       host + port_string 
     233 
     234    # Receive the raw post data. 
     235    # This is useful for services such as REST, XMLRPC and SOAP 
     236    # which communicate over HTTP POST but don't use the traditional parameter format. 
     237    def raw_post 
     238      @env['RAW_POST_DATA'] 
     239    end 
     240 
     241    # Returns both GET and POST parameters in a single hash. 
     242    def parameters 
     243      @parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access 
    228244    end 
    229245 
     
    247263    end 
    248264 
    249     # Returns the lowercase name of the HTTP server software. 
    250     def server_software 
    251       (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil 
    252     end 
    253265 
    254266    #-- 
     
    261273    end 
    262274 
    263     # Returns the host for this request, such as example.com. 
    264     def host 
    265     end 
    266  
    267275    def cookies #:nodoc: 
    268276    end