about summary refs log tree commit homepage
path: root/lib/unicorn/http_request.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-27 17:26:03 -0700
committerEric Wong <normalperson@yhbt.net>2009-03-27 17:26:03 -0700
commitc83b5a903a076fda67c7d062da1ad6ff9337fdd1 (patch)
treec543d22f6227be5f373274a09a05b3ce237fe869 /lib/unicorn/http_request.rb
parent9509f414a88281c93f0a1cb28123b8ae7538ee7f (diff)
downloadunicorn-c83b5a903a076fda67c7d062da1ad6ff9337fdd1.tar.gz
This reworks error handling throughout the entire stack to be
more Ruby-ish.  Exceptions are raised instead of forcing the
us to check return values.

  If a client is sending us a bad request, we send a 400.

  If unicorn or app breaks in an unexpected way, we'll
  send a 500.

Both of these last-resort error responses are sent using
IO#write_nonblock to avoid tying Unicorn up longer than
necessary and all exceptions raised are ignored.

Sending a valid HTTP response back should reduce the chance of
us from being marked as down or broken by a load balancer.
Previously, some load balancers would mark us as down if we close
a socket without sending back a valid response; so make a best
effort to send one.  If for some reason we cannot write a valid
response, we're still susceptible to being marked as down.

A successful HttpResponse.write() call will now close the socket
immediately (instead of doing it higher up the stack).  This
ensures the errors will never get written to the socket on a
successful response.
Diffstat (limited to 'lib/unicorn/http_request.rb')
-rw-r--r--lib/unicorn/http_request.rb12
1 files changed, 4 insertions, 8 deletions
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 7d69943..70378ef 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -75,13 +75,13 @@ module Unicorn
                           socket.unicorn_peeraddr}): #{e.inspect}"
         @logger.error "REQUEST DATA: #{data.inspect}\n---\n" \
                       "PARAMS: #{@params.inspect}\n---\n"
-        nil
+        raise e
     end
 
     private
 
     # Handles dealing with the rest of the request
-    # returns true if successful, false if not
+    # returns a Rack environment if successful, raises an exception if not
     def handle_body(socket)
       http_body = @params[Const::HTTP_BODY]
       content_length = @params[Const::CONTENT_LENGTH].to_i
@@ -101,9 +101,7 @@ module Unicorn
       # Some clients (like FF1.0) report 0 for body and then send a body.
       # This will probably truncate them but at least the request goes through
       # usually.
-      if remain > 0
-        read_body(socket, remain) or return nil # fail!
-      end
+      read_body(socket, remain) if remain > 0
       @body.rewind
       @body.sysseek(0) if @body.respond_to?(:sysseek)
 
@@ -153,16 +151,14 @@ module Unicorn
         # writes always write the requested amount on a POSIX filesystem
         remain -= @body.syswrite(read_socket(socket))
       end
-      true # success!
     rescue Object => e
       @logger.error "Error reading HTTP body: #{e.inspect}"
-      socket.closed? or socket.close rescue nil
 
       # Any errors means we should delete the file, including if the file
       # is dumped.  Truncate it ASAP to help avoid page flushes to disk.
       @body.truncate(0) rescue nil
       reset
-      false
+      raise e
     end
 
     # read(2) on "slow" devices like sockets can be interrupted by signals