about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--ext/unicorn_http/unicorn_http.rl20
-rw-r--r--test/unit/test_http_parser_ng.rb5
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
@@ -619,6 +619,25 @@ static VALUE HttpParser_keepalive(VALUE self)
 
 /**
  * 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
  *
  * This should be used to detect if a request has headers (and if
@@ -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