about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-09-03 01:50:52 -0700
committerEric Wong <normalperson@yhbt.net>2009-09-03 01:50:52 -0700
commit52c2c6273e7c007543de776937a25827c27d0ff1 (patch)
tree4c8330b81701c09d727bf19d916243ed7dae8b2b
parentbd0599c4ac91d95cae1f34df3ae99c92f3225391 (diff)
downloadunicorn-52c2c6273e7c007543de776937a25827c27d0ff1.tar.gz
This method determines if there are headers in the request.
Simple HTTP/0.9 requests did not have headers in the request
(and our responses we make should not have them, either).
-rw-r--r--ext/unicorn_http/unicorn_http.rl16
-rw-r--r--test/unit/test_http_parser_ng.rb2
2 files changed, 18 insertions, 0 deletions
diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl
index 224b95a..7a0c02d 100644
--- a/ext/unicorn_http/unicorn_http.rl
+++ b/ext/unicorn_http/unicorn_http.rl
@@ -537,6 +537,21 @@ static VALUE HttpParser_keepalive(VALUE self)
 
 /**
  * call-seq:
+ *    parser.headers? => true or false
+ *
+ * This should be used to detect if a request has headers (and if
+ * the response will have headers as well).  HTTP/0.9 requests
+ * should return false, all subsequent HTTP versions will return true
+ */
+static VALUE HttpParser_has_headers(VALUE self)
+{
+  struct http_parser *hp = data_get(self);
+
+  return (hp->flags & UH_FL_HASHEADER) ? Qtrue : Qfalse;
+}
+
+/**
+ * call-seq:
  *    parser.filter_body(buf, data) => nil/data
  *
  * Takes a String of +data+, will modify data if dechunking is done.
@@ -618,6 +633,7 @@ void Init_unicorn_http(void)
   rb_define_method(cHttpParser, "content_length", HttpParser_content_length, 0);
   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);
 
   /*
    * The maximum size a single chunk when using chunked transfer encoding.
diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb
index 9b86cd3..6b9c89d 100644
--- a/test/unit/test_http_parser_ng.rb
+++ b/test/unit/test_http_parser_ng.rb
@@ -21,6 +21,7 @@ class HttpParserNgTest < Test::Unit::TestCase
     assert_equal '123', req['CONTENT_LENGTH']
     assert_equal 0, str.size
     assert ! @parser.keepalive?
+    assert @parser.headers?
   end
 
   def test_identity_oneshot_header
@@ -299,6 +300,7 @@ class HttpParserNgTest < Test::Unit::TestCase
       "QUERY_STRING"=>""
     }
     assert_equal expect, req
+    assert ! parser.headers?
   end
 
 end