From 62c844e343978f233e4f2567fb344411c39e263c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Jan 2011 14:06:00 -0800 Subject: http_parser: add clear method, deprecate reset But allows small optimizations to be made to avoid constant/instance variable lookups later :) --- ext/unicorn_http/unicorn_http.rl | 30 ++++++++++++++++++++++++++---- lib/unicorn/http_request.rb | 2 +- test/unit/test_http_parser.rb | 12 ++++++------ test/unit/test_http_parser_ng.rb | 6 +++--- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index fd259b6..292601c 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -572,18 +572,39 @@ static VALUE HttpParser_init(VALUE self) /** * call-seq: - * parser.reset => nil + * parser.clear => parser * * Resets the parser to it's initial state so that you can reuse it * rather than making new ones. */ -static VALUE HttpParser_reset(VALUE self) +static VALUE HttpParser_clear(VALUE self) { struct http_parser *hp = data_get(self); http_parser_init(hp); rb_funcall(hp->env, id_clear, 0); + return self; +} + +/** + * call-seq: + * parser.reset => nil + * + * Resets the parser to it's initial state so that you can reuse it + * rather than making new ones. + * + * This method is deprecated and to be removed in Unicorn 4.x + */ +static VALUE HttpParser_reset(VALUE self) +{ + static int warned; + + if (!warned) { + rb_warn("Unicorn::HttpParser#reset is deprecated; " + "use Unicorn::HttpParser#clear instead"); + } + HttpParser_clear(self); return Qnil; } @@ -854,8 +875,9 @@ void Init_unicorn_http(void) init_globals(); rb_define_alloc_func(cHttpParser, HttpParser_alloc); - rb_define_method(cHttpParser, "initialize", HttpParser_init,0); - rb_define_method(cHttpParser, "reset", HttpParser_reset,0); + rb_define_method(cHttpParser, "initialize", HttpParser_init, 0); + rb_define_method(cHttpParser, "clear", HttpParser_clear, 0); + rb_define_method(cHttpParser, "reset", HttpParser_reset, 0); rb_define_method(cHttpParser, "parse", HttpParser_parse, 0); rb_define_method(cHttpParser, "headers", HttpParser_headers, 2); rb_define_method(cHttpParser, "trailers", HttpParser_headers, 2); diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index 1e3ac26..2c52631 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -50,7 +50,7 @@ class Unicorn::HttpParser # This does minimal exception trapping and it is up to the caller # to handle any socket errors (e.g. user aborted upload). def read(socket) - reset + clear e = env # From http://www.ietf.org/rfc/rfc3875: diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb index a11740d..9da43c9 100644 --- a/test/unit/test_http_parser.rb +++ b/test/unit/test_http_parser.rb @@ -28,7 +28,7 @@ class HttpParserTest < Test::Unit::TestCase assert_equal '', req['QUERY_STRING'] assert parser.keepalive? - parser.reset + parser.clear req.clear http = "G" @@ -326,7 +326,7 @@ class HttpParserTest < Test::Unit::TestCase assert_raises(HttpParserError) { parser.headers(req, bad_http) } # make sure we can recover - parser.reset + parser.clear req.clear assert_equal req, parser.headers(req, "GET / HTTP/1.0\r\n\r\n") assert ! parser.keepalive? @@ -569,7 +569,7 @@ class HttpParserTest < Test::Unit::TestCase get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-#{rand_data(1024, 1024+(c*1024))}: Test\r\n\r\n" assert_raises Unicorn::HttpParserError do parser.headers({}, get) - parser.reset + parser.clear end end @@ -578,7 +578,7 @@ class HttpParserTest < Test::Unit::TestCase get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-Test: #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n" assert_raises Unicorn::HttpParserError do parser.headers({}, get) - parser.reset + parser.clear end end @@ -587,7 +587,7 @@ class HttpParserTest < Test::Unit::TestCase get << "X-Test: test\r\n" * (80 * 1024) assert_raises Unicorn::HttpParserError do parser.headers({}, get) - parser.reset + parser.clear end # finally just that random garbage gets blocked all the time @@ -595,7 +595,7 @@ class HttpParserTest < Test::Unit::TestCase get = "GET #{rand_data(1024, 1024+(c*1024), false)} #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n" assert_raises Unicorn::HttpParserError do parser.headers({}, get) - parser.reset + parser.clear end end diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb index e080d9c..b60afdf 100644 --- a/test/unit/test_http_parser_ng.rb +++ b/test/unit/test_http_parser_ng.rb @@ -91,7 +91,7 @@ class HttpParserNgTest < Test::Unit::TestCase @parser.parse end assert @parser.keepalive? - @parser.reset + @parser.clear assert ! @parser.keepalive? assert ! @parser.next? end @@ -491,7 +491,7 @@ class HttpParserNgTest < Test::Unit::TestCase }.each do |uri,expect| assert_equal req, @parser.headers(req.clear, str % [ uri ]) req = req.dup - @parser.reset + @parser.clear assert_equal uri, req["REQUEST_URI"], "REQUEST_URI mismatch" assert_equal expect[qs], req[qs], "#{qs} mismatch" assert_equal expect[pi], req[pi], "#{pi} mismatch" @@ -516,7 +516,7 @@ class HttpParserNgTest < Test::Unit::TestCase }.each do |uri,expect| assert_equal req, @parser.headers(req.clear, str % [ uri ]) req = req.dup - @parser.reset + @parser.clear assert_equal uri, req["REQUEST_URI"], "REQUEST_URI mismatch" assert_equal "example.com", req["HTTP_HOST"], "Host: mismatch" assert_equal expect[qs], req[qs], "#{qs} mismatch" -- cgit v1.2.3-24-ge0c7