Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parse proto-text format http request body #1690

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/brpc/policy/http_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


#include <google/protobuf/descriptor.h> // MethodDescriptor
#include <google/protobuf/text_format.h>
#include <gflags/gflags.h>
#include <json2pb/pb_to_json.h> // ProtoMessageToJson
#include <json2pb/json_to_pb.h> // JsonToProtoMessage
Expand Down Expand Up @@ -191,6 +192,9 @@ HttpContentType ParseContentType(butil::StringPiece ct, bool* is_grpc_ct) {
if (ct.starts_with("json")) {
type = HTTP_CONTENT_JSON;
ct.remove_prefix(4);
} else if (ct.starts_with("proto-text")) {
type = HTTP_CONTENT_PROTO_TEXT;
ct.remove_prefix(10);
} else if (ct.starts_with("proto")) {
type = HTTP_CONTENT_PROTO;
ct.remove_prefix(5);
Expand Down Expand Up @@ -434,6 +438,11 @@ void ProcessHttpResponse(InputMessageBase* msg) {
cntl->SetFailed(ERESPONSE, "Fail to parse content");
break;
}
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
if (!ParsePbTextFromIOBuf(cntl->response(), res_body)) {
cntl->SetFailed(ERESPONSE, "Fail to parse proto-text content");
break;
}
} else if (content_type == HTTP_CONTENT_JSON) {
// message body is json
butil::IOBufAsZeroCopyInputStream wrapper(res_body);
Expand Down Expand Up @@ -513,6 +522,12 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
return cntl->SetFailed(EREQUEST, "Fail to serialize %s",
pbreq->GetTypeName().c_str());
}
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
if (!google::protobuf::TextFormat::Print(*pbreq, &wrapper)) {
cntl->request_attachment().clear();
return cntl->SetFailed(EREQUEST, "Fail to print %s as proto-text",
pbreq->GetTypeName().c_str());
}
} else if (content_type == HTTP_CONTENT_JSON) {
std::string err;
json2pb::Pb2JsonOptions opt;
Expand Down Expand Up @@ -757,6 +772,10 @@ HttpResponseSender::~HttpResponseSender() {
if (!res->SerializeToZeroCopyStream(&wrapper)) {
cntl->SetFailed(ERESPONSE, "Fail to serialize %s", res->GetTypeName().c_str());
}
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
if (!google::protobuf::TextFormat::Print(*res, &wrapper)) {
cntl->SetFailed(ERESPONSE, "Fail to print %s as proto-text", res->GetTypeName().c_str());
}
} else {
std::string err;
json2pb::Pb2JsonOptions opt;
Expand Down Expand Up @@ -1470,6 +1489,12 @@ void ProcessHttpRequest(InputMessageBase *msg) {
req->GetDescriptor()->full_name().c_str());
return;
}
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
if (!ParsePbTextFromIOBuf(req, req_body)) {
cntl->SetFailed(EREQUEST, "Fail to parse http proto-text body as %s",
req->GetDescriptor()->full_name().c_str());
return;
}
} else {
butil::IOBufAsZeroCopyInputStream wrapper(req_body);
std::string err;
Expand Down
1 change: 1 addition & 0 deletions src/brpc/policy/http_rpc_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ enum HttpContentType {
HTTP_CONTENT_OTHERS = 0,
HTTP_CONTENT_JSON = 1,
HTTP_CONTENT_PROTO = 2,
HTTP_CONTENT_PROTO_TEXT = 3,
};

// Parse from the textual content type. One type may have more than one literals.
Expand Down
12 changes: 12 additions & 0 deletions src/brpc/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const uint64_t PB_TOTAL_BYETS_LIMITS =
#undef private

#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/text_format.h>
#include <gflags/gflags.h>
#include "butil/logging.h"
#include "butil/memory/singleton_on_pthread_once.h"
Expand Down Expand Up @@ -212,12 +213,23 @@ BUTIL_FORCE_INLINE bool ParsePbFromZeroCopyStreamInlined(
return msg->ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();
}

BUTIL_FORCE_INLINE bool ParsePbTextFromZeroCopyStreamInlined(
google::protobuf::Message* msg,
google::protobuf::io::ZeroCopyInputStream* input) {
return google::protobuf::TextFormat::Parse(input, msg);
}

bool ParsePbFromZeroCopyStream(
google::protobuf::Message* msg,
google::protobuf::io::ZeroCopyInputStream* input) {
return ParsePbFromZeroCopyStreamInlined(msg, input);
}

bool ParsePbTextFromIOBuf(google::protobuf::Message* msg, const butil::IOBuf& buf) {
butil::IOBufAsZeroCopyInputStream stream(buf);
return ParsePbTextFromZeroCopyStreamInlined(msg, &stream);
}

bool ParsePbFromIOBuf(google::protobuf::Message* msg, const butil::IOBuf& buf) {
butil::IOBufAsZeroCopyInputStream stream(buf);
return ParsePbFromZeroCopyStreamInlined(msg, &stream);
Expand Down
1 change: 1 addition & 0 deletions src/brpc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void SerializeRequestDefault(butil::IOBuf* buf,
bool ParsePbFromZeroCopyStream(google::protobuf::Message* msg,
google::protobuf::io::ZeroCopyInputStream* input);
bool ParsePbFromIOBuf(google::protobuf::Message* msg, const butil::IOBuf& buf);
bool ParsePbTextFromIOBuf(google::protobuf::Message* msg, const butil::IOBuf& buf);
bool ParsePbFromArray(google::protobuf::Message* msg, const void* data, size_t size);
bool ParsePbFromString(google::protobuf::Message* msg, const std::string& str);

Expand Down
48 changes: 48 additions & 0 deletions test/brpc_http_rpc_protocol_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/text_format.h>
#include "butil/time.h"
#include "butil/macros.h"
#include "butil/files/scoped_file.h"
Expand Down Expand Up @@ -163,6 +164,20 @@ class HttpTest : public ::testing::Test{
return msg;
}

brpc::policy::HttpContext* MakePostProtoTextRequestMessage(
const std::string& path) {
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext(false);
msg->header().uri().set_path(path);
msg->header().set_content_type("application/proto-text");
msg->header().set_method(brpc::HTTP_METHOD_POST);

test::EchoRequest req;
req.set_message(EXP_REQUEST);
butil::IOBufAsZeroCopyOutputStream req_stream(&msg->body());
EXPECT_TRUE(google::protobuf::TextFormat::Print(req, &req_stream));
return msg;
}

brpc::policy::HttpContext* MakeGetRequestMessage(const std::string& path) {
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext(false);
msg->header().uri().set_path(path);
Expand Down Expand Up @@ -310,6 +325,12 @@ TEST_F(HttpTest, verify_request) {
VerifyMessage(msg, false);
msg->Destroy();
}
{
brpc::policy::HttpContext* msg =
MakePostProtoTextRequestMessage("/EchoService/Echo");
VerifyMessage(msg, false);
msg->Destroy();
wwbmmm marked this conversation as resolved.
Show resolved Hide resolved
}
}

TEST_F(HttpTest, process_request_failed_socket) {
Expand Down Expand Up @@ -1480,4 +1501,31 @@ TEST_F(HttpTest, spring_protobuf_content_type) {
ASSERT_EQ("application/x-protobuf", cntl.http_response().content_type());
}

TEST_F(HttpTest, spring_protobuf_text_content_type) {
const int port = 8923;
brpc::Server server;
EXPECT_EQ(0, server.AddService(&_svc, brpc::SERVER_DOESNT_OWN_SERVICE));
EXPECT_EQ(0, server.Start(port, nullptr));

brpc::Channel channel;
brpc::ChannelOptions options;
options.protocol = "http";
ASSERT_EQ(0, channel.Init(butil::EndPoint(butil::my_ip(), port), &options));

brpc::Controller cntl;
test::EchoRequest req;
test::EchoResponse res;
req.set_message(EXP_REQUEST);
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.http_request().uri() = "/EchoService/Echo";
cntl.http_request().set_content_type("application/proto-text");
cntl.request_attachment().append(req.Utf8DebugString());
channel.CallMethod(nullptr, &cntl, nullptr, nullptr, nullptr);
ASSERT_FALSE(cntl.Failed());
ASSERT_EQ("application/proto-text", cntl.http_response().content_type());
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
cntl.response_attachment().to_string(), &res));
ASSERT_EQ(EXP_RESPONSE, res.message());
}

} //namespace