diff --git a/src/brpc/details/http_message.cpp b/src/brpc/details/http_message.cpp index 45a7bf071a..ab8bb4f2a3 100644 --- a/src/brpc/details/http_message.cpp +++ b/src/brpc/details/http_message.cpp @@ -556,9 +556,9 @@ void MakeRawHttpRequest(butil::IOBuf* request, uri.PrintWithoutHost(os); // host is sent by "Host" header. os << " HTTP/" << h->major_version() << '.' << h->minor_version() << BRPC_CRLF; + // Never use "Content-Length" set by user. + h->RemoveHeader("Content-Length"); if (h->method() != HTTP_METHOD_GET) { - h->RemoveHeader("Content-Length"); - // Never use "Content-Length" set by user. os << "Content-Length: " << (content ? content->length() : 0) << BRPC_CRLF; } diff --git a/test/brpc_http_message_unittest.cpp b/test/brpc_http_message_unittest.cpp index 1b13e2b7cf..ff51657eff 100644 --- a/test/brpc_http_message_unittest.cpp +++ b/test/brpc_http_message_unittest.cpp @@ -408,8 +408,9 @@ TEST(HttpMessageTest, serialize_http_request) { MakeRawHttpRequest(&request, &header, ep, &content); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\ndata", request); - // GET does not serialize content + // GET does not serialize content and user-set content-length is ignored. header.set_method(brpc::HTTP_METHOD_GET); + header.SetHeader("Content-Length", "100"); MakeRawHttpRequest(&request, &header, ep, &content); ASSERT_EQ("GET / HTTP/1.1\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\n", request); }