[macruby-changes] [4296] ControlTower/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sat Jun 26 15:12:51 PDT 2010


Revision: 4296
          http://trac.macosforge.org/projects/ruby/changeset/4296
Author:   joshua.ballanco at apple.com
Date:     2010-06-26 15:12:47 -0700 (Sat, 26 Jun 2010)
Log Message:
-----------
Move all app call handling into the rack_socket

Modified Paths:
--------------
    ControlTower/trunk/ext/CTParser/CTParser.m
    ControlTower/trunk/lib/control_tower/rack_socket.rb
    ControlTower/trunk/lib/control_tower/server.rb

Modified: ControlTower/trunk/ext/CTParser/CTParser.m
===================================================================
--- ControlTower/trunk/ext/CTParser/CTParser.m	2010-06-26 08:19:09 UTC (rev 4295)
+++ ControlTower/trunk/ext/CTParser/CTParser.m	2010-06-26 22:12:47 UTC (rev 4296)
@@ -124,13 +124,9 @@
     }
 
     // If we've been given any part of the body, put it here
-    NSMutableData *body = [environment objectForKey:@"rack.input"];
-    if (body != nil) {
-        [body appendData:[NSData dataWithBytes:at length:length]];
-    }
-    else {
-	NSLog(@"Hmm...you seem to have body data but no where to put it. That's probably an error.");
-    }
+    NSMutableData *body = [[NSMutableData alloc] init];
+    [body appendData:[NSData dataWithBytes:at length:length]];
+    [environment setObject:body forKey:@"rack.input"];
 }
 
 @implementation CTParser

Modified: ControlTower/trunk/lib/control_tower/rack_socket.rb
===================================================================
--- ControlTower/trunk/lib/control_tower/rack_socket.rb	2010-06-26 08:19:09 UTC (rev 4295)
+++ ControlTower/trunk/lib/control_tower/rack_socket.rb	2010-06-26 22:12:47 UTC (rev 4296)
@@ -10,7 +10,7 @@
     VERSION = [1,0].freeze
 
     def initialize(host, port, server, concurrent)
-      @server = server
+      @app = server.app
       @socket = TCPServer.new(host, port)
       @status = :closed # Start closed and give the server time to start
 
@@ -19,6 +19,7 @@
         @request_queue = Dispatch::Queue.concurrent
         puts "Caution! Wake turbulance from heavy aircraft landing on parallel runway.\n(Parallel Request Action ENABLED!)"
       else
+        @multithread = false
         @request_queue = Dispatch::Queue.new('com.apple.ControlTower.rack_socket_queue')
       end
       @request_group = Dispatch::Group.new
@@ -30,15 +31,53 @@
         connection = @socket.accept
 
         @request_queue.async(@request_group) do
-          env = prepare_environment
+          env = { 'rack.errors' => $stderr,
+                  'rack.multiprocess' => false,
+                  'rack.multithread' => @multithread,
+                  'rack.run_once' => false,
+                  'rack.version' => VERSION }
           begin
             request_data = parse!(connection, env)
             if request_data
               request_data['REMOTE_ADDR'] = connection.addr[3]
-              response_data = @server.handle_request(request_data)
-              response_data.each do |chunk|
-                connection.write chunk
+              status, headers, body = @app.call(request_data)
+
+              # Unless somebody's already set it for us (or we don't need it), set the Content-Length
+              unless (status == -1 ||
+                      (status >= 100 and status <= 199) ||
+                      status == 204 ||
+                      status == 304 ||
+                      headers.has_key?('Content-Length'))
+                headers['Content-Length'] = if body.respond_to?(:each)
+                                              size = 0
+                                              body.each { |x| size += x.bytesize }
+                                              size
+                                            else
+                                              body.bytesize
+                                            end
               end
+
+              # TODO -- We don't handle keep-alive connections yet
+              headers['Connection'] = 'close'
+
+              resp = "HTTP/1.1 #{status}\r\n"
+              headers.each do |header, value|
+                resp << "#{header}: #{value}\r\n"
+              end
+              resp << "\r\n"
+
+              # Start writing the response
+              connection.write resp
+
+              # Finish writing out the body
+              if body.respond_to?(:each)
+                body.each do |chunk|
+                  connection.write chunk
+                end
+              else
+                connection.write body
+              end
+
             else
               $stderr.puts "Error: No request data received!"
             end
@@ -72,15 +111,6 @@
 
     private
 
-    def prepare_environment
-      { 'rack.errors' => $stderr,
-        'rack.input' => NSMutableData.new,
-        'rack.multiprocess' => false,
-        'rack.multithread' => @multithread,
-        'rack.run_once' => false,
-        'rack.version' => VERSION }
-    end
-
     def parse!(connection, env)
       parser = Thread.current[:http_parser] ||= ::CTParser.new
       parser.reset

Modified: ControlTower/trunk/lib/control_tower/server.rb
===================================================================
--- ControlTower/trunk/lib/control_tower/server.rb	2010-06-26 08:19:09 UTC (rev 4295)
+++ ControlTower/trunk/lib/control_tower/server.rb	2010-06-26 22:12:47 UTC (rev 4296)
@@ -3,6 +3,8 @@
 
 module ControlTower
   class Server
+    attr_reader :app
+
     def initialize(app, options)
       @app = app
       parse_options(options)
@@ -19,10 +21,6 @@
       @socket.open
     end
 
-    def handle_request(env)
-      wrap_output(*@app.call(env))
-    end
-
     private
 
     def parse_options(opt)
@@ -30,42 +28,5 @@
       @host = opt[:host] || `hostname`.chomp
       @concurrent = opt[:concurrent]
     end
-
-    def wrap_output(status, headers, body)
-      # Unless somebody's already set it for us (or we don't need it), set the Content-Length
-      unless (status == -1 ||
-              (status >= 100 and status <= 199) ||
-              status == 204 ||
-              status == 304 ||
-              headers.has_key?("Content-Length"))
-        headers["Content-Length"] = if body.respond_to?(:each)
-          size = 0
-          body.each { |x| size += x.bytesize }
-          size
-        else
-          body.bytesize
-        end
-      end
-
-      # TODO -- We don't handle keep-alive connections yet
-      headers["Connection"] = 'close'
-
-      resp = "HTTP/1.1 #{status}\r\n"
-      headers.each do |header, value|
-        resp << "#{header}: #{value}\r\n"
-      end
-      resp << "\r\n"
-
-      # Assemble our response...
-      chunks = [resp]
-      if body.respond_to?(:each)
-        body.each do |chunk|
-          chunks << chunk
-        end
-      else
-        chunks << body
-      end
-      chunks
-    end
   end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100626/ba8ccbbb/attachment.html>


More information about the macruby-changes mailing list