summaryrefslogtreecommitdiffabout
path: root/lib
authorMichael Krelin <hacker@klever.net>2007-11-26 21:14:28 (UTC)
committer Michael Krelin <hacker@klever.net>2007-11-26 21:14:28 (UTC)
commit34dcd4dd9af494eab2f6e47498c270d27954bf19 (patch) (side-by-side diff)
tree19eeac758bf09905e9e55a24fa2494930ee5c890 /lib
parentd788db9c490575e63506ce502a2f089eaaa624ee (diff)
downloadlibopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.zip
libopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.tar.gz
libopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.tar.bz2
encapsulated write functionality into curl_t
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc50
-rw-r--r--lib/curl.cc12
2 files changed, 40 insertions, 22 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index c155157..62bec71 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -20,2 +20,21 @@ namespace opkele {
+ template<int lim>
+ class curl_fetch_string_t : public curl_t {
+ public:
+ curl_fetch_string_t(CURL *c)
+ : curl_t(c) { }
+ ~curl_fetch_string_t() throw() { }
+
+ string response;
+
+ size_t write(void *p,size_t size,size_t nmemb) {
+ size_t bytes = size*nmemb;
+ size_t get = min(lim-response.length(),bytes);
+ response.append((const char *)p,get);
+ return get;
+ }
+ };
+
+ typedef curl_fetch_string_t<16384> curl_pick_t;
+
class pcre_matches_t {
@@ -64,10 +83,2 @@ namespace opkele {
- static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
- string *str = (string*)stream;
- size_t bytes = size*nmemb;
- size_t get = min(16384-str->length(),bytes);
- str->append((const char*)ptr,get);
- return get;
- }
-
assoc_t consumer_t::associate(const string& server) {
@@ -86,6 +97,5 @@ namespace opkele {
request += util::url_encode(util::bignum_to_base64(dh->pub_key));
- curl_t curl = curl_t::easy_init();
+ curl_pick_t curl = curl_pick_t::easy_init();
if(!curl)
throw exception_curl(OPKELE_CP_ "failed to initialize curl");
- string response;
CURLcode r;
@@ -96,4 +106,3 @@ namespace opkele {
|| (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
- || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring))
- || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
+ || (r=curl.set_write())
;
@@ -103,3 +112,3 @@ namespace opkele {
throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
- params_t p; p.parse_keyvalues(response);
+ params_t p; p.parse_keyvalues(curl.response);
if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
@@ -246,6 +255,5 @@ namespace opkele {
}
- curl_t curl = curl_t::easy_init();
+ curl_pick_t curl = curl_pick_t::easy_init();
if(!curl)
throw exception_curl(OPKELE_CP_ "failed to initialize curl");
- string response;
CURLcode r;
@@ -256,4 +264,3 @@ namespace opkele {
|| (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
- || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring))
- || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
+ || (r=curl.set_write())
;
@@ -263,3 +270,3 @@ namespace opkele {
throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
- params_t pp; pp.parse_keyvalues(response);
+ params_t pp; pp.parse_keyvalues(curl.response);
if(pp.has_param("invalidate_handle"))
@@ -279,6 +286,6 @@ namespace opkele {
delegate.erase();
- curl_t curl = curl_t::easy_init();
+ curl_pick_t curl = curl_pick_t::easy_init();
if(!curl)
throw exception_curl(OPKELE_CP_ "failed to initialize curl");
- string html;
+ string& html = curl.response;
CURLcode r;
@@ -286,4 +293,3 @@ namespace opkele {
|| (r=curl.easy_setopt(CURLOPT_URL,url.c_str()))
- || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring))
- || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&html))
+ || (r=curl.set_write());
;
diff --git a/lib/curl.cc b/lib/curl.cc
index 418aa79..3e69b47 100644
--- a/lib/curl.cc
+++ b/lib/curl.cc
@@ -39,2 +39,14 @@ namespace opkele {
+ static size_t _write(void *p,size_t s,size_t nm,void *stream) {
+ return ((curl_t*)stream)->write(p,s,nm);
+ }
+
+ CURLcode curl_t::set_write() {
+ assert(_c);
+ CURLcode r;
+ (r = easy_setopt(CURLOPT_WRITEDATA,this))
+ || (r = easy_setopt(CURLOPT_WRITEFUNCTION,_write));
+ return r;
+ }
+
}