diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f8d541..b8065e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,11 +3,15 @@ if(WEBCC_ENABLE_UT) add_subdirectory(webcc_unittest) endif() -add_subdirectory(pugixml) +if(WEBCC_ENABLE_SOAP) + add_subdirectory(pugixml) +endif() add_subdirectory(webcc) if(WEBCC_ENABLE_DEMO) - add_subdirectory(demo/soap/calc_client) - add_subdirectory(demo/soap/calc_server) + if(WEBCC_ENABLE_SOAP) + add_subdirectory(demo/soap/calc_client) + add_subdirectory(demo/soap/calc_server) + endif() endif() \ No newline at end of file diff --git a/src/webcc/CMakeLists.txt b/src/webcc/CMakeLists.txt index 3cc237e..5cde4bd 100644 --- a/src/webcc/CMakeLists.txt +++ b/src/webcc/CMakeLists.txt @@ -7,6 +7,43 @@ endif() # Don't use any deprecated definitions (e.g., io_service). add_definitions(-DBOOST_ASIO_NO_DEPRECATED) -file(GLOB SRCS *.cc *.h) +set(SRCS + common.cc + common.h + http_client.cc + http_client.h + http_message.cc + http_message.h + http_parser.cc + http_parser.h + http_request.cc + http_request.h + http_request_handler.cc + http_request_handler.h + http_request_parser.cc + http_request_parser.h + http_response.cc + http_response.h + http_response_parser.cc + http_response_parser.h + http_server.cc + http_server.h + http_session.cc + http_session.h + queue.h + rest_server.cc + rest_server.h + rest_service.h + url.cc + url.h + utility.cc + utility.h + ) + +if(WEBCC_ENABLE_SOAP) + # SOAP specific sources. + file(GLOB SOAP_SRCS soap_*.cc soap_*.h) + set(SRCS ${SRCS} ${SOAP_SRCS}) +endif() add_library(webcc ${SRCS}) diff --git a/src/webcc/http_client.cc b/src/webcc/http_client.cc index 49886eb..ed5dd72 100644 --- a/src/webcc/http_client.cc +++ b/src/webcc/http_client.cc @@ -83,7 +83,7 @@ Error HttpClient::SendRequest(const HttpRequest& request, // Send HTTP request. #if WEBCC_DEBUG_OUTPUT - std::cout << "# REQUEST" << std::endl << request << std::endl; + std::cout << "--- REQUEST ---" << std::endl << request << std::endl; #endif try { @@ -93,7 +93,7 @@ Error HttpClient::SendRequest(const HttpRequest& request, } #if WEBCC_DEBUG_OUTPUT - std::cout << "# RESPONSE" << std::endl; + std::cout << "--- RESPONSE ---" << std::endl; #endif // Read and parse HTTP response. @@ -131,7 +131,7 @@ Error HttpClient::SendRequest(const HttpRequest& request, #if WEBCC_DEBUG_OUTPUT std::cout << std::endl; - std::cout << "# RESPONSE (PARSED)" << std::endl; + std::cout << "--- RESPONSE (PARSED) ---" << std::endl; std::cout << *response << std::endl; #endif diff --git a/src/webcc/http_response.cc b/src/webcc/http_response.cc index 98d596c..ef05e9b 100644 --- a/src/webcc/http_response.cc +++ b/src/webcc/http_response.cc @@ -1,8 +1,5 @@ #include "webcc/http_response.h" -#include "webcc/common.h" -#include "webcc/xml.h" - namespace webcc { std::ostream& operator<<(std::ostream& os, const HttpResponse& response) { @@ -12,12 +9,8 @@ std::ostream& operator<<(std::ostream& os, const HttpResponse& response) { os << h.name << ": " << h.value << std::endl; } - os << std::endl; - - // Pretty print the SOAP response XML. - if (!xml::PrettyPrintXml(os, response.content())) { - os << response.content(); - } + os << std::endl << std::endl; + os << response.content(); return os; } diff --git a/src/webcc/http_response.h b/src/webcc/http_response.h index ba3f221..8748876 100644 --- a/src/webcc/http_response.h +++ b/src/webcc/http_response.h @@ -22,6 +22,7 @@ public: int status() const { return status_; } + void set_status(int status) { status_ = status; } @@ -35,7 +36,7 @@ public: static HttpResponse Fault(HttpStatus::Enum status); private: - int status_ = HttpStatus::kOK; // TODO: HttpStatus + int status_ = HttpStatus::kOK; }; } // namespace webcc diff --git a/src/webcc/soap_message.cc b/src/webcc/soap_message.cc index 02cdb42..bfb7fd9 100644 --- a/src/webcc/soap_message.cc +++ b/src/webcc/soap_message.cc @@ -1,7 +1,7 @@ #include "webcc/soap_message.h" #include -#include "webcc/xml.h" +#include "webcc/soap_xml.h" namespace webcc { @@ -18,15 +18,15 @@ void SoapMessage::ToXml(std::string* xml_string) { // pugi::xml_node xdecl = xdoc.prepend_child(pugi::node_declaration); // xdecl.append_attribute("version").set_value("1.0"); - pugi::xml_node xroot = xml::AddChild(xdoc, soapenv_ns_.name, "Envelope"); + pugi::xml_node xroot = soap_xml::AddChild(xdoc, soapenv_ns_.name, "Envelope"); - xml::AddNSAttr(xroot, soapenv_ns_.name, soapenv_ns_.url); + soap_xml::AddNSAttr(xroot, soapenv_ns_.name, soapenv_ns_.url); - pugi::xml_node xbody = xml::AddChild(xroot, soapenv_ns_.name, "Body"); + pugi::xml_node xbody = soap_xml::AddChild(xroot, soapenv_ns_.name, "Body"); ToXmlBody(xbody); - xml::XmlStrRefWriter writer(xml_string); + soap_xml::XmlStrRefWriter writer(xml_string); xdoc.save(writer, "\t", pugi::format_default, pugi::encoding_utf8); } @@ -40,10 +40,10 @@ bool SoapMessage::FromXml(const std::string& xml_string) { pugi::xml_node xroot = xdoc.document_element(); - soapenv_ns_.name = xml::GetPrefix(xroot); - soapenv_ns_.url = xml::GetNSAttr(xroot, soapenv_ns_.name); + soapenv_ns_.name = soap_xml::GetPrefix(xroot); + soapenv_ns_.url = soap_xml::GetNSAttr(xroot, soapenv_ns_.name); - pugi::xml_node xbody = xml::GetChild(xroot, soapenv_ns_.name, "Body"); + pugi::xml_node xbody = soap_xml::GetChild(xroot, soapenv_ns_.name, "Body"); if (xbody) { return FromXmlBody(xbody); } diff --git a/src/webcc/soap_request.cc b/src/webcc/soap_request.cc index 92b0c4f..e209175 100644 --- a/src/webcc/soap_request.cc +++ b/src/webcc/soap_request.cc @@ -1,5 +1,5 @@ #include "webcc/soap_request.h" -#include "webcc/xml.h" +#include "webcc/soap_xml.h" namespace webcc { @@ -23,11 +23,11 @@ const std::string& SoapRequest::GetParameter(const std::string& key) const { } void SoapRequest::ToXmlBody(pugi::xml_node xbody) { - pugi::xml_node xop = xml::AddChild(xbody, service_ns_.name, operation_); - xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); + pugi::xml_node xop = soap_xml::AddChild(xbody, service_ns_.name, operation_); + soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); for (Parameter& p : parameters_) { - pugi::xml_node xparam = xml::AddChild(xop, service_ns_.name, p.key()); + pugi::xml_node xparam = soap_xml::AddChild(xop, service_ns_.name, p.key()); xparam.text().set(p.value().c_str()); } } @@ -38,13 +38,13 @@ bool SoapRequest::FromXmlBody(pugi::xml_node xbody) { return false; } - xml::SplitName(xoperation, &service_ns_.name, &operation_); - service_ns_.url = xml::GetNSAttr(xoperation, service_ns_.name); + soap_xml::SplitName(xoperation, &service_ns_.name, &operation_); + service_ns_.url = soap_xml::GetNSAttr(xoperation, service_ns_.name); pugi::xml_node xparameter = xoperation.first_child(); while (xparameter) { parameters_.push_back({ - xml::GetNameNoPrefix(xparameter), + soap_xml::GetNameNoPrefix(xparameter), std::string(xparameter.text().as_string()) }); diff --git a/src/webcc/soap_response.cc b/src/webcc/soap_response.cc index b66f2e9..38c5be8 100644 --- a/src/webcc/soap_response.cc +++ b/src/webcc/soap_response.cc @@ -1,16 +1,19 @@ #include "webcc/soap_response.h" #include -#include "webcc/xml.h" +#include "webcc/soap_xml.h" namespace webcc { void SoapResponse::ToXmlBody(pugi::xml_node xbody) { - std::string rsp_operation = operation_ + "Response"; - pugi::xml_node xop = xml::AddChild(xbody, service_ns_.name, rsp_operation); - xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); - - pugi::xml_node xresult = xml::AddChild(xop, service_ns_.name, result_name_); + pugi::xml_node xop = soap_xml::AddChild(xbody, + service_ns_.name, + operation_ + "Response"); + soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); + + pugi::xml_node xresult = soap_xml::AddChild(xop, + service_ns_.name, + result_name_); xresult.text().set(result_.c_str()); } @@ -19,10 +22,10 @@ bool SoapResponse::FromXmlBody(pugi::xml_node xbody) { pugi::xml_node xresponse = xbody.first_child(); if (xresponse) { - xml::SplitName(xresponse, &service_ns_.name, NULL); - service_ns_.url = xml::GetNSAttr(xresponse, service_ns_.name); + soap_xml::SplitName(xresponse, &service_ns_.name, NULL); + service_ns_.url = soap_xml::GetNSAttr(xresponse, service_ns_.name); - pugi::xml_node xresult = xml::GetChildNoNS(xresponse, result_name_); + pugi::xml_node xresult = soap_xml::GetChildNoNS(xresponse, result_name_); if (xresult) { result_ = xresult.text().get(); return true; diff --git a/src/webcc/xml.cc b/src/webcc/soap_xml.cc similarity index 92% rename from src/webcc/xml.cc rename to src/webcc/soap_xml.cc index 6b292c1..b6c49ac 100644 --- a/src/webcc/xml.cc +++ b/src/webcc/soap_xml.cc @@ -1,7 +1,7 @@ -#include "webcc/xml.h" +#include "webcc/soap_xml.h" namespace webcc { -namespace xml { +namespace soap_xml { void SplitName(const pugi::xml_node& xnode, std::string* prefix, @@ -92,9 +92,9 @@ std::string GetNSAttr(pugi::xml_node& xnode, return xnode.attribute(attr_name.c_str()).as_string(); } -bool PrettyPrintXml(std::ostream& os, - const std::string& xml_string, - const char* indent) { +bool PrettyPrint(std::ostream& os, + const std::string& xml_string, + const char* indent) { pugi::xml_document xdoc; if (!xdoc.load_string(xml_string.c_str())) { os << "Invalid XML" << std::endl; @@ -105,5 +105,5 @@ bool PrettyPrintXml(std::ostream& os, return true; } -} // namespace xml +} // namespace soap_xml } // namespace webcc diff --git a/src/webcc/xml.h b/src/webcc/soap_xml.h similarity index 89% rename from src/webcc/xml.h rename to src/webcc/soap_xml.h index 0608b38..b7cd12b 100644 --- a/src/webcc/xml.h +++ b/src/webcc/soap_xml.h @@ -1,5 +1,5 @@ -#ifndef WEBCC_XML_H_ -#define WEBCC_XML_H_ +#ifndef WEBCC_SOAP_XML_H_ +#define WEBCC_SOAP_XML_H_ // XML utilities. @@ -7,7 +7,7 @@ #include "pugixml/pugixml.hpp" namespace webcc { -namespace xml { +namespace soap_xml { // Split the node name into namespace prefix and real name. // E.g., if the node name is "soapenv:Envelope", it will be splited to @@ -81,11 +81,12 @@ private: std::string* result_; }; -bool PrettyPrintXml(std::ostream& os, - const std::string& xml_string, - const char* indent = "\t"); +// Print the XML string to output stream in pretty format. +bool PrettyPrint(std::ostream& os, + const std::string& xml_string, + const char* indent = "\t"); -} // namespace xml +} // namespace soap_xml } // namespace webcc -#endif // WEBCC_XML_H_ +#endif // WEBCC_SOAP_XML_H_