Module: Rack::Request::Helpers

Included in:
Rack::Request
Defined in:
lib/rack/request.rb

Constant Summary collapse

FORM_DATA_MEDIA_TYPES =

The set of form-data media-types. Requests that do not indicate one of the media types present in this list will not be eligible for form-data / param parsing.

[
  'application/x-www-form-urlencoded',
  'multipart/form-data'
]
PARSEABLE_DATA_MEDIA_TYPES =

The set of media-types. Requests that do not indicate one of the media types present in this list will not be eligible for param parsing like soap attachments or generic multiparts

[
  'multipart/related',
  'multipart/mixed'
]
DEFAULT_PORTS =

Default ports depending on scheme. Used to decide whether or not to include the port in a generated URI.

{ 'http' => 80, 'https' => 443, 'coffee' => 80 }
HTTP_X_FORWARDED_SCHEME =
'HTTP_X_FORWARDED_SCHEME'
HTTP_X_FORWARDED_PROTO =
'HTTP_X_FORWARDED_PROTO'
HTTP_X_FORWARDED_HOST =
'HTTP_X_FORWARDED_HOST'
HTTP_X_FORWARDED_PORT =
'HTTP_X_FORWARDED_PORT'
HTTP_X_FORWARDED_SSL =
'HTTP_X_FORWARDED_SSL'

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

shortcut for request.params[key]


436
437
438
439
440
441
442
# File 'lib/rack/request.rb', line 436

def [](key)
  if $VERBOSE
    warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead")
  end

  params[key.to_s]
end

#[]=(key, value) ⇒ Object

shortcut for request.params[key] = value

Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.


447
448
449
450
451
452
453
# File 'lib/rack/request.rb', line 447

def []=(key, value)
  if $VERBOSE
    warn("Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead")
  end

  params[key.to_s] = value
end

#accept_encodingObject


423
424
425
# File 'lib/rack/request.rb', line 423

def accept_encoding
  parse_http_accept_header(get_header("HTTP_ACCEPT_ENCODING"))
end

#accept_languageObject


427
428
429
# File 'lib/rack/request.rb', line 427

def accept_language
  parse_http_accept_header(get_header("HTTP_ACCEPT_LANGUAGE"))
end

#authorityObject


211
212
213
# File 'lib/rack/request.rb', line 211

def authority
  get_header(SERVER_NAME) + ':' + get_header(SERVER_PORT)
end

#base_urlObject


404
405
406
407
408
# File 'lib/rack/request.rb', line 404

def base_url
  url = "#{scheme}://#{host}"
  url = "#{url}:#{port}" if port != DEFAULT_PORTS[scheme]
  url
end

#bodyObject


139
# File 'lib/rack/request.rb', line 139

def body;            get_header(RACK_INPUT)                         end

#content_charsetObject

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.


302
303
304
# File 'lib/rack/request.rb', line 302

def content_charset
  media_type_params['charset']
end

#content_lengthObject


148
# File 'lib/rack/request.rb', line 148

def content_length;  get_header('CONTENT_LENGTH')                   end

#content_typeObject


227
228
229
230
# File 'lib/rack/request.rb', line 227

def content_type
  content_type = get_header('CONTENT_TYPE')
  content_type.nil? || content_type.empty? ? nil : content_type
end

#cookiesObject


215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rack/request.rb', line 215

def cookies
  hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |k|
    set_header(k, {})
  end
  string = get_header HTTP_COOKIE

  return hash if string == get_header(RACK_REQUEST_COOKIE_STRING)
  hash.replace Utils.parse_cookies_header get_header HTTP_COOKIE
  set_header(RACK_REQUEST_COOKIE_STRING, string)
  hash
end

#delete?Boolean

Checks the HTTP request method (or verb) to see if it was of type DELETE

Returns:

  • (Boolean)

170
# File 'lib/rack/request.rb', line 170

def delete?;  request_method == DELETE  end

#delete_param(k) ⇒ Object

Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.

If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.

env['rack.input'] is not touched.


400
401
402
# File 'lib/rack/request.rb', line 400

def delete_param(k)
  [ self.POST.delete(k), self.GET.delete(k) ].compact.first
end

#form_data?Boolean

Determine whether the request body contains form-data by checking the request Content-Type for one of the media-types: “application/x-www-form-urlencoded” or “multipart/form-data”. The list of form-data media types can be modified through the FORM_DATA_MEDIA_TYPES array.

A request body is also assumed to contain form-data when no Content-Type header is provided and the request_method is POST.

Returns:

  • (Boolean)

314
315
316
317
318
# File 'lib/rack/request.rb', line 314

