SamSaffron / MiniProfiler

A simple but effective mini-profiler for ASP.NET MVC, ASP.NET and Ruby.
http://miniprofiler.com
1.05k stars 221 forks source link

Bug Fix - rack + rack-cache throw an error sometimes because HTTP_IF_NONE_MATCH is nil #152

Closed MadanThangavelu closed 11 years ago

MadanThangavelu commented 11 years ago

Making cache headers empty strings rather than nil.

The following is an explanation for this fix:

1) Rack-Cache freezes the environment variable. https://github.com/rtomayko/rack-cache/blob/1.2/lib/rack/cache/context.rb#L61

2) Rack picks it up and looks for 'HTTP_IF_NONE_MATCH. https://github.com/rtomayko/rack-cache/blob/1.1/lib/rack/cache/context.rb#L111

3) Since MiniProfiler sets HTTP_IF_NONE_MATCH to nil, the value returned is nil. But, rack thinks the environment variables are not loaded. https://github.com/jruby/jruby-rack/blob/master/src/main/ruby/rack/handler/servlet/default_env.rb#L87

4) A load results in modifying the env variable (which is frozen) https://github.com/jruby/jruby-rack/blob/master/src/main/ruby/rack/handler/servlet/default_env.rb#L163

5) Runtime error because we are modifying a frozen object.

Here is the traceback below:

org.jruby.exceptions.RaiseException: (TypeError) can't modify frozen hash at org.jruby.RubyHash.[]=(org/jruby/RubyHash.java:897) at Rack::Handler::Servlet::DefaultEnv.load_header(file:/opt/ruby/gems/1.8/gems/jruby-rack-1.1.13.2/lib/jruby-rack-1.1.13.2.jar!/rack/handler/servlet/default_env.rb:163) at Rack::Handler::Servlet::DefaultEnv.load_env_key(file:/opt/ruby/gems/1.8/gems/jruby-rack-1.1.13.2/lib/jruby-rack-1.1.13.2.jar!/rack/handler/servlet/default_env.rb:149) at Rack::Handler::Servlet::DefaultEnv. at Rack::Cache::Context.not_modified?(/opt/ruby/gems/1.8/gems/rack-cache-1.2/lib/rack/cache/context.rb:115) at Rack::Cache::Context.call!(/opt/ruby/gems/1.8/gems/rack-cache-1.2/lib/rack/cache/context.rb:86) at Rack::Cache::Context.call(/opt/ruby/gems/1.8/gems/rack-cache-1.2/lib/rack/cache/context.rb:51) at Rack::MiniProfiler.call(/opt/ruby/gems/1.8/gems/rack-mini-profiler-0.1.26/Ruby/lib/mini_profiler/profiler.rb:278) at Rack::MiniProfiler.call(/opt/ruby/gems/1.8/gems/rack-mini-profiler-0.1.26/Ruby/lib/mini_profiler/profiler.rb:271) at Rails::Engine.call(/opt/ruby/gems/1.8/gems/railties-3.2.13/lib/rails/engine.rb:479) at Rails::Application.call(/opt/ruby/gems/1.8/gems/railties-3.2.13/lib/rails/application.rb:223) at Rack::Handler::Servlet.call(file:/opt/ruby/gems/1.8/gems/jruby-rack-1.1.13.2/lib/jruby-rack-1.1.13.2.jar!/rack/handler/servlet.rb:22)

SamSaffron commented 11 years ago

thanks !

ryancheung commented 11 years ago

Update the rack-mini-profiler gem please! I can still meet the error while using the version 0.1.26, which for now is the latest!