Browse Source

[5077] Provided unit test for extraneous data in the HTTP request.

Marcin Siodelski 8 years ago
parent
commit
b5c14dc7fe
2 changed files with 49 additions and 1 deletions
  1. 8 1
      src/lib/http/request_parser.h
  2. 41 0
      src/lib/http/tests/request_parser_unittests.cc

+ 8 - 1
src/lib/http/request_parser.h

@@ -76,7 +76,14 @@ public:
 /// internal buffer. This method returns control to the caller when the parser
 /// runs out of data in this buffer. The caller must feed the buffer by calling
 /// @ref HttpRequestParser::postBuffer and then run @ref HttpRequestParser::poll
-//// again.
+/// again.
+///
+/// In case the caller provides more data than indicated by the "Content-Length"
+/// header the parser will return from poll() after parsing the data which
+/// constitute the HTTP request and not parse the extraneous data. The caller
+/// should test the @ref HttpRequestParser::needData and
+/// @ref HttpRequestParser::httpParseOk to determine whether parsing has
+/// completed.
 ///
 /// The @ref util::StateModel::runModel must not be used to run the
 /// @ref HttpRequestParser state machine, thus it is made private method.

+ 41 - 0
src/lib/http/tests/request_parser_unittests.cc

@@ -130,6 +130,47 @@ TEST_F(HttpRequestParserTest, postHttpRequestWithJson) {
     EXPECT_EQ("shutdown", json_element->stringValue());
 }
 
+// This test verifies that extranous data in the request will not cause
+// an error if "Content-Length" value refers to the length of the valid
+// part of the request.
+TEST_F(HttpRequestParserTest, extraneousDataInRequest) {
+    std::string http_req = "POST /foo/bar HTTP/1.0\r\n"
+        "Content-Type: application/json\r\n";
+    std::string json = "{ \"service\": \"dhcp4\", \"command\": \"shutdown\" }";
+
+    // Create valid request;
+    http_req = createRequestString(http_req, json);
+
+    // Add some garbage at the end.
+    http_req += "some stuff which, if parsed, will cause errors";
+
+    // Create HTTP request which accepts POST method and JSON as a body.
+    PostHttpRequestJson request;
+
+    // Create a parser and make it use the request we created.
+    HttpRequestParser parser(request);
+    ASSERT_NO_THROW(parser.initModel());
+
+    // Feed the parser with the request containing some garbage at the end.
+    parser.postBuffer(&http_req[0], http_req.size());
+    ASSERT_NO_THROW(parser.poll());
+
+    // The parser should only parse the valid part of the request as indicated
+    // by the Content-Length.
+    ASSERT_FALSE(parser.needData());
+    ASSERT_TRUE(parser.httpParseOk());
+    // There should be no error message.
+    EXPECT_TRUE(parser.getErrorMessage().empty());
+
+    // Do another poll() to see if the parser will parse the garbage. We
+    // expect that it doesn't.
+    ASSERT_NO_THROW(parser.poll());
+    EXPECT_FALSE(parser.needData());
+    EXPECT_TRUE(parser.httpParseOk());
+    EXPECT_TRUE(parser.getErrorMessage().empty());
+}
+
+
 // This test verifies that LWS is parsed correctly. The LWS marks line breaks
 // in the HTTP header values.
 TEST_F(HttpRequestParserTest, getLWS) {