Skip to content

Commit

Permalink
add a flag to manage http body on error
Browse files Browse the repository at this point in the history
  • Loading branch information
gejun.0 committed Nov 9, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 4b08958 commit cc629b6
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/brpc/controller.h
Original file line number Diff line number Diff line change
@@ -143,6 +143,7 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
static const uint32_t FLAGS_ALWAYS_PRINT_PRIMITIVE_FIELDS = (1 << 18);
static const uint32_t FLAGS_HEALTH_CHECK_CALL = (1 << 19);
static const uint32_t FLAGS_PB_SINGLE_REPEATED_TO_ARRAY = (1 << 20);
static const uint32_t FLAGS_MANAGE_HTTP_BODY_ON_ERROR = (1 << 21);

public:
struct Inheritable {
@@ -398,6 +399,16 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
// directly instead of being serialized into protobuf messages.
butil::IOBuf& response_attachment() { return _response_attachment; }

// Response Body of a failed HTTP call is set to be ErrorText() by default,
// even if response_attachment() is non-empty.
// If this flag is true, the http body of a failed HTTP call will not be
// replaced by ErrorText() and should be managed by user self.
void manage_http_body_on_error(bool manage_or_not)
{ set_flag(FLAGS_MANAGE_HTTP_BODY_ON_ERROR, manage_or_not); }

bool does_manage_http_body_on_error() const
{ return has_flag(FLAGS_MANAGE_HTTP_BODY_ON_ERROR); }

// Create a ProgressiveAttachment to write (often after RPC).
// If `stop_style' is FORCE_STOP, the underlying socket will be failed
// immediately when the socket becomes idle or server is stopped.
18 changes: 11 additions & 7 deletions src/brpc/policy/http_rpc_protocol.cpp
Original file line number Diff line number Diff line change
@@ -847,7 +847,9 @@ HttpResponseSender::~HttpResponseSender() {

bool grpc_compressed = false;
if (cntl->Failed()) {
cntl->response_attachment().clear();
if (!cntl->does_manage_http_body_on_error()) {
cntl->response_attachment().clear();
}
if (!is_grpc) {
// Set status-code with default value(converted from error code)
// if user did not set it.
@@ -858,12 +860,14 @@ HttpResponseSender::~HttpResponseSender() {
res_header->SetHeader(common->ERROR_CODE,
butil::string_printf("%d", cntl->ErrorCode()));

// Fill body with ErrorText.
// user may compress the output and change content-encoding. However
// body is error-text right now, remove the header.
res_header->RemoveHeader(common->CONTENT_ENCODING);
res_header->set_content_type(common->CONTENT_TYPE_TEXT);
cntl->response_attachment().append(cntl->ErrorText());
if (!cntl->does_manage_http_body_on_error()) {
// Fill body with ErrorText.
// user may compress the output and change content-encoding. However
// body is error-text right now, remove the header.
res_header->RemoveHeader(common->CONTENT_ENCODING);
res_header->set_content_type(common->CONTENT_TYPE_TEXT);
cntl->response_attachment().append(cntl->ErrorText());
}
}
} else if (cntl->has_progressive_writer()) {
// Transfer-Encoding is supported since HTTP/1.1

0 comments on commit cc629b6

Please sign in to comment.