about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-28 11:20:46 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-28 11:30:38 +0000
commitf309cfaf70cbffd7a39208da869e47784e4cb41b (patch)
tree3ede259f73e09381fe4cd645d2f1763c1b6dcadc /lib
parentd3ecf49abeda931e23023e1afb93d4c6145f559a (diff)
downloadrainbows-f309cfaf70cbffd7a39208da869e47784e4cb41b.tar.gz
Since TCP sockets stream, HTTP requests do not come in at
well-defined boundaries and it's possible for pipelined requests
to come in in a staggered form.  We need to ensure our
receive_data callback doesn't fire any actions at all while
responding with a deferrable @body.

We still need to be careful about buffering, since EM does not
appear to allow temporarily disabling read events (without
pausing writes), so we shutdown the read end of the socket
if it reaches a maximum header size limit.
Diffstat (limited to 'lib')
-rw-r--r--lib/rainbows/event_machine.rb27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index 7fe9864..757817d 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -62,7 +62,19 @@ module Rainbows
       end
 
       alias write send_data
-      alias receive_data on_read
+
+      def receive_data(data)
+        # To avoid clobbering the current streaming response
+        # (often a static file), we do not attempt to process another
+        # request on the same connection until the first is complete
+        if @body
+          @buf << data
+          @_io.shutdown(Socket::SHUT_RD) if @buf.size > 0x1c000
+          return EM.next_tick { receive_data('') }
+        else
+          on_read(data)
+        end
+      end
 
       def quit
         super
@@ -70,11 +82,6 @@ module Rainbows
       end
 
       def app_call
-        # To avoid clobbering the current streaming response
-        # (often a static file), we do not attempt to process another
-        # request on the same connection until the first is complete
-        return EM.next_tick { app_call } if @body
-
         set_comm_inactivity_timeout 0
         @env[RACK_INPUT] = @input
         @env[REMOTE_ADDR] = @remote_addr
@@ -93,10 +100,10 @@ module Rainbows
           @env.clear
           @hp.reset
           @state = :headers
-          if @body.nil? && @hp.headers(@env, @buf)
-            EM.next_tick { on_read('') }
-          else
+          if @buf.empty?
             set_comm_inactivity_timeout(G.kato)
+          else
+            EM.next_tick { receive_data('') }
           end
         end
       end
@@ -130,7 +137,7 @@ module Rainbows
             @body.callback do
               body.close if body.respond_to?(:close)
               @body = nil
-              alive ? on_read('') : quit
+              alive ? receive_data('') : quit
             end
             return
           elsif st.socket? || st.pipe?