Override the default Request implementation to include methods useful for testing.

Methods
Attributes
[RW] cookies The request cookies.
[RW] get_params
[RW] headers The request headers collection. Also called the request environment (env).
[RW] in The request input stream.
[RW] post_params
Public Instance methods
[](param)

Lookup a query parameter.

     # File lib/raw/context/request.rb, line 288
288:   def [](param)
289:     params[param.to_s]
290:   end
[]=(param, value)

Set a query parameter.

     # File lib/raw/context/request.rb, line 294
294:   def []=(param, value)
295:     params[param.to_s] = value
296:   end
boolean(param)

Alias for #true?

content_length()
     # File lib/raw/context/request.rb, line 189
189:   def content_length
190:     @headers["CONTENT_LENGTH"].to_i
191:   end
domain(tld_length = 1)

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‘

    # File lib/raw/context/request.rb, line 83
83:   def domain tld_length = 1
84:     host.split('.').last(1 + tld_length).join('.')
85:   end
enabled?(param)

Alias for #true?

false?(param)

Check if a boolean param (checkbox) is false.

     # File lib/raw/context/request.rb, line 308
308:   def false?(param)
309:     !true?(param)
310:   end
fetch(param, default = nil)

Fetch a parameter with default value.

     # File lib/raw/context/request.rb, line 314
314:   def fetch param, default = nil
315:     params.fetch(param, default)
316:   end
formatted_post?()

Is this a POST request formatted as XML or YAML?

     # File lib/raw/context/request.rb, line 152
152:   def formatted_post?
153:     post? && (post_format == :xml || post_format == :yaml)
154:   end
has?(key)

Alias for #has_key?

has_key?(key)

Check if a param is available.

This method is also aliased as has_param? param? has? is?
     # File lib/raw/context/request.rb, line 324
324:   def has_key?(key)
325:     params.keys.include?(key)
326:   end
has_param?(key)

Alias for #has_key?

host()

The server host name. Also handles proxy forwarding.

     # File lib/raw/context/request.rb, line 248
248:   def host
249:     @headers['HTTP_X_FORWARDED_HOST'] || @headers['HTTP_HOST'] 
250:   end
host_uri()

The host uri.

This method is also aliased as server_uri host_url
     # File lib/raw/context/request.rb, line 254
254:   def host_uri
255:     "#{protocol}#{host}"
256:   end
host_url()

Alias for #host_uri

is?(key)

Alias for #has_key?

keys()
     # File lib/raw/context/request.rb, line 332
332:   def keys
333:     params.keys
334:   end
local?(ip = remote_ip)

Request comming from local?

     # File lib/raw/context/request.rb, line 234
234:   def local?(ip = remote_ip)
235:     # TODO: should check if requesting machine is the one the server is running
236:     return true if ip == '127.0.0.1'
237:   end
local_net?(ip = remote_ip)

Request is from a local network? (RFC1918 + localhost)

     # File lib/raw/context/request.rb, line 217
217:   def local_net?(ip = remote_ip)
218:     bip = ip.split('.').map{ |x| x.to_i }.pack('C4').unpack('N')[0]
219: 
220:     # 127.0.0.1/32    => 2130706433
221:     # 192.168.0.0/16  => 49320
222:     # 172.16.0.0/12   => 2753
223:     # 10.0.0.0/8      => 10
224: 
225:     { 0 => 2130706433, 16 => 49320, 20 => 2753, 24 => 10}.each do |s,c|
226:        return true if (bip >> s) == c
227:     end
228:     
229:     return false
230:   end
method()

The request method. Alternatively you could use the request method predicates.

Examples

if request.method == :get if request.get?

     # File lib/raw/context/request.rb, line 112
112:   def method
113:     @headers["REQUEST_METHOD"].downcase.to_sym
114:   end
param?(key)

Alias for #has_key?

parameters()

Alias for #params

params()

The parsed query parameters collection.

This method is also aliased as query parameters
    # File lib/raw/context/request.rb, line 26
26:   def params
27:           if method == :post
28:                   @post_params
29:           else
30:                   @get_params
31:           end
32:   end
params=(pa)
    # File lib/raw/context/request.rb, line 34
34:   def params=(pa)
35:           if method == :post
36:                   @post_params = pa
37:           else
38:                   @get_params = pa
39:           end
40:   end
path()

Alias for #path_info

path_info()

The path info. Typically this is the rewritten uri without the query string.

