-rw-r--r-- | lib/fields.cc | 2 | ||||
-rw-r--r-- | lib/message.cc | 2 |
2 files changed, 4 insertions, 0 deletions
diff --git a/lib/fields.cc b/lib/fields.cc index d494098..91fbc41 100644 --- a/lib/fields.cc +++ b/lib/fields.cc | |||
@@ -1,86 +1,88 @@ | |||
1 | #include <opkele/types.h> | 1 | #include <opkele/types.h> |
2 | #include <opkele/exception.h> | 2 | #include <opkele/exception.h> |
3 | #include <opkele/util.h> | 3 | #include <opkele/util.h> |
4 | #include <algorithm> | ||
4 | 5 | ||
5 | namespace opkele { | 6 | namespace opkele { |
7 | using std::for_each; | ||
6 | using std::unary_function; | 8 | using std::unary_function; |
7 | 9 | ||
8 | struct __om_copier : public unary_function<const string&,void> { | 10 | struct __om_copier : public unary_function<const string&,void> { |
9 | public: | 11 | public: |
10 | const basic_fields& from; | 12 | const basic_fields& from; |
11 | basic_fields& to; | 13 | basic_fields& to; |
12 | 14 | ||
13 | __om_copier(basic_fields& t,const basic_fields& f) | 15 | __om_copier(basic_fields& t,const basic_fields& f) |
14 | : from(f), to(t) { } | 16 | : from(f), to(t) { } |
15 | 17 | ||
16 | result_type operator()(argument_type f) { | 18 | result_type operator()(argument_type f) { |
17 | to.set_field(f,from.get_field(f)); } | 19 | to.set_field(f,from.get_field(f)); } |
18 | }; | 20 | }; |
19 | 21 | ||
20 | basic_fields::basic_fields(const basic_fields& x) { | 22 | basic_fields::basic_fields(const basic_fields& x) { |
21 | x.copy_to(*this); | 23 | x.copy_to(*this); |
22 | } | 24 | } |
23 | void basic_fields::copy_to(basic_fields& x) const { | 25 | void basic_fields::copy_to(basic_fields& x) const { |
24 | x.reset_fields(); | 26 | x.reset_fields(); |
25 | for_each(fields_begin(),fields_end(), | 27 | for_each(fields_begin(),fields_end(), |
26 | __om_copier(x,*this) ); | 28 | __om_copier(x,*this) ); |
27 | } | 29 | } |
28 | void basic_fields::append_to(basic_fields& x) const { | 30 | void basic_fields::append_to(basic_fields& x) const { |
29 | for_each(fields_begin(),fields_end(), | 31 | for_each(fields_begin(),fields_end(), |
30 | __om_copier(x,*this) ); | 32 | __om_copier(x,*this) ); |
31 | } | 33 | } |
32 | 34 | ||
33 | struct __om_query_builder : public unary_function<const string&,void> { | 35 | struct __om_query_builder : public unary_function<const string&,void> { |
34 | public: | 36 | public: |
35 | const basic_fields& om; | 37 | const basic_fields& om; |
36 | bool first; | 38 | bool first; |
37 | string& rv; | 39 | string& rv; |
38 | const char *pfx; | 40 | const char *pfx; |
39 | 41 | ||
40 | __om_query_builder(const char *p,string& r,const basic_fields& m) | 42 | __om_query_builder(const char *p,string& r,const basic_fields& m) |
41 | : om(m), first(true), rv(r), pfx(p) { | 43 | : om(m), first(true), rv(r), pfx(p) { |
42 | for_each(om.fields_begin(),om.fields_end(),*this); | 44 | for_each(om.fields_begin(),om.fields_end(),*this); |
43 | } | 45 | } |
44 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) | 46 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) |
45 | : om(m), first(true), rv(r), pfx(p) { | 47 | : om(m), first(true), rv(r), pfx(p) { |
46 | rv = u; | 48 | rv = u; |
47 | if(rv.find('?')==string::npos) | 49 | if(rv.find('?')==string::npos) |
48 | rv += '?'; | 50 | rv += '?'; |
49 | else | 51 | else |
50 | first = false; | 52 | first = false; |
51 | for_each(om.fields_begin(),om.fields_end(),*this); | 53 | for_each(om.fields_begin(),om.fields_end(),*this); |
52 | } | 54 | } |
53 | 55 | ||
54 | result_type operator()(argument_type f) { | 56 | result_type operator()(argument_type f) { |
55 | if(first) | 57 | if(first) |
56 | first = false; | 58 | first = false; |
57 | else | 59 | else |
58 | rv += '&'; | 60 | rv += '&'; |
59 | if(pfx) rv += pfx; | 61 | if(pfx) rv += pfx; |
60 | rv+= f; | 62 | rv+= f; |
61 | rv += '='; | 63 | rv += '='; |
62 | rv += util::url_encode(om.get_field(f)); | 64 | rv += util::url_encode(om.get_field(f)); |
63 | } | 65 | } |
64 | }; | 66 | }; |
65 | 67 | ||
66 | string basic_fields::append_query(const string& url,const char *pfx) const { | 68 | string basic_fields::append_query(const string& url,const char *pfx) const { |
67 | string rv; | 69 | string rv; |
68 | return __om_query_builder(pfx,rv,*this,url).rv; | 70 | return __om_query_builder(pfx,rv,*this,url).rv; |
69 | } | 71 | } |
70 | string basic_fields::query_string(const char *pfx) const { | 72 | string basic_fields::query_string(const char *pfx) const { |
71 | string rv; | 73 | string rv; |
72 | return __om_query_builder(pfx,rv,*this).rv; | 74 | return __om_query_builder(pfx,rv,*this).rv; |
73 | } | 75 | } |
74 | 76 | ||
75 | void basic_fields::reset_fields() { | 77 | void basic_fields::reset_fields() { |
76 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | 78 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); |
77 | } | 79 | } |
78 | void basic_fields::set_field(const string&,const string&) { | 80 | void basic_fields::set_field(const string&,const string&) { |
79 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | 81 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); |
80 | } | 82 | } |
81 | void basic_fields::reset_field(const string&) { | 83 | void basic_fields::reset_field(const string&) { |
82 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | 84 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); |
83 | } | 85 | } |
84 | 86 | ||
85 | 87 | ||
86 | } | 88 | } |
diff --git a/lib/message.cc b/lib/message.cc index b2324b7..524946a 100644 --- a/lib/message.cc +++ b/lib/message.cc | |||
@@ -1,97 +1,99 @@ | |||
1 | #include <cassert> | 1 | #include <cassert> |
2 | #include <algorithm> | ||
3 | |||
2 | #include <opkele/types.h> | 4 | #include <opkele/types.h> |
3 | #include <opkele/exception.h> | 5 | #include <opkele/exception.h> |
4 | #include <opkele/util.h> | 6 | #include <opkele/util.h> |
5 | #include <opkele/debug.h> | 7 | #include <opkele/debug.h> |
6 | 8 | ||
7 | #include "config.h" | 9 | #include "config.h" |
8 | 10 | ||
9 | namespace opkele { | 11 | namespace opkele { |
10 | using std::input_iterator_tag; | 12 | using std::input_iterator_tag; |
11 | using std::unary_function; | 13 | using std::unary_function; |
12 | 14 | ||
13 | 15 | ||
14 | struct __om_ns_finder : public unary_function<const string&,bool> { | 16 | struct __om_ns_finder : public unary_function<const string&,bool> { |
15 | public: | 17 | public: |
16 | const basic_openid_message& om; | 18 | const basic_openid_message& om; |
17 | const string& uri; | 19 | const string& uri; |
18 | 20 | ||
19 | __om_ns_finder(const basic_openid_message& m, | 21 | __om_ns_finder(const basic_openid_message& m, |
20 | const string& u) : om(m), uri(u) { } | 22 | const string& u) : om(m), uri(u) { } |
21 | 23 | ||
22 | result_type operator()(argument_type f) { | 24 | result_type operator()(argument_type f) { |
23 | return | 25 | return |
24 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) | 26 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) |
25 | && om.get_field(f)==uri ; | 27 | && om.get_field(f)==uri ; |
26 | } | 28 | } |
27 | }; | 29 | }; |
28 | 30 | ||
29 | bool basic_openid_message::has_ns(const string& uri) const { | 31 | bool basic_openid_message::has_ns(const string& uri) const { |
30 | fields_iterator ei = fields_end(); | 32 | fields_iterator ei = fields_end(); |
31 | fields_iterator i = find_if(fields_begin(),fields_end(), | 33 | fields_iterator i = find_if(fields_begin(),fields_end(), |
32 | __om_ns_finder(*this,uri)); | 34 | __om_ns_finder(*this,uri)); |
33 | return !(i==ei); | 35 | return !(i==ei); |
34 | } | 36 | } |
35 | string basic_openid_message::get_ns(const string& uri) const { | 37 | string basic_openid_message::get_ns(const string& uri) const { |
36 | fields_iterator ei = fields_end(); | 38 | fields_iterator ei = fields_end(); |
37 | fields_iterator i = find_if(fields_begin(),fields_end(), | 39 | fields_iterator i = find_if(fields_begin(),fields_end(), |
38 | __om_ns_finder(*this,uri)); | 40 | __om_ns_finder(*this,uri)); |
39 | if(i==ei) | 41 | if(i==ei) |
40 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); | 42 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); |
41 | return i->substr(3); | 43 | return i->substr(3); |
42 | } | 44 | } |
43 | 45 | ||
44 | void basic_openid_message::from_keyvalues(const string& kv) { | 46 | void basic_openid_message::from_keyvalues(const string& kv) { |
45 | reset_fields(); | 47 | reset_fields(); |
46 | string::size_type p = 0; | 48 | string::size_type p = 0; |
47 | while(true) { | 49 | while(true) { |
48 | string::size_type co = kv.find(':',p); | 50 | string::size_type co = kv.find(':',p); |
49 | if(co==string::npos) | 51 | if(co==string::npos) |
50 | break; | 52 | break; |
51 | #ifndef POSTELS_LAW | 53 | #ifndef POSTELS_LAW |
52 | string::size_type nl = kv.find('\n',co+1); | 54 | string::size_type nl = kv.find('\n',co+1); |
53 | if(nl==string::npos) | 55 | if(nl==string::npos) |
54 | throw bad_input(OPKELE_CP_ "malformed input"); | 56 | throw bad_input(OPKELE_CP_ "malformed input"); |
55 | if(nl>co) | 57 | if(nl>co) |
56 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); | 58 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); |
57 | p = nl+1; | 59 | p = nl+1; |
58 | #else /* POSTELS_LAW */ | 60 | #else /* POSTELS_LAW */ |
59 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 61 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
60 | if(lb==string::npos) { | 62 | if(lb==string::npos) { |
61 | set_field(kv.substr(p,co-p),kv.substr(co+1)); | 63 | set_field(kv.substr(p,co-p),kv.substr(co+1)); |
62 | break; | 64 | break; |
63 | } | 65 | } |
64 | if(lb>co) | 66 | if(lb>co) |
65 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); | 67 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); |
66 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); | 68 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); |
67 | if(nolb==string::npos) | 69 | if(nolb==string::npos) |
68 | break; | 70 | break; |
69 | p = nolb; | 71 | p = nolb; |
70 | #endif /* POSTELS_LAW */ | 72 | #endif /* POSTELS_LAW */ |
71 | } | 73 | } |
72 | } | 74 | } |
73 | 75 | ||
74 | struct __om_kv_outputter : public unary_function<const string&,void> { | 76 | struct __om_kv_outputter : public unary_function<const string&,void> { |
75 | public: | 77 | public: |
76 | const basic_openid_message& om; | 78 | const basic_openid_message& om; |
77 | ostream& os; | 79 | ostream& os; |
78 | 80 | ||
79 | __om_kv_outputter(const basic_openid_message& m,ostream& s) | 81 | __om_kv_outputter(const basic_openid_message& m,ostream& s) |
80 | : om(m), os(s) { } | 82 | : om(m), os(s) { } |
81 | 83 | ||
82 | result_type operator()(argument_type f) { | 84 | result_type operator()(argument_type f) { |
83 | os << f << ':' << om.get_field(f) << '\n'; | 85 | os << f << ':' << om.get_field(f) << '\n'; |
84 | } | 86 | } |
85 | }; | 87 | }; |
86 | 88 | ||
87 | void basic_openid_message::to_keyvalues(ostream& o) const { | 89 | void basic_openid_message::to_keyvalues(ostream& o) const { |
88 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); | 90 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); |
89 | } | 91 | } |
90 | 92 | ||
91 | struct __om_html_outputter : public unary_function<const string&,void> { | 93 | struct __om_html_outputter : public unary_function<const string&,void> { |
92 | public: | 94 | public: |
93 | const basic_openid_message& om; | 95 | const basic_openid_message& om; |
94 | ostream& os; | 96 | ostream& os; |
95 | const char *pfx; | 97 | const char *pfx; |
96 | 98 | ||
97 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) | 99 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) |