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]



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

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.



457
458
459
460
461
462
463
# File 'lib/rack/request.rb', line 457

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



433
434
435
# File 'lib/rack/request.rb', line 433

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

#accept_languageObject



437
438
439
# File 'lib/rack/request.rb', line 437

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

#authorityObject



215
216
217
# File 'lib/rack/request.rb', line 215

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

#base_urlObject



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

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

#bodyObject



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

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.



311
312
313
# File 'lib/rack/request.rb', line 311

def content_charset
  media_type_params['charset']
end

#content_lengthObject



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

def content_length;  get_header('CONTENT_LENGTH')                   end

#content_typeObject



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

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

#cookiesObject



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rack/request.rb', line 219

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 string
  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)


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

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.



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

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)


323
324
325
326
327
# File 'lib/rack/request.rb', line 323

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



429
430
431
# File 'lib/rack/request.rb', line 429

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

#GETObject

Returns the data received in the query string.



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

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)


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

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)


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

def head?;    request_method == HEAD    end

#hostObject



248
249
250
251
252
253
254
255
256
# File 'lib/rack/request.rb', line 248

def host
  # Remove port number.
  h = host_with_port
  if colon_index = h.index(":")
    h[0, colon_index]
  else
    h
  end
end

#host_with_portObject



240
241
242
243
244
245
246
# File 'lib/rack/request.rb', line 240

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



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

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)


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

def link?;    request_method == LINK    end

#loggerObject



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

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



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

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


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

def media_type_params
  MediaType.params(content_type)
end

#multithread?Boolean

Returns:

  • (Boolean)


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

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)


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

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.



378
379
380
381
382
# File 'lib/rack/request.rb', line 378

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)


331
332
333
# File 'lib/rack/request.rb', line 331

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)


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

def patch?;   request_method == PATCH   end

#pathObject



425
426
427
# File 'lib/rack/request.rb', line 425

def path
  script_name + path_info
end

#path_infoObject



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

def path_info;       get_header(PATH_INFO).to_s                     end

#path_info=(s) ⇒ Object



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

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

#portObject



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rack/request.rb', line 258

def port
  if port = extract_port(host_with_port)
    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[extract_proto_header(get_header(HTTP_X_FORWARDED_PROTO))]
  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.



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

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)


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

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)


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

def put?;     request_method == PUT     end

#query_stringObject



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

def query_string;    get_header(QUERY_STRING).to_s                  end

#refererObject Also known as: referrer

the referer of the client



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

def referer;         get_header('HTTP_REFERER')                     end

#request_methodObject



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

def request_method;  get_header(REQUEST_METHOD)                     end

#schemeObject



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

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



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

def script_name;     get_header(SCRIPT_NAME).to_s                   end

#script_name=(s) ⇒ Object



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

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

#sessionObject



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

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

#session_optionsObject



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

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

#ssl?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/rack/request.rb', line 272

def ssl?
  scheme == 'https'
end

#trace?Boolean

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

Returns:

  • (Boolean)


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

def trace?;   request_method == TRACE   end

#trusted_proxy?(ip) ⇒ Boolean

Returns:

  • (Boolean)


441
442
443
# File 'lib/rack/request.rb', line 441

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)


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

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.



389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/rack/request.rb', line 389

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.



421
422
423
# File 'lib/rack/request.rb', line 421

def url
  base_url + fullpath
end

#user_agentObject



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

def user_agent;      get_header('HTTP_USER_AGENT')                  end

#values_at(*keys) ⇒ Object

like Hash#values_at



466
467
468
# File 'lib/rack/request.rb', line 466

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

#xhr?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/rack/request.rb', line 236

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