Class: Rack::Request

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rack/request.rb

Overview

Rack::Request provides a convenient interface to a Rack environment. It is stateless, the environment env passed to the constructor will be directly modified.

req = Rack::Request.new(env)
req.post?
req.params["data"]

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

ALLOWED_SCHEMES =
%w(https http wss ws).freeze
SCHEME_WHITELIST =
ALLOWED_SCHEMES

Constants included from Helpers

Helpers::DEFAULT_PORTS, Helpers::FORM_DATA_MEDIA_TYPES, Helpers::HTTP_X_FORWARDED_FOR, Helpers::HTTP_X_FORWARDED_HOST, Helpers::HTTP_X_FORWARDED_PORT, Helpers::HTTP_X_FORWARDED_PROTO, Helpers::HTTP_X_FORWARDED_SCHEME, Helpers::HTTP_X_FORWARDED_SSL, Helpers::PARSEABLE_DATA_MEDIA_TYPES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#GET, #POST, #[], #[]=, #accept_encoding, #accept_language, #authority, #base_url, #body, #content_charset, #content_length, #content_type, #cookies, #delete?, #form_data?, #forwarded_authority, #forwarded_for, #forwarded_port, #fullpath, #get?, #head?, #host, #host_authority, #host_with_port, #hostname, #ip, #link?, #logger, #media_type, #media_type_params, #multithread?, #options?, #parseable_data?, #patch?, #path, #path_info, #path_info=, #port, #post?, #put?, #query_string, #referer, #request_method, #scheme, #script_name, #script_name=, #server_authority, #server_name, #server_port, #session, #session_options, #ssl?, #trace?, #trusted_proxy?, #unlink?, #url, #user_agent, #values_at, #xhr?

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.


30
31
32
33
# File 'lib/rack/request.rb', line 30

def initialize(env)
  @env = env
  @params = nil
end

Class Attribute Details

.ip_filterObject

Returns the value of attribute ip_filter.


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

def ip_filter
  @ip_filter
end

Instance Attribute Details

#envObject (readonly)

The environment of the request.


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

def env
  @env
end

Instance Method Details

#add_header(key, v) ⇒ Object

Add a header that may have multiple values.

Example:

request.add_header 'Accept', 'image/png'
request.add_header 'Accept', '*/*'

assert_equal 'image/png,*/*', request.get_header('Accept')

www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2


86
87
88
89
90
91
92
93
94
# File 'lib/rack/request.rb', line 86

def add_header(key, v)
  if v.nil?
    get_header key
  elsif has_header? key
    set_header key, "#{get_header key},#{v}"
  else
    set_header key, v
  end
end

#delete_header(name) ⇒ Object

Delete a request specific value for `name`.


97
98
99
# File 'lib/rack/request.rb', line 97

def delete_header(name)
  @env.delete name
end

#delete_param(k) ⇒ Object


44
45
46
47
48
# File 'lib/rack/request.rb', line 44

def delete_param(k)
  v = super
  @params = nil
  v
end

#each_header(&block) ⇒ Object

Loops through each key / value pair in the request specific data.


68
69
70
# File 'lib/rack/request.rb', line 68

def each_header(&block)
  @env.each(&block)
end

#fetch_header(name, &block) ⇒ Object

If a block is given, it yields to the block if the value hasn't been set on the request.


63
64
65
# File 'lib/rack/request.rb', line 63

def fetch_header(name, &block)
  @env.fetch(name, &block)
end

#get_header(name) ⇒ Object

Get a request specific value for `name`.


57
58
59
# File 'lib/rack/request.rb', line 57

def get_header(name)
  @env[name]
end

#has_header?(name) ⇒ Boolean

Predicate method to test to see if `name` has been set as request specific data

Returns:

  • (Boolean)

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

def has_header?(name)
  @env.key? name
end

#initialize_copy(other) ⇒ Object


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

def initialize_copy(other)
  @env = other.env.dup
end

#paramsObject


35
36
37
# File 'lib/rack/request.rb', line 35

def params
  @params ||= super
end

#set_header(name, v) ⇒ Object

Set a request specific value for `name` to `v`


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

def set_header(name, v)
  @env[name] = v
end

#update_param(k, v) ⇒ Object


39
40
41
42
# File 'lib/rack/request.rb', line 39

def update_param(k, v)
  super
  @params = nil
end