From f0e13b4652b1780b603991b97d352256cac86046 Mon Sep 17 00:00:00 2001 From: Leo Ribeiro Date: Thu, 30 Jul 2020 18:49:12 -0400 Subject: [PATCH 1/3] adds http1.1 support and better response handling --- programs/cleos/httpc.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/programs/cleos/httpc.cpp b/programs/cleos/httpc.cpp index 26e3b5b1dd..1b958ea038 100644 --- a/programs/cleos/httpc.cpp +++ b/programs/cleos/httpc.cpp @@ -195,7 +195,7 @@ namespace eosio { namespace client { namespace http { boost::asio::streambuf request; std::ostream request_stream(&request); auto host_header_value = format_host_header(url); - request_stream << "POST " << url.path << " HTTP/1.0\r\n"; + request_stream << "POST " << url.path << " HTTP/1.1\r\n"; request_stream << "Host: " << host_header_value << "\r\n"; request_stream << "content-length: " << postjson.size() << "\r\n"; request_stream << "Accept: */*\r\n"; @@ -253,14 +253,15 @@ namespace eosio { namespace client { namespace http { throw; } - const auto response_result = fc::json::from_string(re); if( print_response ) { std::cerr << "RESPONSE:" << std::endl << "---------------------" << std::endl - << fc::json::to_pretty_string( response_result ) << std::endl + << re << std::endl << "---------------------" << std::endl; } + if( status_code == 200 || status_code == 201 || status_code == 202 ) { + auto response_result = fc::json::from_string(re); return response_result; } else if( status_code == 404 ) { // Unknown endpoint @@ -278,6 +279,7 @@ namespace eosio { namespace client { namespace http { } } } else { + auto response_result = fc::json::from_string(re); auto &&error_info = response_result.as().error; // Construct fc exception from error const auto &error_details = error_info.details; @@ -292,6 +294,6 @@ namespace eosio { namespace client { namespace http { } EOS_ASSERT( status_code == 200, http_request_fail, "Error code ${c}\n: ${msg}\n", ("c", status_code)("msg", re) ); - return response_result; + return variant(); } }}} From 3618276910018a2ccbd4689259c485a9c0be12b3 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 12 Aug 2020 10:32:27 -0500 Subject: [PATCH 2/3] Revert back to pretty print of response unless unable to convert to variant --- programs/cleos/httpc.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/programs/cleos/httpc.cpp b/programs/cleos/httpc.cpp index 1b958ea038..8fb2b70dd4 100644 --- a/programs/cleos/httpc.cpp +++ b/programs/cleos/httpc.cpp @@ -253,15 +253,21 @@ namespace eosio { namespace client { namespace http { throw; } + fc::variant response_result; + try { + response_result = fc::json::from_string(re); + } catch(...) { + // re reported below if print_response requested + } + if( print_response ) { std::cerr << "RESPONSE:" << std::endl << "---------------------" << std::endl - << re << std::endl + << ( response_result.is_null() ? re : fc::json::to_pretty_string( response_result ) ) << std::endl << "---------------------" << std::endl; } if( status_code == 200 || status_code == 201 || status_code == 202 ) { - auto response_result = fc::json::from_string(re); return response_result; } else if( status_code == 404 ) { // Unknown endpoint @@ -279,7 +285,6 @@ namespace eosio { namespace client { namespace http { } } } else { - auto response_result = fc::json::from_string(re); auto &&error_info = response_result.as().error; // Construct fc exception from error const auto &error_details = error_info.details; @@ -294,6 +299,6 @@ namespace eosio { namespace client { namespace http { } EOS_ASSERT( status_code == 200, http_request_fail, "Error code ${c}\n: ${msg}\n", ("c", status_code)("msg", re) ); - return variant(); + return response_result; } }}} From 059cee4650c6e19953c6edd54e229f4978fb6dc2 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 12 Aug 2020 14:19:06 -0500 Subject: [PATCH 3/3] Better handling of invalid response --- programs/cleos/httpc.cpp | 55 ++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/programs/cleos/httpc.cpp b/programs/cleos/httpc.cpp index 8fb2b70dd4..ecdb1541e1 100644 --- a/programs/cleos/httpc.cpp +++ b/programs/cleos/httpc.cpp @@ -267,38 +267,37 @@ namespace eosio { namespace client { namespace http { << "---------------------" << std::endl; } - if( status_code == 200 || status_code == 201 || status_code == 202 ) { - return response_result; - } else if( status_code == 404 ) { - // Unknown endpoint - if (url.path.compare(0, chain_func_base.size(), chain_func_base) == 0) { - throw chain::missing_chain_api_plugin_exception(FC_LOG_MESSAGE(error, "Chain API plugin is not enabled")); - } else if (url.path.compare(0, wallet_func_base.size(), wallet_func_base) == 0) { - throw chain::missing_wallet_api_plugin_exception(FC_LOG_MESSAGE(error, "Wallet is not available")); - } else if (url.path.compare(0, history_func_base.size(), history_func_base) == 0) { - throw chain::missing_history_api_plugin_exception(FC_LOG_MESSAGE(error, "History API plugin is not enabled")); - } else if (url.path.compare(0, net_func_base.size(), net_func_base) == 0) { - throw chain::missing_net_api_plugin_exception(FC_LOG_MESSAGE(error, "Net API plugin is not enabled")); - } else if (url.path.compare(0, trace_api_func_base.size(), trace_api_func_base) == 0) { - if ( re.find("Trace API:") == string::npos ) { - throw chain::missing_trace_api_plugin_exception(FC_LOG_MESSAGE(error, "Trace API plugin is not enabled")); + if( !response_result.is_null() ) { + if( status_code == 200 || status_code == 201 || status_code == 202 ) { + return response_result; + } else if( status_code == 404 ) { + // Unknown endpoint + if (url.path.compare(0, chain_func_base.size(), chain_func_base) == 0) { + throw chain::missing_chain_api_plugin_exception(FC_LOG_MESSAGE(error, "Chain API plugin is not enabled")); + } else if (url.path.compare(0, wallet_func_base.size(), wallet_func_base) == 0) { + throw chain::missing_wallet_api_plugin_exception(FC_LOG_MESSAGE(error, "Wallet is not available")); + } else if (url.path.compare(0, history_func_base.size(), history_func_base) == 0) { + throw chain::missing_history_api_plugin_exception(FC_LOG_MESSAGE(error, "History API plugin is not enabled")); + } else if (url.path.compare(0, net_func_base.size(), net_func_base) == 0) { + throw chain::missing_net_api_plugin_exception(FC_LOG_MESSAGE(error, "Net API plugin is not enabled")); + } + } else { + auto &&error_info = response_result.as().error; + // Construct fc exception from error + const auto &error_details = error_info.details; + + fc::log_messages logs; + for (auto itr = error_details.begin(); itr != error_details.end(); itr++) { + const auto& context = fc::log_context(fc::log_level::error, itr->file.data(), itr->line_number, itr->method.data()); + logs.emplace_back(fc::log_message(context, itr->message)); } - } - } else { - auto &&error_info = response_result.as().error; - // Construct fc exception from error - const auto &error_details = error_info.details; - - fc::log_messages logs; - for (auto itr = error_details.begin(); itr != error_details.end(); itr++) { - const auto& context = fc::log_context(fc::log_level::error, itr->file.data(), itr->line_number, itr->method.data()); - logs.emplace_back(fc::log_message(context, itr->message)); - } - throw fc::exception(logs, error_info.code, error_info.name, error_info.what); + throw fc::exception(logs, error_info.code, error_info.name, error_info.what); + } } - EOS_ASSERT( status_code == 200, http_request_fail, "Error code ${c}\n: ${msg}\n", ("c", status_code)("msg", re) ); + EOS_ASSERT( status_code == 200 && !response_result.is_null(), http_request_fail, + "Error code ${c}\n: ${msg}\n", ("c", status_code)("msg", re) ); return response_result; } }}}