Pass pugi xml_mode by value; refine coding style.

master
Adam Gu 7 years ago
parent b41dfcebd9
commit 79665c75ba

@ -9,7 +9,7 @@
#include "webcc/http_response.h" #include "webcc/http_response.h"
#include "webcc/rest_client.h" #include "webcc/rest_client.h"
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
// Write a JSON object to string. // Write a JSON object to string.
std::string JsonToString(const Json::Value& json) { std::string JsonToString(const Json::Value& json) {
@ -17,7 +17,7 @@ std::string JsonToString(const Json::Value& json) {
return Json::writeString(builder, json); return Json::writeString(builder, json);
} }
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
class BookListClient { class BookListClient {
public: public:
@ -62,7 +62,7 @@ private:
webcc::RestClient rest_client_; webcc::RestClient rest_client_;
}; };
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
class BookDetailClient { class BookDetailClient {
public: public:
@ -118,7 +118,7 @@ private:
webcc::RestClient rest_client_; webcc::RestClient rest_client_;
}; };
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
void Help(const char* argv0) { void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl; std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;

@ -2,10 +2,11 @@
#include <list> #include <list>
#include <iostream> #include <iostream>
#include "boost/lexical_cast.hpp" #include "boost/lexical_cast.hpp"
#include "json/json.h" // jsoncpp #include "json/json.h"
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
// In-memory test data. // In-memory test data.
// There should be some database in a real product. // There should be some database in a real product.
@ -97,7 +98,7 @@ private:
static BookStore g_book_store; static BookStore g_book_store;
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
static bool BookFromJson(const std::string& json, Book* book) { static bool BookFromJson(const std::string& json, Book* book) {
Json::Value root; Json::Value root;
@ -142,7 +143,7 @@ bool BookListService::Post(const std::string& request_content,
return false; return false;
} }
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches, bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
const webcc::UrlQuery& query, const webcc::UrlQuery& query,
@ -182,7 +183,8 @@ bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
return false; return false;
} }
bool BookDetailService::Delete(const std::vector<std::string>& url_sub_matches) { bool BookDetailService::Delete(
const std::vector<std::string>& url_sub_matches) {
if (url_sub_matches.size() != 1) { if (url_sub_matches.size() != 1) {
return false; return false;
} }

@ -3,7 +3,7 @@
#include "webcc/rest_service.h" #include "webcc/rest_service.h"
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
// BookListService handles the HTTP GET and returns the book list based on // BookListService handles the HTTP GET and returns the book list based on
// query parameters specified in the URL. // query parameters specified in the URL.
@ -25,7 +25,7 @@ class BookListService : public webcc::RestListService {
std::string* response_content) final; std::string* response_content) final;
}; };
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
// The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter // The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter
// contains the matched book ID. // contains the matched book ID.

