From fbce6af22f074a0103414e57f5af4efa10b97e72 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Oct 2015 00:20:17 +0100 Subject: Refactor to support using VariableCurlHelper to inject its properties into an pre-existing CurlHandle --- project2/url/curlHelper.cpp | 19 ++++++++++++------- project2/url/curlHelper.h | 11 ++++++----- project2/xml/rawView.cpp | 5 ++--- project2/xml/xmlDocumentCache.cpp | 12 +++++++++--- project2/xml/xmlDocumentCache.h | 6 ++++-- project2/xml/xmlDocumentPrefetch.cpp | 8 +------- project2/xml/xmlDocumentPrefetch.h | 3 +-- project2/xml/xpathRows.cpp | 8 +------- project2/xml/xpathRows.h | 3 +-- 9 files changed, 37 insertions(+), 38 deletions(-) diff --git a/project2/url/curlHelper.cpp b/project2/url/curlHelper.cpp index 3e62c6e..a4ca4d7 100644 --- a/project2/url/curlHelper.cpp +++ b/project2/url/curlHelper.cpp @@ -47,23 +47,28 @@ CurlPtr CurlHelper::newCurl(ExecContext * ec) const { CurlPtr c = new Curl(getUrl(ec)); + setCurlOpts(c.get(), ec); + return c; +} + +void +CurlHelper::setCurlOpts(AdHoc::Net::CurlHandle * c, ExecContext *) const +{ c->setopt(CURLOPT_FOLLOWLOCATION, 1); c->setopt(CURLOPT_ENCODING, "deflate, gzip"); c->setopt(CURLOPT_FAILONERROR, 1); - return c; } -CurlPtr -VariableCurlHelper::newCurl(ExecContext * ec) const +void +VariableCurlHelper::setCurlOpts(AdHoc::Net::CurlHandle * c, ExecContext * ec) const { - CurlPtr c = CurlHelper::newCurl(ec); + CurlHelper::setCurlOpts(c, ec); setopt_s(c, CURLOPT_USERAGENT, userAgent(ec)); setopt_s(c, CURLOPT_PROXY, proxy(ec)); setopt_s(c, CURLOPT_REFERER, referer(ec)); setopt_s(c, CURLOPT_COOKIEFILE, cookieJar(ec)); setopt_s(c, CURLOPT_COOKIEJAR, cookieJar(ec)); setopt_l(c, CURLOPT_TIMEOUT_MS, timeout(ec)); - return c; } void @@ -95,13 +100,13 @@ Curl::setSendHandler(const SendHandler & h) } void -VariableCurlHelper::setopt_s(CurlPtr c, CURLoption o, const char * v) +VariableCurlHelper::setopt_s(AdHoc::Net::CurlHandle * c, CURLoption o, const char * v) { c->setopt(o, v); } void -VariableCurlHelper::setopt_l(CurlPtr c, CURLoption o, int64_t v) +VariableCurlHelper::setopt_l(AdHoc::Net::CurlHandle * c, CURLoption o, int64_t v) { c->setopt(o, (long)v); } diff --git a/project2/url/curlHelper.h b/project2/url/curlHelper.h index d45e415..95b24a6 100644 --- a/project2/url/curlHelper.h +++ b/project2/url/curlHelper.h @@ -29,7 +29,8 @@ class CurlHelper { CurlHelper(); virtual ~CurlHelper(); - virtual CurlPtr newCurl(ExecContext *) const; + CurlPtr newCurl(ExecContext *) const; + virtual void setCurlOpts(AdHoc::Net::CurlHandle *, ExecContext *) const; protected: virtual std::string getUrl(ExecContext *) const = 0; @@ -44,12 +45,12 @@ class VariableCurlHelper : public CurlHelper { const Variable url; protected: - virtual CurlPtr newCurl(ExecContext *) const; - virtual std::string getUrl(ExecContext *) const; + virtual void setCurlOpts(AdHoc::Net::CurlHandle *, ExecContext *) const override; + virtual std::string getUrl(ExecContext *) const override; private: - static void setopt_s(CurlPtr, CURLoption, const char *); - static void setopt_l(CurlPtr, CURLoption, int64_t); + static void setopt_s(AdHoc::Net::CurlHandle *, CURLoption, const char *); + static void setopt_l(AdHoc::Net::CurlHandle *, CURLoption, int64_t); const Variable userAgent; const Variable cookieJar; diff --git a/project2/xml/rawView.cpp b/project2/xml/rawView.cpp index c67a677..e5f7c50 100644 --- a/project2/xml/rawView.cpp +++ b/project2/xml/rawView.cpp @@ -70,12 +70,12 @@ class RawView : public RawViewBase { }; NAMEDFACTORY("rawview", RawView, ViewFactory); -class XmlResourceView : public RawViewBase, XmlDocumentCache, VariableCurlHelper { +class XmlResourceView : public RawViewBase, XmlDocumentCache { public: XmlResourceView(ScriptNodePtr p) : SourceObject(p), RawViewBase(p), - VariableCurlHelper(p), + XmlDocumentCache(p), encoding(p, "encoding", Null()) { } @@ -86,7 +86,6 @@ class XmlResourceView : public RawViewBase, XmlDocumentCache, VariableCurlHelper } bool asHtml(ExecContext *) const { return false; } bool withWarnings(ExecContext *) const { return true; } - CurlPtr newCurl(ExecContext * ec) const { return VariableCurlHelper::newCurl(ec); } private: Variable encoding; diff --git a/project2/xml/xmlDocumentCache.cpp b/project2/xml/xmlDocumentCache.cpp index e0665b0..94157ec 100644 --- a/project2/xml/xmlDocumentCache.cpp +++ b/project2/xml/xmlDocumentCache.cpp @@ -28,6 +28,11 @@ static XmlDocumentCache::DocumentPtr helperReturnDocument(XmlDocumentCache::Docu return dp; } +XmlDocumentCache::XmlDocumentCache(ScriptNodePtr n) : + VariableCurlHelper(n) +{ +} + XmlDocumentCache::DocumentPtr XmlDocumentCache::getDocument(const Glib::ustring & url, boost::optional encoding, bool html, bool warnings, ExecContext * ec) const { @@ -60,10 +65,10 @@ xmlCloseFunc(void *) } void -XmlDocumentCache::queue(const Glib::ustring & url, boost::optional encoding, bool html, bool warnings, ExecContext *) const +XmlDocumentCache::queue(const Glib::ustring & url, boost::optional encoding, bool html, bool warnings, ExecContext * ec) const { if (queued.find(url) == queued.end()) { - cbf.addCurl(url, [url, encoding, html, warnings](std::istream & strm) { + auto curl = cbf.addCurl(url, [url, encoding, html, warnings](std::istream & strm) { if (html) { int flags = warnings ? 0 : XML_PARSE_NOWARNING | XML_PARSE_NOERROR; htmlDocPtr doc = htmlReadIO(xmlReadFunc, xmlCloseFunc, &strm, url.c_str(), @@ -97,7 +102,8 @@ XmlDocumentCache::queue(const Glib::ustring & url, boost::optional boost::bind(helperThrow, error.message))); } } - })->setopt(CURLOPT_ENCODING, "deflate, gzip"); + }); + setCurlOpts(curl.get(), ec); queued.insert(url); } } diff --git a/project2/xml/xmlDocumentCache.h b/project2/xml/xmlDocumentCache.h index e0a14a6..e735a24 100644 --- a/project2/xml/xmlDocumentCache.h +++ b/project2/xml/xmlDocumentCache.h @@ -10,8 +10,9 @@ #include #include #include +#include -class XmlDocumentCache { +class XmlDocumentCache : protected VariableCurlHelper { public: typedef std::set Queued; typedef xmlpp::Document * DocumentPtr; @@ -20,12 +21,13 @@ class XmlDocumentCache { typedef std::map Documents; protected: + XmlDocumentCache(ScriptNodePtr); + static Queued queued; static Documents documents; void queue(const Glib::ustring & url, boost::optional encoding, bool html, bool warnings, ExecContext *) const; - virtual CurlPtr newCurl(ExecContext *) const = 0; virtual bool asHtml(ExecContext * ec) const = 0; virtual bool withWarnings(ExecContext * ec) const = 0; diff --git a/project2/xml/xmlDocumentPrefetch.cpp b/project2/xml/xmlDocumentPrefetch.cpp index 58489ce..769709f 100644 --- a/project2/xml/xmlDocumentPrefetch.cpp +++ b/project2/xml/xmlDocumentPrefetch.cpp @@ -9,7 +9,7 @@ XmlDocumentPrefetch::XmlDocumentPrefetch(ScriptNodePtr p) : SourceObject(p), View(p), Task(p), - VariableCurlHelper(p), + XmlDocumentCache(p), html(p, "html", false), warnings(p, "warnings", true), encoding(p, "encoding", Null()) @@ -32,12 +32,6 @@ XmlDocumentPrefetch::execute(ExecContext * ec) const queue(url(ec), encoding(ec), asHtml(ec), warnings(ec), ec); } -CurlPtr -XmlDocumentPrefetch::newCurl(ExecContext * ec) const -{ - return VariableCurlHelper::newCurl(ec); -} - bool XmlDocumentPrefetch::asHtml(ExecContext * ec) const { diff --git a/project2/xml/xmlDocumentPrefetch.h b/project2/xml/xmlDocumentPrefetch.h index fd9285d..7861971 100644 --- a/project2/xml/xmlDocumentPrefetch.h +++ b/project2/xml/xmlDocumentPrefetch.h @@ -8,7 +8,7 @@ #include /// Project2 component to queue up CURL objects to be downloaded -class XmlDocumentPrefetch : public View, public Task, XmlDocumentCache, VariableCurlHelper { +class XmlDocumentPrefetch : public View, public Task, XmlDocumentCache { public: XmlDocumentPrefetch(ScriptNodePtr p); ~XmlDocumentPrefetch(); @@ -20,7 +20,6 @@ class XmlDocumentPrefetch : public View, public Task, XmlDocumentCache, Variable const Variable warnings; const Variable encoding; - CurlPtr newCurl(ExecContext *) const; bool asHtml(ExecContext * ec) const; bool withWarnings(ExecContext * ec) const; }; diff --git a/project2/xml/xpathRows.cpp b/project2/xml/xpathRows.cpp index eefcbc2..e09458b 100644 --- a/project2/xml/xpathRows.cpp +++ b/project2/xml/xpathRows.cpp @@ -16,7 +16,7 @@ SimpleMessageException(XpathEvalError); XPathRows::XPathRows(ScriptNodePtr p) : RowSet(p), - VariableCurlHelper(p), + XmlDocumentCache(p), html(p, "html", false), warnings(p, "warnings", true), encoding(p, "encoding", Null()) @@ -46,12 +46,6 @@ XPathRows::withWarnings(ExecContext * ec) const return warnings(ec); } -CurlPtr -XPathRows::newCurl(ExecContext * ec) const -{ - return VariableCurlHelper::newCurl(ec); -} - void XPathRows::execute(const Glib::ustring & filter, const RowProcessorCallback & rp, ExecContext * ec) const { diff --git a/project2/xml/xpathRows.h b/project2/xml/xpathRows.h index 03929ab..f81c4c8 100644 --- a/project2/xml/xpathRows.h +++ b/project2/xml/xpathRows.h @@ -12,7 +12,7 @@ #include "definedColumns.h" /// Project2 component to create a row set based on the contents of an XML resource and specific XPaths with its hierarchy -class XPathRows : public RowSet, XmlDocumentCache, VariableCurlHelper { +class XPathRows : public RowSet, XmlDocumentCache { public: XPathRows(ScriptNodePtr p); ~XPathRows(); @@ -40,7 +40,6 @@ class XPathRows : public RowSet, XmlDocumentCache, VariableCurlHelper { typedef std::map FilterViews; FilterViews fvs; - virtual CurlPtr newCurl(ExecContext *) const; virtual bool asHtml(ExecContext *) const; virtual bool withWarnings(ExecContext *) const; -- cgit v1.2.3