Class: Sinatra::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Sinatra::Request
show all
- Defined in:
- lib/sinatra/base.rb
Overview
Defined Under Namespace
Classes: AcceptEntry, MimeTypeEntry
Constant Summary
collapse
/\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/.freeze
%r{(?:(?:\w+|\*)/(?:\w+(?:\.|-|\+)?|\*)*)\s*(?:;#{HEADER_PARAM})*}.freeze
Instance Method Summary
collapse
Instance Method Details
#accept ⇒ Object
Returns an array of acceptable media types for the response
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/sinatra/base.rb', line 33
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
45
46
47
|
# File 'lib/sinatra/base.rb', line 45
def accept?(type)
preferred_type(type).to_s.include?(type)
end
|
#forwarded? ⇒ Boolean
63
64
65
|
# File 'lib/sinatra/base.rb', line 63
def forwarded?
@env.include? 'HTTP_X_FORWARDED_HOST'
end
|
#idempotent? ⇒ Boolean
71
72
73
|
# File 'lib/sinatra/base.rb', line 71
def idempotent?
safe? || put? || delete? || link? || unlink?
end
|
#link? ⇒ Boolean
75
76
77
|
# File 'lib/sinatra/base.rb', line 75
def link?
request_method == 'LINK'
end
|
#params ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/sinatra/base.rb', line 83
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
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/sinatra/base.rb', line 49
def preferred_type(*types)
return accept.first if types.empty?
types.flatten!
return types.first if accept.empty?
accept.detect do ||
type = types.detect { |t| MimeTypeEntry.new(t).accepts?() }
return type if type
end
end
|
#safe? ⇒ Boolean
67
68
69
|
# File 'lib/sinatra/base.rb', line 67
def safe?
get? || head? || options? || trace?
end
|
#unlink? ⇒ Boolean
79
80
81
|
# File 'lib/sinatra/base.rb', line 79
def unlink?
request_method == 'UNLINK'
end
|