Boost asio 同步 https 调用 - Json 响应具有意外字符

Boost asio synchronous https call- Json response have unintended character

我们正在从 http 迁移到 https boost asio sysnchornous 调用,我正在使用下面的代码通过 ssl 证书验证进行 https 同步调用,我们得到了多行响应;到目前为止,我已经删除了换行符(\r\n)(上游系统说他们正在单行发送响应并且没有任何额外字符,如下所述)并尝试解析响应但有时我们得到键值对中带有额外字符的响应如下所示:

try{
fast_ostringstream oss;
boost::asio::streambuf request_;
boost::asio::streambuf response_;
boost::system::error_code ec;
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.set_verify_mode(boost::asio::ssl::verify_peer);
ctx.set_default_verify_paths(ec);
if (ec)
{
        fast_ostringstream oss;
        oss << "Issue in settign the default path:" << ec.message();
        PBLOG_INFO(oss.str());
}
oss << ec.message();
ctx.add_verify_path("/home/test/pemcert/");
ctx.set_options(boost::asio::ssl::context::default_workarounds |
       boost::asio::ssl::context::no_sslv2 |
       boost::asio::ssl::context::no_sslv3);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_service,ctx);

std::ostream request_stream(&request_);
request_stream << "POST " << server_endpoint << " HTTP/1.1\r\n";
request_stream << "Host: " << hostname << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << authorization_token << "\r\n";
request_stream << client_name << "\r\n";
request_stream << "Content-Length: " << req_str.length() << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded \r\n";
request_stream << "Connection: close\r\n\r\n";
request_stream << req_str << "\r\n";
tcp::resolver resolver(io_service);
tcp::resolver resolver(io_service);
tcp::resolver::query query(hostname, port_no);

tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

boost::system::error_code error  = boost::asio::error::host_not_found;
boost::asio::connect(socket.lowest_layer(), endpoint_iterator, error);
boost::system::error_code echs;
socket.handshake(boost::asio::ssl::stream_base::client, echs);
boost::asio::write(socket, request_);
PBLOG_INFO("Trac Request successfully sent");

// Read the response status line.
boost::asio::read_until(socket, response_, "\r\n");
string res=make_string(response_);

// Check that response is OK.
std::istream response_stream(&response_);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);

if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
        PBLOG_WARN("Invalid response\n");
}
if (status_code != 200)
{
        fast_ostringstream oss;
        oss << "Response returned with status code: " << status_code << "\n";
        PBLOG_WARN(oss.str());
}
boost::asio::read(socket, response_, boost::asio::transfer_all(), error);
if (error.value() != 335544539 && strcmp(error.category().name(),"asio.ssl") != 0 )
{
    fast_ostringstream oss;
    oss << "Error : " << error.message() << "Value:" << error.value() << "Category Name:" << error.category().name();
    PBLOG_WARN(oss.str());
    return false;
}
else
{
     string message = make_string(response_);
     size_t pos = message.find( "header" );
     if( pos != std::string::npos)
     {
          pos = pos - 2;
          string msg = message.substr(pos, message.length());
          msg.erase(std::remove(msg.begin(),msg.end(),'\n'),msg.end());
          msg.erase(std::remove(msg.begin(),msg.end(),'\r'),msg.end());
          msg.erase(msg.size()-1); //to ignore the short read error
          response = msg;

     }
     else
     {
        fast_ostringstream oss;
          oss << "Invalid Response: " << message;
          PBLOG_WARN(oss.str());
          return false;
     }
     socket.lowest_layer().shutdown(tcp::socket::shutdown_both);
}
}

Json 回复:

由于安全原因,我无法粘贴完整的响应,但添加了额外字符(此处在我们获得响应时附加了 21f0)的一小部分如下所示:

"SGEType":{"decisionKey"21f0:"SGMtype","decisionValue":null,"decisionGroup":"partyTranslations","ruleName":"Party Details Translations"}

请告诉我我从套接字读取的数据是否准确或需要修改。

I couldn't paste the full response due to security reason but small part where the extra character(here 21f0 is getting appended while we got the response)is getting added is shown below:

我们无从知晓。反响有多大?我对事情的疯狂尝试是它可能正在使用你可能 mis-handling 的分块编码,因为你手动“non-parse”HTTP 响应?

在那种情况下,我的 可能有点预言:

幸运的是,您还可以参考该实例,了解如何使用 Boost Beast 正确读取响应。

Live On Coliru

我也会重复总结,因为课程很重要:


旁注:只读到 EOF 可能适用于 HTTP/1.0。但是服务器可能会正确地拒绝该版本或选择以 HTTP/1.1 响应。