From 39b178cdebe275cbc8ce19cf269bea7cd15ff4ca Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 4 Jul 2010 22:16:52 +0000 Subject: refactor response body handling for sendfile(2) This hopefully allows the "sendfile" gem to be required anywhere in the Rainbows!/Unicorn config file, and not have to be required via RUBYOPT or the '-r' command-line switch. We also modularize HttpResponse and avoids singleton methods in the response path. This (hopefully) makes it easier for individual concurrency models to share code and override individual methods. --- lib/rainbows/sendfile.rb | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'lib/rainbows/sendfile.rb') diff --git a/lib/rainbows/sendfile.rb b/lib/rainbows/sendfile.rb index 146c4c5..3f82047 100644 --- a/lib/rainbows/sendfile.rb +++ b/lib/rainbows/sendfile.rb @@ -57,34 +57,23 @@ class Sendfile < Struct.new(:app) # Body wrapper, this allows us to fall back gracefully to # +each+ in case a given concurrency model does not optimize # +to_path+ calls. - class Body < Struct.new(:to_io) - - def initialize(path, headers) - # Rainbows! will try #to_io if #to_path exists to avoid unnecessary - # open() calls. - self.to_io = File.open(path, 'rb') + class Body < Struct.new(:to_path) + def self.new(path, headers) unless headers['Content-Length'] - stat = to_io.stat + stat = File.stat(path) headers['Content-Length'] = stat.size.to_s if stat.file? end - end - - def to_path - to_io.path + super(path) end # fallback in case our +to_path+ doesn't get handled for whatever reason def each(&block) - buf = '' - while to_io.read(0x4000, buf) - yield buf + File.open(to_path, 'rb') do |fp| + buf = '' + yield buf while fp.read(0x4000, buf) end end - - def close - to_io.close - end end def call(env) -- cgit v1.2.3-24-ge0c7