Revision: 4392 http://trac.macosforge.org/projects/ruby/changeset/4392 Author: joshua.ballanco@apple.com Date: 2010-07-29 12:08:54 -0700 (Thu, 29 Jul 2010) Log Message: ----------- Adding support for X-Sendfile headers Modified Paths: -------------- ControlTower/trunk/lib/control_tower/rack_socket.rb Added Paths: ----------- ControlTower/trunk/sample/body_content.txt ControlTower/trunk/sample/x_sendfile.ru Modified: ControlTower/trunk/lib/control_tower/rack_socket.rb =================================================================== --- ControlTower/trunk/lib/control_tower/rack_socket.rb 2010-07-29 08:26:05 UTC (rev 4391) +++ ControlTower/trunk/lib/control_tower/rack_socket.rb 2010-07-29 19:08:54 UTC (rev 4392) @@ -40,7 +40,7 @@ 'rack.run_once' => false, 'rack.version' => VERSION } resp = nil - x_sendfile_header = "X-Sendfile" + x_sendfile_header = 'X-Sendfile' x_sendfile = nil begin request_data = parse!(connection, env) @@ -48,6 +48,14 @@ request_data['REMOTE_ADDR'] = connection.addr[3] status, headers, body = @app.call(request_data) + # If there's an X-Sendfile header, we'll use sendfile(2) + if headers.has_key?(x_sendfile_header) + x_sendfile = headers[x_sendfile_header] + x_sendfile = ::File.open(x_sendfile, 'r') unless x_sendfile.kind_of? IO + x_sendfile_size = x_sendfile.stat.size + headers['Content-Length'] = x_sendfile_size + end + # Unless somebody's already set it for us (or we don't need it), set the Content-Length unless (status == -1 || (status >= 100 and status <= 199) || @@ -68,9 +76,6 @@ resp = "HTTP/1.1 #{status}\r\n" headers.each do |header, value| - if header == x_sendfile_header - x_sendfile = value - else resp << "#{header}: #{value}\r\n" end resp << "\r\n" @@ -78,8 +83,10 @@ # Start writing the response connection.write resp - # Finish writing out the body - if body.respond_to?(:each) + # Write the body + if x_sendfile + connection.sendfile(x_sendfile, 0, x_sendfile_size) + elsif body.respond_to?(:each) body.each do |chunk| connection.write chunk end Added: ControlTower/trunk/sample/body_content.txt =================================================================== --- ControlTower/trunk/sample/body_content.txt (rev 0) +++ ControlTower/trunk/sample/body_content.txt 2010-07-29 19:08:54 UTC (rev 4392) @@ -0,0 +1 @@ +You should get this as a response... Added: ControlTower/trunk/sample/x_sendfile.ru =================================================================== --- ControlTower/trunk/sample/x_sendfile.ru (rev 0) +++ ControlTower/trunk/sample/x_sendfile.ru 2010-07-29 19:08:54 UTC (rev 4392) @@ -0,0 +1,10 @@ +# NOTE: Your cwd needs to be the sample/ directory when you start ControlTower +class FileSender + def call(env) + headers = { 'Content-Type' => 'text/plain', + 'X-Sendfile' => ::File.expand_path('../body_content.txt', __FILE__) } + [200, headers, "You shouldn't get this body..."] + end +end + +run FileSender.new
participants (1)
-
source_changes@macosforge.org