From 416d3a0f868571319a2b29b0034d2dba68e4d5b3 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 22 Jul 2010 05:42:16 +0000 Subject: enable Range: responses for static files for most models The FileStreamer class of EventMachine (and by extension NeverBlock) unfortunately doesn't handle this. It's possible to do with Revactor (since it uses Rev under the covers), but we'll support what we can easily for now. --- lib/rainbows/fiber/body.rb | 10 ++++++---- lib/rainbows/fiber/rev.rb | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/rainbows/fiber') diff --git a/lib/rainbows/fiber/body.rb b/lib/rainbows/fiber/body.rb index ab5cfc8..c6c4484 100644 --- a/lib/rainbows/fiber/body.rb +++ b/lib/rainbows/fiber/body.rb @@ -12,15 +12,17 @@ module Rainbows::Fiber::Body # :nodoc: # the sendfile 1.0.0+ gem includes IO#sendfile_nonblock if ::IO.method_defined?(:sendfile_nonblock) - def write_body_file(client, body) - sock, off = client.to_io, 0 + def write_body_file(client, body, range) + sock, n = client.to_io, nil + offset, count = range ? range : [ 0, body.stat.size ] begin - off += sock.sendfile_nonblock(body, off, 0x10000) + offset += (n = sock.sendfile_nonblock(body, offset, count)) rescue Errno::EAGAIN client.wait_writable + retry rescue EOFError break - end while true + end while (count -= n) > 0 end else ALIASES[:write_body] = :write_body_each diff --git a/lib/rainbows/fiber/rev.rb b/lib/rainbows/fiber/rev.rb index 5bf4fdd..d837c01 100644 --- a/lib/rainbows/fiber/rev.rb +++ b/lib/rainbows/fiber/rev.rb @@ -100,6 +100,7 @@ module Rainbows::Fiber if hp.headers? headers = HH.new(headers) + range = parse_range(env, status, headers) and status = range.shift headers[CONNECTION] = if hp.keepalive? && G.alive KEEP_ALIVE else @@ -108,7 +109,7 @@ module Rainbows::Fiber end client.write(response_header(status, headers)) end - write_body(client, body) + write_body(client, body, range) end while env && env.clear && hp.reset.nil? rescue => e Error.write(io, e) -- cgit v1.2.3-24-ge0c7