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"]

Methods

Constants

FORM_DATA_MEDIA_TYPES = [ nil, 'application/x-www-form-urlencoded', 'multipart/form-data'   The set of form-data media-types. Requests that do not indicate one of the media types presents in this list will not be eligible for form-data / param parsing.

Attributes

env  [R]  The environment of the request.

Public Class methods

[Source]

    # File lib/rack/request.rb, line 16
16:     def initialize(env)
17:       @env = env
18:     end

Public Instance methods

Returns the data recieved in the query string.

[Source]

     # File lib/rack/request.rb, line 93
 93:     def GET
 94:       if @env["rack.request.query_string"] == query_string
 95:         @env["rack.request.query_hash"]
 96:       else
 97:         @env["rack.request.query_string"] = query_string
 98:         @env["rack.request.query_hash"]   =
 99:           Utils.parse_query(query_string)
100:       end
101:     end

Returns the data recieved in the request body.

This method support both application/x-www-form-urlencoded and multipart/form-data.

[Source]

     # File lib/rack/request.rb, line 107
107:     def POST
108:       if @env["rack.request.form_input"].eql? @env["rack.input"]
109:         @env["rack.request.form_hash"]
110:       elsif form_data?
111:         @env["rack.request.form_input"] = @env["rack.input"]
112:         unless @env["rack.request.form_hash"] =
113:             Utils::Multipart.parse_multipart(env)
114:           @env["rack.request.form_vars"] = @env["rack.input"].read
115:           @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"])
116:           @env["rack.input"].rewind if @env["rack.input"].respond_to?(:rewind)
117:         end
118:         @env["rack.request.form_hash"]
119:       else
120:         {}
121:       end
122:     end

shortcut for request.params[key]

[Source]

     # File lib/rack/request.rb, line 132
132:     def [](key)
133:       params[key.to_s]
134:     end

shortcut for request.params[key] = value

[Source]

     # File lib/rack/request.rb, line 137
137:     def []=(key, value)
138:       params[key.to_s] = value
139:     end

[Source]

     # File lib/rack/request.rb, line 198
198:     def accept_encoding
199:       @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
200:         m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
201: 
202:         if m
203:           [m[1], (m[2] || 1.0).to_f]
204:         else
205:           raise "Invalid value for Accept-Encoding: #{part.inspect}"
206:         end
207:       end
208:     end

[Source]

    # File lib/rack/request.rb, line 20
20:     def body;            @env["rack.input"]                       end

The character set of the request body if a "charset" media type parameter was given, or nil if no "charset" was specified. Note that, per RFC2616, text/* media types that specify no explicit charset are to be considered ISO-8859-1.

[Source]

    # File lib/rack/request.rb, line 56
56:     def content_charset
57:       media_type_params['charset']
58:     end

[Source]

    # File lib/rack/request.rb, line 27
27:     def content_length;  @env['CONTENT_LENGTH']                   end

[Source]

    # File lib/rack/request.rb, line 28
28:     def content_type;    @env['CONTENT_TYPE']                     end

[Source]

     # File lib/rack/request.rb, line 153
153:     def cookies
154:       return {}  unless @env["HTTP_COOKIE"]
155: 
156:       if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
157:         @env["rack.request.cookie_hash"]
158:       else
159:         @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
160:         # According to RFC 2109:
161:         #   If multiple cookies satisfy the criteria above, they are ordered in
162:         #   the Cookie header such that those with more specific Path attributes
163:         #   precede those with less specific.  Ordering with respect to other
164:         #   attributes (e.g., Domain) is unspecified.
165:         @env["rack.request.cookie_hash"] =
166:           Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
167:             h[k] = Array === v ? v.first : v
168:             h
169:           }
170:       end
171:     end

[Source]

    # File lib/rack/request.rb, line 71
71:     def delete?;         request_method == "DELETE"               end

Determine whether the request body contains form-data by checking the request media_type against registered form-data media-types: "application/x-www-form-urlencoded" and "multipart/form-data". The list of form-data media types can be modified through the FORM_DATA_MEDIA_TYPES array.

[Source]

    # File lib/rack/request.rb, line 88
88:     def form_data?
89:       FORM_DATA_MEDIA_TYPES.include?(media_type)
90:     end

[Source]

     # File lib/rack/request.rb, line 192
192:     def fullpath
193:       path = script_name + path_info
194:       path << "?" << query_string  unless query_string.empty?
195:       path
196:     end

[Source]

    # File lib/rack/request.rb, line 68
68:     def get?;            request_method == "GET"                  end

[Source]

    # File lib/rack/request.rb, line 72
72:     def head?;           request_method == "HEAD"                 end

[Source]

    # File lib/rack/request.rb, line 60
60:     def host
61:       # Remove port number.
62:       (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
63:     end

[Source]

     # File lib/rack/request.rb, line 210
210:     def ip
211:       if addr = @env['HTTP_X_FORWARDED_FOR']
212:         addr.split(',').last.strip
213:       else
214:         @env['REMOTE_ADDR']
215:       end
216:     end

The media type (type/subtype) portion of the CONTENT_TYPE header without any media type parameters. e.g., when CONTENT_TYPE is "text/plain;charset=utf-8", the media-type is "text/plain".

For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7

[Source]

    # File lib/rack/request.rb, line 36
36:     def media_type
37:       content_type && content_type.split(/\s*[;,]\s*/, 2)[0].downcase
38:     end

