-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http/filter: Add a callback for sendLocalReply() and encode gRPC mess…
…age for local responses when the request is gRPC (#3299) Some gRPC clients get confused when they get non-gRPC responses to their gRPC requests. While such clients should be fixed, we can play nice and format the local responses returned by Envoy router filter as gRPC responses when the request is gRPC. Fixes: #1930 Signed-off-by: Jarno Rajahalme <[email protected]>
- Loading branch information
1 parent
2741d91
commit 5843954
Showing
39 changed files
with
381 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "common/grpc/status.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
Status::GrpcStatus Utility::httpToGrpcStatus(uint64_t http_response_status) { | ||
// From | ||
// https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md. | ||
switch (http_response_status) { | ||
case 400: | ||
return Status::GrpcStatus::Internal; | ||
case 401: | ||
return Status::GrpcStatus::Unauthenticated; | ||
case 403: | ||
return Status::GrpcStatus::PermissionDenied; | ||
case 404: | ||
return Status::GrpcStatus::Unimplemented; | ||
case 429: | ||
case 502: | ||
case 503: | ||
case 504: | ||
return Status::GrpcStatus::Unavailable; | ||
default: | ||
return Status::GrpcStatus::Unknown; | ||
} | ||
} | ||
|
||
uint64_t Utility::grpcToHttpStatus(Status::GrpcStatus grpc_status) { | ||
// From https://cloud.google.com/apis/design/errors#handling_errors. | ||
switch (grpc_status) { | ||
case Status::GrpcStatus::Ok: | ||
return 200; | ||
case Status::GrpcStatus::Canceled: | ||
// Client closed request. | ||
return 499; | ||
case Status::GrpcStatus::Unknown: | ||
// Internal server error. | ||
return 500; | ||
case Status::GrpcStatus::InvalidArgument: | ||
// Bad request. | ||
return 400; | ||
case Status::GrpcStatus::DeadlineExceeded: | ||
// Gateway Time-out. | ||
return 504; | ||
case Status::GrpcStatus::NotFound: | ||
// Not found. | ||
return 404; | ||
case Status::GrpcStatus::AlreadyExists: | ||
// Conflict. | ||
return 409; | ||
case Status::GrpcStatus::PermissionDenied: | ||
// Forbidden. | ||
return 403; | ||
case Status::GrpcStatus::ResourceExhausted: | ||
// Too many requests. | ||
return 429; | ||
case Status::GrpcStatus::FailedPrecondition: | ||
// Bad request. | ||
return 400; | ||
case Status::GrpcStatus::Aborted: | ||
// Conflict. | ||
return 409; | ||
case Status::GrpcStatus::OutOfRange: | ||
// Bad request. | ||
return 400; | ||
case Status::GrpcStatus::Unimplemented: | ||
// Not implemented. | ||
return 501; | ||
case Status::GrpcStatus::Internal: | ||
// Internal server error. | ||
return 500; | ||
case Status::GrpcStatus::Unavailable: | ||
// Service unavailable. | ||
return 503; | ||
case Status::GrpcStatus::DataLoss: | ||
// Internal server error. | ||
return 500; | ||
case Status::GrpcStatus::Unauthenticated: | ||
// Unauthorized. | ||
return 401; | ||
case Status::GrpcStatus::InvalidCode: | ||
default: | ||
// Internal server error. | ||
return 500; | ||
} | ||
} | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "envoy/grpc/status.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
/** | ||
* Grpc::Status utilities. | ||
*/ | ||
class Utility { | ||
public: | ||
/** | ||
* Returns the gRPC status code from a given HTTP response status code. Ordinarily, it is expected | ||
* that a 200 response is provided, but gRPC defines a mapping for intermediaries that are not | ||
* gRPC aware, see https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md. | ||
* @param http_response_status HTTP status code. | ||
* @return Status::GrpcStatus corresponding gRPC status code. | ||
*/ | ||
static Status::GrpcStatus httpToGrpcStatus(uint64_t http_response_status); | ||
|
||
/** | ||
* @param grpc_status gRPC status from grpc-status header. | ||
* @return uint64_t the canonical HTTP status code corresponding to a gRPC status code. | ||
*/ | ||
static uint64_t grpcToHttpStatus(Status::GrpcStatus grpc_status); | ||
}; | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.