summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-06-29 19:21:34 -0700
committerEric Wong <normalperson@yhbt.net>2009-06-29 19:28:51 -0700
commit516c4a8686911a3b6c5e1837d183cd6f515e877c (patch)
treed40a484cef552ebdae28d63de1f66b0049ba3596
parentec3ac2e4291026a3ebf687d7d0c45c34acac111e (diff)
By responding with a "HTTP/1.1 100 Continue" response to
encourage a client to send the rest of the body.

This is part of the HTTP/1.1 standard but not often implemented
by servers:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3

This will speed up curl uploads since curl sleeps up to 1 second if
no response is received:
http://curl.haxx.se/docs/faq.html#My_HTTP_POST_or_PUT_requests_are
-rw-r--r--TODO2
-rw-r--r--lib/unicorn/const.rb2
-rw-r--r--lib/unicorn/http_request.rb3
3 files changed, 5 insertions, 2 deletions
diff --git a/TODO b/TODO
index 2d5a8c1..65b3328 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,5 @@
 * "Trailers:" support with TE:chunked requests
 
-* Handling of "Expect: 100-continue" header to speed up "curl -T"
-
 * Support HTTP/1.1 keepalive if (and probably only if) pipelining.
   We can do this by testing readability of socket immediately after
   the response is written.
diff --git a/lib/unicorn/const.rb b/lib/unicorn/const.rb
index b81dce0..9dcf031 100644
--- a/lib/unicorn/const.rb
+++ b/lib/unicorn/const.rb
@@ -24,12 +24,14 @@ module Unicorn
     # common errors we'll send back
     ERROR_400_RESPONSE = "HTTP/1.1 400 Bad Request\r\n\r\n".freeze
     ERROR_500_RESPONSE = "HTTP/1.1 500 Internal Server Error\r\n\r\n".freeze
+    EXPECT_100_RESPONSE = "HTTP/1.1 100 Continue\r\n\r\n"
 
     # A frozen format for this is about 15% faster
     HTTP_TRANSFER_ENCODING = 'HTTP_TRANSFER_ENCODING'.freeze
     CONTENT_LENGTH="CONTENT_LENGTH".freeze
     REMOTE_ADDR="REMOTE_ADDR".freeze
     HTTP_X_FORWARDED_FOR="HTTP_X_FORWARDED_FOR".freeze
+    HTTP_EXPECT="HTTP_EXPECT".freeze
     RACK_INPUT="rack.input".freeze
     STREAM_INPUT="unicorn.stream_input".freeze
   end
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 136345f..553c3df 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -88,6 +88,9 @@ module Unicorn
     # Handles dealing with the rest of the request
     # returns a Rack environment if successful
     def handle_body(socket)
+      if /\A100-continue\z/i =~ PARAMS[Const::HTTP_EXPECT]
+        socket.write(Const::EXPECT_100_RESPONSE)
+      end
       PARAMS[Const::RACK_INPUT] = if (body = PARAMS.delete(:http_body))
         length = PARAMS[Const::CONTENT_LENGTH].to_i