summaryrefslogtreecommitdiffabout
path: root/lib
authorMichael Krelin <hacker@klever.net>2007-11-24 12:02:50 (UTC)
committer Michael Krelin <hacker@klever.net>2007-11-24 12:02:50 (UTC)
commit986274e9f9a8e2ef0f92b08d2d2c9485bd19adec (patch) (side-by-side diff)
tree72b3d134d9d635b2aadbdebf615e1717dd9d683b /lib
parentef66d79bf58f099179a932ca80d07ca93a42909c (diff)
downloadlibopkele-986274e9f9a8e2ef0f92b08d2d2c9485bd19adec.zip
libopkele-986274e9f9a8e2ef0f92b08d2d2c9485bd19adec.tar.gz
libopkele-986274e9f9a8e2ef0f92b08d2d2c9485bd19adec.tar.bz2
moved curl_t wrapper to util namespace
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib') (more/less context) (show whitespace changes)
-rw-r--r--lib/consumer.cc25
1 files changed, 5 insertions, 20 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index 7881f5f..20f4174 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,23 +1,22 @@
#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;
class pcre_matches_t {
public:
int *_ov;
@@ -53,38 +52,24 @@ namespace opkele {
pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; }
operator const pcre*(void) const { return _p; }
operator pcre*(void) { return _p; }
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))
#ifdef DISABLE_CURL_SSL_VERIFYHOST
|| (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0))
#endif
#ifdef DISABLE_CURL_SSL_VERIFYPEER
@@ -107,25 +92,25 @@ namespace opkele {
if(!dh)
throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
dh->p = util::dec_to_bignum(data::_default_p);
dh->g = util::dec_to_bignum(data::_default_g);
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()))
|| (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
|| (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
|| (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
;
@@ -267,25 +252,25 @@ namespace opkele {
}
void consumer_t::check_authentication(const string& server,const params_t& p) {
string request = "openid.mode=check_authentication";
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()))
|| (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
|| (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
|| (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
;
@@ -300,25 +285,25 @@ namespace opkele {
if(pp.get_param("is_valid")=="true")
return;
}else if(pp.has_param("lifetime")) {
if(util::string_to_long(pp.get_param("lifetime")))
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))
;
if(r)
throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
r = curl_easy_perform(curl);
@@ -406,25 +391,25 @@ namespace opkele {
if(qm!=string::npos) {
if(sl==string::npos || sl>qm)
rv.insert(qm,1,'/');
}else{
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))
;
if(r)
throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
r = curl_easy_perform(curl);
if(r)