lib/clogger/pure.rb in clogger-0.4.0 vs lib/clogger/pure.rb in clogger-0.5.0

- old
+ new

@@ -3,10 +3,13 @@ # Not at all optimized for performance, this was written based on # 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({}) @app = app @@ -28,12 +31,17 @@ end status, headers, body = resp 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 ] end @@ -41,16 +49,17 @@ @body_bytes_sent = 0 @body.each do |part| @body_bytes_sent += Rack::Utils.bytesize(part) yield part end - ensure - log(@env, @status, @headers) + self end def close @body.close if @body.respond_to?(:close) + ensure + log(@env, @status, @headers) end def reentrant? @reentrant end @@ -134,8 +143,20 @@ (env['rack.request.cookie_hash'][op[1]] rescue "-") || "-" else raise "EDOOFUS #{op.inspect}" end }.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