about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-19 18:47:23 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-19 23:19:25 +0000
commit82ea9b442a9edaae6dc3b06a5c61035b2c2924c9 (patch)
tree04b7e7e15db51a0830de7c9fc1e6ed5725d509af
parent39f264173717287eda70910e7a24fbafd21a4a7e (diff)
downloadunicorn-82ea9b442a9edaae6dc3b06a5c61035b2c2924c9.tar.gz
This allows apps/middlewares on Rainbows! that rely on env in
the response_body#close to hold onto the env.
-rw-r--r--ext/unicorn_http/unicorn_http.rl8
-rw-r--r--test/unit/test_http_parser_ng.rb17
2 files changed, 18 insertions, 7 deletions
diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl
index 6cc2958..42fcf1d 100644
--- a/ext/unicorn_http/unicorn_http.rl
+++ b/ext/unicorn_http/unicorn_http.rl
@@ -21,6 +21,7 @@
 #define UH_FL_REQEOF 0x40
 #define UH_FL_KAVERSION 0x80
 #define UH_FL_HASHEADER 0x100
+#define UH_FL_TO_CLEAR 0x200
 
 /* all of these flags need to be set for keepalive to be supported */
 #define UH_FL_KEEPALIVE (UH_FL_KAVERSION | UH_FL_REQEOF | UH_FL_HASHEADER)
@@ -537,6 +538,11 @@ static VALUE HttpParser_parse(VALUE self)
   struct http_parser *hp = data_get(self);
   VALUE data = hp->buf;
 
+  if (hp->flags == UH_FL_TO_CLEAR) {
+    hp->flags = 0;
+    rb_funcall(hp->env, id_clear, 0);
+  }
+
   http_parser_execute(hp, RSTRING_PTR(data), RSTRING_LEN(data));
   VALIDATE_MAX_LENGTH(hp->offset, HEADER);
 
@@ -630,7 +636,7 @@ static VALUE HttpParser_next(VALUE self)
 
   if (HP_FL_ALL(hp, KEEPALIVE)) {
     http_parser_init(hp);
-    rb_funcall(hp->env, id_clear, 0);
+    hp->flags = UH_FL_TO_CLEAR;
     return Qtrue;
   }
   return Qfalse;
diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb
index ce6c6e6..ea46ea8 100644
--- a/test/unit/test_http_parser_ng.rb
+++ b/test/unit/test_http_parser_ng.rb
@@ -504,9 +504,10 @@ class HttpParserNgTest < Test::Unit::TestCase
   end
 
   def test_pipelined_requests
+    host = "example.com"
     expect = {
-      "HTTP_HOST" => "example.com",
-      "SERVER_NAME" => "example.com",
+      "HTTP_HOST" => host,
+      "SERVER_NAME" => host,
       "REQUEST_PATH" => "/",
       "rack.url_scheme" => "http",
       "SERVER_PROTOCOL" => "HTTP/1.1",
@@ -517,15 +518,19 @@ class HttpParserNgTest < Test::Unit::TestCase
       "REQUEST_METHOD" => "GET",
       "QUERY_STRING" => ""
     }
-    str = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
-    @parser.buf << (str * 2)
+    req1 = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
+    req2 = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n"
+    @parser.buf << (req1 + req2)
     env1 = @parser.parse.dup
     assert_equal expect, env1
-    assert_equal str, @parser.buf
+    assert_equal req2, @parser.buf
     assert ! @parser.env.empty?
     assert @parser.next?
-    assert @parser.env.empty?
+    assert_equal expect, @parser.env
     env2 = @parser.parse.dup
+    host.replace "www.example.com"
+    assert_equal "www.example.com", expect["HTTP_HOST"]
+    assert_equal "www.example.com", expect["SERVER_NAME"]
     assert_equal expect, env2
     assert_equal "", @parser.buf
   end