This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 7c56210e4 clear F_CHUNKED when a new Transfer-Encoding line starts 
(#3644)
7c56210e4 is described below

commit 7c56210e43146d30e22325511c534caa1c59f56a
Author: sahvx655-wq <[email protected]>
AuthorDate: Wed Nov 25 12:57:49 2026 +0530

    clear F_CHUNKED when a new Transfer-Encoding line starts (#3644)
    
    * clear F_CHUNKED when a new Transfer-Encoding line starts
    
    * clear F_CHUNKED at the Transfer-Encoding colon so empty values reset it
---
 src/brpc/details/http_parser.cpp    |  8 ++++++
 test/brpc_http_message_unittest.cpp | 50 +++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/src/brpc/details/http_parser.cpp b/src/brpc/details/http_parser.cpp
index 7f99f1cda..295224155 100644
--- a/src/brpc/details/http_parser.cpp
+++ b/src/brpc/details/http_parser.cpp
@@ -1565,6 +1565,14 @@ size_t http_parser_execute (http_parser *parser,
         }
 
         if (ch == ':') {
+          if (parser->header_state == h_transfer_encoding) {
+            /* Multiple `Transfer-Encoding` headers are one list (RFC 7230
+             * 3.2.2), so `chunked` from an earlier line is no longer final.
+             * Clear it here rather than at value start so that an empty
+             * value also resets it.
+             */
+            parser->flags &= ~F_CHUNKED;
+          }
           parser->state = s_header_value_discard_ws;
           CALLBACK_DATA(header_field);
           break;
diff --git a/test/brpc_http_message_unittest.cpp 
b/test/brpc_http_message_unittest.cpp
index 5a27efd1a..eca80e655 100644
--- a/test/brpc_http_message_unittest.cpp
+++ b/test/brpc_http_message_unittest.cpp
@@ -540,6 +540,56 @@ TEST(HttpMessageTest, 
transfer_encoding_list_with_space_after_comma) {
                                            "0\r\n\r\n"));
 }
 
+TEST(HttpMessageTest, repeated_transfer_encoding_lines) {
+    // RFC 7230 3.2.2: repeated Transfer-Encoding lines form one list, so a
+    // later line decides framing exactly as it would after a comma.
+    ASSERT_EQ(brpc::HPE_INVALID_TRANSFER_ENCODING,
+              ParseHttpErrno("POST / HTTP/1.1\r\n"
+                             "Host: a.com\r\n"
+                             "Transfer-Encoding: chunked, identity\r\n\r\n"
+                             "5\r\nhello\r\n0\r\n\r\n"));
+    ASSERT_EQ(brpc::HPE_INVALID_TRANSFER_ENCODING,
+              ParseHttpErrno("POST / HTTP/1.1\r\n"
+                             "Host: a.com\r\n"
+                             "Transfer-Encoding: chunked\r\n"
+                             "Transfer-Encoding: identity\r\n\r\n"
+                             "5\r\nhello\r\n0\r\n\r\n"));
+    // An empty repeated line is like `chunked, ` and must not keep chunked.
+    ASSERT_EQ(brpc::HPE_INVALID_TRANSFER_ENCODING,
+              ParseHttpErrno("POST / HTTP/1.1\r\n"
+                             "Host: a.com\r\n"
+                             "Transfer-Encoding: chunked\r\n"
+                             "Transfer-Encoding:\r\n\r\n"
+                             "5\r\nhello\r\n0\r\n\r\n"));
+    // A longer field name that only starts with Transfer-Encoding is a
+    // different header and leaves the framing alone.
+    ASSERT_EQ(brpc::HPE_OK, ParseHttpErrno("POST / HTTP/1.1\r\n"
+                                           "Host: a.com\r\n"
+                                           "Transfer-Encoding: chunked\r\n"
+                                           "Transfer-Encoding-X: a\r\n\r\n"
+                                           "0\r\n\r\n"));
+    // chunked as the final coding is still honored.
+    ASSERT_EQ(brpc::HPE_OK, ParseHttpErrno("POST / HTTP/1.1\r\n"
+                                           "Host: a.com\r\n"
+                                           "Transfer-Encoding: gzip\r\n"
+                                           "Transfer-Encoding: chunked\r\n\r\n"
+                                           "0\r\n\r\n"));
+
+    // A response whose final coding is not chunked is read until EOF.
+    butil::IOBuf response;
+    response.append("HTTP/1.1 200 OK\r\n"
+                    "Transfer-Encoding: chunked\r\n"
+                    "Transfer-Encoding: gzip\r\n\r\n"
+                    "5\r\nhello\r\n0\r\n\r\n");
+    brpc::HttpMessage http_message;
+    ASSERT_EQ((ssize_t)response.size(), http_message.ParseFromIOBuf(response))
+        << http_message._parser;
+    ASSERT_FALSE(http_message._parser.flags & brpc::F_CHUNKED)
+        << http_message._parser;
+    ASSERT_FALSE(http_message.Completed());
+    ASSERT_EQ("5\r\nhello\r\n0\r\n\r\n", http_message.body().to_string());
+}
+
 TEST(HttpMessageTest, content_length_with_interior_space) {
     ASSERT_EQ(brpc::HPE_INVALID_CONTENT_LENGTH, ParseHttpErrno("POST / 
HTTP/1.1\r\n"
                                                                "Host: 
a.com\r\n"


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to