From 18edc161c00c63f9651f42bd665e69b5863a4443 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 17 Feb 2011 00:51:50 +0000 Subject: Merge urlRows' and xslRows' CURL code into a common more fully featured (proxies, user agents, etc) base class; curlHelper --- project2/Jamfile.jam | 2 ++ project2/curlHelper.cpp | 37 +++++++++++++++++++++++++++++++++++++ project2/curlHelper.h | 30 ++++++++++++++++++++++++++++++ project2/urlRows.cpp | 11 ++--------- project2/urlRows.h | 7 ++----- project2/xslRows.cpp | 8 ++------ project2/xslRows.h | 5 ++--- 7 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 project2/curlHelper.cpp create mode 100644 project2/curlHelper.h diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 594bbd1..d4e72e8 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -63,6 +63,7 @@ lib p2common : lib p2xml : xmlRows.cpp xslRows.cpp + p2url : ../libmisc//misc libxmlpp @@ -127,6 +128,7 @@ lib p2sql : lib p2url : urlRows.cpp + curlHelper.cpp : ../libmisc//misc libxmlpp diff --git a/project2/curlHelper.cpp b/project2/curlHelper.cpp new file mode 100644 index 0000000..eb6bf74 --- /dev/null +++ b/project2/curlHelper.cpp @@ -0,0 +1,37 @@ +#include "curlHelper.h" + +CurlHelper::CurlHelper(const xmlpp::Element * p) : + url(p, "url"), + userAgent(p, "useragent", false, "project2/0.3"), + cookieJar(p, "cookiejar", false), + proxy(p, "proxy", false), + method(p, "username", false), + userName(p, "username", false), + password(p, "password", false) +{ +} + +CurlHelper::~CurlHelper() +{ +} + +CurlHandle::Ptr +CurlHelper::newCurl() const +{ + CurlHandle::Ptr c = new CurlHandle(); + c->setopt(CURLOPT_FOLLOWLOCATION, 1); + c->setopt(CURLOPT_ENCODING, "deflate, gzip"); + setopt(c, CURLOPT_URL, url()); + setopt(c, CURLOPT_USERAGENT, userAgent()); + setopt(c, CURLOPT_PROXY, proxy()); + setopt(c, CURLOPT_COOKIEFILE, cookieJar()); + setopt(c, CURLOPT_COOKIEJAR, cookieJar()); + return c; +} + +void +CurlHelper::setopt(CurlHandle::Ptr c, CURLoption o, const char * v) +{ + c->setopt(o, v); +} + diff --git a/project2/curlHelper.h b/project2/curlHelper.h new file mode 100644 index 0000000..fd4d265 --- /dev/null +++ b/project2/curlHelper.h @@ -0,0 +1,30 @@ +#ifndef CURLHELPER_H +#define CURLHELPER_H + +#include +#include "variables.h" +#include "../libmisc/curlsup.h" + +class CurlHelper { + public: + CurlHelper(const xmlpp::Element * p); + ~CurlHelper(); + + const Variable url; + + protected: + CurlHandle::Ptr newCurl() const; + + private: + static void setopt(CurlHandle::Ptr, CURLoption, const char *); + + const Variable userAgent; + const Variable cookieJar; + const Variable proxy; + const Variable method; + const Variable userName; + const Variable password; +}; + +#endif + diff --git a/project2/urlRows.cpp b/project2/urlRows.cpp index 447b661..d4910bb 100644 --- a/project2/urlRows.cpp +++ b/project2/urlRows.cpp @@ -11,7 +11,7 @@ DECLARE_LOADER("urlrows", UrlRows); UrlRows::UrlRows(const xmlpp::Element * p) : SourceObject(p), StreamRows(p), - url(p, "url"), + CurlHelper(p), convertRequired(encoding != "utf-8") { } @@ -62,14 +62,7 @@ UrlRows::execute(const RowProcessor * rp) const { rowNum = 1; begin(); - CurlHandle::Ptr c = new CurlHandle(); - c->setopt(CURLOPT_URL, (const char *)url()); - //c->setopt(CURLOPT_PROXY, proxy.c_str()); - c->setopt(CURLOPT_FOLLOWLOCATION, 1); - //c->setopt(CURLOPT_COOKIEFILE, (std::string(cacheRoot) + "/ytfs.cookies").c_str()); - //c->setopt(CURLOPT_COOKIEJAR, (std::string(cacheRoot) + "/ytfs.cookies").c_str()); - c->setopt(CURLOPT_ENCODING, "deflate, gzip"); - c->setopt(CURLOPT_USERAGENT, "project2/0.3"); + CurlHandle::Ptr c = newCurl(); callback cb(this, rp); c->setopt(CURLOPT_WRITEDATA, &cb); c->setopt(CURLOPT_WRITEFUNCTION, &handleDataHelper); diff --git a/project2/urlRows.h b/project2/urlRows.h index fe63a3f..d2bf038 100644 --- a/project2/urlRows.h +++ b/project2/urlRows.h @@ -6,8 +6,9 @@ #include #include #include "streamRows.h" +#include "curlHelper.h" -class UrlRows : public StreamRows { +class UrlRows : public StreamRows, CurlHelper { public: UrlRows(const xmlpp::Element * p); ~UrlRows(); @@ -16,10 +17,6 @@ class UrlRows : public StreamRows { void execute(const RowProcessor *) const; virtual void setFilter(const Glib::ustring &); - const Variable url; - - protected: - private: struct callback { callback(const UrlRows * urlRows, const RowProcessor * rp); diff --git a/project2/xslRows.cpp b/project2/xslRows.cpp index 1a7d6f4..7f53e91 100644 --- a/project2/xslRows.cpp +++ b/project2/xslRows.cpp @@ -19,7 +19,7 @@ SimpleMessageException(XpathEvalError); XslRows::XslRows(const xmlpp::Element * p) : SourceObject(p), RowSet(p), - url(p, "url"), + CurlHelper(p), html(p->get_attribute_value("html") == "true"), warnings(p->get_attribute_value("warnings") != "false") { @@ -70,11 +70,7 @@ XslRows::getDocument(const Glib::ustring & url) const { Documents::const_iterator i = documents.find(url); if (i == documents.end()) { - CurlHandle::Ptr c = new CurlHandle(); - c->setopt(CURLOPT_URL, url.c_str()); - c->setopt(CURLOPT_FOLLOWLOCATION, 1); - c->setopt(CURLOPT_ENCODING, "deflate, gzip"); - c->setopt(CURLOPT_USERAGENT, "project2/0.3"); + CurlHandle::Ptr c = newCurl(); std::string buf; c->setopt(CURLOPT_WRITEDATA, &buf); c->setopt(CURLOPT_WRITEFUNCTION, &handleDataHelper); diff --git a/project2/xslRows.h b/project2/xslRows.h index 8f13c89..27cb1b5 100644 --- a/project2/xslRows.h +++ b/project2/xslRows.h @@ -7,10 +7,10 @@ #include #include "rowSet.h" #include "variables.h" +#include "curlHelper.h" -class XslRows : public RowSet { +class XslRows : public RowSet, CurlHelper { public: - XslRows(const xmlpp::Element * p); ~XslRows(); @@ -24,7 +24,6 @@ class XslRows : public RowSet { bool isNull(unsigned int col) const; bool isNull(const Glib::ustring & id) const; - const Variable url; const bool html; const bool warnings; -- cgit v1.2.3