From 038ea40cbcc55f2cdb8fa31b19b4e224836534df Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Dec 2009 21:34:50 -0600 Subject: 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. Signed-off-by: Joshua Peek --- lib/rack/commonlogger.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/rack/commonlogger.rb b/lib/rack/commonlogger.rb index 880f0fbf..1edc9b83 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 -- cgit v1.2.3-24-ge0c7