-rw-r--r-- | include/opkele/curl.h | 24 | ||||
-rw-r--r-- | lib/consumer.cc | 20 |
2 files changed, 25 insertions, 19 deletions
diff --git a/include/opkele/curl.h b/include/opkele/curl.h index 8020b63..5cf8e48 100644 --- a/include/opkele/curl.h +++ b/include/opkele/curl.h | |||
@@ -1,48 +1,72 @@ | |||
1 | #ifndef __OPKELE_CURL_H | 1 | #ifndef __OPKELE_CURL_H |
2 | #define __OPKELE_CURL_H | 2 | #define __OPKELE_CURL_H |
3 | 3 | ||
4 | #include <cassert> | 4 | #include <cassert> |
5 | #include <string> | ||
6 | #include <algorithm> | ||
5 | #include <curl/curl.h> | 7 | #include <curl/curl.h> |
6 | 8 | ||
7 | namespace opkele { | 9 | namespace opkele { |
10 | using std::min; | ||
11 | using std::string; | ||
8 | 12 | ||
9 | namespace util { | 13 | namespace util { |
10 | 14 | ||
11 | class curl_t { | 15 | class curl_t { |
12 | public: | 16 | public: |
13 | CURL *_c; | 17 | CURL *_c; |
14 | 18 | ||
15 | curl_t() : _c(0) { } | 19 | curl_t() : _c(0) { } |
16 | curl_t(CURL *c) : _c(c) { } | 20 | curl_t(CURL *c) : _c(c) { } |
17 | virtual ~curl_t() throw(); | 21 | virtual ~curl_t() throw(); |
18 | 22 | ||
19 | curl_t& operator=(CURL *c); | 23 | curl_t& operator=(CURL *c); |
20 | 24 | ||
21 | operator const CURL*(void) const { return _c; } | 25 | operator const CURL*(void) const { return _c; } |
22 | operator CURL*(void) { return _c; } | 26 | operator CURL*(void) { return _c; } |
23 | 27 | ||
24 | CURLcode misc_sets(); | 28 | CURLcode misc_sets(); |
25 | 29 | ||
26 | template<typename PT> | 30 | template<typename PT> |
27 | inline CURLcode easy_setopt(CURLoption o,PT p) { assert(_c); return curl_easy_setopt(_c,o,p); } | 31 | 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); } | 32 | CURLcode easy_perform() { assert(_c); return curl_easy_perform(_c); } |
29 | template<typename IT> | 33 | template<typename IT> |
30 | inline CURLcode easy_getinfo(CURLINFO i,IT p) { assert(_c); return curl_easy_getinfo(_c,i,p); } | 34 | inline CURLcode easy_getinfo(CURLINFO i,IT p) { assert(_c); return curl_easy_getinfo(_c,i,p); } |
31 | 35 | ||
32 | static inline CURL *easy_init() { return curl_easy_init(); } | 36 | static inline CURL *easy_init() { return curl_easy_init(); } |
33 | 37 | ||
34 | virtual size_t write(void* /* p */,size_t /* s */,size_t /* nm */) { return 0; } | 38 | virtual size_t write(void* /* p */,size_t /* s */,size_t /* nm */) { return 0; } |
35 | CURLcode set_write(); | 39 | CURLcode set_write(); |
36 | 40 | ||
37 | virtual int progress(double /* dlt */,double /* dln*/ ,double /* ult */,double /* uln */) { return 0; } | 41 | virtual int progress(double /* dlt */,double /* dln*/ ,double /* ult */,double /* uln */) { return 0; } |
38 | CURLcode set_progress(); | 42 | CURLcode set_progress(); |
39 | 43 | ||
40 | virtual size_t header(void* /* p */,size_t s,size_t nm) { return s*nm; } | 44 | virtual size_t header(void* /* p */,size_t s,size_t nm) { return s*nm; } |
41 | CURLcode set_header(); | 45 | CURLcode set_header(); |
42 | }; | 46 | }; |
43 | 47 | ||
48 | template<int lim> | ||
49 | class curl_fetch_string_t : public curl_t { | ||
50 | public: | ||
51 | curl_fetch_string_t(CURL *c) | ||
52 | : curl_t(c) { } | ||
53 | ~curl_fetch_string_t() throw() { } | ||
54 | |||
55 | string response; | ||
56 | |||
57 | size_t write(void *p,size_t size,size_t nmemb) { | ||
58 | size_t bytes = size*nmemb; | ||
59 | size_t get = min(lim-response.length(),bytes); | ||
60 | response.append((const char *)p,get); | ||
61 | return get; | ||
62 | } | ||
63 | }; | ||
64 | |||
65 | typedef curl_fetch_string_t<16384> curl_pick_t; | ||
66 | |||
67 | |||
44 | } | 68 | } |
45 | 69 | ||
46 | } | 70 | } |
47 | 71 | ||
48 | #endif /* __OPKELE_CURL_H */ | 72 | #endif /* __OPKELE_CURL_H */ |
diff --git a/lib/consumer.cc b/lib/consumer.cc index 9f7530f..3c3b4f8 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -1,86 +1,68 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cstring> | 3 | #include <cstring> |
4 | #include <opkele/util.h> | 4 | #include <opkele/util.h> |
5 | #include <opkele/curl.h> | 5 | #include <opkele/curl.h> |
6 | #include <opkele/exception.h> | 6 | #include <opkele/exception.h> |
7 | #include <opkele/data.h> | 7 | #include <opkele/data.h> |
8 | #include <opkele/consumer.h> | 8 | #include <opkele/consumer.h> |
9 | #include <openssl/sha.h> | 9 | #include <openssl/sha.h> |
10 | #include <openssl/hmac.h> | 10 | #include <openssl/hmac.h> |
11 | #include <iostream> | 11 | #include <iostream> |
12 | 12 | ||
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 | using util::curl_pick_t; | |
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 | ||
40 | class pcre_matches_t { | 22 | class pcre_matches_t { |
41 | public: | 23 | public: |
42 | int *_ov; | 24 | int *_ov; |
43 | int _s; | 25 | int _s; |
44 | 26 | ||
45 | pcre_matches_t() : _ov(0), _s(0) { } | 27 | pcre_matches_t() : _ov(0), _s(0) { } |
46 | pcre_matches_t(int s) : _ov(0), _s(s) { | 28 | pcre_matches_t(int s) : _ov(0), _s(s) { |
47 | if(_s&1) ++_s; | 29 | if(_s&1) ++_s; |
48 | _s += _s>>1; | 30 | _s += _s>>1; |
49 | _ov = new int[_s]; | 31 | _ov = new int[_s]; |
50 | } | 32 | } |
51 | ~pcre_matches_t() throw() { if(_ov) delete[] _ov; } | 33 | ~pcre_matches_t() throw() { if(_ov) delete[] _ov; } |
52 | 34 | ||
53 | int begin(int i) const { return _ov[i<<1]; } | 35 | int begin(int i) const { return _ov[i<<1]; } |
54 | int end(int i) const { return _ov[(i<<1)+1]; } | 36 | int end(int i) const { return _ov[(i<<1)+1]; } |
55 | int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; } | 37 | int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; } |
56 | }; | 38 | }; |
57 | 39 | ||
58 | class pcre_t { | 40 | class pcre_t { |
59 | public: | 41 | public: |
60 | pcre *_p; | 42 | pcre *_p; |
61 | 43 | ||
62 | pcre_t() : _p(0) { } | 44 | pcre_t() : _p(0) { } |
63 | pcre_t(pcre *p) : _p(p) { } | 45 | pcre_t(pcre *p) : _p(p) { } |
64 | pcre_t(const char *re,int opts) : _p(0) { | 46 | pcre_t(const char *re,int opts) : _p(0) { |
65 | static const char *errptr; static int erroffset; | 47 | static const char *errptr; static int erroffset; |
66 | _p = pcre_compile(re,opts,&errptr,&erroffset,NULL); | 48 | _p = pcre_compile(re,opts,&errptr,&erroffset,NULL); |
67 | if(!_p) | 49 | if(!_p) |
68 | throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); | 50 | throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); |
69 | } | 51 | } |
70 | ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } | 52 | ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } |
71 | 53 | ||
72 | pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } | 54 | pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } |
73 | 55 | ||
74 | operator const pcre*(void) const { return _p; } | 56 | operator const pcre*(void) const { return _p; } |
75 | operator pcre*(void) { return _p; } | 57 | operator pcre*(void) { return _p; } |
76 | 58 | ||
77 | int exec(const string& s,pcre_matches_t& m) { | 59 | int exec(const string& s,pcre_matches_t& m) { |
78 | if(!_p) | 60 | if(!_p) |
79 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); | 61 | throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); |
80 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); | 62 | return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); |
81 | } | 63 | } |
82 | }; | 64 | }; |
83 | 65 | ||
84 | assoc_t consumer_t::associate(const string& server) { | 66 | assoc_t consumer_t::associate(const string& server) { |
85 | util::dh_t dh = DH_new(); | 67 | util::dh_t dh = DH_new(); |
86 | if(!dh) | 68 | if(!dh) |