diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/clogger.rb | 9 | ||||
-rw-r--r-- | lib/clogger/pure.rb | 24 |
2 files changed, 31 insertions, 2 deletions
diff --git a/lib/clogger.rb b/lib/clogger.rb index ba047f5..a827632 100644 --- a/lib/clogger.rb +++ b/lib/clogger.rb @@ -36,6 +36,15 @@ class Clogger :request_uri => 7 } + # proxy class to avoid clobbering the +to_path+ optimization when + # using static files + class ToPath < Struct.new(:clogger) + def each(&block); clogger.each(&block); end + def close; clogger.close; end + + # to_path is defined in Clogger::Pure or the C extension + end + private CGI_ENV = Regexp.new('\A\$(' << diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb index 50e4f6e..0dd5021 100644 --- a/lib/clogger/pure.rb +++ b/lib/clogger/pure.rb @@ -5,6 +5,9 @@ # the original C extension code so it's not very Ruby-ish... class Clogger + attr_accessor :env, :status, :headers, :body + attr_writer :body_bytes_sent + def initialize(app, opts = {}) # trigger autoload to avoid thread-safety issues later on Rack::Utils::HeaderHash.new({}) @@ -30,8 +33,13 @@ class Clogger headers = Rack::Utils::HeaderHash.new(headers) if @need_resp if @wrap_body @reentrant = env['rack.multithread'] if @reentrant.nil? - @env, @status, @headers, @body = env, status, headers, body - return [ status, headers, @reentrant ? self.dup : self ] + wbody = @reentrant ? self.dup : self + wbody.env = env + wbody.status = status + wbody.headers = headers + wbody.body = body + wbody = Clogger::ToPath.new(wbody) if body.respond_to?(:to_path) + return [ status, headers, wbody ] end log(env, status, headers) [ status, headers, body ] @@ -139,4 +147,16 @@ private }.join('') end + class ToPath + def to_path + rv = (body = clogger.body).to_path + + # try to avoid unnecessary path lookups with to_io.stat instead of + # File.stat + clogger.body_bytes_sent = + (body.respond_to?(:to_io) ? body.to_io.stat : File.stat(rv)).size + rv + end + end + end |