summaryrefslogtreecommitdiffabout
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) (unidiff)
tree19eeac758bf09905e9e55a24fa2494930ee5c890
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 (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/curl.h3
-rw-r--r--lib/consumer.cc50
-rw-r--r--lib/curl.cc12
3 files changed, 43 insertions, 22 deletions
diff --git a/include/opkele/curl.h b/include/opkele/curl.h
index 6a7d084..1029b34 100644
--- a/include/opkele/curl.h
+++ b/include/opkele/curl.h
@@ -30,6 +30,9 @@ namespace opkele {
30 inline CURLcode easy_getinfo(CURLINFO i,IT p) { assert(_c); return curl_easy_getinfo(_c,i,p); } 30 inline CURLcode easy_getinfo(CURLINFO i,IT p) { assert(_c); return curl_easy_getinfo(_c,i,p); }
31 31
32 static inline CURL *easy_init() { return curl_easy_init(); } 32 static inline CURL *easy_init() { return curl_easy_init(); }
33
34 virtual size_t write(void *p,size_t s,size_t nm) { return 0; }
35 CURLcode set_write();
33 }; 36 };
34 37
35 } 38 }
diff --git a/lib/consumer.cc b/lib/consumer.cc
index c155157..62bec71 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -18,6 +18,25 @@ namespace opkele {
18 using namespace std; 18 using namespace std;
19 using util::curl_t; 19 using util::curl_t;
20 20
21 template<int lim>
22 class curl_fetch_string_t : public curl_t {
23 public:
24 curl_fetch_string_t(CURL *c)
25 : curl_t(c) { }
26 ~curl_fetch_string_t() throw() { }
27
28 string response;
29
30 size_t write(void *p,size_t size,size_t nmemb) {
31 size_t bytes = size*nmemb;
32 size_t get = min(lim-response.length(),bytes);
33 response.append((const char *)p,get);
34 return get;
35 }
36 };
37
38 typedef curl_fetch_string_t<16384> curl_pick_t;
39
21 class pcre_matches_t { 40 class pcre_matches_t {
22 public: 41 public:
23 int *_ov; 42 int *_ov;
@@ -62,14 +81,6 @@ namespace opkele {
62 } 81 }
63 }; 82 };
64 83
65 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
66 string *str = (string*)stream;
67 size_t bytes = size*nmemb;
68 size_t get = min(16384-str->length(),bytes);
69 str->append((const char*)ptr,get);
70 return get;
71 }
72
73 assoc_t consumer_t::associate(const string& server) { 84 assoc_t consumer_t::associate(const string& server) {
74 util::dh_t dh = DH_new(); 85 util::dh_t dh = DH_new();
75 if(!dh) 86 if(!dh)
@@ -84,24 +95,22 @@ namespace opkele {
84 "&openid.session_type=DH-SHA1" 95 "&openid.session_type=DH-SHA1"
85 "&openid.dh_consumer_public="; 96 "&openid.dh_consumer_public=";
86 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 97 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
87 curl_t curl = curl_t::easy_init(); 98 curl_pick_t curl = curl_pick_t::easy_init();
88 if(!curl) 99 if(!curl)
89 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 100 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
90 string response;
91 CURLcode r; 101 CURLcode r;
92 (r=curl.misc_sets()) 102 (r=curl.misc_sets())
93 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 103 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
94 || (r=curl.easy_setopt(CURLOPT_POST,1)) 104 || (r=curl.easy_setopt(CURLOPT_POST,1))
95 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 105 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
96 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 106 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
97 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 107 || (r=curl.set_write())
98 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
99 ; 108 ;
100 if(r) 109 if(r)
101 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 110 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
102 if( (r=curl.easy_perform()) ) 111 if( (r=curl.easy_perform()) )
103 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 112 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
104 params_t p; p.parse_keyvalues(response); 113 params_t p; p.parse_keyvalues(curl.response);
105 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 114 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
106 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 115 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
107 string st; 116 string st;
@@ -244,24 +253,22 @@ namespace opkele {
244 request += util::url_encode(i->second); 253 request += util::url_encode(i->second);
245 } 254 }
246 } 255 }
247 curl_t curl = curl_t::easy_init(); 256 curl_pick_t curl = curl_pick_t::easy_init();
248 if(!curl) 257 if(!curl)
249 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 258 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
250 string response;
251 CURLcode r; 259 CURLcode r;
252 (r=curl.misc_sets()) 260 (r=curl.misc_sets())
253 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 261 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
254 || (r=curl.easy_setopt(CURLOPT_POST,1)) 262 || (r=curl.easy_setopt(CURLOPT_POST,1))
255 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 263 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
256 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 264 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
257 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 265 || (r=curl.set_write())
258 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
259 ; 266 ;
260 if(r) 267 if(r)
261 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 268 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
262 if( (r=curl.easy_perform()) ) 269 if( (r=curl.easy_perform()) )
263 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 270 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
264 params_t pp; pp.parse_keyvalues(response); 271 params_t pp; pp.parse_keyvalues(curl.response);
265 if(pp.has_param("invalidate_handle")) 272 if(pp.has_param("invalidate_handle"))
266 invalidate_assoc(server,pp.get_param("invalidate_handle")); 273 invalidate_assoc(server,pp.get_param("invalidate_handle"));
267 if(pp.has_param("is_valid")) { 274 if(pp.has_param("is_valid")) {
@@ -277,15 +284,14 @@ namespace opkele {
277 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 284 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
278 server.erase(); 285 server.erase();
279 delegate.erase(); 286 delegate.erase();
280 curl_t curl = curl_t::easy_init(); 287 curl_pick_t curl = curl_pick_t::easy_init();
281 if(!curl) 288 if(!curl)
282 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 289 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
283 string html; 290 string& html = curl.response;
284 CURLcode r; 291 CURLcode r;
285 (r=curl.misc_sets()) 292 (r=curl.misc_sets())
286 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str())) 293 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str()))
287 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 294 || (r=curl.set_write());
288 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&html))
289 ; 295 ;
290 if(r) 296 if(r)
291 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 297 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
diff --git a/lib/curl.cc b/lib/curl.cc
index 418aa79..3e69b47 100644
--- a/lib/curl.cc
+++ b/lib/curl.cc
@@ -37,6 +37,18 @@ namespace opkele {
37 return r; 37 return r;
38 } 38 }
39 39
40 static size_t _write(void *p,size_t s,size_t nm,void *stream) {
41 return ((curl_t*)stream)->write(p,s,nm);
42 }
43
44 CURLcode curl_t::set_write() {
45 assert(_c);
46 CURLcode r;
47 (r = easy_setopt(CURLOPT_WRITEDATA,this))
48 || (r = easy_setopt(CURLOPT_WRITEFUNCTION,_write));
49 return r;
50 }
51
40 } 52 }
41 53
42} 54}