diff --git a/src/csoap/common.cc b/src/csoap/common.cc index 8f49afc..f616075 100644 --- a/src/csoap/common.cc +++ b/src/csoap/common.cc @@ -18,8 +18,8 @@ const std::string kTextXmlUtf8 = "text/xml; charset=utf-8"; //////////////////////////////////////////////////////////////////////////////// -const char* GetErrorMessage(ErrorCode error_code) { - switch (error_code) { +const char* GetErrorMessage(Error error) { + switch (error) { case kHostResolveError: return "Cannot resolve the host."; diff --git a/src/csoap/common.h b/src/csoap/common.h index f1042f1..d9e4bbc 100644 --- a/src/csoap/common.h +++ b/src/csoap/common.h @@ -35,7 +35,8 @@ const std::size_t kInvalidLength = std::string::npos; //////////////////////////////////////////////////////////////////////////////// -enum ErrorCode { +// Error codes. +enum Error { kNoError = 0, // OK kHostResolveError, @@ -59,7 +60,7 @@ enum ErrorCode { }; // Return a descriptive message for the given error code. -const char* GetErrorMessage(ErrorCode error_code); +const char* GetErrorMessage(Error error); //////////////////////////////////////////////////////////////////////////////// diff --git a/src/csoap/connection.cc b/src/csoap/connection.cc index 292cbf4..e8a6b28 100644 --- a/src/csoap/connection.cc +++ b/src/csoap/connection.cc @@ -50,7 +50,7 @@ void Connection::HandleRead(boost::system::error_code ec, return; } - ErrorCode error = request_parser_.Parse(buffer_.data(), bytes_transferred); + Error error = request_parser_.Parse(buffer_.data(), bytes_transferred); if (error != kNoError) { // Bad request. diff --git a/src/csoap/http_client.cc b/src/csoap/http_client.cc index 1908536..5ff893a 100644 --- a/src/csoap/http_client.cc +++ b/src/csoap/http_client.cc @@ -56,8 +56,8 @@ HttpClient::HttpClient() : timeout_seconds_(15) { } -ErrorCode HttpClient::SendRequest(const HttpRequest& request, - HttpResponse* response) { +Error HttpClient::SendRequest(const HttpRequest& request, + HttpResponse* response) { assert(response != NULL); using boost::asio::ip::tcp; @@ -71,6 +71,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request, port = "80"; } + // TODO: IPv4 or both IPv4 and IPv6 boost::system::error_code ec; tcp::resolver::results_type endpoints = resolver.resolve(/*tcp::v4(), */request.host(), port, ec); @@ -130,7 +131,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request, // Parse the response piece just read. // If the content has been fully received, next time flag "finished_" // will be set. - ErrorCode error = parser.Parse(buffer_.data(), length); + Error error = parser.Parse(buffer_.data(), length); if (error != kNoError) { return error; diff --git a/src/csoap/http_client.h b/src/csoap/http_client.h index dec69c1..a1cb911 100644 --- a/src/csoap/http_client.h +++ b/src/csoap/http_client.h @@ -25,8 +25,8 @@ public: } // Send an HTTP request, wait until the response is received. - ErrorCode SendRequest(const HttpRequest& request, - HttpResponse* response); + Error SendRequest(const HttpRequest& request, + HttpResponse* response); private: boost::asio::io_context io_context_; diff --git a/src/csoap/http_parser.cc b/src/csoap/http_parser.cc index 1abdab7..0dd883e 100644 --- a/src/csoap/http_parser.cc +++ b/src/csoap/http_parser.cc @@ -18,7 +18,7 @@ void HttpParser::Reset() { // TODO: Reset parsing state. } -ErrorCode HttpParser::Parse(const char* data, size_t len) { +Error HttpParser::Parse(const char* data, size_t len) { if (header_parsed_) { // Add the data to the content. message_->AppendContent(data, len); @@ -50,7 +50,7 @@ ErrorCode HttpParser::Parse(const char* data, size_t len) { if (!start_line_parsed_) { start_line_parsed_ = true; - ErrorCode error = ParseStartLine(line); + Error error = ParseStartLine(line); if (error != kNoError) { return error; } diff --git a/src/csoap/http_parser.h b/src/csoap/http_parser.h index fc7bc05..8ae093e 100644 --- a/src/csoap/http_parser.h +++ b/src/csoap/http_parser.h @@ -21,11 +21,11 @@ public: // Reset parsing state. void Reset(); - ErrorCode Parse(const char* data, size_t len); + Error Parse(const char* data, size_t len); protected: // Parse HTTP start line. - virtual ErrorCode ParseStartLine(const std::string& line) = 0; + virtual Error ParseStartLine(const std::string& line) = 0; void ParseContentLength(const std::string& line); @@ -33,7 +33,7 @@ protected: // The result HTTP message. HttpMessage* message_; - ErrorCode error_; + Error error_; // Data waiting to be parsed. std::string pending_data_; diff --git a/src/csoap/http_request_parser.cc b/src/csoap/http_request_parser.cc index 4560332..83e65b3 100644 --- a/src/csoap/http_request_parser.cc +++ b/src/csoap/http_request_parser.cc @@ -12,7 +12,7 @@ HttpRequestParser::HttpRequestParser(HttpRequest* request) , request_(request) { } -ErrorCode HttpRequestParser::ParseStartLine(const std::string& line) { +Error HttpRequestParser::ParseStartLine(const std::string& line) { // Example: POST / HTTP/1.1 std::vector strs; diff --git a/src/csoap/http_request_parser.h b/src/csoap/http_request_parser.h index 7bcbce5..53a7b62 100644 --- a/src/csoap/http_request_parser.h +++ b/src/csoap/http_request_parser.h @@ -12,7 +12,7 @@ public: explicit HttpRequestParser(HttpRequest* request); private: - ErrorCode ParseStartLine(const std::string& line) override; + Error ParseStartLine(const std::string& line) override; private: HttpRequest* request_; diff --git a/src/csoap/http_response_parser.cc b/src/csoap/http_response_parser.cc index 279c6a5..77f0fcf 100644 --- a/src/csoap/http_response_parser.cc +++ b/src/csoap/http_response_parser.cc @@ -10,7 +10,7 @@ HttpResponseParser::HttpResponseParser(HttpResponse* response) } // TODO: Use split. -ErrorCode HttpResponseParser::ParseStartLine(const std::string& line) { +Error HttpResponseParser::ParseStartLine(const std::string& line) { size_t off = 0; size_t pos = line.find(' '); diff --git a/src/csoap/http_response_parser.h b/src/csoap/http_response_parser.h index a5d8352..c6e6f51 100644 --- a/src/csoap/http_response_parser.h +++ b/src/csoap/http_response_parser.h @@ -13,7 +13,7 @@ public: private: // Parse HTTP start line; E.g., "HTTP/1.1 200 OK". - ErrorCode ParseStartLine(const std::string& line) override; + Error ParseStartLine(const std::string& line) override; private: // The result response message. diff --git a/src/csoap/soap_client.cc b/src/csoap/soap_client.cc index ba8384f..36e774d 100644 --- a/src/csoap/soap_client.cc +++ b/src/csoap/soap_client.cc @@ -13,63 +13,57 @@ namespace csoap { -bool SoapClient::Call(const std::string& operation, - const csoap::Parameter* parameters, - size_t count, - std::string* result) { +Error SoapClient::Call(const std::string& operation, + const Parameter* parameters, + std::size_t count, + std::string* result) { assert(!url_.empty() && !host_.empty() && !result_name_.empty() && service_ns_.IsValid()); - csoap::SoapRequest soap_request; + SoapRequest soap_request; - soap_request.set_soapenv_ns(kSoapEnvNamespace); + soap_request.set_soapenv_ns(kSoapEnvNamespace); // TODO: Configurable soap_request.set_service_ns(service_ns_); soap_request.set_operation(operation); - for (size_t i = 0; i < count; ++i) { + for (std::size_t i = 0; i < count; ++i) { soap_request.AddParameter(parameters[i]); } std::string http_request_body; soap_request.ToXml(&http_request_body); - csoap::HttpRequest http_request; + HttpRequest http_request; - http_request.set_version(csoap::kHttpV11); + http_request.set_version(kHttpV11); // TODO http_request.set_url(url_); http_request.set_content_length(http_request_body.size()); http_request.set_content(http_request_body); // TODO: move http_request.set_host(host_, port_); http_request.set_soap_action(operation); - csoap::HttpResponse http_response; + HttpResponse http_response; - csoap::HttpClient http_client; - csoap::ErrorCode ec = http_client.SendRequest(http_request, &http_response); - if (ec != csoap::kNoError) { - std::cerr << csoap::GetErrorMessage(ec) << std::endl; + HttpClient http_client; + Error error = http_client.SendRequest(http_request, &http_response); - if (ec == csoap::kHttpStatusError) { - //std::cerr << "\t" - // << http_response.status() << ", " - // << http_response.reason() << std::endl; - } - - return false; + if (error != kNoError) { + return error; } - csoap::SoapResponse soap_response; + SoapResponse soap_response; soap_response.set_result_name(result_name_); - if (soap_response.FromXml(http_response.content())) { - *result = soap_response.result(); - return true; + if (!soap_response.FromXml(http_response.content())) { + return kXmlError; // TODO: Some SOAP error? } - return false; + *result = soap_response.result(); + + return kNoError; } } // namespace csoap diff --git a/src/csoap/soap_client.h b/src/csoap/soap_client.h index 4c973af..17713e2 100644 --- a/src/csoap/soap_client.h +++ b/src/csoap/soap_client.h @@ -19,10 +19,10 @@ protected: } // A generic wrapper to make a call. - bool Call(const std::string& operation, - const csoap::Parameter* parameters, - size_t count, - std::string* result); + Error Call(const std::string& operation, + const Parameter* parameters, + std::size_t count, + std::string* result); protected: // Request URL. diff --git a/src/demo/calculator_client/calculator_client.cc b/src/demo/calculator_client/calculator_client.cc index 04661b2..3c831b7 100644 --- a/src/demo/calculator_client/calculator_client.cc +++ b/src/demo/calculator_client/calculator_client.cc @@ -55,7 +55,11 @@ bool CalculatorClient::Calc(const std::string& operation, }; std::string result_str; - if (!Call(operation, parameters, 2, &result_str)) { + csoap::Error error = Call(operation, parameters, 2, &result_str); + + if (error != csoap::kNoError) { + std::cerr << "Error: " << error; + std::cerr << ", " << csoap::GetErrorMessage(error) << std::endl; return false; } diff --git a/src/demo/csdm_client/csdm_client.cc b/src/demo/csdm_client/csdm_client.cc index 83a7749..b2a2377 100644 --- a/src/demo/csdm_client/csdm_client.cc +++ b/src/demo/csdm_client/csdm_client.cc @@ -9,7 +9,7 @@ CsdmClient::CsdmClient() { } bool CsdmClient::GetVersion(std::string* result_xml) { - return Call("getVersion", NULL, 0, result_xml); + return Call0("getVersion", result_xml); } void CsdmClient::Init() { @@ -20,7 +20,12 @@ void CsdmClient::Init() { host_ = "127.0.0.1"; port_ = "52540"; - result_name_ = "Result"; + result_name_ = "return"; +} + +bool CsdmClient::Call0(const std::string& operation, + std::string* result_xml) { + return CallEx(operation, NULL, 0, result_xml); } bool CsdmClient::Call1(const std::string& operation, @@ -31,5 +36,27 @@ bool CsdmClient::Call1(const std::string& operation, { name, value }, }; - return Call(operation, parameters, 1, result_xml); + return CallEx(operation, parameters, 1, result_xml); +} + +bool CsdmClient::Call2(const std::string& operation, + const std::string& name1, + const std::string& value1, + const std::string& name2, + const std::string& value2, + std::string* result_xml) { + csoap::Parameter parameters[] = { + { name1, value1 }, + { name2, value2 }, + }; + + return CallEx(operation, parameters, 2, result_xml); +} + +bool CsdmClient::CallEx(const std::string& operation, + const csoap::Parameter* parameters, + std::size_t count, + std::string* result) { + csoap::Error error = Call(operation, parameters, count, result); + return error == csoap::kNoError; } diff --git a/src/demo/csdm_client/csdm_client.h b/src/demo/csdm_client/csdm_client.h index 5b23d6f..28785ae 100644 --- a/src/demo/csdm_client/csdm_client.h +++ b/src/demo/csdm_client/csdm_client.h @@ -15,11 +15,29 @@ public: protected: void Init(); + // Call CSDM API with NO parameter. + bool Call0(const std::string& operation, + std::string* result_xml); + // Call CSDM API with one parameter. bool Call1(const std::string& operation, const std::string& name, const std::string& value, std::string* result_xml); + + // Call CSDM API with two parameters. + bool Call2(const std::string& operation, + const std::string& name1, + const std::string& value1, + const std::string& name2, + const std::string& value2, + std::string* result_xml); + + // A wrapper of CSoapClient::Call(). + bool CallEx(const std::string& operation, + const csoap::Parameter* parameters, + std::size_t count, + std::string* result); }; #endif // CSDM_CLIENT_H_