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*/
HEADER_VALUE_WITH_PARAMS =
/(?:(?:\w+|\*)\/(?:\w+(?:\.|\-|\+)?|\*)*)\s*(?:;#{HEADER_PARAM})*/

Instance Method Summary collapse

Instance Method Details

#acceptObject

Returns an array of acceptable media types for the response


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

def accept
  @env['sinatra.accept'] ||= begin
    if @env.include? 'HTTP_ACCEPT' and @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
end

#accept?(type) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#forwarded?Boolean

Returns:

  • (Boolean)

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

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

#idempotent?Boolean

Returns:

  • (Boolean)

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

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

#link?Boolean

Returns:

  • (Boolean)

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

def link?
  request_method == "LINK"
end

#paramsObject


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

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


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

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)

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

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

#unlink?Boolean

Returns:

  • (Boolean)

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

def unlink?
  request_method == "UNLINK"
end