Class: Sinatra::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/sinatra/base.rb

Overview

The request object. See Rack::Request for more info: http://rubydoc.info/github/rack/rack/master/Rack/Request

Defined Under Namespace

Classes: AcceptEntry, MimeTypeEntry

Constant Summary collapse

HEADER_PARAM =
/\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/.freeze
HEADER_VALUE_WITH_PARAMS =
%r{(?:(?:\w+|\*)/(?:\w+(?:\.|-|\+)?|\*)*)\s*(?:;#{HEADER_PARAM})*}.freeze

Instance Method Summary collapse

Instance Method Details

#acceptObject

Returns an array of acceptable media types for the response


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sinatra/base.rb', line 28

def accept
  @env['sinatra.accept'] ||= if @env.include?('HTTP_ACCEPT') && (@env['HTTP_ACCEPT'].to_s != '')
                               @env['HTTP_ACCEPT']
                                 .to_s
                                 .scan(HEADER_VALUE_WITH_PARAMS)
                                 .map! { |e| AcceptEntry.new(e) }
                                 .sort
                             else
                               [AcceptEntry.new('*/*')]
                             end
end

#accept?(type) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/sinatra/base.rb', line 40

def accept?(type)
  preferred_type(type).to_s.include?(type)
end

#forwarded?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/sinatra/base.rb', line 58

def forwarded?
  @env.include? 'HTTP_X_FORWARDED_HOST'
end

#idempotent?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/sinatra/base.rb', line 66

def idempotent?
  safe? || put? || delete? || link? || unlink?
end

#link?Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/sinatra/base.rb', line 70

def link?
  request_method == 'LINK'
end

#paramsObject


78
79
80
81
82
83
84
# File 'lib/sinatra/base.rb', line 78

def params
  super
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
  raise BadRequest, "Invalid query parameters: #{Rack::Utils.escape_html(e.message)}"
rescue EOFError => e
  raise BadRequest, "Invalid multipart/form-data: #{Rack::Utils.escape_html(e.message)}"
end

#preferred_type(*types) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sinatra/base.rb', line 44

def preferred_type(*types)
  return accept.first if types.empty?

  types.flatten!
  return types.first if accept.empty?

  accept.detect do |accept_header|
    type = types.detect { |t| MimeTypeEntry.new(t).accepts?(accept_header) }
    return type if type
  end
end

#safe?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/sinatra/base.rb', line 62

def safe?
  get? || head? || options? || trace?
end

#unlink?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/sinatra/base.rb', line 74

def unlink?
  request_method == 'UNLINK'
end