Module: Nitro::Request
- Included in:
- Context
- Defined in:
- lib/nitro/cgi/request.rb,
lib/nitro/cgi/utils.rb,
lib/nitro/test/context.rb
Overview
Override the default Request implementation to include methods useful for testing.
Instance Attribute Summary collapse
-
#cookies ⇒ Object
(also: #cookie)
The request cookies.
-
#headers ⇒ Object
(also: #env, #env_table)
The request headers collection.
-
#in ⇒ Object
The request input stream.
-
#params ⇒ Object
(also: #query, #parameters)
The parsed query parameters collection.
Instance Method Summary collapse
-
#[](param) ⇒ Object
Lookup a query parameter.
-
#[]=(param, value) ⇒ Object
Set a query parameter.
-
#action_params ⇒ Object
The parameters the action will be called with.
-
#content_length ⇒ Object
The content_length.
-
#domain(tld_length = 1) ⇒ Object
Returns the domain part of a host.
-
#false?(param) ⇒ Boolean
Check if a boolean param (checkbox) is false.
-
#fetch(param, default = nil) ⇒ Object
Fetch a parameter with default value.
-
#formatted_post? ⇒ Boolean
Is this a POST request formatted as XML or YAML?.
- #from_gecko? ⇒ Boolean (also: #from_mozilla?)
- #from_ie? ⇒ Boolean (also: #from_msie?)
- #from_khtml? ⇒ Boolean (also: #from_safari?)
- #from_mac? ⇒ Boolean
- #from_opera? ⇒ Boolean
- #from_os9? ⇒ Boolean
- #from_osx? ⇒ Boolean
- #from_unix? ⇒ Boolean (also: #from_linux?)
- #from_w3c? ⇒ Boolean
- #from_windows? ⇒ Boolean
-
#has_key?(key) ⇒ Boolean
(also: #has_param?, #param?, #has?, #is?)
Check if a param is available.
-
#host ⇒ Object
The server host name.
-
#host_url ⇒ Object
(also: #server_url)
The host url.
-
#local?(ip = remote_ip) ⇒ Boolean
Request comming from local?.
-
#local_net?(ip = remote_ip) ⇒ Boolean
Request is from a local network? (RFC1918 + localhost).
-
#method ⇒ Object
The request method.
-
#path ⇒ Object
The path is the uri without the query string.
- #path_info ⇒ Object
-
#port ⇒ Object
The server port.
-
#post_format ⇒ Object
Determine whether the body of a POST request is URL-encoded (default), XML, or YAML by checking the Content-Type HTTP header:.
-
#protocol ⇒ Object
The request protocol.
-
#query_string ⇒ Object
The request query string.
-
#raw_body ⇒ Object
The raw data of the request.
-
#referer ⇒ Object
(also: #referrer)
Return the referer.
-
#remote_ip ⇒ Object
The remote IP address.
-
#ssl? ⇒ Boolean
Is this an ssl request?.
-
#subdomains(tld_length = 1) ⇒ Object
Returns all the subdomains as an array.
-
#true?(param) ⇒ Boolean
(also: #enabled?, #boolean)
Check if a boolean param (checkbox) is true.
-
#uri ⇒ Object
The request uri.
-
#user_agent ⇒ Object
Different servers hold user agent in differnet strings (unify this).
-
#xml_http_request? ⇒ Boolean
(also: #xhr?, #script?)
Is this an XhtmlRpcRequest? Returns true if the request’s ‘X-Requested-With’ header contains ‘XMLHttpRequest’.
-
#xml_post? ⇒ Boolean
Is this a POST request formatted as XML?.
-
#yaml_post? ⇒ Boolean
Is this a POST request formatted as YAML?.
Instance Attribute Details
#cookies ⇒ Object Also known as:
The request cookies.
29 30 31 |
# File 'lib/nitro/cgi/request.rb', line 29 def @cookies end |
#headers ⇒ Object Also known as: env, env_table
The request headers collection. Also called the request environment (env).
16 17 18 |
# File 'lib/nitro/cgi/request.rb', line 16 def headers @headers end |
#in ⇒ Object
The request input stream.
11 12 13 |
# File 'lib/nitro/cgi/request.rb', line 11 def in @in end |
#params ⇒ Object Also known as: query, parameters
The parsed query parameters collection.
23 24 25 |
# File 'lib/nitro/cgi/request.rb', line 23 def params @params end |
Instance Method Details
#[](param) ⇒ Object
Lookup a query parameter. – TODO: Check if unescape is needed. ++
280 281 282 283 284 285 286 287 |
# File 'lib/nitro/cgi/request.rb', line 280 def [](param) @params[param] =begin if par = @params[param] and par.is_a?(String) CGI.unescape(par) end =end end |
#[]=(param, value) ⇒ Object
Set a query parameter.
291 292 293 |
# File 'lib/nitro/cgi/request.rb', line 291 def []=(param, value) @params[param] = value end |
#action_params ⇒ Object
The parameters the action will be called with
101 102 103 |
# File 'lib/nitro/cgi/request.rb', line 101 def action_params headers['ACTION_PARAMS'] end |
#content_length ⇒ Object
The content_length
190 191 192 |
# File 'lib/nitro/cgi/request.rb', line 190 def content_length return @headers['CONTENT_LENGTH'].to_i end |
#domain(tld_length = 1) ⇒ Object
Returns the domain part of a host.
Examples
www.nitroproject.org: request.domain # => ‘nitroproject.org’ www.nitroproject.co.uk: request.domain(2) # => ‘nitroproject.co.uk’
68 69 70 |
# File 'lib/nitro/cgi/request.rb', line 68 def domain tld_length = 1 host.split('.').last(1 + tld_length).join('.') end |
#false?(param) ⇒ Boolean
Check if a boolean param (checkbox) is false.
305 306 307 |
# File 'lib/nitro/cgi/request.rb', line 305 def false?(param) !true?(param) end |
#fetch(param, default = nil) ⇒ Object
Fetch a parameter with default value.
311 312 313 |
# File 'lib/nitro/cgi/request.rb', line 311 def fetch param, default = nil @params.fetch(param, default) end |
#formatted_post? ⇒ Boolean
Is this a POST request formatted as XML or YAML?
153 154 155 |
# File 'lib/nitro/cgi/request.rb', line 153 def formatted_post? post? && (post_format == :xml || post_format == :yaml) end |
#from_gecko? ⇒ Boolean Also known as: from_mozilla?
17 18 19 |
# File 'lib/nitro/cgi/utils.rb', line 17 def from_gecko? user_agent =~ /Gecko/ end |
#from_ie? ⇒ Boolean Also known as: from_msie?
22 23 24 |
# File 'lib/nitro/cgi/utils.rb', line 22 def from_ie? user_agent =~ /MSIE/ end |
#from_khtml? ⇒ Boolean Also known as: from_safari?
31 32 33 34 |
# File 'lib/nitro/cgi/utils.rb', line 31 def from_khtml? puts "agent = #{user_agent}" user_agent =~ /KHTML/ end |
#from_mac? ⇒ Boolean
48 49 50 |
# File 'lib/nitro/cgi/utils.rb', line 48 def from_mac? from_osx? or from_os9? end |
#from_opera? ⇒ Boolean
27 28 29 |
# File 'lib/nitro/cgi/utils.rb', line 27 def from_opera? user_agent =~ /Opera/ end |
#from_os9? ⇒ Boolean
45 46 |
# File 'lib/nitro/cgi/utils.rb', line 45 def from_os9? end |
#from_osx? ⇒ Boolean
41 42 43 |
# File 'lib/nitro/cgi/utils.rb', line 41 def from_osx? user_agent =~ /Mac OS X/ or user_agent =~ /Mac_PowerPC/ end |
#from_unix? ⇒ Boolean Also known as: from_linux?
52 53 54 |
# File 'lib/nitro/cgi/utils.rb', line 52 def from_unix? user_agent =~ /linux/ or user_agent =~ /FreeBSD/ end |
#from_w3c? ⇒ Boolean
37 38 39 |
# File 'lib/nitro/cgi/utils.rb', line 37 def from_w3c? from_gecko? or from_khtml? or from_opera? end |
#from_windows? ⇒ Boolean
57 58 59 |
# File 'lib/nitro/cgi/utils.rb', line 57 def from_windows? user_agent =~ /Windows/ end |
#has_key?(key) ⇒ Boolean Also known as: has_param?, param?, has?, is?
Check if a param is available. – gmosx: use nil test instead of has_key? to be more robust. (nil can be a hash element !) ++
321 322 323 |
# File 'lib/nitro/cgi/request.rb', line 321 def has_key? key !@params[key].nil? end |
#host ⇒ Object
The server host name. Also handles proxy forwarding.
249 250 251 |
# File 'lib/nitro/cgi/request.rb', line 249 def host @headers['HTTP_X_FORWARDED_HOST'] || @headers['HTTP_HOST'] end |
#host_url ⇒ Object Also known as: server_url
The host url.
255 256 257 |
# File 'lib/nitro/cgi/request.rb', line 255 def host_url "#{protocol}#{host}" end |
#local?(ip = remote_ip) ⇒ Boolean
Request comming from local?
235 236 237 238 |
# File 'lib/nitro/cgi/request.rb', line 235 def local?(ip = remote_ip) # TODO: should check if requesting machine is the one the server is running return true if ip == '127.0.0.1' end |
#local_net?(ip = remote_ip) ⇒ Boolean
Request is from a local network? (RFC1918 + localhost)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/nitro/cgi/request.rb', line 218 def local_net?(ip = remote_ip) bip = ip.split('.').map{ |x| x.to_i }.pack('C4').unpack('N')[0] # 127.0.0.1/32 => 2130706433 # 192.168.0.0/16 => 49320 # 172.16.0.0/12 => 2753 # 10.0.0.0/8 => 10 { 0 => 2130706433, 16 => 49320, 20 => 2753, 24 => 10}.each do |s,c| return true if (bip >> s) == c end return false end |
#method ⇒ Object
The request method. Alternatively you could use the request method predicates.
Examples
if request.method == :get if request.get?
113 114 115 |
# File 'lib/nitro/cgi/request.rb', line 113 def method @headers['REQUEST_METHOD'].downcase.to_sym end |
#path ⇒ Object
The path is the uri without the query string.
53 54 55 |
# File 'lib/nitro/cgi/request.rb', line 53 def path uri ? uri.split('?').first : '' end |
#path_info ⇒ Object
57 58 59 |
# File 'lib/nitro/cgi/request.rb', line 57 def path_info @headers['PATH_INFO'] end |
#port ⇒ Object
The server port.
242 243 244 |
# File 'lib/nitro/cgi/request.rb', line 242 def port @headers['SERVER_PORT'].to_i end |
#post_format ⇒ Object
Determine whether the body of a POST request is URL-encoded (default), XML, or YAML by checking the Content-Type HTTP header:
Content-Type Post Format
application/xml :xml
text/xml :xml
application/x-yaml :yaml
text/x-yaml :yaml
* :url_encoded
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/nitro/cgi/request.rb', line 139 def post_format @post_format ||= if @headers['HTTP_X_POST_DATA_FORMAT'] @headers['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym else case @headers['CONTENT_TYPE'].to_s.downcase when 'application/xml', 'text/xml' then :xml when 'application/x-yaml', 'text/x-yaml' then :yaml else :url_encoded end end end |
#protocol ⇒ Object
The request protocol.
34 35 36 |
# File 'lib/nitro/cgi/request.rb', line 34 def protocol @headers['HTTPS'] == 'on' ? 'https://' : 'http://' end |
#query_string ⇒ Object
The request query string. – gmosx, FIXME: handles some fcgi problems. ++
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/nitro/cgi/request.rb', line 88 def query_string =begin qs = headers['QUERY_STRING'] if qs.empty? and uri =~ /\?/ qs = headers['QUERY_STRING'] = uri.split('?').last end return qs =end headers['QUERY_STRING'] end |
#raw_body ⇒ Object
The raw data of the request. Useful to implement Webservices. – FIXME: better name and implementation. ++
266 267 268 269 270 271 272 273 |
# File 'lib/nitro/cgi/request.rb', line 266 def raw_body unless @raw_body @in.rewind @raw_body = @in.read(content_length) end @raw_body end |
#referer ⇒ Object Also known as: referrer
Return the referer. For the initial page in the clickstream there is no referer, set “/” by default.
183 184 185 |
# File 'lib/nitro/cgi/request.rb', line 183 def referer return @headers['HTTP_REFERER'] || '/' end |
#remote_ip ⇒ Object
The remote IP address. REMOTE_ADDR is the standard but will fail if the user is behind a proxy.
HTTP_CLIENT_IP and/or HTTP_X_FORWARDED_FOR are set by proxies so check for these before falling back to REMOTE_ADDR. HTTP_X_FORWARDED_FOR may be a comma-delimited list in the case of multiple chained proxies; the first is the originating IP.
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/nitro/cgi/request.rb', line 202 def remote_ip return @headers['HTTP_CLIENT_IP'] if @headers.include?('HTTP_CLIENT_IP') if @headers.include?('HTTP_X_FORWARDED_FOR') then remote_ips = @headers['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| ip =~ /^unknown$/i or local_net?(ip) end return remote_ips.first.strip unless remote_ips.empty? end return @headers['REMOTE_ADDR'] end |
#ssl? ⇒ Boolean
Is this an ssl request?
40 41 42 43 |
# File 'lib/nitro/cgi/request.rb', line 40 def ssl? # 443 == port @headers['HTTPS'] == 'on' end |
#subdomains(tld_length = 1) ⇒ Object
Returns all the subdomains as an array.
Examples
my.name.nitroproject.org: request.subdomains # => [‘my’, ‘name’]
78 79 80 81 |
# File 'lib/nitro/cgi/request.rb', line 78 def subdomains tld_length = 1 parts = host.split('.') parts[0..-(tld_length+2)] end |
#true?(param) ⇒ Boolean Also known as: enabled?, boolean
Check if a boolean param (checkbox) is true.
297 298 299 |
# File 'lib/nitro/cgi/request.rb', line 297 def true?(param) @params[param] == 'on' end |
#uri ⇒ Object
The request uri.
47 48 49 |
# File 'lib/nitro/cgi/request.rb', line 47 def uri @headers['REQUEST_URI'] end |
#user_agent ⇒ Object
Different servers hold user agent in differnet strings (unify this).
13 14 15 |
# File 'lib/nitro/cgi/utils.rb', line 13 def user_agent headers['HTTP_USER_AGENT'] || headers['USER-AGENT'] end |
#xml_http_request? ⇒ Boolean Also known as: xhr?, script?
Is this an XhtmlRpcRequest? Returns true if the request’s ‘X-Requested-With’ header contains ‘XMLHttpRequest’. Compatible with the Prototype Javascript library.
174 175 176 |
# File 'lib/nitro/cgi/request.rb', line 174 def xml_http_request? not /XMLHttpRequest/i.match(@headers['HTTP_X_REQUESTED_WITH']).nil? end |
#xml_post? ⇒ Boolean
Is this a POST request formatted as XML?
159 160 161 |
# File 'lib/nitro/cgi/request.rb', line 159 def xml_post? post? && post_format == :xml end |
#yaml_post? ⇒ Boolean
Is this a POST request formatted as YAML?
165 166 167 |
# File 'lib/nitro/cgi/request.rb', line 165 def yaml_post? post? && post_format == :yaml end |