@@ -149,7 +149,9 @@ def config
149149
150150 def call ( env )
151151
152- client_settings = ClientSettings . new ( env )
152+ start = Time . now
153+ client_settings = ClientSettings . new ( env , @storage , start )
154+ MiniProfiler . deauthorize_request if @config . authorization_mode == :whitelist
153155
154156 status = headers = body = nil
155157 query_string = env [ 'QUERY_STRING' ]
@@ -162,43 +164,39 @@ def call(env)
162164 ( @config . skip_paths && @config . skip_paths . any? { |p | path . start_with? ( p ) } ) ||
163165 query_string =~ /pp=skip/
164166
165- has_profiling_cookie = client_settings . has_cookie?
166-
167- if skip_it || ( @config . authorization_mode == :whitelist && !has_profiling_cookie )
168- status , headers , body = @app . call ( env )
169- if !skip_it && @config . authorization_mode == :whitelist && !has_profiling_cookie && MiniProfiler . request_authorized?
170- client_settings . write! ( headers )
171- end
172- return [ status , headers , body ]
167+ if skip_it || (
168+ @config . authorization_mode == :whitelist &&
169+ !client_settings . has_valid_cookie?
170+ )
171+ return client_settings . handle_cookie ( @app . call ( env ) )
173172 end
174173
175174 # handle all /mini-profiler requests here
176- return serve_html ( env ) if path . start_with? @config . base_url_path
175+ return client_settings . handle_cookie ( serve_html ( env ) ) if path . start_with? @config . base_url_path
177176
178177 has_disable_cookie = client_settings . disable_profiling?
179178 # manual session disable / enable
180179 if query_string =~ /pp=disable/ || has_disable_cookie
181180 skip_it = true
182181 end
183182
184- if query_string =~ /pp=enable/ && ( @config . authorization_mode != :whitelist || MiniProfiler . request_authorized? )
183+ if query_string =~ /pp=enable/
185184 skip_it = false
186185 config . enabled = true
187186 end
188187
189188 if skip_it || !config . enabled
190189 status , headers , body = @app . call ( env )
191190 client_settings . disable_profiling = true
192- client_settings . write! ( headers )
193- return [ status , headers , body ]
191+ return client_settings . handle_cookie ( [ status , headers , body ] )
194192 else
195193 client_settings . disable_profiling = false
196194 end
197195
198196 # profile gc
199197 if query_string =~ /pp=profile-gc/
200198 current . measure = false if current
201- return Rack ::MiniProfiler ::GCProfiler . new . profile_gc ( @app , env )
199+ return client_settings . handle_cookie ( Rack ::MiniProfiler ::GCProfiler . new . profile_gc ( @app , env ) )
202200 end
203201
204202 # profile memory
@@ -215,11 +213,10 @@ def call(env)
215213 body . close if body . respond_to? :close
216214 end
217215 report . pretty_print ( result )
218- return text_result ( result . string )
216+ return client_settings . handle_cookie ( text_result ( result . string ) )
219217 end
220218
221219 MiniProfiler . create_current ( env , @config )
222- MiniProfiler . deauthorize_request if @config . authorization_mode == :whitelist
223220
224221 if query_string =~ /pp=normal-backtrace/
225222 client_settings . backtrace_level = ClientSettings ::BACKTRACE_DEFAULT
@@ -238,7 +235,6 @@ def call(env)
238235 trace_exceptions = query_string =~ /pp=trace-exceptions/ && defined? TracePoint
239236 status , headers , body , exceptions , trace = nil
240237
241- start = Time . now
242238
243239 if trace_exceptions
244240 exceptions = [ ]
@@ -281,43 +277,37 @@ def call(env)
281277 else
282278 status , headers , body = @app . call ( env )
283279 end
284- client_settings . write! ( headers )
285280 ensure
286281 trace . disable if trace
287282 end
288283
289284 skip_it = current . discard
290285
291286 if ( config . authorization_mode == :whitelist && !MiniProfiler . request_authorized? )
292- # this is non-obvious, don't kill the profiling cookie on errors or short requests
293- # this ensures that stuff that never reaches the rails stack does not kill profiling
294- if status . to_i >= 200 && status . to_i < 300 && ( ( Time . now - start ) > 0.1 )
295- client_settings . discard_cookie! ( headers )
296- end
297287 skip_it = true
298288 end
299289
300- return [ status , headers , body ] if skip_it
290+ return client_settings . handle_cookie ( [ status , headers , body ] ) if skip_it
301291
302292 # we must do this here, otherwise current[:discard] is not being properly treated
303293 if trace_exceptions
304294 body . close if body . respond_to? :close
305- return dump_exceptions exceptions
295+ return client_settings . handle_cookie ( dump_exceptions exceptions )
306296 end
307297
308298 if query_string =~ /pp=env/ && !config . disable_env_dump
309299 body . close if body . respond_to? :close
310- return dump_env env
300+ return client_settings . handle_cookie ( dump_env env )
311301 end
312302
313303 if query_string =~ /pp=analyze-memory/
314304 body . close if body . respond_to? :close
315- return analyze_memory
305+ return client_settings . handle_cookie ( analyze_memory )
316306 end
317307
318308 if query_string =~ /pp=help/
319309 body . close if body . respond_to? :close
320- return help ( client_settings , env )
310+ return client_settings . handle_cookie ( help ( client_settings , env ) )
321311 end
322312
323313 page_struct = current . page_struct
@@ -326,7 +316,7 @@ def call(env)
326316
327317 if flamegraph
328318 body . close if body . respond_to? :close
329- return self . flamegraph ( flamegraph )
319+ return client_settings . handle_cookie ( self . flamegraph ( flamegraph ) )
330320 end
331321
332322
@@ -337,18 +327,16 @@ def call(env)
337327
338328 # inject headers, script
339329 if status >= 200 && status < 300
340- client_settings . write! ( headers )
341330 result = inject_profiler ( env , status , headers , body )
342- return result if result
331+ return client_settings . handle_cookie ( result ) if result
343332 end
344333 rescue Exception => e
345334 if @config . storage_failure != nil
346335 @config . storage_failure . call ( e )
347336 end
348337 end
349338
350- client_settings . write! ( headers )
351- [ status , headers , body ]
339+ client_settings . handle_cookie ( [ status , headers , body ] )
352340
353341 ensure
354342 # Make sure this always happens
@@ -543,7 +531,6 @@ def help(client_settings, env)
543531</html>
544532"
545533
546- client_settings . write! ( headers )
547534 [ 200 , headers , [ body ] ]
548535 end
549536
0 commit comments