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: rack.rubyforge.org/doc/classes/Rack/Request.html

Defined Under Namespace

Classes: AcceptEntry

Constant Summary

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/sinatra/base.rb', line 23

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



34
35
36
# File 'lib/sinatra/base.rb', line 34

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

#forwarded?Boolean



51
52
53
# File 'lib/sinatra/base.rb', line 51

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

#idempotent?Boolean



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

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

#link?Boolean



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

def link?
  request_method == "LINK"
end

#preferred_type(*types) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/sinatra/base.rb', line 38

def preferred_type(*types)
  accepts = accept # just evaluate once
  return accepts.first if types.empty?
  types.flatten!
  return types.first if accepts.empty?
  accepts.detect do |pattern|
    type = types.detect { |t| File.fnmatch(pattern, t) }
    return type if type
  end
end

#safe?Boolean



55
56
57
# File 'lib/sinatra/base.rb', line 55

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

#unlink?Boolean



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

def unlink?
  request_method == "UNLINK"
end