about summary refs log tree commit homepage
path: root/lib/rainbows/event_machine/response.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/event_machine/response.rb')
-rw-r--r--lib/rainbows/event_machine/response.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/rainbows/event_machine/response.rb b/lib/rainbows/event_machine/response.rb
new file mode 100644
index 0000000..49bcbd5
--- /dev/null
+++ b/lib/rainbows/event_machine/response.rb
@@ -0,0 +1,38 @@
+# -*- encoding: binary -*-
+# :enddoc:
+module Rainbows::EventMachine::Response
+  def write_response(status, headers, body, alive)
+    if body.respond_to?(:errback) && body.respond_to?(:callback)
+      @body = body
+      body.callback { quit }
+      body.errback { quit }
+      alive = true
+    elsif body.respond_to?(:to_path)
+      st = File.stat(path = body.to_path)
+
+      if st.file?
+        write_headers(status, headers, alive)
+        @body = stream_file_data(path)
+        @body.errback do
+          body.close if body.respond_to?(:close)
+          quit
+        end
+        @body.callback do
+          body.close if body.respond_to?(:close)
+          @body = nil
+          alive ? receive_data(nil) : quit
+        end
+        return
+      elsif st.socket? || st.pipe?
+        io = body_to_io(@body = body)
+        chunk = stream_response_headers(status, headers, alive)
+        m = chunk ? Rainbows::EventMachine::ResponseChunkPipe :
+                    Rainbows::EventMachine::ResponsePipe
+        return EM.watch(io, m, self).notify_readable = true
+      end
+      # char or block device... WTF? fall through to body.each
+    end
+    super(status, headers, body, alive)
+    quit unless alive
+  end
+end