diff options
Diffstat (limited to 'lib/rainbows/response')
-rw-r--r-- | lib/rainbows/response/body.rb | 29 | ||||
-rw-r--r-- | lib/rainbows/response/range.rb | 34 |
2 files changed, 49 insertions, 14 deletions
diff --git a/lib/rainbows/response/body.rb b/lib/rainbows/response/body.rb index 9e36412..cf14f08 100644 --- a/lib/rainbows/response/body.rb +++ b/lib/rainbows/response/body.rb @@ -46,22 +46,23 @@ module Rainbows::Response::Body # :nodoc: end if IO.method_defined?(:sendfile_nonblock) - def write_body_file(sock, body) - sock.sendfile(body, 0) + def write_body_file(sock, body, range) + range ? sock.sendfile(body, range[0], range[1]) : sock.sendfile(body, 0) end end if IO.respond_to?(:copy_stream) unless method_defined?(:write_body_file) # try to use sendfile() via IO.copy_stream, otherwise pread()+write() - def write_body_file(sock, body) - IO.copy_stream(body, sock, nil, 0) + def write_body_file(sock, body, range) + range ? IO.copy_stream(body, sock, range[1], range[0]) : + IO.copy_stream(body, sock, nil, 0) end end # only used when body is a pipe or socket that can't handle # pread() semantics - def write_body_stream(sock, body) + def write_body_stream(sock, body, range) IO.copy_stream(body, sock) ensure body.respond_to?(:close) and body.close @@ -74,40 +75,40 @@ module Rainbows::Response::Body # :nodoc: if method_defined?(:write_body_file) # middlewares/apps may return with a body that responds to +to_path+ - def write_body_path(sock, body) + def write_body_path(sock, body, range) inp = body_to_io(body) if inp.stat.file? begin - write_body_file(sock, inp) + write_body_file(sock, inp, range) ensure inp.close if inp != body end else - write_body_stream(sock, inp) + write_body_stream(sock, inp, range) end ensure body.respond_to?(:close) && inp != body and body.close end elsif method_defined?(:write_body_stream) - def write_body_path(sock, body) - write_body_stream(sock, inp = body_to_io(body)) + def write_body_path(sock, body, range) + write_body_stream(sock, inp = body_to_io(body), range) ensure body.respond_to?(:close) && inp != body and body.close end end if method_defined?(:write_body_path) - def write_body(client, body) + def write_body(client, body, range) body.respond_to?(:to_path) ? - write_body_path(client, body) : - write_body_each(client, body) + write_body_path(client, body, range) : + write_body_each(client, body, range) end else ALIASES[:write_body] = :write_body_each end # generic body writer, used for most dynamically generated responses - def write_body_each(socket, body) + def write_body_each(socket, body, range = nil) body.each { |chunk| socket.write(chunk) } ensure body.respond_to?(:close) and body.close diff --git a/lib/rainbows/response/range.rb b/lib/rainbows/response/range.rb new file mode 100644 index 0000000..4c0d4a1 --- /dev/null +++ b/lib/rainbows/response/range.rb @@ -0,0 +1,34 @@ +# -*- encoding: binary -*- +# :enddoc: +module Rainbows::Response::Range + HTTP_RANGE = 'HTTP_RANGE' + Content_Range = 'Content-Range'.freeze + Content_Length = 'Content-Length'.freeze + + # This does not support multipart responses (does anybody actually + # use those?) +headers+ is always a Rack::Utils::HeaderHash + def parse_range(env, status, headers) + if 200 == status.to_i && + (clen = headers[Content_Length]) && + /\Abytes=(\d+-\d*|\d*-\d+)\z/ =~ env[HTTP_RANGE] + a, b = $1.split(/-/) + clen = clen.to_i + if b.nil? # bytes=M- + offset = a.to_i + count = clen - offset + elsif a.empty? # bytes=-N + offset = clen - b.to_i + count = clen - offset + else # bytes=M-N + offset = a.to_i + count = b.to_i + 1 - offset + end + raise Rainbows::Response416 if count <= 0 || offset >= clen + count = clen if count > clen + headers[Content_Length] = count.to_s + headers[Content_Range] = "bytes #{offset}-#{offset+count-1}/#{clen}" + [ status, offset, count ] + end + # nil if no status + end +end |