-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/curl.cc | 20 | ||||
-rw-r--r-- | lib/fields.cc | 86 | ||||
-rw-r--r-- | lib/message.cc (renamed from lib/openid_message.cc) | 84 |
4 files changed, 108 insertions, 84 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index e8bfbf5..20d15b8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -30,3 +30,3 @@ libopkele_la_SOURCES = \ | |||
30 | basic_rp.cc prequeue_rp.cc \ | 30 | basic_rp.cc prequeue_rp.cc \ |
31 | openid_message.cc \ | 31 | fields.cc message.cc \ |
32 | basic_op.cc verify_op.cc | 32 | basic_op.cc verify_op.cc |
diff --git a/lib/curl.cc b/lib/curl.cc index 6172828..734e2ca 100644 --- a/lib/curl.cc +++ b/lib/curl.cc | |||
@@ -1 +1,2 @@ | |||
1 | #include <opkele/exception.h> | ||
1 | #include <opkele/curl.h> | 2 | #include <opkele/curl.h> |
@@ -8,2 +9,21 @@ namespace opkele { | |||
8 | 9 | ||
10 | curl_slist_t::~curl_slist_t() throw() { | ||
11 | if(_s) | ||
12 | curl_slist_free_all(_s); | ||
13 | } | ||
14 | |||
15 | curl_slist_t& curl_slist_t::operator=(curl_slist *s) { | ||
16 | if(_s) | ||
17 | curl_slist_free_all(_s); | ||
18 | _s = s; | ||
19 | return *this; | ||
20 | } | ||
21 | |||
22 | void curl_slist_t::append(const char *str) { | ||
23 | curl_slist *s = curl_slist_append(_s,str); | ||
24 | if(!s) | ||
25 | throw opkele::exception(OPKELE_CP_ "failed to curl_slist_append()"); | ||
26 | _s=s; | ||
27 | } | ||
28 | |||
9 | curl_t::~curl_t() throw() { | 29 | curl_t::~curl_t() throw() { |
diff --git a/lib/fields.cc b/lib/fields.cc new file mode 100644 index 0000000..d494098 --- a/dev/null +++ b/lib/fields.cc | |||
@@ -0,0 +1,86 @@ | |||
1 | #include <opkele/types.h> | ||
2 | #include <opkele/exception.h> | ||
3 | #include <opkele/util.h> | ||
4 | |||
5 | namespace opkele { | ||
6 | using std::unary_function; | ||
7 | |||
8 | struct __om_copier : public unary_function<const string&,void> { | ||
9 | public: | ||
10 | const basic_fields& from; | ||
11 | basic_fields& to; | ||
12 | |||
13 | __om_copier(basic_fields& t,const basic_fields& f) | ||
14 | : from(f), to(t) { } | ||
15 | |||
16 | result_type operator()(argument_type f) { | ||
17 | to.set_field(f,from.get_field(f)); } | ||
18 | }; | ||
19 | |||
20 | basic_fields::basic_fields(const basic_fields& x) { | ||
21 | x.copy_to(*this); | ||
22 | } | ||
23 | void basic_fields::copy_to(basic_fields& x) const { | ||
24 | x.reset_fields(); | ||
25 | for_each(fields_begin(),fields_end(), | ||
26 | __om_copier(x,*this) ); | ||
27 | } | ||
28 | void basic_fields::append_to(basic_fields& x) const { | ||
29 | for_each(fields_begin(),fields_end(), | ||
30 | __om_copier(x,*this) ); | ||
31 | } | ||
32 | |||
33 | struct __om_query_builder : public unary_function<const string&,void> { | ||
34 | public: | ||
35 | const basic_fields& om; | ||
36 | bool first; | ||
37 | string& rv; | ||
38 | const char *pfx; | ||
39 | |||
40 | __om_query_builder(const char *p,string& r,const basic_fields& m) | ||
41 | : om(m), first(true), rv(r), pfx(p) { | ||
42 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
43 | } | ||
44 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) | ||
45 | : om(m), first(true), rv(r), pfx(p) { | ||
46 | rv = u; | ||
47 | if(rv.find('?')==string::npos) | ||
48 | rv += '?'; | ||
49 | else | ||
50 | first = false; | ||
51 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
52 | } | ||
53 | |||
54 | result_type operator()(argument_type f) { | ||
55 | if(first) | ||
56 | first = false; | ||
57 | else | ||
58 | rv += '&'; | ||
59 | if(pfx) rv += pfx; | ||
60 | rv+= f; | ||
61 | rv += '='; | ||
62 | rv += util::url_encode(om.get_field(f)); | ||
63 | } | ||
64 | }; | ||
65 | |||
66 | string basic_fields::append_query(const string& url,const char *pfx) const { | ||
67 | string rv; | ||
68 | return __om_query_builder(pfx,rv,*this,url).rv; | ||
69 | } | ||
70 | string basic_fields::query_string(const char *pfx) const { | ||
71 | string rv; | ||
72 | return __om_query_builder(pfx,rv,*this).rv; | ||
73 | } | ||
74 | |||
75 | void basic_fields::reset_fields() { | ||
76 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | ||
77 | } | ||
78 | void basic_fields::set_field(const string&,const string&) { | ||
79 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | ||
80 | } | ||
81 | void basic_fields::reset_field(const string&) { | ||
82 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | ||
83 | } | ||
84 | |||
85 | |||
86 | } | ||
diff --git a/lib/openid_message.cc b/lib/message.cc index e244f43..b2324b7 100644 --- a/lib/openid_message.cc +++ b/lib/message.cc | |||
@@ -12,26 +12,2 @@ namespace opkele { | |||
12 | 12 | ||
13 | struct __om_copier : public unary_function<const string&,void> { | ||
14 | public: | ||
15 | const basic_openid_message& from; | ||
16 | basic_openid_message& to; | ||
17 | |||
18 | __om_copier(basic_openid_message& t,const basic_openid_message& f) | ||
19 | : from(f), to(t) { } | ||
20 | |||
21 | result_type operator()(argument_type f) { | ||
22 | to.set_field(f,from.get_field(f)); } | ||
23 | }; | ||
24 | |||
25 | basic_openid_message::basic_openid_message(const basic_openid_message& x) { | ||
26 | x.copy_to(*this); | ||
27 | } | ||
28 | void basic_openid_message::copy_to(basic_openid_message& x) const { | ||
29 | x.reset_fields(); | ||
30 | for_each(fields_begin(),fields_end(), | ||
31 | __om_copier(x,*this) ); | ||
32 | } | ||
33 | void basic_openid_message::append_to(basic_openid_message& x) const { | ||
34 | for_each(fields_begin(),fields_end(), | ||
35 | __om_copier(x,*this) ); | ||
36 | } | ||
37 | 13 | ||
@@ -67,54 +43,2 @@ namespace opkele { | |||
67 | 43 | ||
68 | struct __om_query_builder : public unary_function<const string&,void> { | ||
69 | public: | ||
70 | const basic_openid_message& om; | ||
71 | bool first; | ||
72 | string& rv; | ||
73 | const char *pfx; | ||
74 | |||
75 | __om_query_builder(const char *p,string& r,const basic_openid_message& m) | ||
76 | : om(m), first(true), rv(r), pfx(p) { | ||
77 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
78 | } | ||
79 | __om_query_builder(const char *p,string& r,const basic_openid_message& m,const string& u) | ||
80 | : om(m), first(true), rv(r), pfx(p) { | ||
81 | rv = u; | ||
82 | if(rv.find('?')==string::npos) | ||
83 | rv += '?'; | ||
84 | else | ||
85 | first = false; | ||
86 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
87 | } | ||
88 | |||
89 | result_type operator()(argument_type f) { | ||
90 | if(first) | ||
91 | first = false; | ||
92 | else | ||
93 | rv += '&'; | ||
94 | if(pfx) rv += pfx; | ||
95 | rv+= f; | ||
96 | rv += '='; | ||
97 | rv += util::url_encode(om.get_field(f)); | ||
98 | } | ||
99 | }; | ||
100 | |||
101 | string basic_openid_message::append_query(const string& url,const char *pfx) const { | ||
102 | string rv; | ||
103 | return __om_query_builder(pfx,rv,*this,url).rv; | ||
104 | } | ||
105 | string basic_openid_message::query_string(const char *pfx) const { | ||
106 | string rv; | ||
107 | return __om_query_builder(pfx,rv,*this).rv; | ||
108 | } | ||
109 | |||
110 | void basic_openid_message::reset_fields() { | ||
111 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | ||
112 | } | ||
113 | void basic_openid_message::set_field(const string&,const string&) { | ||
114 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | ||
115 | } | ||
116 | void basic_openid_message::reset_field(const string&) { | ||
117 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | ||
118 | } | ||
119 | |||
120 | void basic_openid_message::from_keyvalues(const string& kv) { | 44 | void basic_openid_message::from_keyvalues(const string& kv) { |
@@ -131,3 +55,3 @@ namespace opkele { | |||
131 | if(nl>co) | 55 | if(nl>co) |
132 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 56 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); |
133 | p = nl+1; | 57 | p = nl+1; |
@@ -242,8 +166,2 @@ namespace opkele { | |||
242 | 166 | ||
243 | void openid_message_t::copy_to(basic_openid_message& x) const { | ||
244 | x.reset_fields(); | ||
245 | for(const_iterator i=begin();i!=end();++i) | ||
246 | x.set_field(i->first,i->second); | ||
247 | } | ||
248 | |||
249 | bool openid_message_t::has_field(const string& n) const { | 167 | bool openid_message_t::has_field(const string& n) const { |