def form_data?
  type = media_type
  meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)
  (meth == POST && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
end

#fullpathObject


419
420
421
# File 'lib/rack/request.rb', line 419

def fullpath
  query_string.empty? ? path : "#{path}?#{query_string}"
end

#GETObject

Returns the data received in the query string.


327
328
329
330
331
332
333
334
335
# File 'lib/rack/request.rb', line 327

def GET
  if get_header(RACK_REQUEST_QUERY_STRING) == query_string
    get_header(RACK_REQUEST_QUERY_HASH)
  else
    query_hash = parse_query(query_string, '&;')
    set_header(RACK_REQUEST_QUERY_STRING, query_string)
    set_header(RACK_REQUEST_QUERY_HASH, query_hash)
  end
end

#get?Boolean

Checks the HTTP request method (or verb) to see if it was of type GET

Returns:

  • (Boolean)

173
# File 'lib/rack/request.rb', line 173

def get?;     request_method == GET     end

#head?Boolean

Checks the HTTP request method (or verb) to see if it was of type HEAD

Returns:

  • (Boolean)

176
# File 'lib/rack/request.rb', line 176

def head?;    request_method == HEAD    end

#hostObject


244
245
246
247
# File 'lib/rack/request.rb', line 244

def host
  # Remove port number.
  host_with_port.to_s.sub(/:\d+\z/, '')
end

#host_with_portObject


236
237
238
239
240
241
242
# File 'lib/rack/request.rb', line 236

def host_with_port
  if forwarded = get_header(HTTP_X_FORWARDED_HOST)
    forwarded.split(/,\s?/).last
  else
    get_header(HTTP_HOST) || "#{get_header(SERVER_NAME) || get_header(SERVER_ADDR)}:#{get_header(SERVER_PORT)}"
  end
end

#ipObject


267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rack/request.rb', line 267

def ip
  remote_addrs = split_ip_addresses(get_header('REMOTE_ADDR'))
  remote_addrs = reject_trusted_ip_addresses(remote_addrs)

  return remote_addrs.first if remote_addrs.any?

  forwarded_ips = split_ip_addresses(get_header('HTTP_X_FORWARDED_FOR'))
    .map { |ip| strip_port(ip) }

  return reject_trusted_ip_addresses(forwarded_ips).last || forwarded_ips.first || get_header("REMOTE_ADDR")
end

#link?Boolean

Checks the HTTP request method (or verb) to see if it was of type LINK

Returns:

  • (Boolean)

182
# File 'lib/rack/request.rb', line 182

def link?;    request_method == LINK    end

#loggerObject


149
# File 'lib/rack/request.rb', line 149

def logger;          get_header(RACK_LOGGER)                        end

#media_typeObject

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


285
286
287
# File 'lib/rack/request.rb', line 285

def media_type
  MediaType.type(content_type)
end

#media_type_paramsObject

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' }

294
295
296
# File 'lib/rack/request.rb', line 294

def media_type_params
  MediaType.params(content_type)
end

#multithread?Boolean

Returns:

  • (Boolean)

151
# File 'lib/rack/request.rb', line 151

def multithread?;    get_header(RACK_MULTITHREAD)                   end

#options?Boolean

Checks the HTTP request method (or verb) to see if it was of type OPTIONS

Returns:

  • (Boolean)

179
# File 'lib/rack/request.rb', line 179

def options?; request_method == OPTIONS end

#paramsObject

The union of GET and POST data.

Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.


369
370
371
372
373
# File 'lib/rack/request.rb', line 369

def params
  self.GET.merge(self.POST)
rescue EOFError
  self.GET.dup
end

#parseable_data?Boolean

Determine whether the request body contains data by checking the request media_type against registered parse-data media-types

Returns:

  • (Boolean)

322
323
324
# File 'lib/rack/request.rb', line 322

def parseable_data?
  PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
end

#patch?Boolean

Checks the HTTP request method (or verb) to see if it was of type PATCH

Returns:

  • (Boolean)

185
# File 'lib/rack/request.rb', line 185

def patch?;   request_method == PATCH   end

#pathObject


415
416
417
# File 'lib/rack/request.rb', line 415

def path
  script_name + path_info
end

#path_infoObject


143
# File 'lib/rack/request.rb', line 143

def path_info;       get_header(PATH_INFO).to_s                     end

#path_info=(s) ⇒ Object


144
# File 'lib/rack/request.rb', line 144

def path_info=(s);   set_header(PATH_INFO, s.to_s)                  end

#portObject


249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rack/request.rb', line 249

def port
  if port = host_with_port.split(/:/)[1]
    port.to_i
  elsif port = get_header(HTTP_X_FORWARDED_PORT)
    port.to_i
  elsif has_header?(HTTP_X_FORWARDED_HOST)
    DEFAULT_PORTS[scheme]
  elsif has_header?(HTTP_X_FORWARDED_PROTO)
    DEFAULT_PORTS[get_header(HTTP_X_FORWARDED_PROTO).split(',')[0]]
  else
    get_header(SERVER_PORT).to_i
  end
end

#POSTObject

Returns the data received in the request body.

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


341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/rack/request.rb', line 341

def POST
  if get_header(RACK_INPUT).nil?
    raise "Missing rack.input"
  elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
    get_header(RACK_REQUEST_FORM_HASH)
  elsif form_data? || parseable_data?
    unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
      form_vars = get_header(RACK_INPUT).read

      # Fix for Safari Ajax postings that always append \0
      # form_vars.sub!(/\0\z/, '') # performance replacement:
      form_vars.slice!(-1) if form_vars[-1] == ?\0

      set_header RACK_REQUEST_FORM_VARS, form_vars
      set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')

      get_header(RACK_INPUT).rewind
    end
    set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
    get_header RACK_REQUEST_FORM_HASH
  else
    {}
  end
end

#post?Boolean

Checks the HTTP request method (or verb) to see if it was of type POST

Returns:

  • (Boolean)

188
# File 'lib/rack/request.rb', line 188

def post?;    request_method == POST    end

#put?Boolean

Checks the HTTP request method (or verb) to see if it was of type PUT

Returns:

  • (Boolean)

191
# File 'lib/rack/request.rb', line 191

def put?;     request_method == PUT     end

#query_stringObject


147
# File 'lib/rack/request.rb', line 147

def query_string;    get_header(QUERY_STRING).to_s                  end

#refererObject Also known as: referrer

the referer of the client


154
# File 'lib/rack/request.rb', line 154

def referer;         get_header('HTTP_REFERER')                     end

#request_methodObject


146
# File 'lib/rack/request.rb', line 146

def request_method;  get_header(REQUEST_METHOD)                     end

#schemeObject


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rack/request.rb', line 199

def scheme
  if get_header(HTTPS) == 'on'
    'https'
  elsif get_header(HTTP_X_FORWARDED_SSL) == 'on'
    'https'
  elsif forwarded_scheme
    forwarded_scheme
  else
    get_header(RACK_URL_SCHEME)
  end
end

#script_nameObject


140
# File 'lib/rack/request.rb', line 140

def script_name;     get_header(SCRIPT_NAME).to_s                   end

#script_name=(s) ⇒ Object


141
# File 'lib/rack/request.rb', line 141

def script_name=(s); set_header(SCRIPT_NAME, s.to_s)                end

#sessionObject


157
158
159
160
161
# File 'lib/rack/request.rb', line 157

def session
  fetch_header(RACK_SESSION) do |k|
    set_header RACK_SESSION, default_session
  end
end

#session_optionsObject


163
164
165
166
167
# File 'lib/rack/request.rb', line 163

def session_options
  fetch_header(RACK_SESSION_OPTIONS) do |k|
    set_header RACK_SESSION_OPTIONS, {}
  end
end

#ssl?Boolean

Returns:

  • (Boolean)

263
264
265
# File 'lib/rack/request.rb', line 263

def ssl?
  scheme == 'https'
end

#trace?Boolean

Checks the HTTP request method (or verb) to see if it was of type TRACE

Returns:

  • (Boolean)

194
# File 'lib/rack/request.rb', line 194

def trace?;   request_method == TRACE   end

#trusted_proxy?(ip) ⇒ Boolean

Returns:

  • (Boolean)

431
432
433
# File 'lib/rack/request.rb', line 431

def trusted_proxy?(ip)
  Rack::Request.ip_filter.call(ip)
end

#unlink?Boolean

Checks the HTTP request method (or verb) to see if it was of type UNLINK

Returns:

  • (Boolean)

197
# File 'lib/rack/request.rb', line 197

def unlink?;  request_method == UNLINK  end

#update_param(k, v) ⇒ Object

Destructively update a parameter, whether it's in GET and/or POST. Returns nil.

The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.

env['rack.input'] is not touched.


380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/rack/request.rb', line 380

def update_param(k, v)
  found = false
  if self.GET.has_key?(k)
    found = true
    self.GET[k] = v
  end
  if self.POST.has_key?(k)
    found = true
    self.POST[k] = v
  end
  unless found
    self.GET[k] = v
  end
end

#urlObject

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


411
412
413
# File 'lib/rack/request.rb', line 411

def url
  base_url + fullpath
end

#user_agentObject


150
# File 'lib/rack/request.rb', line 150

def user_agent;      get_header('HTTP_USER_AGENT')                  end

#values_at(*keys) ⇒ Object

like Hash#values_at


456
457
458
# File 'lib/rack/request.rb', line 456

def values_at(*keys)
  keys.map { |key| params[key] }
end

#xhr?Boolean

Returns:

  • (Boolean)

232
233
234
# File 'lib/rack/request.rb', line 232

def xhr?
  get_header("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
end