summaryrefslogtreecommitdiffabout
path: root/lib/message.cc
Unidiff
Diffstat (limited to 'lib/message.cc') (more/less context) (show whitespace changes)
-rw-r--r--lib/message.cc78
1 files changed, 1 insertions, 77 deletions
diff --git a/lib/message.cc b/lib/message.cc
index 78f20f4..b2324b7 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -1,180 +1,104 @@
1#include <cassert> 1#include <cassert>
2#include <opkele/types.h> 2#include <opkele/types.h>
3#include <opkele/exception.h> 3#include <opkele/exception.h>
4#include <opkele/util.h> 4#include <opkele/util.h>
5#include <opkele/debug.h> 5#include <opkele/debug.h>
6 6
7#include "config.h" 7#include "config.h"
8 8
9namespace opkele { 9namespace 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;
93 const string& uri; 17 const string& uri;
94 18
95 __om_ns_finder(const basic_openid_message& m, 19 __om_ns_finder(const basic_openid_message& m,
96 const string& u) : om(m), uri(u) { } 20 const string& u) : om(m), uri(u) { }
97 21
98 result_type operator()(argument_type f) { 22 result_type operator()(argument_type f) {
99 return 23 return
100 (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) 24 (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1))
101 && om.get_field(f)==uri ; 25 && om.get_field(f)==uri ;
102 } 26 }
103 }; 27 };
104 28
105 bool basic_openid_message::has_ns(const string& uri) const { 29 bool basic_openid_message::has_ns(const string& uri) const {
106 fields_iterator ei = fields_end(); 30 fields_iterator ei = fields_end();
107 fields_iterator i = find_if(fields_begin(),fields_end(), 31 fields_iterator i = find_if(fields_begin(),fields_end(),
108 __om_ns_finder(*this,uri)); 32 __om_ns_finder(*this,uri));
109 return !(i==ei); 33 return !(i==ei);
110 } 34 }
111 string basic_openid_message::get_ns(const string& uri) const { 35 string basic_openid_message::get_ns(const string& uri) const {
112 fields_iterator ei = fields_end(); 36 fields_iterator ei = fields_end();
113 fields_iterator i = find_if(fields_begin(),fields_end(), 37 fields_iterator i = find_if(fields_begin(),fields_end(),
114 __om_ns_finder(*this,uri)); 38 __om_ns_finder(*this,uri));
115 if(i==ei) 39 if(i==ei)
116 throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); 40 throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri);
117 return i->substr(3); 41 return i->substr(3);
118 } 42 }
119 43
120 void basic_openid_message::from_keyvalues(const string& kv) { 44 void basic_openid_message::from_keyvalues(const string& kv) {
121 reset_fields(); 45 reset_fields();
122 string::size_type p = 0; 46 string::size_type p = 0;
123 while(true) { 47 while(true) {
124 string::size_type co = kv.find(':',p); 48 string::size_type co = kv.find(':',p);
125 if(co==string::npos) 49 if(co==string::npos)
126 break; 50 break;
127#ifndef POSTELS_LAW 51#ifndef POSTELS_LAW
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) {
137 set_field(kv.substr(p,co-p),kv.substr(co+1)); 61 set_field(kv.substr(p,co-p),kv.substr(co+1));
138 break; 62 break;
139 } 63 }
140 if(lb>co) 64 if(lb>co)
141 set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); 65 set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1));
142 string::size_type nolb = kv.find_first_not_of("\r\n",lb); 66 string::size_type nolb = kv.find_first_not_of("\r\n",lb);
143 if(nolb==string::npos) 67 if(nolb==string::npos)
144 break; 68 break;
145 p = nolb; 69 p = nolb;
146#endif /* POSTELS_LAW */ 70#endif /* POSTELS_LAW */
147 } 71 }
148 } 72 }
149 73
150 struct __om_kv_outputter : public unary_function<const string&,void> { 74 struct __om_kv_outputter : public unary_function<const string&,void> {
151 public: 75 public:
152 const basic_openid_message& om; 76 const basic_openid_message& om;
153 ostream& os; 77 ostream& os;
154 78
155 __om_kv_outputter(const basic_openid_message& m,ostream& s) 79 __om_kv_outputter(const basic_openid_message& m,ostream& s)
156 : om(m), os(s) { } 80 : om(m), os(s) { }
157 81
158 result_type operator()(argument_type f) { 82 result_type operator()(argument_type f) {
159 os << f << ':' << om.get_field(f) << '\n'; 83 os << f << ':' << om.get_field(f) << '\n';
160 } 84 }
161 }; 85 };
162 86
163 void basic_openid_message::to_keyvalues(ostream& o) const { 87 void basic_openid_message::to_keyvalues(ostream& o) const {
164 for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); 88 for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o));
165 } 89 }
166 90
167 struct __om_html_outputter : public unary_function<const string&,void> { 91 struct __om_html_outputter : public unary_function<const string&,void> {
168 public: 92 public:
169 const basic_openid_message& om; 93 const basic_openid_message& om;
170 ostream& os; 94 ostream& os;
171 const char *pfx; 95 const char *pfx;
172 96
173 __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) 97 __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0)
174 : om(m), os(s), pfx(p) { } 98 : om(m), os(s), pfx(p) { }
175 99
176 result_type operator()(argument_type f) { 100 result_type operator()(argument_type f) {
177 os << 101 os <<
178 "<input type=\"hidden\"" 102 "<input type=\"hidden\""
179 " name=\""; 103 " name=\"";
180 if(pfx) 104 if(pfx)