This method is also aliased as path
    # File lib/raw/context/request.rb, line 71
71:   def path_info
72:     @headers["PATH_INFO"]
73:   end
port()

The server port.

     # File lib/raw/context/request.rb, line 241
241:   def port
242:     @headers['SERVER_PORT'].to_i
243:   end
post_format()

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
     # File lib/raw/context/request.rb, line 138
138:   def post_format
139:     @post_format ||= if @headers['HTTP_X_POST_DATA_FORMAT']
140:       @headers['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym
141:     else
142:       case @headers['CONTENT_TYPE'].to_s.downcase
143:       when 'application/xml', 'text/xml' then :xml
144:       when 'application/x-yaml', 'text/x-yaml' then :yaml
145:       else :url_encoded
146:       end
147:     end
148:   end
protocol()

The request protocol.

    # File lib/raw/context/request.rb, line 52
52:   def protocol
53:     @headers['HTTPS'] == "on" ? "https://" : "http://"
54:   end
query()

Alias for #params

query_string()

The request query string.

     # File lib/raw/context/request.rb, line 100
100:   def query_string 
101:     headers["QUERY_STRING"]
102:   end
raw_body()

The raw data of the request. Useful to implement Webservices.

     # File lib/raw/context/request.rb, line 274
274:   def raw_body
275:     unless @raw_body
276:       @in.rewind
277:       @raw_body = @in.read(content_length)
278:     end
279: 
280:     @raw_body
281:   end
referer()

Return the referer. For the initial page in the clickstream there is no referer, set "/" by default.

This method is also aliased as referrer
     # File lib/raw/context/request.rb, line 182
182:   def referer
183:     @headers["HTTP_REFERER"] || "/"
184:   end
referrer()

Alias for #referer

remote_ip()

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.

     # File lib/raw/context/request.rb, line 201
201:   def remote_ip
202:     return @headers['HTTP_CLIENT_IP'] if @headers.include?('HTTP_CLIENT_IP')
203: 
204:     if @headers.include?('HTTP_X_FORWARDED_FOR') then
205:       remote_ips = @headers['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip|
206:         ip =~ /^unknown$/i or local_net?(ip)
207:       end
208: 
209:       return remote_ips.first.strip unless remote_ips.empty?
210:     end
211: 
212:     return @headers['REMOTE_ADDR']
213:   end
script?()

Alias for #xml_http_request?

server_uri()

Alias for #host_uri

ssl?()

Is this an ssl request?

    # File lib/raw/context/request.rb, line 58
58:   def ssl?
59:     @headers["HTTPS"] == "on"
60:   end
subdomains(tld_length = 1)

Returns all the subdomains as an array.

Examples

my.name.nitroproject.org: request.subdomains # => [‘my’, ‘name’]

    # File lib/raw/context/request.rb, line 93
93:   def subdomains tld_length = 1 
94:     parts = host.split('.')
95:     parts[0..-(tld_length+2)]
96:   end
true?(param)

Check if a boolean param (checkbox) is true.

This method is also aliased as enabled? boolean
     # File lib/raw/context/request.rb, line 300
300:   def true?(param)
301:     params[param] == 'on'
302:   end
uri()

The request uri.

    # File lib/raw/context/request.rb, line 64
64:   def uri
65:     @headers["REQUEST_URI"]
66:   end
user_agent()

Different servers hold user agent in differnet strings (unify this).

     # File lib/raw/context/request.rb, line 264
264:   def user_agent
265:     headers["HTTP_USER_AGENT"] || headers["USER-AGENT"]
266:   end
xhr?()

Alias for #xml_http_request?

xml_http_request?()

Is this an XhtmlRpcRequest? Returns true if the request’s ‘X-Requested-With’ header contains ‘XMLHttpRequest’. Compatible with the Prototype Javascript library.

This method is also aliased as xhr? script?
     # File lib/raw/context/request.rb, line 173
173:   def xml_http_request?
174:     not /XMLHttpRequest/i.match(@headers['HTTP_X_REQUESTED_WITH']).nil?
175:   end
xml_post?()

Is this a POST request formatted as XML?

     # File lib/raw/context/request.rb, line 158
158:   def xml_post?
159:     post? && post_format == :xml
160:   end
yaml_post?()

Is this a POST request formatted as YAML?

     # File lib/raw/context/request.rb, line 164
164:   def yaml_post?
165:     post? && post_format == :yaml
166:   end