Support CDATA for SOAP request and response; Rename Parameter to SoapParameter; Add book server and client example for SOAP.
This commit is contained in:
@@ -3,21 +3,10 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
// Sleep several seconds for the client to test timeout control.
|
||||
#define SLEEP_FOR_TIMEOUT_TEST 0
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
#include "boost/thread/thread.hpp"
|
||||
#endif
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_request.h"
|
||||
#include "webcc/soap_response.h"
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
static const int kSleepSeconds = 3;
|
||||
#endif
|
||||
|
||||
bool CalcService::Handle(const webcc::SoapRequest& soap_request,
|
||||
webcc::SoapResponse* soap_response) {
|
||||
double x = 0.0;
|
||||
@@ -26,22 +15,22 @@ bool CalcService::Handle(const webcc::SoapRequest& soap_request,
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& op = soap_request.operation();
|
||||
const std::string& operation = soap_request.operation();
|
||||
|
||||
LOG_INFO("Soap operation '%s': %.2f, %.2f", op.c_str(), x, y);
|
||||
LOG_INFO("Soap operation '%s': %.2f, %.2f", operation.c_str(), x, y);
|
||||
|
||||
std::function<double(double, double)> calc;
|
||||
|
||||
if (op == "add") {
|
||||
if (operation == "add") {
|
||||
calc = [](double x, double y) { return x + y; };
|
||||
|
||||
} else if (op == "subtract") {
|
||||
} else if (operation == "subtract") {
|
||||
calc = [](double x, double y) { return x - y; };
|
||||
|
||||
} else if (op == "multiply") {
|
||||
} else if (operation == "multiply") {
|
||||
calc = [](double x, double y) { return x * y; };
|
||||
|
||||
} else if (op == "divide") {
|
||||
} else if (operation == "divide") {
|
||||
calc = [](double x, double y) { return x / y; };
|
||||
|
||||
if (y == 0.0) {
|
||||
@@ -49,7 +38,7 @@ bool CalcService::Handle(const webcc::SoapRequest& soap_request,
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
LOG_ERRO("Operation '%s' is not supported.", op.c_str());
|
||||
LOG_ERRO("Operation '%s' is not supported.", operation.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -68,13 +57,7 @@ bool CalcService::Handle(const webcc::SoapRequest& soap_request,
|
||||
soap_response->set_operation(soap_request.operation());
|
||||
|
||||
soap_response->set_result_name("Result");
|
||||
soap_response->set_result(std::to_string(result));
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
LOG_INFO("Sleep %d seconds for the client to test timeout control.",
|
||||
kSleepSeconds);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(kSleepSeconds));
|
||||
#endif
|
||||
soap_response->set_result_moved(std::to_string(result), false);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -85,7 +68,7 @@ bool CalcService::GetParameters(const webcc::SoapRequest& soap_request,
|
||||
*x = std::stod(soap_request.GetParameter("x"));
|
||||
*y = std::stod(soap_request.GetParameter("y"));
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERRO("Parameter cast error: %s", e.what());
|
||||
LOG_ERRO("SoapParameter cast error: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
#include "webcc/soap_service.h"
|
||||
|
||||
class CalcService : public webcc::SoapService {
|
||||
public:
|
||||
public:
|
||||
CalcService() = default;
|
||||
~CalcService() override = default;
|
||||
|
||||
bool Handle(const webcc::SoapRequest& soap_request,
|
||||
webcc::SoapResponse* soap_response) override;
|
||||
|
||||
private:
|
||||
private:
|
||||
bool GetParameters(const webcc::SoapRequest& soap_request,
|
||||
double* x, double* y);
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "example/soap_calc_server/calc_service.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
@@ -24,6 +24,11 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
try {
|
||||
webcc::SoapServer server(port, workers);
|
||||
|
||||
// Customize response XML format.
|
||||
server.set_format_raw(false);
|
||||
server.set_indent_str(" ");
|
||||
|
||||
server.Bind(std::make_shared<CalcService>(), "/calculator");
|
||||
server.Run();
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
Reference in New Issue
Block a user