Module: N::Request

Included in:
Context
Defined in:
lib/nitro/request.rb

Overview

Encapsulates a request. This is an abstract request typically extended by sub-classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject

The request cookies.



32
33
34
# File 'lib/nitro/request.rb', line 32

def cookies
  @cookies
end

#headersObject Also known as: env, env_table

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



19
20
21
# File 'lib/nitro/request.rb', line 19

def headers
  @headers
end

#inObject

The request input stream.



14
15
16
# File 'lib/nitro/request.rb', line 14

def in
  @in
end

#paramsObject Also known as: query

The parsed query parameters collection.



27
28
29
# File 'lib/nitro/request.rb', line 27

def params
  @params
end

Instance Method Details

#[](param) ⇒ Object

Lookup a query parameter.



117
118
119
# File 'lib/nitro/request.rb', line 117

def [](param)
	@params[param]
end

#[]=(param, value) ⇒ Object

Set a query parameter.



123
124
125
# File 'lib/nitro/request.rb', line 123

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

#hostObject

The server host name.



107
108
109
# File 'lib/nitro/request.rb', line 107

def host
	@headers['HTTP_HOST'] 
end

#host_urlObject



111
112
113
# File 'lib/nitro/request.rb', line 111

def host_url
	"http://#{host}"
end

#methodObject

The request method.



66
67
68
# File 'lib/nitro/request.rb', line 66

def method
	@headers['REQUEST_METHOD'].downcase.intern
end

#pathObject

The path is the uri without the query string.



54
55
56
# File 'lib/nitro/request.rb', line 54

def path
	uri ? uri.split('?').first : ''
end

#portObject

The server port.



101
102
103
# File 'lib/nitro/request.rb', line 101

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

#protocolObject

The request protocol.



36
37
38
# File 'lib/nitro/request.rb', line 36

def protocol
	443 == port ? 'https://' : 'http://'
end

#query_stringObject

The request query string.



60
61
62
# File 'lib/nitro/request.rb', line 60

def query_string 
	@headers['QUERY_STRING']
end

#refererObject

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



73
74
75
# File 'lib/nitro/request.rb', line 73

def referer
	return @headers['REFERER'] || '/'
end

#remote_ipObject

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nitro/request.rb', line 85

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$|^(10|172\.16|192\.168)\./i
		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)


42
43
44
# File 'lib/nitro/request.rb', line 42

def ssl?
	443 == port
end

#uriObject

The request uri.



48
49
50
# File 'lib/nitro/request.rb', line 48

def uri
	@headers['REQUEST_URI']
end