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:         end
117:         @env["rack.request.form_hash"]
118:       else
119:         {}
120:       end
121:     end

shortcut for request.params[key]

[Source]

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

shortcut for request.params[key] = value

[Source]

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

[Source]

     # File lib/rack/request.rb, line 197
197:     def accept_encoding
198:       @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
199:         m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
200: 
201:         if m
202:           [m[1], (m[2] || 1.0).to_f]
203:         else
204:           raise "Invalid value for Accept-Encoding: #{part.inspect}"
205:         end
206:       end
207:     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 152
152:     def cookies
153:       return {}  unless @env["HTTP_COOKIE"]
154: 
155:       if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
156:         @env["rack.request.cookie_hash"]
157:       else
158:         @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
159:         # According to RFC 2109:
160:         #   If multiple cookies satisfy the criteria above, they are ordered in
161:         #   the Cookie header such that those with more specific Path attributes
162:         #   precede those with less specific.  Ordering with respect to other
163:         #   attributes (e.g., Domain) is unspecified.
164:         @env["rack.request.cookie_hash"] =
165:           Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
166:             h[k] = Array === v ? v.first : v
167:             h
168:           }
169:       end
170:     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 191
191:     def fullpath
192:       path = script_name + path_info
193:       path << "?" << query_string  unless query_string.empty?
194:       path
195:     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

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 124
124:     def params
125:       self.GET.update(self.POST)
126:     rescue EOFError => e
127:       self.GET
128:     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 146
146:     def referer
147:       @env['HTTP_REFERER'] || '/'
148:     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 177
177:     def url
178:       url = scheme + "://"
179:       url << host
180: 
181:       if scheme == "https" && port != 443 ||
182:           scheme == "http" && port != 80
183:         url << ":#{port}"
184:       end
185: 
186:       url << fullpath
187: 
188:       url
189:     end

like Hash#values_at

[Source]

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

[Source]

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

[Validate]