From 60a9ec94f1f738f881e67f0a881c44c104f07c04 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 6 Nov 2010 10:30:44 +0800 Subject: http_parser: add HttpParser#next? method An easy combination of the existing HttpParser#keepalive? and HttpParser#reset methods, this makes it easier to implement persistence. --- ext/unicorn_http/unicorn_http.rl | 20 ++++++++++++++++++++ test/unit/test_http_parser_ng.rb | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index fb99f9c..147750a 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -617,6 +617,25 @@ static VALUE HttpParser_keepalive(VALUE self) return HP_FL_ALL(hp, KEEPALIVE) ? Qtrue : Qfalse; } +/** + * call-seq: + * parser.next? => true or false + * + * Exactly like HttpParser#keepalive?, except it will reset the internal + * parser state if it returns true. + */ +static VALUE HttpParser_next(VALUE self) +{ + struct http_parser *hp = data_get(self); + + if (HP_FL_ALL(hp, KEEPALIVE)) { + http_parser_init(hp); + rb_funcall(hp->env, id_clear, 0); + return Qtrue; + } + return Qfalse; +} + /** * call-seq: * parser.headers? => true or false @@ -736,6 +755,7 @@ void Init_unicorn_http(void) rb_define_method(cHttpParser, "body_eof?", HttpParser_body_eof, 0); rb_define_method(cHttpParser, "keepalive?", HttpParser_keepalive, 0); rb_define_method(cHttpParser, "headers?", HttpParser_has_headers, 0); + rb_define_method(cHttpParser, "next?", HttpParser_next, 0); rb_define_method(cHttpParser, "buf", HttpParser_buf, 0); rb_define_method(cHttpParser, "env", HttpParser_env, 0); diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb index 699ee64..d430109 100644 --- a/test/unit/test_http_parser_ng.rb +++ b/test/unit/test_http_parser_ng.rb @@ -509,8 +509,9 @@ class HttpParserNgTest < Test::Unit::TestCase env1 = @parser.parse.dup assert_equal expect, env1 assert_equal str, @parser.buf - assert @parser.keepalive? - @parser.reset + assert ! @parser.env.empty? + assert @parser.next? + assert @parser.env.empty? env2 = @parser.parse.dup assert_equal expect, env2 assert_equal "", @parser.buf -- cgit v1.2.3-24-ge0c7