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_FOR =

The address of the client which connected to the proxy.

'HTTP_X_FORWARDED_FOR'
HTTP_X_FORWARDED_HOST =

The contents of the host/:authority header sent to the proxy.

'HTTP_X_FORWARDED_HOST'
HTTP_X_FORWARDED_SCHEME =

The value of the scheme sent to the proxy.

'HTTP_X_FORWARDED_SCHEME'
HTTP_X_FORWARDED_PROTO =

The protocol used to connect to the proxy.

'HTTP_X_FORWARDED_PROTO'
HTTP_X_FORWARDED_PORT =

The port used to connect to the proxy.

'HTTP_X_FORWARDED_PORT'
HTTP_X_FORWARDED_SSL =

Another way for specifying https scheme was used.

'HTTP_X_FORWARDED_SSL'

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

shortcut for request.params[key]


526
527
528
529
530
# File 'lib/rack/request.rb', line 526

def [](key)
  warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead", uplevel: 1)

  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.


535
536
537
538
539
# File 'lib/rack/request.rb', line 535

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

  params[key.to_s] = value
end

#accept_encodingObject


513
514
515
# File 'lib/rack/request.rb', line 513

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

#accept_languageObject


517
518
519
# File 'lib/rack/request.rb', line 517

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

#authorityObject

The authority of the incoming request as defined by RFC3976. tools.ietf.org/html/rfc3986#section-3.2

In HTTP/1, this is the `host` header. In HTTP/2, this is the `:authority` pseudo-header.


221
222
223
# File 'lib/rack/request.rb', line 221

def authority
  forwarded_authority || host_authority || server_authority
end

#base_urlObject


496
497
498
# File 'lib/rack/request.rb', line 496

def base_url
  "#{scheme}://#{host_with_port}"
end

#bodyObject


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

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.


394
395
396
# File 'lib/rack/request.rb', line 394

def content_charset
  media_type_params['charset']
end

#content_lengthObject


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

def content_length;  get_header('CONTENT_LENGTH')                   end

#content_typeObject


265
266
267
268
# File 'lib/rack/request.rb', line 265

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

#cookiesObject


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

def cookies
  hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |key|
    set_header(key, {})
  end

  string = get_header(HTTP_COOKIE)

  unless string == get_header(RACK_REQUEST_COOKIE_STRING)
    hash.replace Utils.parse_cookies_header(string)
    set_header(RACK_REQUEST_COOKIE_STRING, string)
  end

  hash
end

#delete?Boolean

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

Returns:

  • (Boolean)

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

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.


491
492
493
494
# File 'lib/rack/request.rb', line 491

def delete_param(k)
  post_value, get_value = self.POST.delete(k), self.GET.delete(k)
  post_value || get_value
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)

406
407
408
409
410
411
# File 'lib/rack/request.rb', line 406

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

#forwarded_authorityObject


338
339
340
341
342
# File 'lib/rack/request.rb', line 338

def forwarded_authority
  if value = get_header(HTTP_X_FORWARDED_HOST)
    wrap_ipv6(split_header(value).first)
  end
end

#forwarded_forObject


324
325
326
327
328
329
330
# File 'lib/rack/request.rb', line 324

def forwarded_for
  if value = get_header(HTTP_X_FORWARDED_FOR)
    split_header(value).map do |authority|
      split_authority(wrap_ipv6(authority))[1]
    end
  end
end

#forwarded_portObject


332
333
334
335
336
# File 'lib/rack/request.rb', line 332

def forwarded_port
  if value = get_header(HTTP_X_FORWARDED_PORT)
    split_header(value).map(&:to_i)
  end
end

#fullpathObject


509
510
511
# File 'lib/rack/request.rb', line 509

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

#GETObject

Returns the data received in the query string.


420
421
422
423
424
425
426
427
428
# File 'lib/rack/request.rb', line 420

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)

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

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)

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

def head?;    request_method == HEAD    end

#hostObject

Returns a formatted host, suitable for being used in a URI.


290
291
292
# File 'lib/rack/request.rb', line 290

def host
  split_authority(self.authority)[0]
end

#host_authorityObject

The `HTTP_HOST` header.


275
276
277
# File 'lib/rack/request.rb', line 275

def host_authority
  get_header(HTTP_HOST)
end

#host_with_port(authority = self.authority) ⇒ Object


279
280
281
282
283
284
285
286
287
# File 'lib/rack/request.rb', line 279

def host_with_port(authority = self.authority)
  host, _, port = split_authority(authority)

  if port == DEFAULT_PORTS[self.scheme]
    host
  else
    authority
  end
end

