From 82ea9b442a9edaae6dc3b06a5c61035b2c2924c9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 19 Dec 2010 18:47:23 +0000 Subject: http: delay clearing env on HttpParser#next? This allows apps/middlewares on Rainbows! that rely on env in the response_body#close to hold onto the env. --- ext/unicorn_http/unicorn_http.rl | 8 +++++++- test/unit/test_http_parser_ng.rb | 17 +++++++++++------ 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 -- cgit v1.2.3-24-ge0c7