diff --git a/example/rest_book_client/main.cc b/example/rest_book_client/main.cc index f0e8173..0849646 100644 --- a/example/rest_book_client/main.cc +++ b/example/rest_book_client/main.cc @@ -9,7 +9,7 @@ #include "webcc/http_response.h" #include "webcc/rest_client.h" -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- // Write a JSON object to string. std::string JsonToString(const Json::Value& json) { @@ -17,7 +17,7 @@ std::string JsonToString(const Json::Value& json) { return Json::writeString(builder, json); } -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- class BookListClient { public: @@ -62,7 +62,7 @@ private: webcc::RestClient rest_client_; }; -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- class BookDetailClient { public: @@ -118,7 +118,7 @@ private: webcc::RestClient rest_client_; }; -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- void Help(const char* argv0) { std::cout << "Usage: " << argv0 << " " << std::endl; diff --git a/example/rest_book_server/book_services.cc b/example/rest_book_server/book_services.cc index af7a197..7cc15a5 100644 --- a/example/rest_book_server/book_services.cc +++ b/example/rest_book_server/book_services.cc @@ -2,10 +2,11 @@ #include #include + #include "boost/lexical_cast.hpp" -#include "json/json.h" // jsoncpp +#include "json/json.h" -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- // In-memory test data. // There should be some database in a real product. @@ -97,7 +98,7 @@ private: static BookStore g_book_store; -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- static bool BookFromJson(const std::string& json, Book* book) { Json::Value root; @@ -142,7 +143,7 @@ bool BookListService::Post(const std::string& request_content, return false; } -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- bool BookDetailService::Get(const std::vector& url_sub_matches, const webcc::UrlQuery& query, @@ -182,7 +183,8 @@ bool BookDetailService::Put(const std::vector& url_sub_matches, return false; } -bool BookDetailService::Delete(const std::vector& url_sub_matches) { +bool BookDetailService::Delete( + const std::vector& url_sub_matches) { if (url_sub_matches.size() != 1) { return false; } diff --git a/example/rest_book_server/book_services.h b/example/rest_book_server/book_services.h index f124718..4bdd773 100644 --- a/example/rest_book_server/book_services.h +++ b/example/rest_book_server/book_services.h @@ -3,7 +3,7 @@ #include "webcc/rest_service.h" -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- // BookListService handles the HTTP GET and returns the book list based on // query parameters specified in the URL. @@ -25,7 +25,7 @@ class BookListService : public webcc::RestListService { std::string* response_content) final; }; -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- // The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter // contains the matched book ID. diff --git a/example/soap_calc_client/main.cc b/example/soap_calc_client/main.cc index 799df26..aeda68a 100644 --- a/example/soap_calc_client/main.cc +++ b/example/soap_calc_client/main.cc @@ -3,7 +3,7 @@ #include "calc_client.h" int main() { - LOG_INIT(webcc::INFO, 0); + LOG_INIT(webcc::VERB, 0); CalcClient calc; diff --git a/src/webcc/globals.cc b/src/webcc/globals.cc index 41af1ff..2c64408 100644 --- a/src/webcc/globals.cc +++ b/src/webcc/globals.cc @@ -4,6 +4,8 @@ namespace webcc { +// ----------------------------------------------------------------------------- + // NOTE: // Field names are case-insensitive. // See: https://stackoverflow.com/a/5259004 @@ -27,7 +29,7 @@ const std::string kHttpPatch = "PATCH"; const std::string kHttpPut = "PUT"; const std::string kHttpDelete = "DELETE"; -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- const char* GetErrorMessage(Error error) { switch (error) { @@ -63,7 +65,7 @@ const char* GetErrorMessage(Error error) { } } -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- Parameter::Parameter(const std::string& key, const char* value) : key_(key), value_(value) { diff --git a/src/webcc/http_client.cc b/src/webcc/http_client.cc index 7226da7..c7b4a55 100644 --- a/src/webcc/http_client.cc +++ b/src/webcc/http_client.cc @@ -19,14 +19,10 @@ namespace webcc { -//////////////////////////////////////////////////////////////////////////////// - static const int kConnectMaxSeconds = 10; static const int kSendMaxSeconds = 10; static const int kReceiveMaxSeconds = 30; -//////////////////////////////////////////////////////////////////////////////// - HttpClient::HttpClient() : socket_(io_context_), timeout_seconds_(kReceiveMaxSeconds), @@ -38,7 +34,7 @@ HttpClient::HttpClient() } Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) { - assert(response != NULL); + assert(response != nullptr); Error error = kNoError; diff --git a/src/webcc/http_response.cc b/src/webcc/http_response.cc index 68cdba9..2858c17 100644 --- a/src/webcc/http_response.cc +++ b/src/webcc/http_response.cc @@ -81,8 +81,6 @@ const char CRLF[] = { '\r', '\n' }; } // misc_strings -//////////////////////////////////////////////////////////////////////////////// - // ATTENTION: The buffers don't hold the memory! std::vector HttpResponse::ToBuffers() const { std::vector buffers; diff --git a/src/webcc/http_server.cc b/src/webcc/http_server.cc index a4b7dfd..2ae9e59 100644 --- a/src/webcc/http_server.cc +++ b/src/webcc/http_server.cc @@ -38,7 +38,7 @@ HttpServer::HttpServer(unsigned short port, std::size_t workers) } void HttpServer::Run() { - assert(GetRequestHandler() != NULL); + assert(GetRequestHandler() != nullptr); LOG_INFO("Server is going to run..."); diff --git a/src/webcc/logger.cc b/src/webcc/logger.cc index d4be330..9ef5323 100644 --- a/src/webcc/logger.cc +++ b/src/webcc/logger.cc @@ -11,8 +11,6 @@ namespace webcc { -//////////////////////////////////////////////////////////////////////////////// - static std::string GetThreadId() { boost::thread::id thread_id = boost::this_thread::get_id(); std::stringstream ss; @@ -20,8 +18,6 @@ static std::string GetThreadId() { return ss.str(); } -//////////////////////////////////////////////////////////////////////////////// - static const char* kLevelNames[] = { "VERB", "INFO", "WARN", "ERRO", "FATA" }; @@ -35,8 +31,6 @@ struct Logger { // Global logger. static Logger g_logger{ VERB }; -//////////////////////////////////////////////////////////////////////////////// - void LogInit(int level, int modes) { g_logger.modes = modes; g_logger.level = level; diff --git a/src/webcc/rest_service.cc b/src/webcc/rest_service.cc index 4817443..1dca6ad 100644 --- a/src/webcc/rest_service.cc +++ b/src/webcc/rest_service.cc @@ -4,6 +4,8 @@ namespace webcc { +// ----------------------------------------------------------------------------- + bool RestListService::Handle(const std::string& http_method, const std::vector& url_sub_matches, const UrlQuery& query, @@ -22,7 +24,7 @@ bool RestListService::Handle(const std::string& http_method, return false; } -//////////////////////////////////////////////////////////////////////////////// +// ----------------------------------------------------------------------------- bool RestDetailService::Handle(const std::string& http_method, const std::vector& url_sub_matches, diff --git a/src/webcc/rest_service_manager.cc b/src/webcc/rest_service_manager.cc index 626b2c2..af5bdf5 100644 --- a/src/webcc/rest_service_manager.cc +++ b/src/webcc/rest_service_manager.cc @@ -32,10 +32,8 @@ bool RestServiceManager::AddService(RestServicePtr service, } RestServicePtr RestServiceManager::GetService( - const std::string& url, - std::vector* sub_matches) { - - assert(sub_matches != NULL); + const std::string& url, std::vector* sub_matches) { + assert(sub_matches != nullptr); for (ServiceItem& item : service_items_) { if (item.is_regex) { diff --git a/src/webcc/soap_message.cc b/src/webcc/soap_message.cc index 1d64653..54a194e 100644 --- a/src/webcc/soap_message.cc +++ b/src/webcc/soap_message.cc @@ -24,12 +24,11 @@ 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 = soap_xml::AddChild(soapenv_ns_.name, "Envelope", - &xdoc); + pugi::xml_node xroot = soap_xml::AddChild(xdoc, soapenv_ns_.name, "Envelope"); 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); diff --git a/src/webcc/soap_request.cc b/src/webcc/soap_request.cc index 0aa339b..ea7332b 100644 --- a/src/webcc/soap_request.cc +++ b/src/webcc/soap_request.cc @@ -24,11 +24,11 @@ const std::string& SoapRequest::GetParameter(const std::string& key) const { } 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); 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()); } } diff --git a/src/webcc/soap_response.cc b/src/webcc/soap_response.cc index 383a1ef..0a2cec4 100644 --- a/src/webcc/soap_response.cc +++ b/src/webcc/soap_response.cc @@ -6,12 +6,12 @@ namespace webcc { void SoapResponse::ToXmlBody(pugi::xml_node xbody) { - pugi::xml_node xop = soap_xml::AddChild(service_ns_.name, - operation_ + "Response", &xbody); + 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(service_ns_.name, result_name_, - &xop); + pugi::xml_node xresult = soap_xml::AddChild(xop, service_ns_.name, + result_name_); xresult.text().set(result_.c_str()); } @@ -20,7 +20,7 @@ bool SoapResponse::FromXmlBody(pugi::xml_node xbody) { pugi::xml_node xresponse = xbody.first_child(); 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); pugi::xml_node xresult = soap_xml::GetChildNoNS(xresponse, result_name_); diff --git a/src/webcc/soap_xml.cc b/src/webcc/soap_xml.cc index 98212ef..5ac7f9b 100644 --- a/src/webcc/soap_xml.cc +++ b/src/webcc/soap_xml.cc @@ -10,17 +10,17 @@ void SplitName(const pugi::xml_node& xnode, std::string* prefix, size_t pos = full_name.find(':'); if (pos != std::string::npos) { - if (prefix != NULL) { + if (prefix != nullptr) { *prefix = full_name.substr(0, pos); } - if (name != NULL) { + if (name != nullptr) { *name = full_name.substr(pos + 1); } } else { - if (prefix != NULL) { + if (prefix != nullptr) { *prefix = ""; } - if (name != NULL) { + if (name != nullptr) { *name = full_name; } } @@ -38,9 +38,9 @@ std::string GetNameNoPrefix(const pugi::xml_node& xnode) { return name; } -pugi::xml_node AddChild(const std::string& ns, const std::string& name, - pugi::xml_node* xnode) { - return xnode->append_child((ns + ":" + name).c_str()); +pugi::xml_node AddChild(pugi::xml_node xnode, + const std::string& ns, const std::string& name) { + return xnode.append_child((ns + ":" + name).c_str()); } 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(); } -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) { std::string ns_name = ns + ":" + name; 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) { AddAttr(xnode, "xmlns", ns_name, ns_url); } diff --git a/src/webcc/soap_xml.h b/src/webcc/soap_xml.h index 8a143fb..4f6b938 100644 --- a/src/webcc/soap_xml.h +++ b/src/webcc/soap_xml.h @@ -13,8 +13,8 @@ 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 splitted to // "soapenv" and "Envelope". -void SplitName(const pugi::xml_node& xnode, std::string* prefix = NULL, - std::string* name = NULL); +void SplitName(const pugi::xml_node& xnode, std::string* prefix = nullptr, + std::string* name = nullptr); // Get the namespace prefix from node name. // 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. // E.g., AppendChild(xnode, "soapenv", "Envelope") will append a child with // name "soapenv:Envelope". -pugi::xml_node AddChild(const std::string& ns, const std::string& name, - pugi::xml_node* xnode); +pugi::xml_node AddChild(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, const std::string& name); -// TODO: Remove pugi::xml_node GetChildNoNS(const pugi::xml_node& xnode, const std::string& name); // 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); // Append "xmlns" attribute. @@ -45,7 +44,7 @@ void AddAttr(pugi::xml_node& xnode, const std::string& ns, // { "soapenv", "http://schemas.xmlsoap.org/soap/envelope/" } // the attribute added will be // 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); // Get namespace attribute value. @@ -67,7 +66,7 @@ class XmlStrRefWriter : public pugi::xml_writer { 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(data), size); }