Skip to content

Commit

Permalink
Remove Content-Length header of Get request
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Nov 26, 2023
1 parent 806f4d4 commit 2429882
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/brpc/details/http_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion test/brpc_http_message_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2429882

Please sign in to comment.