Skip to content

Instantly share code, notes, and snippets.

@moonpolysoft
Created January 6, 2009 19:55
Show Gist options
  • Save moonpolysoft/43961 to your computer and use it in GitHub Desktop.
Save moonpolysoft/43961 to your computer and use it in GitHub Desktop.
diff --git a/lib/thin/connection.rb b/lib/thin/connection.rb
index e267f45..88064ff 100644
--- a/lib/thin/connection.rb
+++ b/lib/thin/connection.rb
@@ -69,25 +69,42 @@ module Thin
def post_process(result)
return unless result
+ send_results = Proc.new do |result|
+ # Set the Content-Length header if possible
+ set_content_length(result) if need_content_length?(result)
- # Set the Content-Length header if possible
- set_content_length(result) if need_content_length?(result)
+ @response.status, @response.headers, @response.body = result
- @response.status, @response.headers, @response.body = result
+ log "!! Rack application returned nil body. Probably you wanted it to be an empty string?" if @response.body.nil?
+ # Make the response persistent if requested by the client
+ @response.persistent! if @request.persistent?
- log "!! Rack application returned nil body. Probably you wanted it to be an empty string?" if @response.body.nil?
- # Make the response persistent if requested by the client
- @response.persistent! if @request.persistent?
+ # Send the response
+ @response.each do |chunk|
+ trace { chunk }
+ send_data chunk
+ end
- # Send the response
- @response.each do |chunk|
- trace { chunk }
- send_data chunk
+ # If no more request on that same connection, we close it.
+ close_connection_after_writing unless persistent?
+ end
+ if result.is_a?(Proc)
+ callback = Proc.new do |result|
+ result = result.call
+ if result.is_a?(Proc)
+ EM.next_tick do
+ callback.call(result)
+ end
+ else
+ send_results.call(result)
+ end
+ end
+ EM.next_tick do
+ callback.call(result)
+ end
+ else
+ send_results.call(result)
end
-
- # If no more request on that same connection, we close it.
- close_connection_after_writing unless persistent?
-
rescue Exception
handle_error
ensure
diff --git a/spec/connection_spec.rb b/spec/connection_spec.rb
index 151e783..fea896d 100644
--- a/spec/connection_spec.rb
+++ b/spec/connection_spec.rb
@@ -102,4 +102,31 @@ describe Connection do
it "should not set as threaded when app do not respond to deferred?" do
@connection.should_not be_threaded
end
+
+ it "should support continuations from the app" do
+ @connection.app = Proc.new do |env|
+ Proc.new do
+ [200, {}, ['']]
+ end
+ end
+ EventMachine.should_receive(:next_tick).and_yield()
+ @connection.should_receive(:send_data).twice()
+
+ @connection.process
+ end
+
+ it "should support unlimited continuations" do
+ @connection.app = Proc.new do |env|
+ Proc.new do
+ Proc.new do
+ [200, {}, ['']]
+ end
+ end
+ end
+
+ EventMachine.should_receive(:next_tick).twice().and_yield()
+ @connection.should_receive(:send_data).twice()
+
+ @connection.process
+ end
end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment