Commit 2e471c02 authored by zhujiashun's avatar zhujiashun

Fix PercentEncode

parent 6dd0af66
......@@ -52,14 +52,18 @@ int GrpcStatusToErrorCode(GrpcStatus grpc_status) {
return EINTERNAL;
}
void percent_encode(const std::string& str, std::string* str_out) {
void PercentEncode(const std::string& str, std::string* str_out) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (std::string::const_iterator it = str.begin();
it != str.end(); ++it) {
const std::string::value_type& c = *it;
if (c >= ' ' && c <= '~' && c != '%') {
// Unreserved Characters are referred from
// https://en.wikipedia.org/wiki/Percent-encoding
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
......@@ -81,7 +85,7 @@ static int hex_to_int(char c) {
return 0;
}
void percent_decode(const std::string& str, std::string* str_out) {
void PercentDecode(const std::string& str, std::string* str_out) {
std::ostringstream unescaped;
for (std::string::const_iterator it = str.begin();
it != str.end(); ++it) {
......
......@@ -146,9 +146,9 @@ GrpcStatus ErrorCodeToGrpcStatus(int error_code);
int GrpcStatusToErrorCode(GrpcStatus grpc_status);
void percent_encode(const std::string& str, std::string* str_out);
void PercentEncode(const std::string& str, std::string* str_out);
void percent_decode(const std::string& str, std::string* str_out);
void PercentDecode(const std::string& str, std::string* str_out);
} // namespace brpc
......
......@@ -1206,8 +1206,6 @@ int H2StreamContext::ConsumeHeaders(butil::IOBufBytesIterator& it) {
if (rc == 0) {
break;
}
RPC_VLOG << "Header name: " << pair.name
<< ", header value: " << pair.value;
const char* const name = pair.name.c_str();
bool matched = false;
if (name[0] == ':') { // reserved names
......@@ -1637,7 +1635,7 @@ H2UnsentResponse::H2UnsentResponse(Controller* c, int stream_id, bool grpc)
_data.swap(c->response_attachment());
if (grpc) {
_grpc_status = ErrorCodeToGrpcStatus(c->ErrorCode());
percent_encode(c->ErrorText(), &_grpc_message);
PercentEncode(c->ErrorText(), &_grpc_message);
}
}
......
......@@ -364,9 +364,14 @@ void ProcessHttpResponse(InputMessageBase* msg) {
if (grpc_status) {
GrpcStatus status = (GrpcStatus)strtol(grpc_status->data(), NULL, 10);
if (status != GRPC_OK) {
const std::string err =
grpc_message ? grpc_message->data() : "Unknown grpc error";
cntl->SetFailed(GrpcStatusToErrorCode(status), "%s", err.c_str());
std::string message_decoded;
if (grpc_message) {
PercentDecode(*grpc_message, &message_decoded);
} else {
message_decoded = "Unknown grpc error";
}
cntl->SetFailed(GrpcStatusToErrorCode(status),
"%s", message_decoded.c_str());
break;
}
}
......
......@@ -120,26 +120,28 @@ protected:
TEST_F(GrpcTest, percent_encode) {
std::string out;
std::string s1("abcdefg !@#$^&*()/");
brpc::percent_encode(s1, &out);
EXPECT_TRUE(out == s1) << s1 << " vs " << out;
std::string s1_out("abcdefg%20%21%40%23%24%5e%26%2a%28%29%2f");
brpc::PercentEncode(s1, &out);
EXPECT_TRUE(out == s1_out) << s1_out << " vs " << out;
char s2_buf[] = "\0\0%\33\35 brpc";
std::string s2(s2_buf, sizeof(s2_buf) - 1);
std::string s2_expected_out("%00%00%25%1b%1d brpc");
brpc::percent_encode(s2, &out);
std::string s2_expected_out("%00%00%25%1b%1d%20brpc");
brpc::PercentEncode(s2, &out);
EXPECT_TRUE(out == s2_expected_out) << s2_expected_out << " vs " << out;
}
TEST_F(GrpcTest, percent_decode) {
std::string out;
std::string s1("abcdefg !@#$^&*()/");
brpc::percent_decode(s1, &out);
EXPECT_TRUE(out == s1) << s1 << " vs " << out;
std::string s1("abcdefg%20%21%40%23%24%5e%26%2a%28%29%2f");
std::string s1_out("abcdefg !@#$^&*()/");
brpc::PercentDecode(s1, &out);
EXPECT_TRUE(out == s1_out) << s1_out << " vs " << out;
std::string s2("%00%00%1b%1d brpc");
std::string s2("%00%00%1b%1d%20brpc");
char s2_expected_out_buf[] = "\0\0\33\35 brpc";
std::string s2_expected_out(s2_expected_out_buf, sizeof(s2_expected_out_buf) - 1);
brpc::percent_decode(s2, &out);
brpc::PercentDecode(s2, &out);
EXPECT_TRUE(out == s2_expected_out) << s2_expected_out << " vs " << out;
}
......
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