| Class | Rack::Request |
| In: |
lib/rack/request.rb
|
| Parent: | Object |
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"]
| env | [R] | The environment of the request. |
Returns the data recieved in the query string.
# File lib/rack/request.rb, line 42
42: def GET
43: if @env["rack.request.query_string"] == query_string
44: @env["rack.request.query_hash"]
45: else
46: @env["rack.request.query_string"] = query_string
47: @env["rack.request.query_hash"] =
48: Utils.parse_query(query_string)
49: end
50: end
Returns the data recieved in the request body.
This method support both application/x-www-form-urlencoded and multipart/form-data.
# File lib/rack/request.rb, line 56
56: def POST
57: if @env["rack.request.form_input"] == @env["rack.input"]
58: @env["rack.request.form_hash"]
59: else
60: @env["rack.request.form_input"] = @env["rack.input"]
61: unless @env["rack.request.form_hash"] =
62: Utils::Multipart.parse_multipart(env)
63: @env["rack.request.form_vars"] = @env["rack.input"].read
64: @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"])
65: end
66: @env["rack.request.form_hash"]
67: end
68: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 76
76: def [](key)
77: params[key.to_s]
78: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 81
81: def []=(key, value)
82: params[key.to_s] = value
83: end
# File lib/rack/request.rb, line 97
97: def cookies
98: return {} unless @env["HTTP_COOKIE"]
99:
100: if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
101: @env["rack.request.cookie_hash"]
102: else
103: @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
104: # According to RFC 2109:
105: # If multiple cookies satisfy the criteria above, they are ordered in
106: # the Cookie header such that those with more specific Path attributes
107: # precede those with less specific. Ordering with respect to other
108: # attributes (e.g., Domain) is unspecified.
109: @env["rack.request.cookie_hash"] =
110: Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
111: h[k] = Array === v ? v.first : v
112: h
113: }
114: end
115: end
# File lib/rack/request.rb, line 136
136: def fullpath
137: path = script_name + path_info
138: path << "?" << query_string unless query_string.empty?
139: path
140: end
# File lib/rack/request.rb, line 28
28: def host
29: # Remove port number.
30: (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
31: end
Tries to return a remake of the original request URL as a string.
# File lib/rack/request.rb, line 122
122: def url
123: url = scheme + "://"
124: url << host
125:
126: if scheme == "https" && port != 443 ||
127: scheme == "http" && port != 80
128: url << ":#{port}"
129: end
130:
131: url << fullpath
132:
133: url
134: end