-rw-r--r-- | include/Makefile.am | 3 | ||||
-rw-r--r-- | include/opkele/curl.h | 28 | ||||
-rw-r--r-- | lib/consumer.cc | 25 |
3 files changed, 35 insertions, 21 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index f3551f5..b31786d 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -8,9 +8,10 @@ nobase_include_HEADERS = \ opkele/consumer.h \ opkele/extension.h \ opkele/sreg.h \ opkele/extension_chain.h \ opkele/xconsumer.h \ opkele/xserver.h EXTRA_DIST = \ opkele/data.h \ - opkele/util.h + opkele/util.h \ + opkele/curl.h diff --git a/include/opkele/curl.h b/include/opkele/curl.h new file mode 100644 index 0000000..8794ece --- a/dev/null +++ b/include/opkele/curl.h @@ -0,0 +1,28 @@ +#ifndef __OPKELE_CURL_H +#define __OPKELE_CURL_H + +#include <curl/curl.h> + +namespace opkele { + + namespace util { + + class curl_t { + public: + CURL *_c; + + curl_t() : _c(0) { } + curl_t(CURL *c) : _c(c) { } + ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } + + curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } + + operator const CURL*(void) const { return _c; } + operator CURL*(void) { return _c; } + }; + + } + +} + +#endif /* __OPKELE_CURL_H */ diff --git a/lib/consumer.cc b/lib/consumer.cc index 7881f5f..20f4174 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc @@ -1,19 +1,18 @@ #include <algorithm> #include <cassert> #include <cstring> #include <opkele/util.h> +#include <opkele/curl.h> #include <opkele/exception.h> #include <opkele/data.h> #include <opkele/consumer.h> #include <openssl/sha.h> #include <openssl/hmac.h> -#include <curl/curl.h> - #include <iostream> #include "config.h" #include <pcre.h> namespace opkele { using namespace std; @@ -57,30 +56,16 @@ namespace opkele { int exec(const string& s,pcre_matches_t& m) { if(!_p) throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); } }; - class curl_t { - public: - CURL *_c; - - curl_t() : _c(0) { } - curl_t(CURL *c) : _c(c) { } - ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } - - curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } - - operator const CURL*(void) const { return _c; } - operator CURL*(void) { return _c; } - }; - static CURLcode curl_misc_sets(CURL* c) { CURLcode r; (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) @@ -111,17 +96,17 @@ namespace opkele { if(!DH_generate_key(dh)) throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); string request = "openid.mode=associate" "&openid.assoc_type=HMAC-SHA1" "&openid.session_type=DH-SHA1" "&openid.dh_consumer_public="; request += util::url_encode(util::bignum_to_base64(dh->pub_key)); - curl_t curl = curl_easy_init(); + util::curl_t curl = curl_easy_init(); if(!curl) throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); string response; CURLcode r; (r=curl_misc_sets(curl)) || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) @@ -271,17 +256,17 @@ namespace opkele { for(params_t::const_iterator i=p.begin();i!=p.end();++i) { if(i->first!="openid.mode") { request += '&'; request += i->first; request += '='; request += util::url_encode(i->second); } } - curl_t curl = curl_easy_init(); + util::curl_t curl = curl_easy_init(); if(!curl) throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); string response; CURLcode r; (r=curl_misc_sets(curl)) || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) @@ -304,17 +289,17 @@ namespace opkele { return; } throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); } void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { server.erase(); delegate.erase(); - curl_t curl = curl_easy_init(); + util::curl_t curl = curl_easy_init(); if(!curl) throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); string html; CURLcode r; (r=curl_misc_sets(curl)) || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) @@ -410,17 +395,17 @@ namespace opkele { if(sl==string::npos) rv += '/'; } return rv; } string consumer_t::canonicalize(const string& url) { string rv = normalize(url); - curl_t curl = curl_easy_init(); + util::curl_t curl = curl_easy_init(); if(!curl) throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); string html; CURLcode r; (r=curl_misc_sets(curl)) || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) ; |