about summary refs log tree commit homepage
path: root/lib/rainbows/fiber
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-10-20 17:48:58 -0700
committerEric Wong <normalperson@yhbt.net>2010-10-21 00:49:36 +0000
commitd4a2b5dd2b85f4b2d3bb120ee1e1b0dde31bc25c (patch)
tree85866cc775a213216701ccaedf16061f15d13975 /lib/rainbows/fiber
parenta085ba3586756ac1f778d2862f75889de2449b0e (diff)
downloadrainbows-d4a2b5dd2b85f4b2d3bb120ee1e1b0dde31bc25c.tar.gz
We get basic internal API changes from Unicorn,
code simplifications coming next.
Diffstat (limited to 'lib/rainbows/fiber')
-rw-r--r--lib/rainbows/fiber/io.rb34
-rw-r--r--lib/rainbows/fiber/rev.rb19
2 files changed, 36 insertions, 17 deletions
diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb
index f83b8b7..571f070 100644
--- a/lib/rainbows/fiber/io.rb
+++ b/lib/rainbows/fiber/io.rb
@@ -10,7 +10,7 @@ module Rainbows
     # TODO: subclass off IO and include Kgio::SocketMethods instead
     class IO < Struct.new(:to_io, :f)
       # :stopdoc:
-      LOCALHOST = Unicorn::HttpRequest::LOCALHOST
+      LOCALHOST = Kgio::LOCALHOST
 
       # needed to write errors with
       def write_nonblock(buf)
@@ -82,14 +82,36 @@ module Rainbows
       end
 
       def readpartial(length, buf = "")
-        begin
-          to_io.read_nonblock(length, buf)
-        rescue Errno::EAGAIN
-          wait_readable
-          retry
+        if to_io.respond_to?(:kgio_tryread)
+          # TODO: use kgio_read!
+          begin
+            rv = to_io.kgio_tryread(length, buf)
+            case rv
+            when nil
+              raise EOFError, "end of file reached", []
+            when Kgio::WaitReadable
+              wait_readable
+            else
+              return rv
+            end
+          end while true
+        else
+          begin
+            to_io.read_nonblock(length, buf)
+          rescue Errno::EAGAIN
+            wait_readable
+            retry
+          end
         end
       end
 
+      def kgio_read(*args)
+        to_io.kgio_read(*args)
+      end
+
+      def kgio_read!(*args)
+        to_io.kgio_read!(*args)
+      end
     end
   end
 end
diff --git a/lib/rainbows/fiber/rev.rb b/lib/rainbows/fiber/rev.rb
index 2c1abb7..6969f5b 100644
--- a/lib/rainbows/fiber/rev.rb
+++ b/lib/rainbows/fiber/rev.rb
@@ -78,16 +78,17 @@ module Rainbows::Fiber
       def process(io)
         G.cur += 1
         client = FIO.new(io, ::Fiber.current)
-        buf = client.read_timeout or return
         hp = HttpParser.new
-        env = {}
+        client.readpartial(16384, buf = hp.buf)
 
         begin # loop
-          buf << (client.read_timeout or return) until hp.headers(env, buf)
+          until env = hp.parse
+            buf << (client.read_timeout or return)
+          end
 
           env[CLIENT_IO] = client
           env[RACK_INPUT] = 0 == hp.content_length ?
-                    HttpRequest::NULL_IO : TeeInput.new(client, env, hp, buf)
+                    HttpRequest::NULL_IO : TeeInput.new(client, hp)
           env[REMOTE_ADDR] = io.kgio_addr
           status, headers, body = APP.call(env.update(RACK_DEFAULTS))
 
@@ -100,16 +101,12 @@ module Rainbows::Fiber
           if hp.headers?
             headers = HH.new(headers)
             range = make_range!(env, status, headers) and status = range.shift
-            headers[CONNECTION] = if hp.keepalive? && G.alive
-              KEEP_ALIVE
-            else
-              env = false
-              CLOSE
-            end
+            env = hp.keepalive? && G.alive
+            headers[CONNECTION] = env ? KEEP_ALIVE : CLOSE
             client.write(response_header(status, headers))
           end
           write_body(client, body, range)
-        end while env && env.clear && hp.reset.nil?
+        end while env && hp.reset.nil?
       rescue => e
         Error.write(io, e)
       ensure