Skip to content
Browse files

fix Rack::Lock, use same logic for Rack::CommonLogger

  • Loading branch information...
1 parent 86763dc commit fc51ee7dabb033234bdcb69defde6ad999aecef8 @rkh rkh committed May 31, 2011
Showing with 27 additions and 22 deletions.
  1. +1 −0 lib/rack.rb
  2. +21 −0 lib/rack/body_proxy.rb
  3. +3 −1 lib/rack/commonlogger.rb
  4. +2 −21 lib/rack/lock.rb
View
1 lib/rack.rb
@@ -24,6 +24,7 @@ def self.release
end
autoload :Builder, "rack/builder"
+ autoload :BodyProxy, "rack/body_proxy"
autoload :Cascade, "rack/cascade"
autoload :Chunked, "rack/chunked"
autoload :CommonLogger, "rack/commonlogger"
View
21 lib/rack/body_proxy.rb
@@ -0,0 +1,21 @@
+module Rack
+ class BodyProxy < defined?(BasicObject) ? BasicObject : Object
+ def initialize(body, &block)
+ @body, @block = body, block
+ end
+
+ def respond_to?(*args)
+ super or @body.respond_to?(*args)
+ end
+
+ def close
+ @body.close if @body.respond_to? :close
+ ensure
+ @block.call
+ end
+
+ def method_missing(*args, &block)
+ @body.__send__(*args, &block)
+ end
+ end
+end
View
4 lib/rack/commonlogger.rb
@@ -1,3 +1,5 @@
+require 'rack/body_proxy'
+
module Rack
# Rack::CommonLogger forwards every request to an +app+ given, and
# logs a line in the Apache common log format to the +logger+, or
@@ -17,7 +19,7 @@ def call(env)
began_at = Time.now
status, header, body = @app.call(env)
header = Utils::HeaderHash.new(header)
- log(env, status, header, began_at)
+ body = BodyProxy.new(body) { log(env, status, header, began_at) }
[status, header, body]
end
View
23 lib/rack/lock.rb
@@ -1,27 +1,8 @@
require 'thread'
+require 'rack/body_proxy'
module Rack
class Lock
- class Proxy < Struct.new(:target, :mutex) # :nodoc:
- def each
- target.each { |x| yield x }
- end
-
- def close
- target.close if target.respond_to?(:close)
- ensure
- mutex.unlock
- end
-
- def to_path
- target.to_path
- end
-
- def respond_to?(sym)
- sym.to_sym == :close || target.respond_to?(sym)
- end
- end
-
FLAG = 'rack.multithread'.freeze
def initialize(app, mutex = Mutex.new)
@@ -32,7 +13,7 @@ def call(env)
old, env[FLAG] = env[FLAG], false
@mutex.lock
response = @app.call(env)
- response[2] = Proxy.new(response[2], @mutex)
+ response[2] = BodyProxy.new(response[2]) { @mutex.unlock }
response
rescue Exception
@mutex.unlock

0 comments on commit fc51ee7

Please sign in to comment.
Something went wrong with that request. Please try again.