about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/mongrel.rb44
-rw-r--r--lib/mongrel/http_request.rb21
-rw-r--r--lib/mongrel/http_response.rb21
3 files changed, 56 insertions, 30 deletions
diff --git a/lib/mongrel.rb b/lib/mongrel.rb
index f09a617..e3c56be 100644
--- a/lib/mongrel.rb
+++ b/lib/mongrel.rb
@@ -17,6 +17,7 @@ require 'mongrel/gems'
 Mongrel::Gems.require 'cgi_multipart_eof_fix'
 Mongrel::Gems.require 'fastthread'
 require 'thread'
+require 'rack'
 
 # Ruby Mongrel
 require 'mongrel/cgi'
@@ -88,21 +89,21 @@ module Mongrel
     # The throttle parameter is a sleep timeout (in hundredths of a second) that is placed between
     # socket.accept calls in order to give the server a cheap throttle time.  It defaults to 0 and
     # actually if it is 0 then the sleep is not done at all.
-    def initialize(host, port, num_processors=950, throttle=0, timeout=60)
-      
+    def initialize(host, port, app, opts = {})
       tries = 0
       @socket = TCPServer.new(host, port)
       if defined?(Fcntl::FD_CLOEXEC)
         @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
       end
-      
       @classifier = URIClassifier.new
       @host = host
       @port = port
       @workers = ThreadGroup.new
-      @throttle = throttle / 100.0
-      @num_processors = num_processors
-      @timeout = timeout
+      # Set default opts
+      @app = app
+      @num_processors = opts.delete(:num_processors)
+      @throttle       = (opts.delete(:throttle) || 0) / 100
+      @timeout        = opts.delete(:timeout) || 60
     end
 
     # Does the majority of the IO processing.  It has been written in Ruby using
@@ -134,6 +135,7 @@ module Mongrel
 
             raise "No REQUEST PATH" if not params[Const::REQUEST_PATH]
 
+
             script_name, path_info, handlers = @classifier.resolve(params[Const::REQUEST_PATH])
 
             if handlers
@@ -154,20 +156,23 @@ module Mongrel
 
               # in the case of large file uploads the user could close the socket, so skip those requests
               break if request.body == nil  # nil signals from HttpRequest::initialize that the request was aborted
+              raise "CALLING APPPPPPP"
+              app_responce = @app.call(request.env)
+              response = HttpResponse.new(client, app_response).start
 
               # request is good so far, continue processing the response
-              response = HttpResponse.new(client)
-
-              # Process each handler in registered order until we run out or one finalizes the response.
-              handlers.each do |handler|
-                handler.process(request, response)
-                break if response.done or client.closed?
-              end
-
-              # And finally, if nobody closed the response off, we finalize it.
-              unless response.done or client.closed?
-                response.finished
-              end
+              # response = HttpResponse.new(client)
+
+              # # Process each handler in registered order until we run out or one finalizes the response.
+              # handlers.each do |handler|
+              #   handler.process(request, response)
+              #   break if response.done or client.closed?
+              # end
+
+              # # And finally, if nobody closed the response off, we finalize it.
+              # unless response.done or client.closed?
+              #   response.finished
+              # end
             else
               # Didn't find it, return a stock 404 response.
               client.write(Const::ERROR_404_RESPONSE)
@@ -260,7 +265,8 @@ module Mongrel
     
     # Runs the thing.  It returns the thread used so you can "join" it.  You can also
     # access the HttpServer::acceptor attribute to get the thread later.
-    def run
+    def start!
+      p @num_processors
       BasicSocket.do_not_reverse_lookup=true
 
       configure_socket_options
diff --git a/lib/mongrel/http_request.rb b/lib/mongrel/http_request.rb
index c8d4ce4..8df3a5a 100644
--- a/lib/mongrel/http_request.rb
+++ b/lib/mongrel/http_request.rb
@@ -58,6 +58,25 @@ module Mongrel
       @body.rewind if @body
     end
 
+    # returns an environment which is rackable
+    # http://rack.rubyforge.org/doc/files/SPEC.html
+    def env
+      env = params.clone
+      env.delete "HTTP_CONTENT_TYPE"
+      env.delete "HTTP_CONTENT_LENGTH"
+      env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
+      env.update({"rack.version" => [0,1],
+              "rack.input" => @body,
+              "rack.errors" => STDERR,
+
+              "rack.multithread" => true,
+              "rack.multiprocess" => false, # ???
+              "rack.run_once" => false,
+
+              "rack.url_scheme" => "http",
+            })
+    end
+
     # updates all dispatchers about our progress
     def update_request_progress(clen, total)
       return if @dispatchers.nil? || @dispatchers.empty?
@@ -152,4 +171,4 @@ module Mongrel
       return params
     end
   end
-end \ No newline at end of file
+end
diff --git a/lib/mongrel/http_response.rb b/lib/mongrel/http_response.rb
index 3076712..08d4d75 100644
--- a/lib/mongrel/http_response.rb
+++ b/lib/mongrel/http_response.rb
@@ -39,12 +39,13 @@ module Mongrel
     attr_reader :header_sent
     attr_reader :status_sent
 
-    def initialize(socket)
+    def initialize(socket, app_responce)
       @socket = socket
-      @body = StringIO.new
-      @status = 404
+      @app_responce = app_responce
+      @body = app_responce[2]
+      @status = app_responce[0]
       @reason = nil
-      @header = HeaderOut.new(StringIO.new)
+      @header = HeaderOut.new(app_responce[1])
       @header[Const::DATE] = Time.now.httpdate
       @body_sent = false
       @header_sent = false
@@ -59,10 +60,10 @@ module Mongrel
     # by simple passing "finalize=true" to the start method.  By default
     # all handlers run and then mongrel finalizes the request when they're
     # all done.
-    def start(status=200, finalize=false, reason=nil)
-      @status = status.to_i
-      @reason = reason
-      yield @header, @body
+    # TODO: docs
+    def start #(status=200, finalize=false, reason=nil)
+      @reason = nil # TODO take this out
+      out.write(@body)
       finished if finalize
     end
 
@@ -78,7 +79,7 @@ module Mongrel
         # XXX Dubious ( http://mongrel.rubyforge.org/ticket/19 )
         @header.out.close
         @header = HeaderOut.new(StringIO.new)
-        
+
         @body.close
         @body = StringIO.new
       end
@@ -163,4 +164,4 @@ module Mongrel
     end
 
   end
-end \ No newline at end of file
+end