-rw-r--r-- | include/opkele/types.h | 22 | ||||
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/fields.cc | 86 | ||||
-rw-r--r-- | lib/message.cc | 78 |
4 files changed, 97 insertions, 91 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h index bf50e2b..f63bf5d 100644 --- a/include/opkele/types.h +++ b/include/opkele/types.h | |||
@@ -117,19 +117,19 @@ namespace opkele { | |||
117 | * the shared_ptr<> for association_t object type | 117 | * the shared_ptr<> for association_t object type |
118 | */ | 118 | */ |
119 | typedef tr1mem::shared_ptr<association_t> assoc_t; | 119 | typedef tr1mem::shared_ptr<association_t> assoc_t; |
120 | 120 | ||
121 | class basic_message { | 121 | class basic_fields { |
122 | public: | 122 | public: |
123 | typedef util::forward_iterator_proxy< | 123 | typedef util::forward_iterator_proxy< |
124 | string,const string&,const string* | 124 | string,const string&,const string* |
125 | > fields_iterator; | 125 | > fields_iterator; |
126 | 126 | ||
127 | basic_message() { } | 127 | basic_fields() { } |
128 | virtual ~basic_message() { } | 128 | virtual ~basic_fields() { } |
129 | basic_message(const basic_message& x); | 129 | basic_fields(const basic_fields& x); |
130 | void copy_to(basic_message& x) const; | 130 | void copy_to(basic_fields& x) const; |
131 | void append_to(basic_message& x) const; | 131 | void append_to(basic_fields& x) const; |
132 | 132 | ||
133 | virtual bool has_field(const string& n) const = 0; | 133 | virtual bool has_field(const string& n) const = 0; |
134 | virtual const string& get_field(const string& n) const = 0; | 134 | virtual const string& get_field(const string& n) const = 0; |
135 | 135 | ||
@@ -144,25 +144,21 @@ namespace opkele { | |||
144 | virtual void reset_field(const string& n); | 144 | virtual void reset_field(const string& n); |
145 | 145 | ||
146 | }; | 146 | }; |
147 | 147 | ||
148 | class basic_openid_message : public basic_message { | 148 | class basic_openid_message : public basic_fields { |
149 | public: | 149 | public: |
150 | typedef list<string> fields_t; | ||
151 | typedef util::forward_iterator_proxy< | ||
152 | string,const string&,const string* | ||
153 | > fields_iterator; | ||
154 | 150 | ||
155 | basic_openid_message() { } | 151 | basic_openid_message() { } |
156 | basic_openid_message(const basic_openid_message& x); | 152 | basic_openid_message(const basic_openid_message& x); |
157 | 153 | ||
158 | virtual bool has_ns(const string& uri) const; | 154 | virtual bool has_ns(const string& uri) const; |
159 | virtual string get_ns(const string& uri) const; | 155 | virtual string get_ns(const string& uri) const; |
160 | 156 | ||
161 | virtual string append_query(const string& url,const char *pfx="openid.") const { | 157 | virtual string append_query(const string& url,const char *pfx="openid.") const { |
162 | return basic_message::append_query(url,pfx); } | 158 | return basic_fields::append_query(url,pfx); } |
163 | virtual string query_string(const char *pfx="openid.") const { | 159 | virtual string query_string(const char *pfx="openid.") const { |
164 | return basic_message::query_string(pfx); } | 160 | return basic_fields::query_string(pfx); } |
165 | 161 | ||
166 | virtual void from_keyvalues(const string& kv); | 162 | virtual void from_keyvalues(const string& kv); |
167 | virtual void to_keyvalues(ostream& o) const; | 163 | virtual void to_keyvalues(ostream& o) const; |
168 | 164 | ||
diff --git a/lib/Makefile.am b/lib/Makefile.am index 9b25b42..20d15b8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -27,8 +27,8 @@ libopkele_la_SOURCES = \ | |||
27 | extension_chain.cc \ | 27 | extension_chain.cc \ |
28 | curl.cc expat.cc \ | 28 | curl.cc expat.cc \ |
29 | discovery.cc \ | 29 | discovery.cc \ |
30 | basic_rp.cc prequeue_rp.cc \ | 30 | basic_rp.cc prequeue_rp.cc \ |
31 | message.cc \ | 31 | fields.cc message.cc \ |
32 | basic_op.cc verify_op.cc | 32 | basic_op.cc verify_op.cc |
33 | libopkele_la_LDFLAGS = \ | 33 | libopkele_la_LDFLAGS = \ |
34 | -version-info 2:0:0 | 34 | -version-info 2:0:0 |
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/message.cc b/lib/message.cc index 78f20f4..b2324b7 100644 --- a/lib/message.cc +++ b/lib/message.cc | |||
@@ -9,84 +9,8 @@ | |||
9 | namespace opkele { | 9 | namespace opkele { |
10 | using std::input_iterator_tag; | 10 | using std::input_iterator_tag; |
11 | using std::unary_function; | 11 | using std::unary_function; |
12 | 12 | ||
13 | struct __om_copier : public unary_function<const string&,void> { | ||
14 | public: | ||
15 | const basic_message& from; | ||
16 | basic_message& to; | ||
17 | |||
18 | __om_copier(basic_message& t,const basic_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_message::basic_message(const basic_message& x) { | ||
26 | x.copy_to(*this); | ||
27 | } | ||
28 | void basic_message::copy_to(basic_message& x) const { | ||
29 | x.reset_fields(); | ||
30 | for_each(fields_begin(),fields_end(), | ||
31 | __om_copier(x,*this) ); | ||
32 | } | ||
33 | void basic_message::append_to(basic_message& x) const { | ||
34 | for_each(fields_begin(),fields_end(), | ||
35 | __om_copier(x,*this) ); | ||
36 | } | ||
37 | |||
38 | struct __om_query_builder : public unary_function<const string&,void> { | ||
39 | public: | ||
40 | const basic_message& om; | ||
41 | bool first; | ||
42 | string& rv; | ||
43 | const char *pfx; | ||
44 | |||
45 | __om_query_builder(const char *p,string& r,const basic_message& m) | ||
46 | : om(m), first(true), rv(r), pfx(p) { | ||
47 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
48 | } | ||
49 | __om_query_builder(const char *p,string& r,const basic_message& m,const string& u) | ||
50 | : om(m), first(true), rv(r), pfx(p) { | ||
51 | rv = u; | ||
52 | if(rv.find('?')==string::npos) | ||
53 | rv += '?'; | ||
54 | else | ||
55 | first = false; | ||
56 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
57 | } | ||
58 | |||
59 | result_type operator()(argument_type f) { | ||
60 | if(first) | ||
61 | first = false; | ||
62 | else | ||
63 | rv += '&'; | ||
64 | if(pfx) rv += pfx; | ||
65 | rv+= f; | ||
66 | rv += '='; | ||
67 | rv += util::url_encode(om.get_field(f)); | ||
68 | } | ||
69 | }; | ||
70 | |||
71 | string basic_message::append_query(const string& url,const char *pfx) const { | ||
72 | string rv; | ||
73 | return __om_query_builder(pfx,rv,*this,url).rv; | ||
74 | } | ||
75 | string basic_message::query_string(const char *pfx) const { | ||
76 | string rv; | ||
77 | return __om_query_builder(pfx,rv,*this).rv; | ||
78 | } | ||
79 | |||
80 | void basic_message::reset_fields() { | ||
81 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | ||
82 | } | ||
83 | void basic_message::set_field(const string&,const string&) { | ||
84 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | ||
85 | } | ||
86 | void basic_message::reset_field(const string&) { | ||
87 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | ||
88 | } | ||
89 | 13 | ||
90 | struct __om_ns_finder : public unary_function<const string&,bool> { | 14 | struct __om_ns_finder : public unary_function<const string&,bool> { |
91 | public: | 15 | public: |
92 | const basic_openid_message& om; | 16 | const basic_openid_message& om; |
@@ -128,9 +52,9 @@ namespace opkele { | |||
128 | string::size_type nl = kv.find('\n',co+1); | 52 | string::size_type nl = kv.find('\n',co+1); |
129 | if(nl==string::npos) | 53 | if(nl==string::npos) |
130 | throw bad_input(OPKELE_CP_ "malformed input"); | 54 | throw bad_input(OPKELE_CP_ "malformed input"); |
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; |
134 | #else /* POSTELS_LAW */ | 58 | #else /* POSTELS_LAW */ |
135 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 59 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
136 | if(lb==string::npos) { | 60 | if(lb==string::npos) { |