globally refactor Range handling for responses
[rainbows.git] / lib / rainbows / fiber / body.rb
blob872b1df063fc914ff7be2a9bf47c3f867034f478
1 # -*- encoding: binary -*-
2 # :enddoc:
3 # non-portable body handling for Fiber-based concurrency goes here
4 # this module is required and included in worker processes only
5 # this is meant to be included _after_ Rainbows::Response::Body
6 module Rainbows::Fiber::Body # :nodoc:
8   # the sendfile 1.0.0+ gem includes IO#sendfile_nonblock
9   if IO.method_defined?(:sendfile_nonblock)
10     def write_body_file(body, range)
11       sock, n, body = to_io, nil, body_to_io(body)
12       offset, count = range ? range : [ 0, body.stat.size ]
13       begin
14         offset += (n = sock.sendfile_nonblock(body, offset, count))
15       rescue Errno::EAGAIN
16         kgio_wait_writable
17         retry
18       rescue EOFError
19         break
20       end while (count -= n) > 0
21       ensure
22         close_if_private(body)
23     end
24   end
26   def self.included(klass)
27     klass.__send__ :alias_method, :write_body_stream, :write_body_each
28   end
29 end