author | Michael Krelin <hacker@klever.net> | 2007-01-12 14:21:03 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-01-12 14:21:03 (UTC) |
commit | 83516ff76d24142cdc5193875ee7b684652f2eaf (patch) (unidiff) | |
tree | ee0eedac2f6bb63beaa25291fab47fe40d1e8662 /lib | |
parent | 782d7a9e2c07ff6621b754595642aa3fec377bd2 (diff) | |
download | libopkele-83516ff76d24142cdc5193875ee7b684652f2eaf.zip libopkele-83516ff76d24142cdc5193875ee7b684652f2eaf.tar.gz libopkele-83516ff76d24142cdc5193875ee7b684652f2eaf.tar.bz2 |
Be a bit more liberal in what we accept as key-value pairs
-rw-r--r-- | lib/params.cc | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/lib/params.cc b/lib/params.cc index 03867d5..b181811 100644 --- a/lib/params.cc +++ b/lib/params.cc | |||
@@ -1,91 +1,92 @@ | |||
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 <openssl/sha.h> | 4 | #include <openssl/sha.h> |
5 | #include <openssl/hmac.h> | 5 | #include <openssl/hmac.h> |
6 | 6 | ||
7 | namespace opkele { | 7 | namespace opkele { |
8 | using namespace std; | 8 | using namespace std; |
9 | 9 | ||
10 | bool params_t::has_param(const string& n) const { | 10 | bool params_t::has_param(const string& n) const { |
11 | return find(n)!=end(); | 11 | return find(n)!=end(); |
12 | } | 12 | } |
13 | const string& params_t::get_param(const string& n) const { | 13 | const string& params_t::get_param(const string& n) const { |
14 | const_iterator i = find(n); | 14 | const_iterator i = find(n); |
15 | if(i==end()) | 15 | if(i==end()) |
16 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 16 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
17 | return i->second; | 17 | return i->second; |
18 | } | 18 | } |
19 | string& params_t::get_param(const string& n) { | 19 | string& params_t::get_param(const string& n) { |
20 | iterator i = find(n); | 20 | iterator i = find(n); |
21 | if(i==end()) | 21 | if(i==end()) |
22 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 22 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
23 | return i->second; | 23 | return i->second; |
24 | } | 24 | } |
25 | 25 | ||
26 | void params_t::parse_keyvalues(const string& kv) { | 26 | void params_t::parse_keyvalues(const string& kv) { |
27 | clear(); | 27 | clear(); |
28 | string::size_type p = 0; | 28 | string::size_type p = 0; |
29 | while(true) { | 29 | while(true) { |
30 | string::size_type co = kv.find(':',p); | 30 | string::size_type co = kv.find(':',p); |
31 | if(co==string::npos) | 31 | if(co==string::npos) |
32 | break; | 32 | break; |
33 | string::size_type nl = kv.find('\n',co+1); | 33 | string::size_type nl = kv.find('\n',co+1); |
34 | if(nl==string::npos) | 34 | if(nl==string::npos) |
35 | throw bad_input(OPKELE_CP_ "malformed input"); | 35 | throw bad_input(OPKELE_CP_ "malformed input"); |
36 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 36 | if(nl>co) |
37 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | ||
37 | p = nl+1; | 38 | p = nl+1; |
38 | } | 39 | } |
39 | } | 40 | } |
40 | 41 | ||
41 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { | 42 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { |
42 | string kv; | 43 | string kv; |
43 | string::size_type p = 0; | 44 | string::size_type p = 0; |
44 | while(true) { | 45 | while(true) { |
45 | string::size_type co = slist.find(',',p); | 46 | string::size_type co = slist.find(',',p); |
46 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); | 47 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); |
47 | kv += f; | 48 | kv += f; |
48 | kv += ':'; | 49 | kv += ':'; |
49 | if(prefix) f.insert(0,prefix); | 50 | if(prefix) f.insert(0,prefix); |
50 | kv += get_param(f); | 51 | kv += get_param(f); |
51 | kv += '\n'; | 52 | kv += '\n'; |
52 | if(co==string::npos) | 53 | if(co==string::npos) |
53 | break; | 54 | break; |
54 | p = co+1; | 55 | p = co+1; |
55 | } | 56 | } |
56 | unsigned int md_len = 0; | 57 | unsigned int md_len = 0; |
57 | unsigned char *md = HMAC( | 58 | unsigned char *md = HMAC( |
58 | EVP_sha1(), | 59 | EVP_sha1(), |
59 | &(secret.front()),secret.size(), | 60 | &(secret.front()),secret.size(), |
60 | (const unsigned char *)kv.data(),kv.length(), | 61 | (const unsigned char *)kv.data(),kv.length(), |
61 | 0,&md_len); | 62 | 0,&md_len); |
62 | sig = util::encode_base64(md,md_len); | 63 | sig = util::encode_base64(md,md_len); |
63 | } | 64 | } |
64 | 65 | ||
65 | string params_t::append_query(const string& url,const char *prefix) const { | 66 | string params_t::append_query(const string& url,const char *prefix) const { |
66 | string rv = url; | 67 | string rv = url; |
67 | bool p = true; | 68 | bool p = true; |
68 | if(rv.find('?')==string::npos) { | 69 | if(rv.find('?')==string::npos) { |
69 | rv += '?'; | 70 | rv += '?'; |
70 | p = false; | 71 | p = false; |
71 | } | 72 | } |
72 | for(const_iterator i=begin();i!=end();++i) { | 73 | for(const_iterator i=begin();i!=end();++i) { |
73 | if(p) | 74 | if(p) |
74 | rv += '&'; | 75 | rv += '&'; |
75 | else | 76 | else |
76 | p = true; | 77 | p = true; |
77 | rv += prefix; | 78 | rv += prefix; |
78 | rv += i->first; | 79 | rv += i->first; |
79 | rv += '='; | 80 | rv += '='; |
80 | rv += util::url_encode(i->second); | 81 | rv += util::url_encode(i->second); |
81 | } | 82 | } |
82 | return rv; | 83 | return rv; |
83 | } | 84 | } |
84 | 85 | ||
85 | ostream& operator << (ostream& o,const params_t& p) { | 86 | ostream& operator << (ostream& o,const params_t& p) { |
86 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) | 87 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) |
87 | o << i->first << ':' << i->second << '\n'; | 88 | o << i->first << ':' << i->second << '\n'; |
88 | return o; | 89 | return o; |
89 | } | 90 | } |
90 | 91 | ||
91 | } | 92 | } |