Commit 27a3cd51 authored by gejun's avatar gejun

Try to convert h2 error in RST_STREAM to related status code

parent 5a487ac1
......@@ -82,4 +82,31 @@ const char* H2ErrorToString(H2Error e) {
return "Unknown-H2Error";
}
int H2ErrorToStatusCode(H2Error e) {
switch (e) {
case H2_NO_ERROR:
return HTTP_STATUS_OK;
case H2_SETTINGS_TIMEOUT:
return HTTP_STATUS_GATEWAY_TIMEOUT;
case H2_STREAM_CLOSED_ERROR:
return HTTP_STATUS_BAD_REQUEST;
case H2_REFUSED_STREAM:
case H2_CANCEL:
case H2_ENHANCE_YOUR_CALM:
return HTTP_STATUS_SERVICE_UNAVAILABLE;
case H2_INADEQUATE_SECURITY:
return HTTP_STATUS_UNAUTHORIZED;
case H2_HTTP_1_1_REQUIRED:
return HTTP_STATUS_VERSION_NOT_SUPPORTED;
case H2_PROTOCOL_ERROR:
case H2_FLOW_CONTROL_ERROR:
case H2_FRAME_SIZE_ERROR:
case H2_COMPRESSION_ERROR:
case H2_CONNECT_ERROR:
case H2_INTERNAL_ERROR:
return HTTP_STATUS_INTERNAL_SERVER_ERROR;
}
return HTTP_STATUS_INTERNAL_SERVER_ERROR;
}
} // namespace brpc
......@@ -15,7 +15,7 @@
#ifndef BAIDU_RPC_HTTP2_H
#define BAIDU_RPC_HTTP2_H
#include "butil/iobuf.h"
#include "brpc/http_status_code.h"
// To baidu-rpc developers: This is a header included by user, don't depend
// on internal structures, use opaque pointers instead.
......@@ -111,8 +111,12 @@ enum H2Error {
H2_HTTP_1_1_REQUIRED = 0xd, // Use HTTP/1.1 for the request
};
// Get description of the error.
const char* H2ErrorToString(H2Error e);
// Convert the error to status code with similar semantics
int H2ErrorToStatusCode(H2Error e);
} // namespace brpc
#endif // BAIDU_RPC_HTTP2_H
......@@ -785,7 +785,7 @@ H2ParseResult H2StreamContext::OnResetStream(
return MakeH2Error(H2_PROTOCOL_ERROR);
}
if (_conn_ctx->is_client_side()) {
sctx->header().set_status_code(HTTP_STATUS_INTERNAL_SERVER_ERROR);
sctx->header().set_status_code(H2ErrorToStatusCode(h2_error));
return MakeH2Message(sctx);
} else {
// No need to process the request.
......
......@@ -299,26 +299,21 @@ void ProcessHttpResponse(InputMessageBase* msg) {
// ErrorCode of RPC is unified to EHTTP.
const int sc = res_header->status_code();
if (sc < 200 || sc >= 300) {
std::string err = butil::string_printf(
"HTTP/%d.%d %d %s",
res_header->major_version(),
res_header->minor_version(),
static_cast<int>(res_header->status_code()),
res_header->reason_phrase());
if (!res_body.empty()) {
// Use content as error text if it's present. Notice that
// content may be binary data, so the size limit is a must.
std::string body_str;
res_body.copy_to(
&body_str, std::min((int)res_body.size(),
err.append(": ");
res_body.append_to(
&err, std::min((int)res_body.size(),
FLAGS_http_max_error_length));
cntl->SetFailed(EHTTP, "HTTP/%d.%d %d %s: %.*s",
res_header->major_version(),
res_header->minor_version(),
static_cast<int>(res_header->status_code()),
res_header->reason_phrase(),
(int)body_str.size(), body_str.c_str());
} else {
cntl->SetFailed(EHTTP, "HTTP/%d.%d %d %s",
res_header->major_version(),
res_header->minor_version(),
static_cast<int>(res_header->status_code()),
res_header->reason_phrase());
}
cntl->SetFailed(EHTTP, "%s", err.c_str());
if (cntl->response() == NULL ||
cntl->response()->GetDescriptor()->field_count() == 0) {
// A http call. Http users may need the body(containing a html,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment