Override the default Request implementation to include methods useful for testing.
- []
- []=
- boolean
- content_length
- domain
- enabled?
- false?
- fetch
- formatted_post?
- has?
- has_key?
- has_param?
- host
- host_uri
- host_url
- is?
- keys
- local?
- local_net?
- method
- param?
- parameters
- params
- params=
- path
- path_info
- port
- post_format
- protocol
- query
- query_string
- raw_body
- referer
- referrer
- remote_ip
- script?
- server_uri
- ssl?
- subdomains
- true?
- uri
- user_agent
- xhr?
- xml_http_request?
- xml_post?
- yaml_post?
| [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 |
Lookup a query parameter.
[ show source ]
# File lib/raw/context/request.rb, line 288
288: def [](param)
289: params[param.to_s]
290: end
Set a query parameter.
[ show source ]
# File lib/raw/context/request.rb, line 294
294: def []=(param, value)
295: params[param.to_s] = value
296: end
Alias for #true?
The content_length
[ show source ]
# File lib/raw/context/request.rb, line 189
189: def content_length
190: @headers["CONTENT_LENGTH"].to_i
191: end
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‘
[ show source ]
# File lib/raw/context/request.rb, line 83
83: def domain tld_length = 1
84: host.split('.').last(1 + tld_length).join('.')
85: end
Alias for #true?
Check if a boolean param (checkbox) is false.
[ show source ]
# File lib/raw/context/request.rb, line 308
308: def false?(param)
309: !true?(param)
310: end
Fetch a parameter with default value.
[ show source ]
# File lib/raw/context/request.rb, line 314
314: def fetch param, default = nil
315: params.fetch(param, default)
316: end
Is this a POST request formatted as XML or YAML?
[ show source ]
# File lib/raw/context/request.rb, line 152
152: def formatted_post?
153: post? && (post_format == :xml || post_format == :yaml)
154: end
Alias for #has_key?
Check if a param is available.
[ show source ]
# File lib/raw/context/request.rb, line 324
324: def has_key?(key)
325: params.keys.include?(key)
326: end
Alias for #has_key?
The server host name. Also handles proxy forwarding.
[ show source ]
# File lib/raw/context/request.rb, line 248
248: def host
249: @headers['HTTP_X_FORWARDED_HOST'] || @headers['HTTP_HOST']
250: end
The host uri.
[ show source ]
# File lib/raw/context/request.rb, line 254
254: def host_uri
255: "#{protocol}#{host}"
256: end
Alias for #host_uri
Alias for #has_key?
[ show source ]
# File lib/raw/context/request.rb, line 332
332: def keys
333: params.keys
334: end
Request comming from local?
[ show source ]
# 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
Request is from a local network? (RFC1918 + localhost)
[ show source ]
# 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
The request method. Alternatively you could use the request method predicates.
Examples
if request.method == :get if request.get?
[ show source ]
# File lib/raw/context/request.rb, line 112
112: def method
113: @headers["REQUEST_METHOD"].downcase.to_sym
114: end
Alias for #has_key?
Alias for #params
The parsed query parameters collection.
[ show source ]
# 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
[ show source ]
# 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
Alias for #path_info
The path info. Typically this is the rewritten uri without the query string.
[ show source ]
# File lib/raw/context/request.rb, line 71
71: def path_info
72: @headers["PATH_INFO"]
73: end
The server port.
[ show source ]
# File lib/raw/context/request.rb, line 241
241: def port
242: @headers['SERVER_PORT'].to_i
243: end
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
[ show source ]
# 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
The request protocol.
[ show source ]
# File lib/raw/context/request.rb, line 52
52: def protocol
53: @headers['HTTPS'] == "on" ? "https://" : "http://"
54: end
Alias for #params
The request query string.
[ show source ]
# File lib/raw/context/request.rb, line 100
100: def query_string
101: headers["QUERY_STRING"]
102: end
The raw data of the request. Useful to implement Webservices.
[ show source ]
# 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
Return the referer. For the initial page in the clickstream there is no referer, set "/" by default.
[ show source ]
# File lib/raw/context/request.rb, line 182
182: def referer
183: @headers["HTTP_REFERER"] || "/"
184: end
Alias for #referer
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.
[ show source ]
# 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
Alias for #xml_http_request?
Alias for #host_uri
Is this an ssl request?
[ show source ]
# File lib/raw/context/request.rb, line 58
58: def ssl?
59: @headers["HTTPS"] == "on"
60: end
Returns all the subdomains as an array.
Examples
my.name.nitroproject.org: request.subdomains # => [‘my’, ‘name’]
[ show source ]
# 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
Check if a boolean param (checkbox) is true.
[ show source ]
# File lib/raw/context/request.rb, line 300
300: def true?(param)
301: params[param] == 'on'
302: end
The request uri.
[ show source ]
# File lib/raw/context/request.rb, line 64
64: def uri
65: @headers["REQUEST_URI"]
66: end
Different servers hold user agent in differnet strings (unify this).
[ show source ]
# File lib/raw/context/request.rb, line 264
264: def user_agent
265: headers["HTTP_USER_AGENT"] || headers["USER-AGENT"]
266: end
Alias for #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.
[ show source ]
# 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
Is this a POST request formatted as XML?
[ show source ]
# File lib/raw/context/request.rb, line 158
158: def xml_post?
159: post? && post_format == :xml
160: end
Is this a POST request formatted as YAML?
[ show source ]
# File lib/raw/context/request.rb, line 164
164: def yaml_post?
165: post? && post_format == :yaml
166: end