author | Michael Krelin <hacker@klever.net> | 2007-11-26 21:14:28 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-11-26 21:14:28 (UTC) |
commit | 34dcd4dd9af494eab2f6e47498c270d27954bf19 (patch) (unidiff) | |
tree | 19eeac758bf09905e9e55a24fa2494930ee5c890 | |
parent | d788db9c490575e63506ce502a2f089eaaa624ee (diff) | |
download | libopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.zip libopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.tar.gz libopkele-34dcd4dd9af494eab2f6e47498c270d27954bf19.tar.bz2 |
encapsulated write functionality into curl_t
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/curl.h | 3 | ||||
-rw-r--r-- | lib/consumer.cc | 50 | ||||
-rw-r--r-- | lib/curl.cc | 12 |
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 | |||
@@ -25,15 +25,18 @@ namespace opkele { | |||
25 | 25 | ||
26 | template<typename PT> | 26 | template<typename PT> |
27 | inline CURLcode easy_setopt(CURLoption o,PT p) { assert(_c); return curl_easy_setopt(_c,o,p); } | 27 | inline CURLcode easy_setopt(CURLoption o,PT p) { assert(_c); return curl_easy_setopt(_c,o,p); } |
28 | CURLcode easy_perform() { assert(_c); return curl_easy_perform(_c); } | 28 | CURLcode easy_perform() { assert(_c); return curl_easy_perform(_c); } |
29 | template<typename IT> | 29 | template<typename IT> |
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 | } |
36 | 39 | ||
37 | } | 40 | } |
38 | 41 | ||
39 | #endif /* __OPKELE_CURL_H */ | 42 | #endif /* __OPKELE_CURL_H */ |
diff --git a/lib/consumer.cc b/lib/consumer.cc index c155157..62bec71 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -13,16 +13,35 @@ | |||
13 | #include "config.h" | 13 | #include "config.h" |
14 | 14 | ||
15 | #include <pcre.h> | 15 | #include <pcre.h> |
16 | 16 | ||
17 | namespace opkele { | 17 | 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; |
24 | int _s; | 43 | int _s; |
25 | 44 | ||
26 | pcre_matches_t() : _ov(0), _s(0) { } | 45 | pcre_matches_t() : _ov(0), _s(0) { } |
27 | pcre_matches_t(int s) : _ov(0), _s(s) { | 46 | pcre_matches_t(int s) : _ov(0), _s(s) { |
28 | if(_s&1) ++_s; | 47 | if(_s&1) ++_s; |
@@ -57,56 +76,46 @@ namespace opkele { | |||
57 | 76 | ||
58 | int exec(const string& s,pcre_matches_t& m) { | 77 | int exec(const string& s,pcre_matches_t& m) { |
59 | if(!_p) | 78 | if(!_p) |
60 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); | 79 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); |
61 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); | 80 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); |
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) |
76 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); | 87 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); |
77 | dh->p = util::dec_to_bignum(data::_default_p); | 88 | dh->p = util::dec_to_bignum(data::_default_p); |
78 | dh->g = util::dec_to_bignum(data::_default_g); | 89 | dh->g = util::dec_to_bignum(data::_default_g); |
79 | if(!DH_generate_key(dh)) | 90 | if(!DH_generate_key(dh)) |
80 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 91 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
81 | string request = | 92 | string request = |
82 | "openid.mode=associate" | 93 | "openid.mode=associate" |
83 | "&openid.assoc_type=HMAC-SHA1" | 94 | "&openid.assoc_type=HMAC-SHA1" |
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; |
108 | if(p.has_param("session_type")) st = p.get_param("session_type"); | 117 | if(p.has_param("session_type")) st = p.get_param("session_type"); |
109 | if((!st.empty()) && st!="DH-SHA1") | 118 | if((!st.empty()) && st!="DH-SHA1") |
110 | throw bad_input(OPKELE_CP_ "unsupported session_type"); | 119 | throw bad_input(OPKELE_CP_ "unsupported session_type"); |
111 | secret_t secret; | 120 | secret_t secret; |
112 | if(st.empty()) { | 121 | if(st.empty()) { |
@@ -239,58 +248,55 @@ namespace opkele { | |||
239 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { | 248 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { |
240 | if(i->first!="openid.mode") { | 249 | if(i->first!="openid.mode") { |
241 | request += '&'; | 250 | request += '&'; |
242 | request += i->first; | 251 | request += i->first; |
243 | request += '='; | 252 | request += '='; |
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")) { |
268 | if(pp.get_param("is_valid")=="true") | 275 | if(pp.get_param("is_valid")=="true") |
269 | return; | 276 | return; |
270 | }else if(pp.has_param("lifetime")) { | 277 | }else if(pp.has_param("lifetime")) { |
271 | if(util::string_to_long(pp.get_param("lifetime"))) | 278 | if(util::string_to_long(pp.get_param("lifetime"))) |
272 | return; | 279 | return; |
273 | } | 280 | } |
274 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); | 281 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); |
275 | } | 282 | } |
276 | 283 | ||
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); |
292 | r = curl.easy_perform(); | 298 | r = curl.easy_perform(); |
293 | if(r && r!=CURLE_WRITE_ERROR) | 299 | if(r && r!=CURLE_WRITE_ERROR) |
294 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); | 300 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
295 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", | 301 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", |
296 | *re_lre = "<\\s*link\\b([^>]+)>", | 302 | *re_lre = "<\\s*link\\b([^>]+)>", |
diff --git a/lib/curl.cc b/lib/curl.cc index 418aa79..3e69b47 100644 --- a/lib/curl.cc +++ b/lib/curl.cc | |||
@@ -32,11 +32,23 @@ namespace opkele { | |||
32 | #endif | 32 | #endif |
33 | #ifdefDISABLE_CURL_SSL_VERIFYPEER | 33 | #ifdefDISABLE_CURL_SSL_VERIFYPEER |
34 | || (r=easy_setopt(CURLOPT_SSL_VERIFYPEER,0)) | 34 | || (r=easy_setopt(CURLOPT_SSL_VERIFYPEER,0)) |
35 | #endif | 35 | #endif |
36 | ; | 36 | ; |
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 | } |