diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-09 13:58:38 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-09 13:58:38 +0000 |
commit | 62d506c24aaf543f84c82e2850a05b1387e4990d (patch) | |
tree | 3d356dcef6cb64baf648119c6b61aa9afd4e450b | |
parent | Exclude api.h from tidy checks (diff) | |
download | mirrorsearch-62d506c24aaf543f84c82e2850a05b1387e4990d.tar.bz2 mirrorsearch-62d506c24aaf543f84c82e2850a05b1387e4990d.tar.xz mirrorsearch-62d506c24aaf543f84c82e2850a05b1387e4990d.zip |
Clang format
-rw-r--r-- | service/apiImpl.cpp | 70 | ||||
-rw-r--r-- | service/apiImpl.h | 20 | ||||
-rw-r--r-- | service/ex.cpp | 5 | ||||
-rw-r--r-- | service/main.cpp | 39 | ||||
-rw-r--r-- | service/test.cpp | 27 | ||||
-rw-r--r-- | service/uptr.cpp | 6 | ||||
-rw-r--r-- | service/uptr.h | 21 | ||||
-rw-r--r-- | site/custom.cpp | 39 | ||||
-rw-r--r-- | site/test.cpp | 99 |
9 files changed, 162 insertions, 164 deletions
diff --git a/service/apiImpl.cpp b/service/apiImpl.cpp index 5a1e7a6..01f06c7 100644 --- a/service/apiImpl.cpp +++ b/service/apiImpl.cpp @@ -1,51 +1,53 @@ #include "apiImpl.h" #include "uptr.h" -#include <sql/getServices.sql.h> #include <buffer.h> #include <compileTimeFormatter.h> +#include <sql/getServices.sql.h> -#include <libxml/xpath.h> -#include <libxml/xpathInternals.h> +#include <curl/curl.h> #include <libxml/HTMLparser.h> #include <libxml/HTMLtree.h> -#include <curl/curl.h> +#include <libxml/xpath.h> +#include <libxml/xpathInternals.h> #define CESSO(curl, opt, expr) \ BOOST_VERIFY_MSG(CURLE_OK == curl_easy_setopt(curl.get(), opt, expr), "Failed setting option " #opt); namespace MirrorSearch { SearchImpl::SearchImpl(const DB::ConnectionPoolPtr & db) : - IceTray::AbstractDatabaseClient(db), - log(LOGMANAGER()->getLogger<SearchImpl>()) + IceTray::AbstractDatabaseClient(db), log(LOGMANAGER()->getLogger<SearchImpl>()) { } - SearchServices SearchImpl::getServices(const ::Ice::Current&) + SearchServices + SearchImpl::getServices(const ::Ice::Current &) { return fetch<SearchServices>(sql::getServices); } - template<typename Fmt, typename ... P> + template<typename Fmt, typename... P> void - libxmlErrorHandler(const std::string & fn, const P & ... p) + libxmlErrorHandler(const std::string & fn, const P &... p) { throw XmlError(Fmt::get(fn, xmlGetLastError()->message, p...)); } - template<typename Fmt, typename ... P> - auto lEHB(const P & ... p) + template<typename Fmt, typename... P> + auto + lEHB(const P &... p) { return std::bind(&libxmlErrorHandler<Fmt, P...>, std::placeholders::_1, p...); } - template<typename Fmt, typename ... P> + template<typename Fmt, typename... P> void - curlErrorHandler(const std::string & fn, const char * errbuf, const P & ... p) + curlErrorHandler(const std::string & fn, const char * errbuf, const P &... p) { throw CurlError(Fmt::get(fn, errbuf, p...)); } - template<typename Fmt, typename ... P> - auto cEHB(const char * errbuf, const P & ... p) + template<typename Fmt, typename... P> + auto + cEHB(const char * errbuf, const P &... p) { return std::bind(&curlErrorHandler<Fmt, P...>, std::placeholders::_1, errbuf, p...); } @@ -57,18 +59,21 @@ namespace MirrorSearch { typedef std::function<size_t(const char *, size_t)> CurlWriteCallback; - static size_t write_callback(char * ptr, size_t size, size_t nmemb, void * userdata) + static size_t + write_callback(char * ptr, size_t size, size_t nmemb, void * userdata) { return (*(MirrorSearch::CurlWriteCallback *)(userdata))(ptr, size * nmemb); } AdHocFormatter(Read, "Failed to read in %? (%?) [%?]"); - UPtr<xmlDoc> getDoc(const SearchServicePtr & ss, const std::string & fn) { + UPtr<xmlDoc> + getDoc(const SearchServicePtr & ss, const std::string & fn) + { auto fmt = AdHoc::Buffer::getFormat(ss->baseurl); auto url = (fmt % fn).str(); char errbuf[CURL_ERROR_SIZE] = ""; - xmlParserCtxtSPtr ctx { nullptr, nullptr }; + xmlParserCtxtSPtr ctx {nullptr, nullptr}; auto curl = make_unique(curl_easy_init, curl_easy_cleanup, cEHB<Read>(errbuf, url)); BOOST_ASSERT(curl); @@ -76,8 +81,8 @@ namespace MirrorSearch { CESSO(curl, CURLOPT_WRITEFUNCTION, write_callback); CurlWriteCallback cb = [&ctx, &url, &ss](auto data, auto size) { if (!ctx) { - ctx = make_unique(htmlCreatePushParserCtxt, htmlFreeParserCtxt, lEHB<Read>(url), - (xmlSAXHandlerPtr)NULL, (void*)NULL, data, size, url.c_str(), XML_CHAR_ENCODING_NONE); + ctx = make_unique(htmlCreatePushParserCtxt, htmlFreeParserCtxt, lEHB<Read>(url), (xmlSAXHandlerPtr)NULL, + (void *)NULL, data, size, url.c_str(), XML_CHAR_ENCODING_NONE); htmlCtxtUseOptions(ctx.get(), ss->parserflags); } else { @@ -99,7 +104,7 @@ namespace MirrorSearch { CESSO(curl, CURLOPT_TCP_FASTOPEN, 1L); CESSO(curl, CURLOPT_FAILONERROR, 1L); if (curl_easy_perform(curl.get()) != CURLE_OK) { - curlErrorHandler<Read>(failingFunction((void*)&curl_easy_perform), errbuf, url); + curlErrorHandler<Read>(failingFunction((void *)&curl_easy_perform), errbuf, url); } if (!ctx) { throw CurlError("Did not retrieve any data."); @@ -109,27 +114,31 @@ namespace MirrorSearch { throw XmlError("Could not construct a document."); } - UPtr<xmlDoc> doc = { ctx->myDoc, xmlFreeDoc }; + UPtr<xmlDoc> doc = {ctx->myDoc, xmlFreeDoc}; return doc; } AdHocFormatter(XPathCtx, "Failed to create xpath context in %? (%?)"); - static auto getXPathCxt(const xmlDocSPtr & doc) + static auto + getXPathCxt(const xmlDocSPtr & doc) { return make_unique(xmlXPathNewContext, xmlXPathFreeContext, lEHB<XPathCtx>(), doc.get()); } AdHocFormatter(XPathEval, "Failed to evaluate xpath in %? (%?) [%?]"); - static auto getXPathObj(const ::std::string & xpath, const xmlXPathContextSPtr & ctx, xmlXPathObjectType type) + static auto + getXPathObj(const ::std::string & xpath, const xmlXPathContextSPtr & ctx, xmlXPathObjectType type) { - auto xpathObj = make_unique(xmlXPathEvalExpression, xmlXPathFreeObject, lEHB<XPathEval>(xpath), BAD_CAST xpath.c_str(), ctx.get()); + auto xpathObj = make_unique( + xmlXPathEvalExpression, xmlXPathFreeObject, lEHB<XPathEval>(xpath), BAD_CAST xpath.c_str(), ctx.get()); if (xpathObj->type != type) { throw XmlError("Xpath evaluates to wrong type " + xpath); } return xpathObj; } - void SearchImpl::callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const + void + SearchImpl::callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const { auto doc = getDoc(s, fn); auto xpathCtx = getXPathCxt(doc); @@ -142,12 +151,13 @@ namespace MirrorSearch { xpathCtx->node = xpathObj->nodesetval->nodeTab[row]; auto xpathObjI = getXPathObj(s->urlxpath, xpathCtx, xmlXPathObjectType::XPATH_STRING); if (xpathObjI->stringval && *xpathObjI->stringval) { - sh.push_back(std::make_shared<SearchHit>(0, s->id, (const char *) xpathObjI->stringval)); + sh.push_back(std::make_shared<SearchHit>(0, s->id, (const char *)xpathObjI->stringval)); } } } - SearchHits SearchImpl::getMatches(const ::std::string fn, const ::Ice::Current & c) + SearchHits + SearchImpl::getMatches(const ::std::string fn, const ::Ice::Current & c) { SearchHits sh; for (const auto & s : getServices(c)) { @@ -156,7 +166,8 @@ namespace MirrorSearch { return sh; } - ::IceUtil::Optional<::std::string> SearchImpl::feelingLucky(const ::std::string fn, const ::Ice::Current & c) + ::IceUtil::Optional<::std::string> + SearchImpl::feelingLucky(const ::std::string fn, const ::Ice::Current & c) { const auto ms = getMatches(fn, c); if (ms.empty()) @@ -164,4 +175,3 @@ namespace MirrorSearch { return ms.front()->url; } } - diff --git a/service/apiImpl.h b/service/apiImpl.h index 0ee7972..93aaf9e 100644 --- a/service/apiImpl.h +++ b/service/apiImpl.h @@ -1,25 +1,25 @@ #ifndef MIRRORSEARCH_APIIMPL_H #define MIRRORSEARCH_APIIMPL_H -#include <api.h> #include <abstractDatabaseClient.h> +#include <api.h> #include <logger.h> namespace MirrorSearch { class SearchImpl : public Search, public IceTray::AbstractDatabaseClient { - public: - SearchImpl(const DB::ConnectionPoolPtr &); + public: + SearchImpl(const DB::ConnectionPoolPtr &); - virtual SearchServices getServices(const ::Ice::Current& = ::Ice::Current()) override; - virtual SearchHits getMatches(const ::std::string, const ::Ice::Current& = ::Ice::Current()) override; - virtual ::IceUtil::Optional<::std::string> feelingLucky(const ::std::string, const ::Ice::Current& = ::Ice::Current()) override; + virtual SearchServices getServices(const ::Ice::Current & = ::Ice::Current()) override; + virtual SearchHits getMatches(const ::std::string, const ::Ice::Current & = ::Ice::Current()) override; + virtual ::IceUtil::Optional<::std::string> feelingLucky( + const ::std::string, const ::Ice::Current & = ::Ice::Current()) override; - private: - void callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const; + private: + void callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const; - LOG::LoggerPtr log; + LOG::LoggerPtr log; }; } #endif - diff --git a/service/ex.cpp b/service/ex.cpp index f0e8e2e..1a207cd 100644 --- a/service/ex.cpp +++ b/service/ex.cpp @@ -2,15 +2,14 @@ namespace MirrorSearch { void - XmlError::ice_print(std::ostream&s) const + XmlError::ice_print(std::ostream & s) const { s << msg; } void - CurlError::ice_print(std::ostream&s) const + CurlError::ice_print(std::ostream & s) const { s << msg; } } - diff --git a/service/main.cpp b/service/main.cpp index c84b11b..b008e08 100644 --- a/service/main.cpp +++ b/service/main.cpp @@ -1,30 +1,31 @@ +#include "apiImpl.h" #include <Ice/Initialize.h> #include <Ice/ObjectAdapter.h> -#include <icetrayService.h> -#include "apiImpl.h" #include <curl/curl.h> +#include <icetrayService.h> #include <libxml/parser.h> namespace MirrorSearch { class Api : public IceTray::Service { - public: - Api() - { - xmlInitParser(); - curl_global_init(0); - } - ~Api() - { - xmlCleanupParser(); - curl_global_cleanup(); - } - void addObjects(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &, const Ice::ObjectAdapterPtr & adp) override - { - auto dbpool = getConnectionPool(ic, "postgresql", "MirrorSearch"); - adp->add(std::make_shared<SearchImpl>(dbpool), Ice::stringToIdentity("Search")); - } + public: + Api() + { + xmlInitParser(); + curl_global_init(0); + } + ~Api() + { + xmlCleanupParser(); + curl_global_cleanup(); + } + void + addObjects(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &, + const Ice::ObjectAdapterPtr & adp) override + { + auto dbpool = getConnectionPool(ic, "postgresql", "MirrorSearch"); + adp->add(std::make_shared<SearchImpl>(dbpool), Ice::stringToIdentity("Search")); + } }; NAMEDFACTORY("default", MirrorSearch::Api, IceTray::ServiceFactory); } - diff --git a/service/test.cpp b/service/test.cpp index 6d064fc..dabf8b7 100644 --- a/service/test.cpp +++ b/service/test.cpp @@ -1,28 +1,24 @@ #define BOOST_TEST_MODULE MirrorSearch #include <boost/test/unit_test.hpp> -#include <pq-mock.h> -#include <dryice.h> -#include <definedDirs.h> #include <api.h> +#include <definedDirs.h> +#include <dryice.h> +#include <pq-mock.h> class Service : DB::PluginMock<PQ::Mock>, public IceTray::DryIce { - public: - Service() : DB::PluginMock<PQ::Mock>("MirrorSearch", { - rootDir / "schema.sql", - rootDir / "data.sql" - }, "user=postgres") { } - +public: + Service() : + DB::PluginMock<PQ::Mock>("MirrorSearch", {rootDir / "schema.sql", rootDir / "data.sql"}, "user=postgres") + { + } }; class TestClient : public IceTray::DryIceClient { - public: - TestClient() : - s(getProxy<MirrorSearch::SearchPrx>("Search")) - { - } +public: + TestClient() : s(getProxy<MirrorSearch::SearchPrx>("Search")) { } - MirrorSearch::SearchPrxPtr s; + MirrorSearch::SearchPrxPtr s; }; BOOST_TEST_GLOBAL_FIXTURE(Service); @@ -90,4 +86,3 @@ BOOST_AUTO_TEST_CASE(getMatches_xtrans_lucky) } BOOST_AUTO_TEST_SUITE_END(); - diff --git a/service/uptr.cpp b/service/uptr.cpp index 1669a69..de38d0b 100644 --- a/service/uptr.cpp +++ b/service/uptr.cpp @@ -1,9 +1,9 @@ #include "uptr.h" -#include <execinfo.h> #include <boost/assert.hpp> -#include <string> -#include <stdlib.h> #include <exception> +#include <execinfo.h> +#include <stdlib.h> +#include <string> namespace MirrorSearch { diff --git a/service/uptr.h b/service/uptr.h index 17eb28d..e73e098 100644 --- a/service/uptr.h +++ b/service/uptr.h @@ -1,24 +1,22 @@ #ifndef MIRRORSEARCH_UPTR_H #define MIRRORSEARCH_UPTR_H -#include <memory> #include <functional> +#include <memory> #include <stdexcept> namespace MirrorSearch { typedef std::function<void(const std::string &)> OnError; - std::string - failingFunction(void * const func); + std::string failingFunction(void * const func); - void - defaultErrorHandler(const std::string &); + void defaultErrorHandler(const std::string &); - template<typename O> using UPtr = std::unique_ptr<O, void(*)(O*)>; + template<typename O> using UPtr = std::unique_ptr<O, void (*)(O *)>; - template<typename R, typename ... P, typename ... A> + template<typename R, typename... P, typename... A> R * - make_unique(R * (*func)(P...), OnError onError, A ... p) + make_unique(R * (*func)(P...), OnError onError, A... p) { if (auto obj = func(p...)) { return obj; @@ -27,13 +25,12 @@ namespace MirrorSearch { throw std::runtime_error("Error handler did not throw"); } - template<typename R, typename ... P, typename ... A> + template<typename R, typename... P, typename... A> UPtr<R> - make_unique(R*(*get)(P...), void(*release)(R*), OnError onError, A ... p) + make_unique(R * (*get)(P...), void (*release)(R *), OnError onError, A... p) { - return std::unique_ptr<R, void(*)(R*)>(make_unique(get, onError, p...), release); + return std::unique_ptr<R, void (*)(R *)>(make_unique(get, onError, p...), release); } } #endif - diff --git a/site/custom.cpp b/site/custom.cpp index 5d955a2..00dcb6a 100644 --- a/site/custom.cpp +++ b/site/custom.cpp @@ -7,27 +7,27 @@ namespace MirrorSearch { // Route name: download // path: /download/{filename} class download : public IceSpider::IRouteHandler { - public: - download(const IceSpider::Core * core) : - IceSpider::IRouteHandler(IceSpider::HttpMethod::GET, "/download/{filename}"), - prx0(core->getProxy<MirrorSearch::Search>()), - _pi_filename(1) - { + public: + download(const IceSpider::Core * core) : + IceSpider::IRouteHandler(IceSpider::HttpMethod::GET, "/download/{filename}"), + prx0(core->getProxy<MirrorSearch::Search>()), _pi_filename(1) + { + } + + void + execute(IceSpider::IHttpRequest * request) const + { + auto _p_filename(request->getURLParam<::std::string>(_pi_filename)); + auto _responseModel = prx0->feelingLucky(_p_filename, request->getContext()); + if (_responseModel) { + request->responseRedirect(*_responseModel, "Mirror found"s); } + request->response(404, "No mirror found"); + } - void execute(IceSpider::IHttpRequest * request) const - { - auto _p_filename(request->getURLParam<::std::string>(_pi_filename)); - auto _responseModel = prx0->feelingLucky(_p_filename, request->getContext()); - if (_responseModel) { - request->responseRedirect(*_responseModel, "Mirror found"s); - } - request->response(404, "No mirror found"); - } - - private: - const MirrorSearch::SearchPrxPtr prx0; - const unsigned int _pi_filename; + private: + const MirrorSearch::SearchPrxPtr prx0; + const unsigned int _pi_filename; }; } // namespace MirrorSearch @@ -36,4 +36,3 @@ namespace MirrorSearch { FACTORY(MirrorSearch::download, IceSpider::RouteHandlerFactory); // End generated code. - diff --git a/site/test.cpp b/site/test.cpp index 4cd9763..bcff64b 100644 --- a/site/test.cpp +++ b/site/test.cpp @@ -1,68 +1,66 @@ #define BOOST_TEST_MODULE mirrorsearchsite #include <boost/test/unit_test.hpp> -#include <testRequest.h> -#include <core.h> #include <api.h> +#include <core.h> +#include <testRequest.h> -#include <Ice/ObjectAdapter.h> #include <Ice/Initialize.h> +#include <Ice/ObjectAdapter.h> using namespace IceSpider; using namespace MirrorSearch; class TestSerice : public Search { - public: - virtual SearchServices getServices(const ::Ice::Current& = ::Ice::Current()) override - { - return {}; +public: + virtual SearchServices + getServices(const ::Ice::Current & = ::Ice::Current()) override + { + return {}; + } + virtual SearchHits + getMatches(const ::std::string fn, const ::Ice::Current & = ::Ice::Current()) override + { + BOOST_TEST_INFO(fn); + if (fn == "good.txt") { + return {std::make_shared<SearchHit>(1, 1, "file:///some/file/path"), + std::make_shared<SearchHit>(1, 1, "file:///some/other/path")}; } - virtual SearchHits getMatches(const ::std::string fn, const ::Ice::Current& = ::Ice::Current()) override - { - BOOST_TEST_INFO(fn); - if (fn == "good.txt") { - return { - std::make_shared<SearchHit>(1, 1, "file:///some/file/path"), - std::make_shared<SearchHit>(1, 1, "file:///some/other/path") - }; - } - return {}; - } - virtual ::Ice::optional<::std::string> feelingLucky(const ::std::string fn, const ::Ice::Current & c = ::Ice::Current()) override - { - auto ms = getMatches(fn, c); - if (ms.empty()) { - return IceUtil::None; - - } - return ms.front()->url; + return {}; + } + virtual ::Ice::optional<::std::string> + feelingLucky(const ::std::string fn, const ::Ice::Current & c = ::Ice::Current()) override + { + auto ms = getMatches(fn, c); + if (ms.empty()) { + return IceUtil::None; } + return ms.front()->url; + } }; class TestApp : public CoreWithDefaultRouter { - public: - TestApp() : - CoreWithDefaultRouter({ - "--MirrorSearch.Search=Search" - }), - adp(communicator->createObjectAdapterWithEndpoints("test", "default")) - { - adp->activate(); - adp->add(std::make_shared<TestSerice>(), Ice::stringToIdentity("Search")); - } - - ~TestApp() - { - adp->deactivate(); - adp->destroy(); - } - - Ice::ObjectAdapterPtr adp; +public: + TestApp() : + CoreWithDefaultRouter({"--MirrorSearch.Search=Search"}), + adp(communicator->createObjectAdapterWithEndpoints("test", "default")) + { + adp->activate(); + adp->add(std::make_shared<TestSerice>(), Ice::stringToIdentity("Search")); + } + + ~TestApp() + { + adp->deactivate(); + adp->destroy(); + } + + Ice::ObjectAdapterPtr adp; }; BOOST_FIXTURE_TEST_SUITE(ta, TestApp); -BOOST_AUTO_TEST_CASE( testCallIndexNoFile ) +BOOST_AUTO_TEST_CASE(testCallIndexNoFile) { TestRequest requestGetIndex(this, HttpMethod::GET, "/list"); process(&requestGetIndex); @@ -70,7 +68,7 @@ BOOST_AUTO_TEST_CASE( testCallIndexNoFile ) BOOST_CHECK_EQUAL(h["Status"], "404 Not found"); } -BOOST_AUTO_TEST_CASE( testCallServiceList ) +BOOST_AUTO_TEST_CASE(testCallServiceList) { TestRequest requestGetIndex(this, HttpMethod::GET, "/services"); process(&requestGetIndex); @@ -79,7 +77,7 @@ BOOST_AUTO_TEST_CASE( testCallServiceList ) BOOST_CHECK_EQUAL(h["Content-Type"], "application/json"); } -BOOST_AUTO_TEST_CASE( testCallIndex ) +BOOST_AUTO_TEST_CASE(testCallIndex) { TestRequest requestGetIndex(this, HttpMethod::GET, "/list/somefile.txt"); process(&requestGetIndex); @@ -88,7 +86,7 @@ BOOST_AUTO_TEST_CASE( testCallIndex ) BOOST_CHECK_EQUAL(h["Content-Type"], "application/json"); } -BOOST_AUTO_TEST_CASE( testCallDownloadNotFile ) +BOOST_AUTO_TEST_CASE(testCallDownloadNotFile) { TestRequest requestGetIndex(this, HttpMethod::GET, "/download"); process(&requestGetIndex); @@ -96,7 +94,7 @@ BOOST_AUTO_TEST_CASE( testCallDownloadNotFile ) BOOST_CHECK_EQUAL(h["Status"], "404 Not found"); } -BOOST_AUTO_TEST_CASE( testCallDownloadBad ) +BOOST_AUTO_TEST_CASE(testCallDownloadBad) { TestRequest requestGetIndex(this, HttpMethod::GET, "/download/bad.txt"); process(&requestGetIndex); @@ -104,7 +102,7 @@ BOOST_AUTO_TEST_CASE( testCallDownloadBad ) BOOST_CHECK_EQUAL(h["Status"], "404 No mirror found"); } -BOOST_AUTO_TEST_CASE( testCallDownloadGood ) +BOOST_AUTO_TEST_CASE(testCallDownloadGood) { TestRequest requestGetIndex(this, HttpMethod::GET, "/download/good.txt"); process(&requestGetIndex); @@ -114,4 +112,3 @@ BOOST_AUTO_TEST_CASE( testCallDownloadGood ) } BOOST_AUTO_TEST_SUITE_END(); - |