#hostnameObject

Returns an address suitable for being to resolve to an address. In the case of a domain name or IPv4 address, the result is the same as host. In the case of IPv6 or future address formats, the square brackets are removed.


298
299
300
# File 'lib/rack/request.rb', line 298

def hostname
  split_authority(self.authority)[1]
end

#ipObject


348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rack/request.rb', line 348

def ip
  remote_addresses = split_header(get_header('REMOTE_ADDR'))
  external_addresses = reject_trusted_ip_addresses(remote_addresses)

  unless external_addresses.empty?
    return external_addresses.first
  end

  if forwarded_for = self.forwarded_for
    unless forwarded_for.empty?
      # The forwarded for addresses are ordered: client, proxy1, proxy2.
      # So we reject all the trusted addresses (proxy*) and return the
      # last client. Or if we trust everyone, we just return the first
      # address.
      return reject_trusted_ip_addresses(forwarded_for).last || forwarded_for.first
    end
  end

  # If all the addresses are trusted, and we aren't forwarded, just return
  # the first remote address, which represents the source of the request.
  remote_addresses.first
end

#link?Boolean

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

Returns:

  • (Boolean)

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

def link?;    request_method == LINK    end

#loggerObject


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

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


377
378
379
# File 'lib/rack/request.rb', line 377

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

386
387
388
# File 'lib/rack/request.rb', line 386

def media_type_params
  MediaType.params(content_type)
end

#multithread?Boolean

Returns:

  • (Boolean)

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

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)

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

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.


462
463
464
# File 'lib/rack/request.rb', line 462

def params
  self.GET.merge(self.POST)
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)

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

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)

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

def patch?;   request_method == PATCH   end

#pathObject


505
506
507
# File 'lib/rack/request.rb', line 505

def path
  script_name + path_info
end

#path_infoObject


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

def path_info;       get_header(PATH_INFO).to_s                     end

#path_info=(s) ⇒ Object


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

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

#portObject


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/rack/request.rb', line 302

def port
  if authority = self.authority
    _, _, port = split_authority(authority)

    if port
      return port
    end
  end

  if forwarded_port = self.forwarded_port
    return forwarded_port.first
  end

  if scheme = self.scheme
    if port = DEFAULT_PORTS[scheme]
      return port
    end
  end

  self.server_port
end

#POSTObject

Returns the data received in the request body.

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


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/rack/request.rb', line 434

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.end_with?("\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)

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

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)

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

def put?;     request_method == PUT     end

#query_stringObject


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

def query_string;    get_header(QUERY_STRING).to_s                  end

#refererObject Also known as: referrer

the referer of the client


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

def referer;         get_header('HTTP_REFERER')                     end

#request_methodObject


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

def request_method;  get_header(REQUEST_METHOD)                     end

#schemeObject


204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rack/request.rb', line 204

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


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

def script_name;     get_header(SCRIPT_NAME).to_s                   end

#script_name=(s) ⇒ Object


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

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

#server_authorityObject

The authority as defined by the `SERVER_NAME` and `SERVER_PORT` variables.


227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rack/request.rb', line 227

def server_authority
  host = self.server_name
  port = self.server_port

  if host
    if port
      "#{host}:#{port}"
    else
      host
    end
  end
end

#server_nameObject


240
241
242
# File 'lib/rack/request.rb', line 240

def server_name
  get_header(SERVER_NAME)
end

#server_portObject


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

def server_port
  if port = get_header(SERVER_PORT)
    Integer(port)
  end
end

#sessionObject


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

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

#session_optionsObject


168
169
170
171
172
# File 'lib/rack/request.rb', line 168

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

#ssl?Boolean

Returns:

  • (Boolean)

344
345
346
# File 'lib/rack/request.rb', line 344

def ssl?
  scheme == 'https' || scheme == 'wss'
end

#trace?Boolean

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

Returns:

  • (Boolean)

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

def trace?;   request_method == TRACE   end

#trusted_proxy?(ip) ⇒ Boolean

Returns:

  • (Boolean)

521
522
523
# File 'lib/rack/request.rb', line 521

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)

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

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.


471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/rack/request.rb', line 471

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.


501
502
503
# File 'lib/rack/request.rb', line 501

def url
  base_url + fullpath
end

#user_agentObject


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

def user_agent;      get_header('HTTP_USER_AGENT')                  end

#values_at(*keys) ⇒ Object

like Hash#values_at


542
543
544
# File 'lib/rack/request.rb', line 542

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

#xhr?Boolean

Returns:

  • (Boolean)

270
271
272
# File 'lib/rack/request.rb', line 270

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