Module: Raw::Request

Included in:
Context
Defined in:
lib/raw/test/context.rb,
lib/raw/context/request.rb

Overview

Encapsulates a request. This is an abstract request typically extended by sub-classes. This module is included in Context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject Also known as: cookie

The request cookies.



47
48
49
# File 'lib/raw/context/request.rb', line 47

def cookies
  @cookies
end

#get_paramsObject

Returns the value of attribute get_params.



22
23
24
# File 'lib/raw/context/request.rb', line 22

def get_params
  @get_params
end

#headersObject Also known as: env, env_table

The request headers collection. Also called the request environment (env).



16
17
18
# File 'lib/raw/context/request.rb', line 16

def headers
  @headers
end

#inObject

The request input stream.



11
12
13
# File 'lib/raw/context/request.rb', line 11

def in
  @in
end

#post_paramsObject

Returns the value of attribute post_params.



21
22
23
# File 'lib/raw/context/request.rb', line 21

def post_params
  @post_params
end

Instance Method Details

#[](param) ⇒ Object

Lookup a query parameter. – TODO: Check if unescape is needed. ++



288
289
290
# File 'lib/raw/context/request.rb', line 288

def [](param)
  params[param]
end

#[]=(param, value) ⇒ Object

Set a query parameter.



294
295
296
# File 'lib/raw/context/request.rb', line 294

def []=(param, value)
  params[param] = value
end

#content_lengthObject

The content_length



189
190
191
# File 'lib/raw/context/request.rb', line 189

def content_length
  @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’



83
84
85
# File 'lib/raw/context/request.rb', line 83

def domain tld_length = 1
  host.split('.').last(1 + tld_length).join('.')
end

#false?(param) ⇒ Boolean

Check if a boolean param (checkbox) is false.

Returns:

  • (Boolean)


308
309
310
# File 'lib/raw/context/request.rb', line 308

def false?(param)
  !true?(param)
end

#fetch(param, default = nil) ⇒ Object

Fetch a parameter with default value.



314
315
316
# File 'lib/raw/context/request.rb', line 314

def fetch param, default = nil
  params.fetch(param, default)
end

#formatted_post?Boolean

Is this a POST request formatted as XML or YAML?

Returns:

  • (Boolean)


152
153
154
# File 'lib/raw/context/request.rb', line 152

def formatted_post?
  post? && (post_format == :xml || post_format == :yaml)
end

#has_key?(key) ⇒ Boolean Also known as: has_param?, param?, has?, is?

Check if a param is available. – gmosx: use instead of nil test to be more robust. (nil can be a hash element !) ++

Returns:

  • (Boolean)


324
325
326
# File 'lib/raw/context/request.rb', line 324

def has_key?(key)
  params.keys.include?(key)
end

#hostObject

The server host name. Also handles proxy forwarding.



248
249
250
# File 'lib/raw/context/request.rb', line 248

def host
  @headers['HTTP_X_FORWARDED_HOST'] || @headers['HTTP_HOST'] 
end

#host_uriObject Also known as: server_uri, host_url

The host uri.



254
255
256
# File 'lib/raw/context/request.rb', line 254

def host_uri
  "#{protocol}#{host}"
end

#keysObject



332
333
334
# File 'lib/raw/context/request.rb', line 332

def keys
  params.keys
end

#local?(ip = remote_ip) ⇒ Boolean

Request comming from local?

Returns:

  • (Boolean)


234
235
236
237
# File 'lib/raw/context/request.rb', line 234

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)

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/raw/context/request.rb', line 217

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

#methodObject

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

Examples

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



112
113
114
# File 'lib/raw/context/request.rb', line 112

def method
  @headers["REQUEST_METHOD"].downcase.to_sym
end

#paramsObject Also known as: query, parameters

The parsed query parameters collection.



26
27
28
29
30
31
32
# File 'lib/raw/context/request.rb', line 26

def params
 if method == :post
  @post_params
 else
  @get_params
 end
end

#params=(pa) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/raw/context/request.rb', line 34

def params=(pa)
 if method == :post
  @post_params = pa
 else
  @get_params = pa
 end
end

#path_infoObject Also known as: path

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



71
72
73
# File 'lib/raw/context/request.rb', line 71

def path_info
  @headers["PATH_INFO"]
end

#portObject

The server port.



241
242
243
# File 'lib/raw/context/request.rb', line 241

def port
  @headers['SERVER_PORT'].to_i
end

#post_formatObject

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


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/raw/context/request.rb', line 138

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

#protocolObject

The request protocol.



52
53
54
# File 'lib/raw/context/request.rb', line 52

def protocol
  @headers['HTTPS'] == "on" ? "https://" : "http://"
end

#query_stringObject

The request query string.



100
101
102
# File 'lib/raw/context/request.rb', line 100

def query_string 
  headers["QUERY_STRING"]
end

#raw_bodyObject

The raw data of the request. Useful to implement Webservices. – FIXME: better name and implementation. ++



274
275
276
277
278
279
280
281
# File 'lib/raw/context/request.rb', line 274

def raw_body
  unless @raw_body
    @in.rewind
    @raw_body = @in.read(content_length)
  end

  @raw_body
end

#refererObject Also known as: referrer

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



182
183
184
# File 'lib/raw/context/request.rb', line 182

def referer
  @headers["HTTP_REFERER"] || "/"
end

#remote_ipObject

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.



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/raw/context/request.rb', line 201

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?

Returns:

  • (Boolean)


58
59
60
# File 'lib/raw/context/request.rb', line 58

def ssl?
  @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’]



93
94
95
96
# File 'lib/raw/context/request.rb', line 93

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.

Returns:

  • (Boolean)


300
301
302
# File 'lib/raw/context/request.rb', line 300

def true?(param)
  params[param] == 'on'
end

#uriObject

The request uri.



64
65
66
# File 'lib/raw/context/request.rb', line 64

def uri
  @headers["REQUEST_URI"]
end

#user_agentObject

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



264
265
266
# File 'lib/raw/context/request.rb', line 264

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.

Returns:

  • (Boolean)


173
174
175
# File 'lib/raw/context/request.rb', line 173

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?

Returns:

  • (Boolean)


158
159
160
# File 'lib/raw/context/request.rb', line 158

def xml_post?
  post? && post_format == :xml
end

#yaml_post?Boolean

Is this a POST request formatted as YAML?

Returns:

  • (Boolean)


164
165
166
# File 'lib/raw/context/request.rb', line 164

def yaml_post?
  post? && post_format == :yaml
end