summaryrefslogtreecommitdiffabout
path: root/lib/message.cc
Unidiff
Diffstat (limited to 'lib/message.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/message.cc194
1 files changed, 194 insertions, 0 deletions
diff --git a/lib/message.cc b/lib/message.cc
new file mode 100644
index 0000000..b2324b7
--- a/dev/null
+++ b/lib/message.cc
@@ -0,0 +1,194 @@
1#include <cassert>
2#include <opkele/types.h>
3#include <opkele/exception.h>
4#include <opkele/util.h>
5#include <opkele/debug.h>
6
7#include "config.h"
8
9namespace opkele {
10 using std::input_iterator_tag;
11 using std::unary_function;
12
13
14 struct __om_ns_finder : public unary_function<const string&,bool> {
15 public:
16 const basic_openid_message& om;
17 const string& uri;
18
19 __om_ns_finder(const basic_openid_message& m,
20 const string& u) : om(m), uri(u) { }
21
22 result_type operator()(argument_type f) {
23 return
24 (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1))
25 && om.get_field(f)==uri ;
26 }
27 };
28
29 bool basic_openid_message::has_ns(const string& uri) const {
30 fields_iterator ei = fields_end();
31 fields_iterator i = find_if(fields_begin(),fields_end(),
32 __om_ns_finder(*this,uri));
33 return !(i==ei);
34 }
35 string basic_openid_message::get_ns(const string& uri) const {
36 fields_iterator ei = fields_end();
37 fields_iterator i = find_if(fields_begin(),fields_end(),
38 __om_ns_finder(*this,uri));
39 if(i==ei)
40 throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri);
41 return i->substr(3);
42 }
43
44 void basic_openid_message::from_keyvalues(const string& kv) {
45 reset_fields();
46 string::size_type p = 0;
47 while(true) {
48 string::size_type co = kv.find(':',p);
49 if(co==string::npos)
50 break;
51#ifndef POSTELS_LAW
52 string::size_type nl = kv.find('\n',co+1);
53 if(nl==string::npos)
54 throw bad_input(OPKELE_CP_ "malformed input");
55 if(nl>co)
56 set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1));
57 p = nl+1;
58#else /* POSTELS_LAW */
59 string::size_type lb = kv.find_first_of("\r\n",co+1);
60 if(lb==string::npos) {
61 set_field(kv.substr(p,co-p),kv.substr(co+1));
62 break;
63 }
64 if(lb>co)
65 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);
67 if(nolb==string::npos)
68 break;
69 p = nolb;
70#endif /* POSTELS_LAW */
71 }
72 }
73
74 struct __om_kv_outputter : public unary_function<const string&,void> {
75 public:
76 const basic_openid_message& om;
77 ostream& os;
78
79 __om_kv_outputter(const basic_openid_message& m,ostream& s)
80 : om(m), os(s) { }
81
82 result_type operator()(argument_type f) {
83 os << f << ':' << om.get_field(f) << '\n';
84 }
85 };
86
87 void basic_openid_message::to_keyvalues(ostream& o) const {
88 for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o));
89 }
90
91 struct __om_html_outputter : public unary_function<const string&,void> {
92 public:
93 const basic_openid_message& om;
94 ostream& os;
95 const char *pfx;
96
97 __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0)
98 : om(m), os(s), pfx(p) { }
99
100 result_type operator()(argument_type f) {
101 os <<
102 "<input type=\"hidden\""
103 " name=\"";
104 if(pfx)
105 os << util::attr_escape(pfx);
106 os << util::attr_escape(f) << "\""
107 " value=\"" << util::attr_escape(om.get_field(f)) << "\" />";
108 }
109 };
110
111 void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const {
112 for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx));
113 }
114
115 void basic_openid_message::add_to_signed(const string& fields) {
116 string::size_type fnc = fields.find_first_not_of(",");
117 if(fnc==string::npos)
118 throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields");
119 string signeds;
120 try {
121 signeds = get_field("signed");
122 string::size_type lnc = signeds.find_last_not_of(",");
123 if(lnc==string::npos)
124 signeds.assign(fields,fnc,fields.size()-fnc);
125 else{
126 string::size_type ss = signeds.size();
127 if(lnc==(ss-1)) {
128 signeds+= ',';
129 signeds.append(fields,fnc,fields.size()-fnc);
130 }else{
131 if(lnc<(ss-2))
132 signeds.replace(lnc+2,ss-lnc-2,
133 fields,fnc,fields.size()-fnc);
134 else
135 signeds.append(fields,fnc,fields.size()-fnc);
136 }
137 }
138 }catch(failed_lookup&) {
139 signeds.assign(fields,fnc,fields.size()-fnc);
140 }
141 set_field("signed",signeds);
142 }
143
144 string basic_openid_message::find_ns(const string& uri,const char *pfx) const {
145 try {
146 return get_ns(uri);
147 }catch(failed_lookup&) {
148 return pfx;
149 }
150 }
151 string basic_openid_message::allocate_ns(const string& uri,const char *pfx) {
152 if(!has_field("ns"))
153 return pfx;
154 if(has_ns(uri))
155 throw bad_input(OPKELE_CP_ "OpenID message already contains namespace");
156 string rv = pfx;
157 if(has_field("ns."+rv)) {
158 string::reference c=rv[rv.length()];
159 for(c='a';c<='z' && has_field("ns."+rv);++c);
160 if(c=='z')
161 throw exception(OPKELE_CP_ "Failed to allocate namespace");
162 }
163 set_field("ns."+rv,uri);
164 return rv;
165 }
166
167 bool openid_message_t::has_field(const string& n) const {
168 return find(n)!=end();
169 }
170 const string& openid_message_t::get_field(const string& n) const {
171 const_iterator i=find(n);
172 if(i==end())
173 throw failed_lookup(OPKELE_CP_ n+": no such field");
174 return i->second;
175 }
176
177 openid_message_t::fields_iterator openid_message_t::fields_begin() const {
178 return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end());
179 }
180 openid_message_t::fields_iterator openid_message_t::fields_end() const {
181 return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end());
182 }
183
184 void openid_message_t::reset_fields() {
185 clear();
186 }
187 void openid_message_t::set_field(const string& n,const string& v) {
188 (*this)[n]=v;
189 }
190 void openid_message_t::reset_field(const string& n) {
191 erase(n);
192 }
193
194}