summaryrefslogtreecommitdiff
path: root/service/apiImpl.cpp
blob: 01f06c7eeabb071dbff6e766f44b62661df1aaf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "apiImpl.h"
#include "uptr.h"

#include <buffer.h>
#include <compileTimeFormatter.h>
#include <sql/getServices.sql.h>

#include <curl/curl.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.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>())
	{
	}

	SearchServices
	SearchImpl::getServices(const ::Ice::Current &)
	{
		return fetch<SearchServices>(sql::getServices);
	}

	template<typename Fmt, typename... P>
	void
	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)
	{
		return std::bind(&libxmlErrorHandler<Fmt, P...>, std::placeholders::_1, p...);
	}

	template<typename Fmt, typename... P>
	void
	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)
	{
		return std::bind(&curlErrorHandler<Fmt, P...>, std::placeholders::_1, errbuf, p...);
	}

	typedef UPtr<xmlDoc> xmlDocSPtr;
	typedef UPtr<xmlXPathContext> xmlXPathContextSPtr;
	typedef UPtr<xmlXPathObject> xmlXPathObjectSPtr;
	typedef UPtr<xmlParserCtxt> xmlParserCtxtSPtr;

	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)
	{
		return (*(MirrorSearch::CurlWriteCallback *)(userdata))(ptr, size * nmemb);
	}

	AdHocFormatter(Read, "Failed to read in %? (%?) [%?]");
	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};

		auto curl = make_unique(curl_easy_init, curl_easy_cleanup, cEHB<Read>(errbuf, url));
		BOOST_ASSERT(curl);
		CESSO(curl, CURLOPT_URL, url.c_str());
		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);
				htmlCtxtUseOptions(ctx.get(), ss->parserflags);
			}
			else {
				htmlParseChunk(ctx.get(), data, size, 0);
			}
			return size;
		};
		CESSO(curl, CURLOPT_WRITEDATA, &cb);
		if (ss->referrer) {
			CESSO(curl, CURLOPT_REFERER, ss->referrer->c_str());
		}
		if (ss->useragent) {
			CESSO(curl, CURLOPT_USERAGENT, ss->useragent->c_str());
		}
		CESSO(curl, CURLOPT_TIMEOUT, 5L);
		// Enables compression
		CESSO(curl, CURLOPT_ACCEPT_ENCODING, "");
		CESSO(curl, CURLOPT_HTTP_CONTENT_DECODING, 1L);
		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);
		}
		if (!ctx) {
			throw CurlError("Did not retrieve any data.");
		}
		htmlParseChunk(ctx.get(), "", 0, 1);
		if (!ctx->myDoc) {
			throw XmlError("Could not construct a document.");
		}

		UPtr<xmlDoc> doc = {ctx->myDoc, xmlFreeDoc};
		return doc;
	}

	AdHocFormatter(XPathCtx, "Failed to create xpath context in %? (%?)");
	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)
	{
		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
	{
		auto doc = getDoc(s, fn);
		auto xpathCtx = getXPathCxt(doc);
		auto xpathObj = getXPathObj(s->listxpath, xpathCtx, xmlXPathObjectType::XPATH_NODESET);
		if (!xpathObj->nodesetval) {
			throw XmlError("Nodeset is null");
		}
		log->messagebf(LOG::LogLevel::INFO, "%d nodes matched %s", xpathObj->nodesetval->nodeNr, s->listxpath);
		for (int row = 0; row < xpathObj->nodesetval->nodeNr; row += 1) {
			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));
			}
		}
	}

	SearchHits
	SearchImpl::getMatches(const ::std::string fn, const ::Ice::Current & c)
	{
		SearchHits sh;
		for (const auto & s : getServices(c)) {
			callService(fn, s, sh);
		}
		return sh;
	}

	::IceUtil::Optional<::std::string>
	SearchImpl::feelingLucky(const ::std::string fn, const ::Ice::Current & c)
	{
		const auto ms = getMatches(fn, c);
		if (ms.empty())
			return IceUtil::None;
		return ms.front()->url;
	}
}