]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#708,!423] make request and response parser accept Content-Length set to 0
authorRazvan Becheriu <razvan@isc.org>
Tue, 16 Jul 2019 19:52:08 +0000 (22:52 +0300)
committerRazvan Becheriu <razvan@isc.org>
Wed, 7 Aug 2019 11:37:05 +0000 (14:37 +0300)
src/lib/http/request_parser.cc
src/lib/http/response_parser.cc
src/lib/http/tests/request_parser_unittests.cc
src/lib/http/tests/response_parser_unittests.cc

index 98547040bd66e932fd1cd200315f43cdf85d3adc..d413a60336d672d6adab48c164ca23e43c45a24f 100644 (file)
@@ -293,6 +293,8 @@ HttpRequestParser::expectingNewLineHandler(const unsigned int next_state) {
                     if (content_length > 0) {
                         // There is body in this request, so let's parse it.
                         transition(HTTP_BODY_ST, DATA_READ_OK_EVT);
+                    } else {
+                        transition(HTTP_PARSE_OK_ST, HTTP_PARSE_OK_EVT);
                     }
                 } catch (const std::exception& ex) {
                     // There is no body in this message. If the body is required
index 9bbace0d74727b8e6df530c1d0628679e8e6830c..46657b15def9f0ee4863cb1c4da9116591fa8fa3 100644 (file)
@@ -295,6 +295,8 @@ HttpResponseParser::expectingNewLineHandler(const unsigned int next_state) {
                     if (content_length > 0) {
                         // There is body in this request, so let's parse it.
                         transition(HTTP_BODY_ST, DATA_READ_OK_EVT);
+                    } else {
+                        transition(HTTP_PARSE_OK_ST, HTTP_PARSE_OK_EVT);
                     }
                 } catch (const std::exception& ex) {
                     // There is no body in this message. If the body is required
index 57e183ab20670f4c1c34fd5aa40b6bc1d7ef42c7..cf8d40684b28f0e0b5d0eb204a296481a68d1ef4 100644 (file)
@@ -368,4 +368,20 @@ TEST_F(HttpRequestParserTest, getBufferAsString) {
               parser.getBufferAsString(3));
 }
 
+TEST_F(HttpRequestParserTest, parseEmptyRequest) {
+    std::string http_req = "POST / HTTP/1.1\r\n"
+        "Content-Type: application/json\r\n";
+    std::string json = "";
+
+    http_req = createRequestString(http_req, json);
+
+    ASSERT_NO_FATAL_FAILURE(doParse(http_req));
+
+    EXPECT_EQ(HttpRequest::Method::HTTP_POST, request_.getMethod());
+    EXPECT_EQ("/", request_.getUri());
+    EXPECT_EQ("", request_.getBody());
+    EXPECT_EQ(1, request_.getHttpVersion().major_);
+    EXPECT_EQ(1, request_.getHttpVersion().minor_);
+}
+
 }
index 10e23b46e5264f43d14fea4ae4fdc608834b26e1..5461c5080cb74febf7d0f919786906572598b31e 100644 (file)
@@ -330,5 +330,22 @@ TEST_F(HttpResponseParserTest, logFormatHttpMessage) {
               HttpResponseParser::logFormatHttpMessage(message, 3));
 }
 
+TEST_F(HttpResponseParserTest, parseEmptyResponse) {
+    std::string http_resp = "HTTP/1.1 200 OK\r\n"
+        "Content-Type: application/json\r\n";
+    std::string json = "";
+
+    http_resp = createResponseString(http_resp, json);
+
+    ASSERT_NO_FATAL_FAILURE(doParse(http_resp));
+
+    HttpResponseJson response = testResponseWithJson(http_resp, json);
+
+    EXPECT_EQ("", response_.getBody());
+    EXPECT_EQ(1, response_.getHttpVersion().major_);
+    EXPECT_EQ(1, response_.getHttpVersion().minor_);
+    EXPECT_EQ(HttpStatusCode::OK, response_.getStatusCode());
+    EXPECT_EQ("OK", response_.getStatusPhrase());
+}
 
 }