Class: Rack::MiniProfiler::ClientSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_profiler/client_settings.rb

Constant Summary collapse

"__profilin"
BACKTRACE_DEFAULT =
nil
BACKTRACE_FULL =
1
BACKTRACE_NONE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, store, start) ⇒ ClientSettings

Returns a new instance of ClientSettings.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mini_profiler/client_settings.rb', line 15

def initialize(env, store, start)
  request = ::Rack::Request.new(env)
  @cookie = request.cookies[COOKIE_NAME]
  @store = store
  @start = start

  @allowed_tokens, @orig_auth_tokens = nil

  if @cookie
    @cookie.split(",").map{|pair| pair.split("=")}.each do |k,v|
      @orig_disable_profiling = @disable_profiling = (v=='t') if k == "dp"
      @backtrace_level = v.to_i if k == "bt"
      @orig_auth_tokens = v.to_s.split("|") if k == "a"
    end
  end

  if !@backtrace_level.nil? && (@backtrace_level == 0 || @backtrace_level > BACKTRACE_NONE)
    @backtrace_level = nil
  end

  @orig_backtrace_level = @backtrace_level

end

Instance Attribute Details

#backtrace_levelObject

Returns the value of attribute backtrace_level.



12
13
14
# File 'lib/mini_profiler/client_settings.rb', line 12

def backtrace_level
  @backtrace_level
end

#disable_profilingObject

Returns the value of attribute disable_profiling.



11
12
13
# File 'lib/mini_profiler/client_settings.rb', line 11

def disable_profiling
  @disable_profiling
end

Instance Method Details

#backtrace_default?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/mini_profiler/client_settings.rb', line 107

def backtrace_default?
  @backtrace_level == BACKTRACE_DEFAULT
end

#backtrace_full?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/mini_profiler/client_settings.rb', line 103

def backtrace_full?
  @backtrace_level == BACKTRACE_FULL
end

#backtrace_none?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/mini_profiler/client_settings.rb', line 111

def backtrace_none?
  @backtrace_level == BACKTRACE_NONE
end

#disable_profiling?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mini_profiler/client_settings.rb', line 99

def disable_profiling?
  @disable_profiling
end

#discard_cookie!(headers) ⇒ Object



79
80
81
82
83
# File 'lib/mini_profiler/client_settings.rb', line 79

def discard_cookie!(headers)
  if @cookie
    Rack::Utils.delete_cookie_header!(headers, COOKIE_NAME, :path => '/')
  end
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mini_profiler/client_settings.rb', line 39

def handle_cookie(result)
  status,headers,_body = result

  if (MiniProfiler.config.authorization_mode == :whitelist && !MiniProfiler.request_authorized?)
    # this is non-obvious, don't kill the profiling cookie on errors or short requests
    # this ensures that stuff that never reaches the rails stack does not kill profiling
    if status.to_i >= 200 && status.to_i < 300 && ((Time.now - @start) > 0.1)
      discard_cookie!(headers)
    end
  else
    write!(headers)
  end

  result
end

#has_valid_cookie?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mini_profiler/client_settings.rb', line 85

def has_valid_cookie?
  valid_cookie = !@cookie.nil?

  if (MiniProfiler.config.authorization_mode == :whitelist)
    @allowed_tokens ||= @store.allowed_tokens

    valid_cookie = (Array === @orig_auth_tokens) &&
      ((@allowed_tokens & @orig_auth_tokens).length > 0)
  end

  valid_cookie
end

#write!(headers) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mini_profiler/client_settings.rb', line 55

def write!(headers)

  tokens_changed = false

  if MiniProfiler.request_authorized? && MiniProfiler.config.authorization_mode == :whitelist
    @allowed_tokens ||= @store.allowed_tokens
    tokens_changed = !@orig_auth_tokens || ((@allowed_tokens - @orig_auth_tokens).length > 0)
  end

  if  @orig_disable_profiling != @disable_profiling ||
      @orig_backtrace_level != @backtrace_level ||
      @cookie.nil? ||
      tokens_changed

    settings = {"p" =>  "t" }
    settings["dp"] = "t"                  if @disable_profiling
    settings["bt"] = @backtrace_level     if @backtrace_level
    settings["a"] = @allowed_tokens.join("|") if @allowed_tokens && MiniProfiler.request_authorized?

    settings_string = settings.map{|k,v| "#{k}=#{v}"}.join(",")
    Rack::Utils.set_cookie_header!(headers, COOKIE_NAME, :value => settings_string, :path => '/')
  end
end