From 75742d880477f23c5bf4c459e79cc4601bdfd8c0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 4 Sep 2009 14:19:59 -0700 Subject: [PATCH 3/3] CommonLogger uses HeaderHash to lookup Content-Length Since HeaderHash is cheaper to use now, encourage its usage instead of reinventing a way to lookup header values with an enforced O(n) overhead. Under best conditions, this can now be done in O(1) time if the rest of our middleware stack already uses (and passes) HeaderHash. This does make things slower if CommonLogger is the only middleware in the stack, however that's probably not too common. --- lib/rack/commonlogger.rb | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/rack/commonlogger.rb b/lib/rack/commonlogger.rb index 880f0fb..1edc9b8 100644 --- a/lib/rack/commonlogger.rb +++ b/lib/rack/commonlogger.rb @@ -16,6 +16,7 @@ module Rack def call(env) began_at = Time.now status, header, body = @app.call(env) + header = Utils::HeaderHash.new(header) log(env, status, header, began_at) [status, header, body] end @@ -41,12 +42,8 @@ module Rack end def extract_content_length(headers) - headers.each do |key, value| - if key.downcase == 'content-length' - return value.to_s == '0' ? '-' : value - end - end - '-' + value = headers['Content-Length'] or return '-' + value.to_s == '0' ? '-' : value end end end -- 1.6.4.2.236.gf324c