The media type parameters provided in CONTENT_TYPE as a Hash, or an empty Hash if no CONTENT_TYPE or media-type parameters were provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8", this method responds with the following Hash:

  { 'charset' => 'utf-8' }

[Source]

    # File lib/rack/request.rb, line 45
45:     def media_type_params
46:       return {} if content_type.nil?
47:       content_type.split(/\s*[;,]\s*/)[1..-1].
48:         collect { |s| s.split('=', 2) }.
49:         inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
50:     end

The union of GET and POST data.

[Source]

     # File lib/rack/request.rb, line 125
125:     def params
126:       self.put? ? self.GET : self.GET.update(self.POST)
127:     rescue EOFError => e
128:       self.GET
129:     end

[Source]

    # File lib/rack/request.rb, line 23
23:     def path_info;       @env["PATH_INFO"].to_s                   end

[Source]

    # File lib/rack/request.rb, line 66
66:     def path_info=(s);   @env["PATH_INFO"] = s.to_s               end

[Source]

    # File lib/rack/request.rb, line 24
24:     def port;            @env["SERVER_PORT"].to_i                 end

[Source]

    # File lib/rack/request.rb, line 69
69:     def post?;           request_method == "POST"                 end

[Source]

    # File lib/rack/request.rb, line 70
70:     def put?;            request_method == "PUT"                  end

[Source]

    # File lib/rack/request.rb, line 26
26:     def query_string;    @env["QUERY_STRING"].to_s                end

the referer of the client or ’/’

[Source]

     # File lib/rack/request.rb, line 147
147:     def referer
148:       @env['HTTP_REFERER'] || '/'
149:     end
referrer()

Alias for referer

[Source]

    # File lib/rack/request.rb, line 25
25:     def request_method;  @env["REQUEST_METHOD"]                   end

[Source]

    # File lib/rack/request.rb, line 21
21:     def scheme;          @env["rack.url_scheme"]                  end

[Source]

    # File lib/rack/request.rb, line 22
22:     def script_name;     @env["SCRIPT_NAME"].to_s                 end

[Source]

    # File lib/rack/request.rb, line 65
65:     def script_name=(s); @env["SCRIPT_NAME"] = s.to_s             end

Tries to return a remake of the original request URL as a string.

[Source]

     # File lib/rack/request.rb, line 178
178:     def url
179:       url = scheme + "://"
180:       url << host
181: 
182:       if scheme == "https" && port != 443 ||
183:           scheme == "http" && port != 80
184:         url << ":#{port}"
185:       end
186: 
187:       url << fullpath
188: 
189:       url
190:     end

like Hash#values_at

[Source]

     # File lib/rack/request.rb, line 142
142:     def values_at(*keys)
143:       keys.map{|key| params[key] }
144:     end

[Source]

     # File lib/rack/request.rb, line 173
173:     def xhr?
174:       @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
175:     end

[Validate]