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

grpc-json: no buffering at end_stream or non-gRPC response #2363

Merged
merged 9 commits into from
Jan 17, 2018
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
11 changes: 11 additions & 0 deletions source/common/grpc/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ bool Common::hasGrpcContentType(const Http::HeaderMap& headers) {
return false;
}

bool Common::isGrpcResponseHeader(const Http::HeaderMap& headers, bool end_stream) {
if (end_stream) {
// Trailers-only response, only grpc-status is required.
return headers.GrpcStatus() != nullptr;
}
if (Http::Utility::getResponseStatus(headers) != enumToInt(Http::Code::OK)) {
return false;
}
return hasGrpcContentType(headers);
}

void Common::chargeStat(const Upstream::ClusterInfo& cluster, const std::string& protocol,
const std::string& grpc_service, const std::string& grpc_method,
const Http::HeaderEntry* grpc_status) {
Expand Down
7 changes: 7 additions & 0 deletions source/common/grpc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Common {
*/
static bool hasGrpcContentType(const Http::HeaderMap& headers);

/**
* @param headers the headers to parse.
* @param bool indicating wether the header is at end_stream.
* @return bool indicating whether the header is a gRPC reseponse header
*/
static bool isGrpcResponseHeader(const Http::HeaderMap& headers, bool end_stream);

/**
* Returns the GrpcStatus code from a given set of trailers, if present.
* @param trailers the trailers to parse.
Expand Down
6 changes: 5 additions & 1 deletion source/common/grpc/json_transcoder_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ void JsonTranscoderFilter::setDecoderFilterCallbacks(

Http::FilterHeadersStatus JsonTranscoderFilter::encodeHeaders(Http::HeaderMap& headers,
bool end_stream) {
if (!Common::isGrpcResponseHeader(headers, end_stream)) {
error_ = true;
}

if (error_ || !transcoder_) {
return Http::FilterHeadersStatus::Continue;
}
Expand Down Expand Up @@ -329,7 +333,7 @@ Http::FilterDataStatus JsonTranscoderFilter::encodeData(Buffer::Instance& data,

readToBuffer(*transcoder_->ResponseOutput(), data);

if (!method_->server_streaming()) {
if (!method_->server_streaming() && !end_stream) {
// Buffer until the response is complete.
return Http::FilterDataStatus::StopIterationAndBuffer;
}
Expand Down
16 changes: 16 additions & 0 deletions test/common/grpc/common_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,21 @@ TEST(GrpcCommonTest, HasGrpcContentType) {
EXPECT_FALSE(isGrpcContentType("application/grpc-web+foo"));
}

TEST(GrpcCommonTest, IsGrpcResponseHeader) {
Http::TestHeaderMapImpl grpc_status_only{{":status", "500"}, {"grpc-status", "14"}};
EXPECT_TRUE(Common::isGrpcResponseHeader(grpc_status_only, true));
EXPECT_FALSE(Common::isGrpcResponseHeader(grpc_status_only, false));

Http::TestHeaderMapImpl grpc_response_header{{":status", "200"},
{"content-type", "application/grpc"}};
EXPECT_FALSE(Common::isGrpcResponseHeader(grpc_response_header, true));
EXPECT_TRUE(Common::isGrpcResponseHeader(grpc_response_header, false));

Http::TestHeaderMapImpl json_response_header{{":status", "200"},
{"content-type", "application/json"}};
EXPECT_FALSE(Common::isGrpcResponseHeader(json_response_header, true));
EXPECT_FALSE(Common::isGrpcResponseHeader(json_response_header, false));
}

} // namespace Grpc
} // namespace Envoy
50 changes: 48 additions & 2 deletions test/common/grpc/json_transcoder_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,16 @@ TEST_F(GrpcJsonTranscoderFilterTest, NoTranscoding) {
Http::TestHeaderMapImpl request_trailers;
EXPECT_EQ(Http::FilterTrailersStatus::Continue, filter_.decodeTrailers(request_trailers));

EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.encodeHeaders(request_headers, false));
EXPECT_EQ(expected_request_headers, request_headers);
Http::TestHeaderMapImpl response_headers{{"content-type", "application/grpc"},
{":status", "200"},
{":path", "/grpc.service/UnknownGrpcMethod"}};

Http::TestHeaderMapImpl expected_response_headers{{"content-type", "application/grpc"},
{":status", "200"},
{":path", "/grpc.service/UnknownGrpcMethod"}};

EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.encodeHeaders(response_headers, false));
EXPECT_EQ(expected_response_headers, response_headers);

Buffer::OwnedImpl response_data{"{}"};
EXPECT_EQ(Http::FilterDataStatus::Continue, filter_.encodeData(response_data, false));
Expand Down Expand Up @@ -334,6 +342,44 @@ TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryError) {
EXPECT_EQ(0, request_data.length());
}

TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryTimeout) {
Http::TestHeaderMapImpl request_headers{
{"content-type", "application/json"}, {":method", "POST"}, {":path", "/shelf"}};

EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.decodeHeaders(request_headers, false));
EXPECT_EQ("application/grpc", request_headers.get_("content-type"));
EXPECT_EQ("/bookstore.Bookstore/CreateShelf", request_headers.get_(":path"));
EXPECT_EQ("trailers", request_headers.get_("te"));

Buffer::OwnedImpl request_data{"{\"theme\": \"Children\"}"};

EXPECT_EQ(Http::FilterDataStatus::Continue, filter_.decodeData(request_data, true));

Http::TestHeaderMapImpl response_headers{
{":status", "504"}, {"content-length", "24"}, {"content-type", "text/plain"}};
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.encodeHeaders(response_headers, false));
EXPECT_EQ(Http::FilterDataStatus::Continue, filter_.encodeData(request_data, true));
}

TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryNotGrpcResponse) {
Http::TestHeaderMapImpl request_headers{
{"content-type", "application/json"}, {":method", "POST"}, {":path", "/shelf"}};

EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.decodeHeaders(request_headers, false));
EXPECT_EQ("application/grpc", request_headers.get_("content-type"));
EXPECT_EQ("/bookstore.Bookstore/CreateShelf", request_headers.get_(":path"));
EXPECT_EQ("trailers", request_headers.get_("te"));

Buffer::OwnedImpl request_data{"{\"theme\": \"Children\"}"};

EXPECT_EQ(Http::FilterDataStatus::Continue, filter_.decodeData(request_data, true));

Http::TestHeaderMapImpl response_headers{
{":status", "200"}, {"content-length", "24"}, {"content-type", "text/plain"}};
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.encodeHeaders(response_headers, false));
EXPECT_EQ(Http::FilterDataStatus::Continue, filter_.encodeData(request_data, true));
}

struct GrpcJsonTranscoderFilterPrintTestParam {
std::string config_json_;
std::string expected_response_;
Expand Down