-rw-r--r-- | lib/params.cc | 7 |
1 files changed, 1 insertions, 6 deletions
diff --git a/lib/params.cc b/lib/params.cc index 14f1a53..03867d5 100644 --- a/lib/params.cc +++ b/lib/params.cc | |||
@@ -1,96 +1,91 @@ | |||
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 | #include <mimetic/mimetic.h> | ||
7 | 6 | ||
8 | namespace opkele { | 7 | namespace opkele { |
9 | using namespace std; | 8 | using namespace std; |
10 | 9 | ||
11 | bool params_t::has_param(const string& n) const { | 10 | bool params_t::has_param(const string& n) const { |
12 | return find(n)!=end(); | 11 | return find(n)!=end(); |
13 | } | 12 | } |
14 | const string& params_t::get_param(const string& n) const { | 13 | const string& params_t::get_param(const string& n) const { |
15 | const_iterator i = find(n); | 14 | const_iterator i = find(n); |
16 | if(i==end()) | 15 | if(i==end()) |
17 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 16 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
18 | return i->second; | 17 | return i->second; |
19 | } | 18 | } |
20 | string& params_t::get_param(const string& n) { | 19 | string& params_t::get_param(const string& n) { |
21 | iterator i = find(n); | 20 | iterator i = find(n); |
22 | if(i==end()) | 21 | if(i==end()) |
23 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 22 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
24 | return i->second; | 23 | return i->second; |
25 | } | 24 | } |
26 | 25 | ||
27 | void params_t::parse_keyvalues(const string& kv) { | 26 | void params_t::parse_keyvalues(const string& kv) { |
28 | clear(); | 27 | clear(); |
29 | string::size_type p = 0; | 28 | string::size_type p = 0; |
30 | while(true) { | 29 | while(true) { |
31 | string::size_type co = kv.find(':',p); | 30 | string::size_type co = kv.find(':',p); |
32 | if(co==string::npos) | 31 | if(co==string::npos) |
33 | break; | 32 | break; |
34 | string::size_type nl = kv.find('\n',co+1); | 33 | string::size_type nl = kv.find('\n',co+1); |
35 | if(nl==string::npos) | 34 | if(nl==string::npos) |
36 | throw bad_input(OPKELE_CP_ "malformed input"); | 35 | throw bad_input(OPKELE_CP_ "malformed input"); |
37 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 36 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); |
38 | p = nl+1; | 37 | p = nl+1; |
39 | } | 38 | } |
40 | } | 39 | } |
41 | 40 | ||
42 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { | 41 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { |
43 | string kv; | 42 | string kv; |
44 | string::size_type p = 0; | 43 | string::size_type p = 0; |
45 | while(true) { | 44 | while(true) { |
46 | string::size_type co = slist.find(',',p); | 45 | string::size_type co = slist.find(',',p); |
47 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); | 46 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); |
48 | kv += f; | 47 | kv += f; |
49 | kv += ':'; | 48 | kv += ':'; |
50 | if(prefix) f.insert(0,prefix); | 49 | if(prefix) f.insert(0,prefix); |
51 | kv += get_param(f); | 50 | kv += get_param(f); |
52 | kv += '\n'; | 51 | kv += '\n'; |
53 | if(co==string::npos) | 52 | if(co==string::npos) |
54 | break; | 53 | break; |
55 | p = co+1; | 54 | p = co+1; |
56 | } | 55 | } |
57 | unsigned int md_len = 0; | 56 | unsigned int md_len = 0; |
58 | unsigned char *md = HMAC( | 57 | unsigned char *md = HMAC( |
59 | EVP_sha1(), | 58 | EVP_sha1(), |
60 | &(secret.front()),secret.size(), | 59 | &(secret.front()),secret.size(), |
61 | (const unsigned char *)kv.data(),kv.length(), | 60 | (const unsigned char *)kv.data(),kv.length(), |
62 | 0,&md_len); | 61 | 0,&md_len); |
63 | mimetic::Base64::Encoder b(0); | 62 | sig = util::encode_base64(md,md_len); |
64 | sig.erase(); | ||
65 | mimetic::encode( | ||
66 | md,md+md_len, b, | ||
67 | back_insert_iterator<string>(sig) ); | ||
68 | } | 63 | } |
69 | 64 | ||
70 | string params_t::append_query(const string& url,const char *prefix) const { | 65 | string params_t::append_query(const string& url,const char *prefix) const { |
71 | string rv = url; | 66 | string rv = url; |
72 | bool p = true; | 67 | bool p = true; |
73 | if(rv.find('?')==string::npos) { | 68 | if(rv.find('?')==string::npos) { |
74 | rv += '?'; | 69 | rv += '?'; |
75 | p = false; | 70 | p = false; |
76 | } | 71 | } |
77 | for(const_iterator i=begin();i!=end();++i) { | 72 | for(const_iterator i=begin();i!=end();++i) { |
78 | if(p) | 73 | if(p) |
79 | rv += '&'; | 74 | rv += '&'; |
80 | else | 75 | else |
81 | p = true; | 76 | p = true; |
82 | rv += prefix; | 77 | rv += prefix; |
83 | rv += i->first; | 78 | rv += i->first; |
84 | rv += '='; | 79 | rv += '='; |
85 | rv += util::url_encode(i->second); | 80 | rv += util::url_encode(i->second); |
86 | } | 81 | } |
87 | return rv; | 82 | return rv; |
88 | } | 83 | } |
89 | 84 | ||
90 | ostream& operator << (ostream& o,const params_t& p) { | 85 | ostream& operator << (ostream& o,const params_t& p) { |
91 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) | 86 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) |
92 | o << i->first << ':' << i->second << '\n'; | 87 | o << i->first << ':' << i->second << '\n'; |
93 | return o; | 88 | return o; |
94 | } | 89 | } |
95 | 90 | ||
96 | } | 91 | } |