From d2f240d7ea11738ba889668a669ffb7845d06e7b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 6 Jul 2010 09:58:40 +0000 Subject: fix string slicing under 1.9 after short writes Fortunately this only affects the hardly-used FiberSpawn and FiberPool concurrency models, and also unreleased revisions of Rev. 1.9 encoding is tricky to handle right when doing I/O in Ruby... --- lib/rainbows.rb | 1 + lib/rainbows/byte_slice.rb | 16 ++++++++++++++++ lib/rainbows/fiber/io.rb | 5 +++-- lib/rainbows/rev/client.rb | 3 ++- t/t0016-onenine-encoding-is-tricky.sh | 28 ++++++++++++++++++++++++++++ t/t0016.rb | 15 +++++++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 lib/rainbows/byte_slice.rb create mode 100755 t/t0016-onenine-encoding-is-tricky.sh create mode 100644 t/t0016.rb diff --git a/lib/rainbows.rb b/lib/rainbows.rb index 906806f..b1ec220 100644 --- a/lib/rainbows.rb +++ b/lib/rainbows.rb @@ -122,4 +122,5 @@ module Rainbows end # :startdoc: autoload :Fiber, 'rainbows/fiber' # core class + autoload :ByteSlice, 'rainbows/byte_slice' end diff --git a/lib/rainbows/byte_slice.rb b/lib/rainbows/byte_slice.rb new file mode 100644 index 0000000..09d1188 --- /dev/null +++ b/lib/rainbows/byte_slice.rb @@ -0,0 +1,16 @@ +# -*- encoding: binary -*- +module Rainbows::ByteSlice + if String.method_defined?(:encoding) + def byte_slice(buf, range) + if buf.encoding != Encoding::BINARY + buf.dup.force_encoding(Encoding::BINARY).slice!(range) + else + buf[range] + end + end + else + def byte_slice(buf, range) + buf[range] + end + end +end diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb index d4f2512..f6b8bdf 100644 --- a/lib/rainbows/fiber/io.rb +++ b/lib/rainbows/fiber/io.rb @@ -7,6 +7,7 @@ module Rainbows # interface that yields away from the current Fiber whenever # the underlying IO object cannot read or write class IO < Struct.new(:to_io, :f) + include Rainbows::ByteSlice # :stopdoc: LOCALHOST = Unicorn::HttpRequest::LOCALHOST @@ -58,8 +59,8 @@ module Rainbows def write(buf) begin - (w = to_io.write_nonblock(buf)) == buf.size and return - buf = buf[w..-1] + (w = to_io.write_nonblock(buf)) == buf.bytesize and return + buf = byte_slice(buf, w..-1) rescue Errno::EAGAIN wait_writable retry diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb index f854a63..bc8d7fa 100644 --- a/lib/rainbows/rev/client.rb +++ b/lib/rainbows/rev/client.rb @@ -4,6 +4,7 @@ module Rainbows module Rev class Client < ::Rev::IO + include Rainbows::ByteSlice include Rainbows::EvCore include Rainbows::HttpResponse G = Rainbows::G @@ -33,7 +34,7 @@ module Rainbows end # we never care for the return value, but yes, we may return # a "fake" short write from super(buf) if anybody cares. - buf = buf[w..-1] + buf = byte_slice(buf, w..-1) rescue Errno::EAGAIN break # fall through to super(buf) rescue diff --git a/t/t0016-onenine-encoding-is-tricky.sh b/t/t0016-onenine-encoding-is-tricky.sh new file mode 100755 index 0000000..8757e43 --- /dev/null +++ b/t/t0016-onenine-encoding-is-tricky.sh @@ -0,0 +1,28 @@ +#!/bin/sh +. ./test-lib.sh +t_plan 4 "proper handling of onenine encoding for $model" + +t_begin "setup and startup" && { + rainbows_setup $model + rainbows -D ./t0016.rb -c $unicorn_config + rainbows_wait_start + expect_sha1=8ff79d8115f9fe38d18be858c66aa08a1cc27a66 +} + +t_begin "response matches expected" && { + rm -f $ok + ( + curl -sSf http://$listen/ && echo ok > $ok + ) | rsha1 > $tmp + test x$expect_sha1 = x"$(cat $tmp)" +} + +t_begin "shutdown server" && { + kill -QUIT $rainbows_pid +} + +dbgcat r_err + +t_begin "check stderr" && check_stderr + +t_done diff --git a/t/t0016.rb b/t/t0016.rb new file mode 100644 index 0000000..98c9a2e --- /dev/null +++ b/t/t0016.rb @@ -0,0 +1,15 @@ +# -*- encoding: utf-8 -*- +module T0016 + CHUNK = '©' * 1024 * 1024 + BODY = (1..50).map { CHUNK } + HEADER = { + # BODY.inject(0) { |m,c| m += c.bytesize }.to_s, + 'Content-Length' => '104857600', + 'Content-Type' => 'text/plain', + } + + def self.call(env) + [ 200, HEADER, BODY ] + end +end +$0 == __FILE__ and T0016::BODY.each { |x| $stdout.syswrite(x) } -- cgit v1.2.3-24-ge0c7