@ -3,7 +3,7 @@
#include "calc_client.h" #include "calc_client.h"
int main() { int main() {
LOG_INIT(webcc::INFO, 0); LOG_INIT(webcc::VERB, 0);
CalcClient calc; CalcClient calc;

@ -4,6 +4,8 @@
namespace webcc { namespace webcc {
// -----------------------------------------------------------------------------
// NOTE: // NOTE:
// Field names are case-insensitive. // Field names are case-insensitive.
// See: https://stackoverflow.com/a/5259004 // See: https://stackoverflow.com/a/5259004
@ -27,7 +29,7 @@ const std::string kHttpPatch = "PATCH";
const std::string kHttpPut = "PUT"; const std::string kHttpPut = "PUT";
const std::string kHttpDelete = "DELETE"; const std::string kHttpDelete = "DELETE";
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
const char* GetErrorMessage(Error error) { const char* GetErrorMessage(Error error) {
switch (error) { switch (error) {
@ -63,7 +65,7 @@ const char* GetErrorMessage(Error error) {
} }
} }
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
Parameter::Parameter(const std::string& key, const char* value) Parameter::Parameter(const std::string& key, const char* value)
: key_(key), value_(value) { : key_(key), value_(value) {

@ -19,14 +19,10 @@
namespace webcc { namespace webcc {
////////////////////////////////////////////////////////////////////////////////
static const int kConnectMaxSeconds = 10; static const int kConnectMaxSeconds = 10;
static const int kSendMaxSeconds = 10; static const int kSendMaxSeconds = 10;
static const int kReceiveMaxSeconds = 30; static const int kReceiveMaxSeconds = 30;
////////////////////////////////////////////////////////////////////////////////
HttpClient::HttpClient() HttpClient::HttpClient()
: socket_(io_context_), : socket_(io_context_),
timeout_seconds_(kReceiveMaxSeconds), timeout_seconds_(kReceiveMaxSeconds),
@ -38,7 +34,7 @@ HttpClient::HttpClient()
} }
Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) { Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) {
assert(response != NULL); assert(response != nullptr);
Error error = kNoError; Error error = kNoError;

@ -81,8 +81,6 @@ const char CRLF[] = { '\r', '\n' };
} // misc_strings } // misc_strings
////////////////////////////////////////////////////////////////////////////////
// ATTENTION: The buffers don't hold the memory! // ATTENTION: The buffers don't hold the memory!
std::vector<boost::asio::const_buffer> HttpResponse::ToBuffers() const { std::vector<boost::asio::const_buffer> HttpResponse::ToBuffers() const {
std::vector<boost::asio::const_buffer> buffers; std::vector<boost::asio::const_buffer> buffers;

@ -38,7 +38,7 @@ HttpServer::HttpServer(unsigned short port, std::size_t workers)
} }
void HttpServer::Run() { void HttpServer::Run() {
assert(GetRequestHandler() != NULL); assert(GetRequestHandler() != nullptr);
LOG_INFO("Server is going to run..."); LOG_INFO("Server is going to run...");

@ -11,8 +11,6 @@
namespace webcc { namespace webcc {
////////////////////////////////////////////////////////////////////////////////
static std::string GetThreadId() { static std::string GetThreadId() {
boost::thread::id thread_id = boost::this_thread::get_id(); boost::thread::id thread_id = boost::this_thread::get_id();
std::stringstream ss; std::stringstream ss;
@ -20,8 +18,6 @@ static std::string GetThreadId() {
return ss.str(); return ss.str();
} }
////////////////////////////////////////////////////////////////////////////////
static const char* kLevelNames[] = { static const char* kLevelNames[] = {
"VERB", "INFO", "WARN", "ERRO", "FATA" "VERB", "INFO", "WARN", "ERRO", "FATA"
}; };
@ -35,8 +31,6 @@ struct Logger {
// Global logger. // Global logger.
static Logger g_logger{ VERB }; static Logger g_logger{ VERB };
////////////////////////////////////////////////////////////////////////////////
void LogInit(int level, int modes) { void LogInit(int level, int modes) {
g_logger.modes = modes; g_logger.modes = modes;
g_logger.level = level; g_logger.level = level;

@ -4,6 +4,8 @@
namespace webcc { namespace webcc {
// -----------------------------------------------------------------------------
bool RestListService::Handle(const std::string& http_method, bool RestListService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const UrlQuery& query, const UrlQuery& query,
@ -22,7 +24,7 @@ bool RestListService::Handle(const std::string& http_method,
return false; return false;
} }
//////////////////////////////////////////////////////////////////////////////// // -----------------------------------------------------------------------------
bool RestDetailService::Handle(const std::string& http_method, bool RestDetailService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,

@ -32,10 +32,8 @@ bool RestServiceManager::AddService(RestServicePtr service,
} }
RestServicePtr RestServiceManager::GetService( RestServicePtr RestServiceManager::GetService(
const std::string& url, const std::string& url, std::vector<std::string>* sub_matches) {
std::vector<std::string>* sub_matches) { assert(sub_matches != nullptr);
assert(sub_matches != NULL);
for (ServiceItem& item : service_items_) { for (ServiceItem& item : service_items_) {
if (item.is_regex) { if (item.is_regex) {

@ -24,12 +24,11 @@ void SoapMessage::ToXml(std::string* xml_string) {
// pugi::xml_node xdecl = xdoc.prepend_child(pugi::node_declaration); // pugi::xml_node xdecl = xdoc.prepend_child(pugi::node_declaration);
// xdecl.append_attribute("version").set_value("1.0"); // xdecl.append_attribute("version").set_value("1.0");
pugi::xml_node xroot = soap_xml::AddChild(soapenv_ns_.name, "Envelope", pugi::xml_node xroot = soap_xml::AddChild(xdoc, soapenv_ns_.name, "Envelope");
&xdoc);
soap_xml::AddNSAttr(xroot, soapenv_ns_.name, soapenv_ns_.url); soap_xml::AddNSAttr(xroot, soapenv_ns_.name, soapenv_ns_.url);
pugi::xml_node xbody = soap_xml::AddChild(soapenv_ns_.name, "Body", &xroot); pugi::xml_node xbody = soap_xml::AddChild(xroot, soapenv_ns_.name, "Body");
ToXmlBody(xbody); ToXmlBody(xbody);

@ -24,11 +24,11 @@ const std::string& SoapRequest::GetParameter(const std::string& key) const {
} }
void SoapRequest::ToXmlBody(pugi::xml_node xbody) { void SoapRequest::ToXmlBody(pugi::xml_node xbody) {
pugi::xml_node xop = soap_xml::AddChild(service_ns_.name, operation_, &xbody); pugi::xml_node xop = soap_xml::AddChild(xbody, service_ns_.name, operation_);
soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url);
for (Parameter& p : parameters_) { for (Parameter& p : parameters_) {
pugi::xml_node xparam = soap_xml::AddChild(service_ns_.name, p.key(), &xop); pugi::xml_node xparam = soap_xml::AddChild(xop, service_ns_.name, p.key());
xparam.text().set(p.value().c_str()); xparam.text().set(p.value().c_str());
} }
} }

@ -6,12 +6,12 @@
namespace webcc { namespace webcc {
void SoapResponse::ToXmlBody(pugi::xml_node xbody) { void SoapResponse::ToXmlBody(pugi::xml_node xbody) {
pugi::xml_node xop = soap_xml::AddChild(service_ns_.name, pugi::xml_node xop = soap_xml::AddChild(xbody, service_ns_.name,
operation_ + "Response", &xbody); operation_ + "Response");
soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url); soap_xml::AddNSAttr(xop, service_ns_.name, service_ns_.url);
pugi::xml_node xresult = soap_xml::AddChild(service_ns_.name, result_name_, pugi::xml_node xresult = soap_xml::AddChild(xop, service_ns_.name,
&xop); result_name_);
xresult.text().set(result_.c_str()); xresult.text().set(result_.c_str());
} }
@ -20,7 +20,7 @@ bool SoapResponse::FromXmlBody(pugi::xml_node xbody) {
pugi::xml_node xresponse = xbody.first_child(); pugi::xml_node xresponse = xbody.first_child();
if (xresponse) { if (xresponse) {
soap_xml::SplitName(xresponse, &service_ns_.name, NULL); soap_xml::SplitName(xresponse, &service_ns_.name, nullptr);
service_ns_.url = soap_xml::GetNSAttr(xresponse, service_ns_.name); service_ns_.url = soap_xml::GetNSAttr(xresponse, service_ns_.name);
pugi::xml_node xresult = soap_xml::GetChildNoNS(xresponse, result_name_); pugi::xml_node xresult = soap_xml::GetChildNoNS(xresponse, result_name_);

@ -10,17 +10,17 @@ void SplitName(const pugi::xml_node& xnode, std::string* prefix,
size_t pos = full_name.find(':'); size_t pos = full_name.find(':');
if (pos != std::string::npos) { if (pos != std::string::npos) {
if (prefix != NULL) { if (prefix != nullptr) {
*prefix = full_name.substr(0, pos); *prefix = full_name.substr(0, pos);
} }
if (name != NULL) { if (name != nullptr) {
*name = full_name.substr(pos + 1); *name = full_name.substr(pos + 1);
} }
} else { } else {
if (prefix != NULL) { if (prefix != nullptr) {
*prefix = ""; *prefix = "";
} }
if (name != NULL) { if (name != nullptr) {
*name = full_name; *name = full_name;
} }
} }
@ -38,9 +38,9 @@ std::string GetNameNoPrefix(const pugi::xml_node& xnode) {
return name; return name;
} }
pugi::xml_node AddChild(const std::string& ns, const std::string& name, pugi::xml_node AddChild(pugi::xml_node xnode,
pugi::xml_node* xnode) { const std::string& ns, const std::string& name) {
return xnode->append_child((ns + ":" + name).c_str()); return xnode.append_child((ns + ":" + name).c_str());
} }
pugi::xml_node GetChild(const pugi::xml_node& xnode, const std::string& ns, pugi::xml_node GetChild(const pugi::xml_node& xnode, const std::string& ns,
@ -70,13 +70,13 @@ pugi::xml_node GetChildNoNS(const pugi::xml_node& xnode,
return pugi::xml_node(); return pugi::xml_node();
} }
void AddAttr(pugi::xml_node& xnode, const std::string& ns, void AddAttr(pugi::xml_node xnode, const std::string& ns,
const std::string& name, const std::string& value) { const std::string& name, const std::string& value) {
std::string ns_name = ns + ":" + name; std::string ns_name = ns + ":" + name;
xnode.append_attribute(ns_name.c_str()) = value.c_str(); xnode.append_attribute(ns_name.c_str()) = value.c_str();
} }
void AddNSAttr(pugi::xml_node& xnode, const std::string& ns_name, void AddNSAttr(pugi::xml_node xnode, const std::string& ns_name,
const std::string& ns_url) { const std::string& ns_url) {
AddAttr(xnode, "xmlns", ns_name, ns_url); AddAttr(xnode, "xmlns", ns_name, ns_url);
} }

@ -13,8 +13,8 @@ namespace soap_xml {
// Split the node name into namespace prefix and real name. // Split the node name into namespace prefix and real name.
// E.g., if the node name is "soapenv:Envelope", it will be splitted to // E.g., if the node name is "soapenv:Envelope", it will be splitted to
// "soapenv" and "Envelope". // "soapenv" and "Envelope".
void SplitName(const pugi::xml_node& xnode, std::string* prefix = NULL, void SplitName(const pugi::xml_node& xnode, std::string* prefix = nullptr,
std::string* name = NULL); std::string* name = nullptr);
// Get the namespace prefix from node name. // Get the namespace prefix from node name.
// E.g., if the node name is "soapenv:Envelope", NS prefix will be "soapenv". // E.g., if the node name is "soapenv:Envelope", NS prefix will be "soapenv".
@ -26,18 +26,17 @@ std::string GetNameNoPrefix(const pugi::xml_node& xnode);
// Add a child with the given name which is prefixed by a namespace. // Add a child with the given name which is prefixed by a namespace.
// E.g., AppendChild(xnode, "soapenv", "Envelope") will append a child with // E.g., AppendChild(xnode, "soapenv", "Envelope") will append a child with
// name "soapenv:Envelope". // name "soapenv:Envelope".
pugi::xml_node AddChild(const std::string& ns, const std::string& name, pugi::xml_node AddChild(pugi::xml_node xnode,
pugi::xml_node* xnode); const std::string& ns, const std::string& name);
pugi::xml_node GetChild(const pugi::xml_node& xnode, const std::string& ns, pugi::xml_node GetChild(const pugi::xml_node& xnode, const std::string& ns,
const std::string& name); const std::string& name);
// TODO: Remove
pugi::xml_node GetChildNoNS(const pugi::xml_node& xnode, pugi::xml_node GetChildNoNS(const pugi::xml_node& xnode,
const std::string& name); const std::string& name);
// Add an attribute with the given name which is prefixed by a namespace. // Add an attribute with the given name which is prefixed by a namespace.
void AddAttr(pugi::xml_node& xnode, const std::string& ns, void AddAttr(pugi::xml_node xnode, const std::string& ns,
const std::string& name, const std::string& value); const std::string& name, const std::string& value);
// Append "xmlns" attribute. // Append "xmlns" attribute.
@ -45,7 +44,7 @@ void AddAttr(pugi::xml_node& xnode, const std::string& ns,
// { "soapenv", "http://schemas.xmlsoap.org/soap/envelope/" } // { "soapenv", "http://schemas.xmlsoap.org/soap/envelope/" }
// the attribute added will be // the attribute added will be
// xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" // xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
void AddNSAttr(pugi::xml_node& xnode, const std::string& ns_name, void AddNSAttr(pugi::xml_node xnode, const std::string& ns_name,
const std::string& ns_url); const std::string& ns_url);
// Get namespace attribute value. // Get namespace attribute value.
@ -67,7 +66,7 @@ class XmlStrRefWriter : public pugi::xml_writer {
result_->clear(); result_->clear();
} }
void write(const void* data, size_t size) override { void write(const void* data, std::size_t size) override {
result_->append(static_cast<const char*>(data), size); result_->append(static_cast<const char*>(data), size);
} }

Loading…
Cancel
Save